source: rtos_arduino/trunk/examples/CompositeExample/Adafruit_VCNL4000/Adafruit_VCNL4000.cpp@ 137

Last change on this file since 137 was 137, checked in by ertl-honda, 8 years ago

サンプルの追加.

File size: 4.1 KB
Line 
1#if ARDUINO >= 100
2 #include "Arduino.h"
3#else
4 #include "WProgram.h"
5#endif
6
7#ifdef __AVR_ATtiny85__
8 #include "TinyWireM.h"
9 #define Wire TinyWireM
10#else
11 #include <Wire.h>
12#endif
13
14#include "Adafruit_VCNL4000.h"
15
16/**************************************************************************/
17/*!
18 @brief Instantiates a new VCNL4000 class
19*/
20/**************************************************************************/
21Adafruit_VCNL4000::Adafruit_VCNL4000() {
22}
23
24/**************************************************************************/
25/*!
26 @brief Setups the HW
27*/
28/**************************************************************************/
29boolean Adafruit_VCNL4000::begin(uint8_t addr) {
30 _i2caddr = addr;
31 Wire.begin();
32
33 uint8_t rev = read8(VCNL4000_PRODUCTID);
34 //Serial.println(rev, HEX);
35 if ((rev & 0xF0) != 0x10) {
36 return false;
37 }
38
39 setLEDcurrent(20);
40 setFrequency(VCNL4000_781K25);
41 write8(VCNL4000_PROXINITYADJUST, 0x81);
42
43 return true;
44}
45
46
47/**************************************************************************/
48/*!
49 @brief Get and set the LED current draw
50*/
51/**************************************************************************/
52
53void Adafruit_VCNL4000::setLEDcurrent(uint8_t c) {
54 if (c > 20) c = 20;
55 write8(VCNL4000_IRLED, c);
56}
57
58uint8_t Adafruit_VCNL4000::getLEDcurrent(void) {
59 return read8(VCNL4000_IRLED);
60}
61
62/**************************************************************************/
63/*!
64 @brief Get and set the measurement signal frequency
65*/
66/**************************************************************************/
67
68void Adafruit_VCNL4000::setFrequency(vcnl4000_freq f) {
69 write8(VCNL4000_SIGNALFREQ, f);
70}
71
72
73/**************************************************************************/
74/*!
75 @brief Get proximity measurement
76*/
77/**************************************************************************/
78
79uint16_t Adafruit_VCNL4000::readProximity(void) {
80 write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY);
81 while (1) {
82 //Serial.println(read8(VCNL4000_INTSTAT), HEX);
83 uint8_t result = read8(VCNL4000_COMMAND);
84 //Serial.print("Ready = 0x"); Serial.println(result, HEX);
85 if (result & VCNL4000_PROXIMITYREADY) {
86 return read16(VCNL4000_PROXIMITYDATA);
87 }
88 delay(1);
89 }
90}
91
92uint16_t Adafruit_VCNL4000::readAmbient(void) {
93 write8(VCNL4000_COMMAND, VCNL4000_MEASUREAMBIENT);
94 while (1) {
95 //Serial.println(read8(VCNL4000_INTSTAT), HEX);
96 uint8_t result = read8(VCNL4000_COMMAND);
97 //Serial.print("Ready = 0x"); Serial.println(result, HEX);
98 if (result & VCNL4000_AMBIENTREADY) {
99 return read16(VCNL4000_AMBIENTDATA);
100 }
101 delay(1);
102 }
103}
104
105/**************************************************************************/
106/*!
107 @brief I2C low level interfacing
108*/
109/**************************************************************************/
110
111
112// Read 1 byte from the VCNL4000 at 'address'
113uint8_t Adafruit_VCNL4000::read8(uint8_t address)
114{
115 uint8_t data;
116
117 Wire.beginTransmission(_i2caddr);
118#if ARDUINO >= 100
119 Wire.write(address);
120#else
121 Wire.send(address);
122#endif
123 Wire.endTransmission();
124
125 delayMicroseconds(170); // delay required
126
127 Wire.requestFrom(_i2caddr, (uint8_t)1);
128 while(!Wire.available());
129
130#if ARDUINO >= 100
131 return Wire.read();
132#else
133 return Wire.receive();
134#endif
135}
136
137
138// Read 2 byte from the VCNL4000 at 'address'
139uint16_t Adafruit_VCNL4000::read16(uint8_t address)
140{
141 uint16_t data;
142
143 Wire.beginTransmission(_i2caddr);
144#if ARDUINO >= 100
145 Wire.write(address);
146#else
147 Wire.send(address);
148#endif
149 Wire.endTransmission();
150
151 Wire.requestFrom(_i2caddr, (uint8_t)2);
152 while(!Wire.available());
153#if ARDUINO >= 100
154 data = Wire.read();
155 data <<= 8;
156 while(!Wire.available());
157 data |= Wire.read();
158#else
159 data = Wire.receive();
160 data <<= 8;
161 while(!Wire.available());
162 data |= Wire.receive();
163#endif
164
165 return data;
166}
167
168// write 1 byte
169void Adafruit_VCNL4000::write8(uint8_t address, uint8_t data)
170{
171 Wire.beginTransmission(_i2caddr);
172#if ARDUINO >= 100
173 Wire.write(address);
174 Wire.write(data);
175#else
176 Wire.send(address);
177 Wire.send(data);
178#endif
179 Wire.endTransmission();
180}
Note: See TracBrowser for help on using the repository browser.