-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
252 lines (230 loc) · 6.08 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* makecode APDS9930 Digital Proximity and Ambient Light Sensor package.
* From microbit/micropython Chinese community.
* http://www.micropython.org.cn
*/
const APDS9930_I2C_ADDRESS = 0x39
const APDS9930_ENABLE = 0x00
const APDS9930_ATIME = 0x01
const APDS9930_PTIME = 0x02
const APDS9930_WTIME = 0x03
const APDS9930_PERS = 0x0C
const APDS9930_CONFIG = 0x0D
const APDS9930_PPULSE = 0x0E
const APDS9930_CONTROL = 0x0F
const APDS9930_Ch0DATAL = 0x14
const APDS9930_Ch0DATAH = 0x15
const APDS9930_Ch1DATAL = 0x16
const APDS9930_Ch1DATAH = 0x17
const APDS9930_PDATAL = 0x18
const APDS9930_PDATAH = 0x19
const APDS9930_POFFSET = 0x1E
// AGAIN
enum APDS9930_AGAIN {
AGAIN_1 = 1,
AGAIN_8 = 8,
AGAIN_16 = 16,
AGAIN_120 = 120
};
// PGAIN
enum APDS9930_PGAIN {
PGAIN_1 = 1,
PGAIN_2 = 2,
PGAIN_4 = 4,
PGAIN_8 = 8
};
// ALS coefficients
const DF = 52
const GA = 490
const B = 1862
const C = 746
const D = 1291
/**
* APDS9930 mudule
*/
//% weight=100 color=#102010 icon="\uf0eb" block="APDS9930"
namespace APDS9930 {
let _wbuf = pins.createBuffer(2);
let _AGAIN = 1;
let _PGAIN = 8;
/**
* set APDS9930's reg
*/
function setReg(reg: number, dat: number): void {
_wbuf[0] = reg | 0xA0;
_wbuf[1] = dat;
pins.i2cWriteBuffer(APDS9930_I2C_ADDRESS, _wbuf);
}
/**
* get a reg
*/
function getReg(reg: number): number {
pins.i2cWriteNumber(APDS9930_I2C_ADDRESS, reg, NumberFormat.UInt8BE);
return pins.i2cReadNumber(APDS9930_I2C_ADDRESS, NumberFormat.UInt8BE);
}
/**
* get two reg, UInt16LE format
*/
function get2Reg(reg: number): number {
pins.i2cWriteNumber(APDS9930_I2C_ADDRESS, reg, NumberFormat.UInt8BE);
return pins.i2cReadNumber(APDS9930_I2C_ADDRESS, NumberFormat.UInt16LE);
}
/**
* get CH0 value
*/
function getCH0(): number {
return get2Reg(APDS9930_Ch0DATAL);
}
/**
* get CH1 value
*/
function getCH1(): number {
return get2Reg(APDS9930_Ch1DATAL);
}
/**
* set ATIME reg
*/
function ATIME(v: number) {
setReg(APDS9930_ATIME, v);
}
/**
* set ALS GAIN
*/
//% blockId="APDS9930_SET_AGAIN" block="set ALS GAIN %gain"
//% weight=100 blockGap=8
export function AGAIN(gain: APDS9930_AGAIN) {
let t = getReg(APDS9930_CONTROL)
t &= 0xFC
switch (gain) {
case APDS9930_AGAIN.AGAIN_8:
t |= 1;
break;
case APDS9930_AGAIN.AGAIN_16:
t |= 2;
break;
case APDS9930_AGAIN.AGAIN_120:
t |= 3;
break;
}
setReg(APDS9930_CONTROL, t)
_AGAIN = gain
}
/**
* set Proximity GAIN
* @param gain is Proximity GAIN, eg: APDS9930_PGAIN.PGAIN_8
*/
//% blockId="APDS9930_SET_PGAIN" block="set Proximity GAIN %gain"
//% weight=100 blockGap=8
export function PGAIN(gain: APDS9930_PGAIN) {
let t = getReg(APDS9930_CONTROL)
t &= 0xF3
switch (gain) {
case APDS9930_PGAIN.PGAIN_2:
t |= 4;
break;
case APDS9930_PGAIN.PGAIN_4:
t |= 8;
break;
case APDS9930_PGAIN.PGAIN_8:
t |= 12;
break;
}
setReg(APDS9930_CONTROL, t)
_PGAIN = gain
}
/**
* Power On
*/
//% blockId="APDS9930_ON" block="Power On"
//% weight=81 blockGap=8
export function PowerOn() {
let t = getReg(APDS9930_ENABLE)
t |= 1
setReg(APDS9930_ENABLE, t)
basic.pause(3)
}
/**
* Power Off
*/
//% blockId="APDS9930_OFF" block="Power Off"
//% weight=80 blockGap=8
export function PowerOff() {
let t = getReg(APDS9930_ENABLE)
t &= 0xFE
setReg(APDS9930_ENABLE, t)
}
/**
* ALS Enable
* @param en is enable/disable ALS, eg: true
*/
//% blockId="APDS9930_ALS_ENABLE" block="ALS Enable %en"
//% weight=120 blockGap=8
export function ALSEnable(en: boolean = true) {
let t = getReg(APDS9930_ENABLE)
t &= 0xFD
if (en) t |= 2
setReg(APDS9930_ENABLE, t)
}
/**
* Proximity Enable
* @param en is enable/disable Proximity, eg: true
*/
//% blockId="APDS9930_Proximity_ENABLE" block="Proximity Enable %en"
//% weight=120 blockGap=8
export function ProximityEnable(en: boolean = true) {
let t = getReg(APDS9930_ENABLE)
t &= 0xFB
if (en) t |= 4
setReg(APDS9930_ENABLE, t)
}
/**
* Wait Enable
* @param en is enable/disable wait timer, eg: true
*/
export function WaitEnable(en: boolean = true) {
let t = getReg(APDS9930_ENABLE)
t &= 0xF7
if (en) t |= 8
setReg(APDS9930_ENABLE, t)
}
/**
* get ALS
*/
//% blockId="APDS9930_GET_ALS" block="get ALS"
//% weight=201 blockGap=8
export function getALS(): number {
let Ch0 = getCH0()
let Ch1 = getCH1()
let ALSIT = 87 * (256 - getReg(APDS9930_ATIME)) >> 5
let IAC = Math.max(Ch0 - Math.idiv(B * Ch1, 1000), Math.idiv(C * Ch0 - D * Ch1, 1000))
IAC = Math.max(IAC, 0)
let LPC = Math.idiv(GA * DF, ALSIT)
return Math.idiv(IAC * LPC, _AGAIN)
}
/**
* get Proximity
*/
//% blockId="APDS9930_GET_Proximity" block="get Proximity"
//% weight=200 blockGap=8
export function getProximity(): number {
return Math.idiv(get2Reg(APDS9930_PDATAL), _PGAIN)
}
/**
* Initialize
*/
//% blockId="APDS9930_INIT" block="APDS9930 Initialize"
//% weight=210 blockGap=8
export function init() {
ATIME(256 - 8)
setReg(APDS9930_ENABLE, 0)
setReg(APDS9930_ATIME, 0xFF)
setReg(APDS9930_PTIME, 0xFF)
setReg(APDS9930_WTIME, 0xFF)
setReg(APDS9930_PERS, 0x22)
setReg(APDS9930_CONFIG, 0)
setReg(APDS9930_PPULSE, 8)
setReg(APDS9930_CONTROL, 0x2C)
ALSEnable()
PowerOn()
}
}