8
8
"time"
9
9
10
10
"tinygo.org/x/drivers"
11
+ "tinygo.org/x/drivers/internal/legacy"
11
12
)
12
13
13
14
// Device wraps an I2C connection to a APDS-9960 device.
@@ -68,7 +69,7 @@ func New(bus drivers.I2C) Device {
68
69
// It does a "who am I" request and checks the response.
69
70
func (d * Device ) Connected () bool {
70
71
data := []byte {0 }
71
- d . bus . ReadRegister (d .Address , APDS9960_ID_REG , data )
72
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_ID_REG , data )
72
73
return data [0 ] == 0xAB
73
74
}
74
75
@@ -80,21 +81,21 @@ func (d *Device) GetMode() uint8 {
80
81
// DisableAll turns off the device and all functions
81
82
func (d * Device ) DisableAll () {
82
83
d .enable (enableConfig {})
83
- d . bus . WriteRegister (d .Address , APDS9960_GCONF4_REG , []byte {0x00 })
84
+ legacy . WriteRegister (d . bus , d .Address , APDS9960_GCONF4_REG , []byte {0x00 })
84
85
d .mode = MODE_NONE
85
86
d .gesture .detected = GESTURE_NONE
86
87
}
87
88
88
89
// SetProximityPulse sets proximity pulse length (4, 8, 16, 32) and count (1~64)
89
90
// default: 16, 64
90
91
func (d * Device ) SetProximityPulse (length , count uint8 ) {
91
- d . bus . WriteRegister (d .Address , APDS9960_PPULSE_REG , []byte {getPulseLength (length )<< 6 | getPulseCount (count )})
92
+ legacy . WriteRegister (d . bus , d .Address , APDS9960_PPULSE_REG , []byte {getPulseLength (length )<< 6 | getPulseCount (count )})
92
93
}
93
94
94
95
// SetGesturePulse sets gesture pulse length (4, 8, 16, 32) and count (1~64)
95
96
// default: 16, 64
96
97
func (d * Device ) SetGesturePulse (length , count uint8 ) {
97
- d . bus . WriteRegister (d .Address , APDS9960_GPULSE_REG , []byte {getPulseLength (length )<< 6 | getPulseCount (count )})
98
+ legacy . WriteRegister (d . bus , d .Address , APDS9960_GPULSE_REG , []byte {getPulseLength (length )<< 6 | getPulseCount (count )})
98
99
}
99
100
100
101
// SetADCIntegrationCycles sets ALS/color ADC internal integration cycles (1~256, 1 cycle = 2.78 ms)
@@ -103,14 +104,14 @@ func (d *Device) SetADCIntegrationCycles(cycles uint16) {
103
104
if cycles > 256 {
104
105
cycles = 256
105
106
}
106
- d . bus . WriteRegister (d .Address , APDS9960_ATIME_REG , []byte {uint8 (256 - cycles )})
107
+ legacy . WriteRegister (d . bus , d .Address , APDS9960_ATIME_REG , []byte {uint8 (256 - cycles )})
107
108
}
108
109
109
110
// SetGains sets proximity/gesture gain (1, 2, 4, 8x) and ALS/color gain (1, 4, 16, 64x)
110
111
// default: 1, 1, 4
111
112
func (d * Device ) SetGains (proximityGain , gestureGain , colorGain uint8 ) {
112
- d . bus . WriteRegister (d .Address , APDS9960_CONTROL_REG , []byte {getProximityGain (proximityGain )<< 2 | getALSGain (colorGain )})
113
- d . bus . WriteRegister (d .Address , APDS9960_GCONF2_REG , []byte {getProximityGain (gestureGain ) << 5 })
113
+ legacy . WriteRegister (d . bus , d .Address , APDS9960_CONTROL_REG , []byte {getProximityGain (proximityGain )<< 2 | getALSGain (colorGain )})
114
+ legacy . WriteRegister (d . bus , d .Address , APDS9960_GCONF2_REG , []byte {getProximityGain (gestureGain ) << 5 })
114
115
}
115
116
116
117
// LEDBoost sets proximity and gesture LED current level (100, 150, 200, 300 (%))
@@ -127,7 +128,7 @@ func (d *Device) LEDBoost(percent uint16) {
127
128
case 300 :
128
129
v = 3
129
130
}
130
- d . bus . WriteRegister (d .Address , APDS9960_CONFIG2_REG , []byte {0x01 | v << 4 })
131
+ legacy . WriteRegister (d . bus , d .Address , APDS9960_CONFIG2_REG , []byte {0x01 | v << 4 })
131
132
}
132
133
133
134
// Setthreshold sets threshold (0~255) for detecting gestures
@@ -168,7 +169,7 @@ func (d *Device) ReadProximity() (proximity int32) {
168
169
return 0
169
170
}
170
171
data := []byte {0 }
171
- d . bus . ReadRegister (d .Address , APDS9960_PDATA_REG , data )
172
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_PDATA_REG , data )
172
173
return 255 - int32 (data [0 ])
173
174
}
174
175
@@ -195,14 +196,14 @@ func (d *Device) ReadColor() (r int32, g int32, b int32, clear int32) {
195
196
return
196
197
}
197
198
data := []byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
198
- d . bus . ReadRegister (d .Address , APDS9960_CDATAL_REG , data [:1 ])
199
- d . bus . ReadRegister (d .Address , APDS9960_CDATAH_REG , data [1 :2 ])
200
- d . bus . ReadRegister (d .Address , APDS9960_RDATAL_REG , data [2 :3 ])
201
- d . bus . ReadRegister (d .Address , APDS9960_RDATAH_REG , data [3 :4 ])
202
- d . bus . ReadRegister (d .Address , APDS9960_GDATAL_REG , data [4 :5 ])
203
- d . bus . ReadRegister (d .Address , APDS9960_GDATAH_REG , data [5 :6 ])
204
- d . bus . ReadRegister (d .Address , APDS9960_BDATAL_REG , data [6 :7 ])
205
- d . bus . ReadRegister (d .Address , APDS9960_BDATAH_REG , data [7 :])
199
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_CDATAL_REG , data [:1 ])
200
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_CDATAH_REG , data [1 :2 ])
201
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_RDATAL_REG , data [2 :3 ])
202
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_RDATAH_REG , data [3 :4 ])
203
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_GDATAL_REG , data [4 :5 ])
204
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_GDATAH_REG , data [5 :6 ])
205
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_BDATAL_REG , data [6 :7 ])
206
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_BDATAH_REG , data [7 :])
206
207
clear = int32 (uint16 (data [1 ])<< 8 | uint16 (data [0 ]))
207
208
r = int32 (uint16 (data [3 ])<< 8 | uint16 (data [2 ]))
208
209
g = int32 (uint16 (data [5 ])<< 8 | uint16 (data [4 ]))
@@ -234,13 +235,13 @@ func (d *Device) GestureAvailable() bool {
234
235
data := []byte {0 , 0 , 0 , 0 }
235
236
236
237
// check GVALID
237
- d . bus . ReadRegister (d .Address , APDS9960_GSTATUS_REG , data [:1 ])
238
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_GSTATUS_REG , data [:1 ])
238
239
if data [0 ]& 0x01 == 0 {
239
240
return false
240
241
}
241
242
242
243
// get number of data sets available in FIFO
243
- d . bus . ReadRegister (d .Address , APDS9960_GFLVL_REG , data [:1 ])
244
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_GFLVL_REG , data [:1 ])
244
245
availableDataSets := data [0 ]
245
246
if availableDataSets == 0 {
246
247
return false
@@ -249,10 +250,10 @@ func (d *Device) GestureAvailable() bool {
249
250
// read up, down, left and right proximity data from FIFO
250
251
var dataSets [32 ][4 ]uint8
251
252
for i := uint8 (0 ); i < availableDataSets ; i ++ {
252
- d . bus . ReadRegister (d .Address , APDS9960_GFIFO_U_REG , data [:1 ])
253
- d . bus . ReadRegister (d .Address , APDS9960_GFIFO_D_REG , data [1 :2 ])
254
- d . bus . ReadRegister (d .Address , APDS9960_GFIFO_L_REG , data [2 :3 ])
255
- d . bus . ReadRegister (d .Address , APDS9960_GFIFO_R_REG , data [3 :4 ])
253
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_GFIFO_U_REG , data [:1 ])
254
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_GFIFO_D_REG , data [1 :2 ])
255
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_GFIFO_L_REG , data [2 :3 ])
256
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_GFIFO_R_REG , data [3 :4 ])
256
257
for j := uint8 (0 ); j < 4 ; j ++ {
257
258
dataSets [i ][j ] = data [j ]
258
259
}
@@ -385,7 +386,7 @@ func (d *Device) enable(cfg enableConfig) {
385
386
}
386
387
387
388
data := []byte {gen << 6 | pien << 5 | aien << 4 | wen << 3 | pen << 2 | aen << 1 | pon }
388
- d . bus . WriteRegister (d .Address , APDS9960_ENABLE_REG , data )
389
+ legacy . WriteRegister (d . bus , d .Address , APDS9960_ENABLE_REG , data )
389
390
390
391
if cfg .PON {
391
392
time .Sleep (time .Millisecond * 10 )
@@ -394,7 +395,7 @@ func (d *Device) enable(cfg enableConfig) {
394
395
395
396
func (d * Device ) readStatus (param string ) bool {
396
397
data := []byte {0 }
397
- d . bus . ReadRegister (d .Address , APDS9960_STATUS_REG , data )
398
+ legacy . ReadRegister (d . bus , d .Address , APDS9960_STATUS_REG , data )
398
399
399
400
switch param {
400
401
case "CPSAT" :
0 commit comments