forked from YFROBOT-TM/pxt-yfrobot-colorsensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
281 lines (245 loc) · 11.2 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @file TCS3472X 颜色传感器
* @brief YFROBOT's sensors makecode library.
* @n This is a MakeCode graphics programming extension for YFROBOT's sensors module.
*
* @copyright YFROBOT,2022
* @copyright MIT Lesser General Public License
*
* @author [email]([email protected])
* @date 2022-01-08
*/
enum RGB {
//% block="红"
RED,
//% block="绿"
GREEN,
//% block="蓝"
BLUE
}
enum RGB_RAW {
//% block="红"
RED,
//% block="绿"
GREEN,
//% block="蓝"
BLUE,
//% block="明光感应值"
CLEAR
}
//% weight=6 color=#45b787 icon="\uf12e" block="颜色传感器"
namespace TCS3472X {
enum LCS_Constants {
// Constants
ADDRESS = 0x29,
ID = 0x12, // Register should be equal to 0x44 for the TCS34721 or TCS34725, or 0x4D for the TCS34723 or TCS34727.
COMMAND_BIT = 0x80,
ENABLE = 0x00,
ENABLE_AIEN = 0x10, // RGBC Interrupt Enable
ENABLE_WEN = 0x08, // Wait enable - Writing 1 activates the wait timer
ENABLE_AEN = 0x02, // RGBC Enable - Writing 1 actives the ADC, 0 disables it
ENABLE_PON = 0x01, // Power on - Writing 1 activates the internal oscillator, 0 disables it
ATIME = 0x01, // Integration time
WTIME = 0x03, // Wait time (if ENABLE_WEN is asserted)
AILTL = 0x04, // Clear channel lower interrupt threshold
AILTH = 0x05,
AIHTL = 0x06, // Clear channel upper interrupt threshold
AIHTH = 0x07,
PERS = 0x0C, // Persistence register - basic SW filtering mechanism for interrupts
PERS_NONE = 0x00, // Every RGBC cycle generates an interrupt
PERS_1_CYCLE = 0x01, // 1 clean channel value outside threshold range generates an interrupt
PERS_2_CYCLE = 0x02, // 2 clean channel values outside threshold range generates an interrupt
PERS_3_CYCLE = 0x03, // 3 clean channel values outside threshold range generates an interrupt
PERS_5_CYCLE = 0x04, // 5 clean channel values outside threshold range generates an interrupt
PERS_10_CYCLE = 0x05, // 10 clean channel values outside threshold range generates an interrupt
PERS_15_CYCLE = 0x06, // 15 clean channel values outside threshold range generates an interrupt
PERS_20_CYCLE = 0x07, // 20 clean channel values outside threshold range generates an interrupt
PERS_25_CYCLE = 0x08, // 25 clean channel values outside threshold range generates an interrupt
PERS_30_CYCLE = 0x09, // 30 clean channel values outside threshold range generates an interrupt
PERS_35_CYCLE = 0x0A, // 35 clean channel values outside threshold range generates an interrupt
PERS_40_CYCLE = 0x0B, // 40 clean channel values outside threshold range generates an interrupt
PERS_45_CYCLE = 0x0C, // 45 clean channel values outside threshold range generates an interrupt
PERS_50_CYCLE = 0x0D, // 50 clean channel values outside threshold range generates an interrupt
PERS_55_CYCLE = 0x0E, // 55 clean channel values outside threshold range generates an interrupt
PERS_60_CYCLE = 0x0F, // 60 clean channel values outside threshold range generates an interrupt
CONFIG = 0x0D,
CONFIG_WLONG = 0x02, // Choose between short and long (12x) wait times via WTIME
CONTROL = 0x0F, // Set the gain level for the sensor
STATUS = 0x13,
STATUS_AINT = 0x10, // RGBC Clean channel interrupt
STATUS_AVALID = 0x01, // Indicates that the RGBC channels have completed an integration cycle
CDATAL = 0x14, // Clear channel data
CDATAH = 0x15,
RDATAL = 0x16, // Red channel data
RDATAH = 0x17,
GDATAL = 0x18, // Green channel data
GDATAH = 0x19,
BDATAL = 0x1A, // Blue channel data
BDATAH = 0x1B,
GAIN_1X = 0x00, // 1x gain
GAIN_4X = 0x01, // 4x gain
GAIN_16X = 0x02, // 16x gain
GAIN_60X = 0x03 // 60x gain
}
let LCS_integration_time_val = 0
// I2C functions
function I2C_WriteReg8(addr: number, reg: number, val: number) {
let buf = pins.createBuffer(2)
buf.setNumber(NumberFormat.UInt8BE, 0, reg)
buf.setNumber(NumberFormat.UInt8BE, 1, val)
pins.i2cWriteBuffer(addr, buf)
}
function I2C_ReadReg8(addr: number, reg: number): number {
let buf = pins.createBuffer(1)
buf.setNumber(NumberFormat.UInt8BE, 0, reg)
pins.i2cWriteBuffer(addr, buf)
buf = pins.i2cReadBuffer(addr, 1)
return buf.getNumber(NumberFormat.UInt8BE, 0);
}
function I2C_ReadReg16(addr: number, reg: number): number {
let buf = pins.createBuffer(1)
buf.setNumber(NumberFormat.UInt8BE, 0, reg)
pins.i2cWriteBuffer(addr, buf)
buf = pins.i2cReadBuffer(addr, 2)
// Little endian
return ((buf.getNumber(NumberFormat.UInt8BE, 1) << 8) | buf.getNumber(NumberFormat.UInt8BE, 0));
}
//% blockId="initialize_sensor" block="初始化颜色传感器"
//% weight=100
export function LCS_initialize() {
// Make sure we're connected to the right sensor.
let chip_id = I2C_ReadReg8(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.ID))
if (chip_id != 0x44 && chip_id != 0x4D) {
return // Incorrect chip ID
}
// Set default integration time and gain.
LCS_set_integration_time(0.0048)
LCS_set_gain(LCS_Constants.GAIN_16X)
// Enable the device (by default, the device is in power down mode on bootup).
LCS_enable()
}
function LCS_enable() {
// Set the power and enable bits.
I2C_WriteReg8(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.ENABLE), LCS_Constants.ENABLE_PON)
basic.pause(10) // not sure if this is right time.sleep(0.01) // FIXME delay for 10ms
I2C_WriteReg8(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.ENABLE), (LCS_Constants.ENABLE_PON | LCS_Constants.ENABLE_AEN))
}
function LCS_set_integration_time(time: number) {
let val = 0x100 - (time / 0.0024) // FIXME was cast to int type
if (val > 255) {
val = 255
} else if (val < 0) {
val = 0
}
I2C_WriteReg8(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.ATIME), val)
LCS_integration_time_val = val
}
function LCS_set_gain(gain: number) {
I2C_WriteReg8(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.CONTROL), gain)
}
function LCS_set_led_state(state: boolean) {
I2C_WriteReg8(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.PERS), LCS_Constants.PERS_NONE)
let val = I2C_ReadReg8(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.ENABLE))
if (state) {
val |= LCS_Constants.ENABLE_AIEN
} else {
val &= ~LCS_Constants.ENABLE_AIEN
}
I2C_WriteReg8(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.ENABLE), val)
basic.pause(2 * (256 - LCS_integration_time_val) * 2.4) // delay for long enough for there to be new (post-change) complete values available
}
// // blockId="getColor" block="读取颜色的颜色是"
// export function getColor(): RGB {
// basic.pause((256 - LCS_integration_time_val) * 2.4);
// let r = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.RDATAL));
// let g = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.GDATAL));
// let b = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.BDATAL));
// // serial.writeLine("R:"+r + " G:" + g + " B:" + b);
// let color = RGB.RED;
// let max = r;
// if(g > max){
// max = g;
// color = RGB.GREEN;
// }
// if(b > max){
// max = b;
// color = RGB.BLUE;
// }
// // serial.writeLine("val: " + color);
// return color;
// }
// // blockId="colorType" block="颜色值 %colorType"
// export function colorType(colorType:RGB): RGB{
// return colorType;
// }
//% blockId="getColorRawData" block="读取传感器原始值 %colorId"
//% weight=5
export function getColorRawData(color: RGB_RAW): number {
basic.pause((256 - LCS_integration_time_val) * 2.4);
let vue = 0;
switch (color) {
case RGB_RAW.RED:
vue = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.RDATAL));
break;
case RGB_RAW.GREEN:
vue = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.GDATAL));
break;
case RGB_RAW.BLUE:
vue = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.BDATAL));
break;
case RGB_RAW.CLEAR:
vue = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.CDATAL));
break;
}
return vue;
}
//% blockId="getColorData" block="读取颜色值 %colorId"
//% weight=95
export function getColorData(color: RGB): number {
basic.pause((256 - LCS_integration_time_val) * 2.4);
let sum = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.CDATAL));
// Avoid divide by zero errors ... if clear = 0 return black
if(sum == 0){
return 0;
}
let vue = 0;
switch (color) {
case RGB.RED:
vue = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.RDATAL));
break;
case RGB.GREEN:
vue = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.GDATAL));
break;
case RGB.BLUE:
vue = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.BDATAL));
break;
}
vue = Math.floor(vue / sum * 255);
return vue;
}
function LCS_get_raw_data(delay: boolean = false): number[] {
if (delay) {
// Delay for the integration time to allow reading immediately after the previous read.
basic.pause((256 - LCS_integration_time_val) * 2.4)
}
let div = (256 - LCS_integration_time_val) * 1024
let rgbc = [0, 0, 0, 0]
rgbc[0] = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.RDATAL)) / div
rgbc[1] = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.GDATAL)) / div
rgbc[2] = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.BDATAL)) / div
rgbc[3] = I2C_ReadReg16(LCS_Constants.ADDRESS, (LCS_Constants.COMMAND_BIT | LCS_Constants.CDATAL)) / div
if (rgbc[0] > 1) {
rgbc[0] = 1
}
if (rgbc[1] > 1) {
rgbc[1] = 1
}
if (rgbc[2] > 1) {
rgbc[2] = 1
}
if (rgbc[3] > 1) {
rgbc[3] = 1
}
return rgbc
}
}