Statistics
| Branch: | Tag: | Revision:

amiro-os / include / amiro / proximity / vcnl4020.hpp @ 05d54823

History | View | Annotate | Download (5.354 KB)

1 58fe0e0b Thomas Schöpping
#ifndef AMIRO_VCNL4020_H_
2
#define AMIRO_VCNL4020_H_
3
4
#include <amiro/bus/i2c/I2CParams.hpp>
5
6
namespace amiro {
7
8
class I2CDriver;
9
10
/**
11
 * VCNL4020 IR Proximity/Ambient Light Sensor Driver
12
 * \todo Interrupt Support
13
 */
14
class VCNL4020 : public chibios_rt::BaseStaticThread<256> {
15
16
    enum { SLA = 0x13u };
17
18
    enum { PRODUCT_ID_REVISION = 0x21u };
19
20
    struct registers {
21
22
      uint8_t  reserved_0x00_0x7F[0x80u];
23
      uint8_t  command;
24
      uint8_t  revision;
25
      uint8_t  proximity_rate;
26
      uint8_t  ir_led_current;
27
      uint8_t  ambient_param;
28
      uint16_t ambient_result; /* BE */
29
      uint16_t proximity_result; /* BE */
30
      uint8_t  interrupt_ctrl;
31
      uint16_t low_thres; /* BE */
32
      uint16_t high_thres; /* BE */
33
      uint8_t  interrupt_status;
34
      uint8_t  proximity_adjust;
35
      uint8_t  ambient_ir_level; /* Do Not Use */
36
    } __attribute__((packed));
37
38
  public:
39
40
    struct VCNL4020Config {
41
42
      uint8_t command;
43
      uint8_t ambient_parameter;
44
      uint8_t ir_led_current_mA;
45
      uint8_t proximity_rate;
46
47
    };
48
49
    enum {
50
51
      PROX_RATE_1_95    = 0x00u, /* 250 / (2**(7-0)) */
52
      PROX_RATE_3_90625 = 0x01u, /* 250 / (2**(7-1)) */
53
      PROX_RATE_7_8125  = 0x02u, /* 250 / (2**(7-2)) */
54
      PROX_RATE_15_625  = 0x03u, /* 250 / (2**(7-3)) */
55
      PROX_RATE_31_25   = 0x04u, /* 250 / (2**(7-4)) */
56
      PROX_RATE_62_5    = 0x05u, /* 250 / (2**(7-5)) */
57
      PROX_RATE_125     = 0x06u, /* 250 / (2**(7-6)) */
58
      PROX_RATE_250     = 0x07u  /* 250 / (2**(7-7)) */
59
60
    };
61
62
    enum {
63
64
      AMBIENT_CONT_CONV   = 0x80u,
65
      AMBIENT_RATE_1      = 0x00u,
66
      AMBIENT_RATE_2      = 0x10u,
67
      AMBIENT_RATE_3      = 0x20u,
68
      AMBIENT_RATE_4      = 0x30u,
69
      AMBIENT_RATE_5      = 0x40u,
70
      AMBIENT_RATE_6      = 0x50u,
71
      AMBIENT_RATE_8      = 0x60u,
72
      AMBIENT_RATE_10     = 0x70u,
73
      AMBIENT_AUTO_OFFSET = 0x10u,
74
      AMBIENT_AVG_1       = 0x00u,
75
      AMBIENT_AVG_2       = 0x01u,
76
      AMBIENT_AVG_4       = 0x02u,
77
      AMBIENT_AVG_8       = 0x03u,
78
      AMBIENT_AVG_16      = 0x04u,
79
      AMBIENT_AVG_32      = 0x05u,
80
      AMBIENT_AVG_64      = 0x06u,
81
      AMBIENT_AVG_128     = 0x07u,
82
83
    };
84
85
    enum {
86
      INT_THRES_SEL     = 0x01u,
87
      INT_THRES_EN      = 0x02u,
88
      INT_ALS_READY_EN  = 0x04u,
89
      INT_PROX_READY_EN = 0x08u,
90
    };
91
92
    enum {
93
      INT_TH_HI      = 0x01u,
94
      INT_TH_LOW     = 0x02u,
95
      INT_ALS_READY  = 0x04u,
96
      INT_PROX_READY = 0x08u,
97
      INT_CNT_1      = 0x00u,
98
      INT_CNT_2      = 0x20u,
99
      INT_CNT_4      = 0x40u,
100
      INT_CNT_8      = 0x60u,
101
      INT_CNT_16     = 0x80u,
102
      INT_CNT_32     = 0xA0u,
103
      INT_CNT_64     = 0xC0u,
104
      INT_CNT_128    = 0xE0u,
105
    };
106
107
    enum {
108
109
      MOD_DLY_MASK    = 0x07u,
110
      MOD_DLY_SHIFT   = 0x05u,
111
      MOD_DEAD_MASK   = 0x07u,
112
      MOD_DEAD_SHIFT  = 0x00u,
113
      PROX_FREQ_390   = 0x00u,
114
      PROX_FREQ_781   = 0x08u,
115
      PROX_FREQ_1_562 = 0x10u,
116
      PROX_FREQ_3_125 = 0x18u,
117
118
    };
119
120
    enum {
121
122
      CONFIG_LOCK   = 0x80u,
123
      ALS_DATA_RDY  = 0x40u,
124
      PROX_DATA_RDY = 0x20u,
125
      ALS_OD        = 0x10u,
126
      PROX_OD       = 0x08u,
127
      ALS_EN        = 0x04u,
128
      PROX_EN       = 0x02u,
129
      SELFTIMED_EN  = 0x01u,
130
131
    };
132
133
    /**
134
     * Return types of getCheck()
135
     */
136
    enum {
137
      CHECK_OK   = 0x00u,
138
      CHECK_FAIL = 0x01u,
139
    };
140
141
    /**
142
     * Return types of calibration
143
     */
144
    enum {
145
      CALIB_OK   = 0x00u,
146
      CALIB_FAIL = 0x01u,
147
    };
148
149
  public:
150
    VCNL4020(I2CDriver *driver, const VCNL4020Config *config);
151
    virtual ~VCNL4020();
152
153
    chibios_rt::EvtSource* getEventSource();
154
155
    /**
156
     * Returns the last measured ambient light value.
157
     *
158
     * @return Measured ambient light value
159
     */
160
    uint16_t getAmbientLight();
161
162
    /**
163
     * Returns the last measured proximity value w/o offset.
164
     * Additionally, the value is scaled, so that full-scale
165
     * as a measurement can be reached.
166
     *
167
     * @return Measured proximity value without offset
168
     */
169
    uint16_t getProximityScaledWoOffset();
170
171
    /**
172
     * Returns the last measured proximity value.
173
     *
174
     * @return Measured proximity value
175
     */
176
    uint16_t getProximity();
177
178
    /**
179
     * Returns the offset of the vcnl4020.
180
     * calibrate() or setOffset() needs to be run first,
181
     * otherwise the return value is 0.
182
     *
183
     * @return Offset value of the device
184
     */
185
    uint16_t getProximityOffset();
186
187
    /**
188
     * Sets the offset value, which will be substracted from
189
     * the measured value.
190
     *
191
     * @param offset
192
     */
193
    void setProximityOffset(uint16_t offset);
194
195
    /**
196
     * Checks the vcnl4020 by reading the product
197
     * id from it an comparing it to the standard
198
     * value
199
     *
200
     * @return [CHECK_OK | CHECK_FAIL]
201
     */
202
    uint8_t getCheck();
203
204
    /**
205
     * Starts the calibration of the vcnl4020 and
206
     *
207
     * @return [ CALIB_OK | CALIB_FAIL]
208
     */
209
    uint8_t calibrate();
210
211
  protected:
212
    virtual msg_t main(void);
213
214
  private:
215
    inline msg_t readIntensities();
216
    inline msg_t writeIRConf();
217
218
    /**
219
     * Starts the offset calibration
220
     *
221
     * @return [ CALIB_OK | CALIB_FAIL]
222
     */
223
    uint8_t calibrateOffset(uint16_t &proximityFloorMeanValue);
224
225
    I2CDriver *driver;
226
    const VCNL4020Config *config;
227
    chibios_rt::EvtSource eventSource;
228
    uint16_t ambient;
229
    uint16_t proximity;
230
    uint16_t proximityOffset;
231
    I2CTxParams tx_params;
232
};
233
234
}
235
236
#endif /* AMIRO_VCNL4020_H_ */