-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
158 lines (142 loc) · 5.28 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
enum Sensitivity {
//% blockIdentity=calliopeBunt.sensitivity
//% block="16496 Lux - 40ms"
S16 = 0b000,
//% blockIdentity=calliopeBunt.sensitivity
//% block="8248 Lux - 80ms"
S8 = 0b001,
//% blockIdentity=calliopeBunt.sensitivity
//% block="4124 Lux - 160ms"
S4 = 0b010,
//% blockIdentity=calliopeBunt.sensitivity
//% block="2062 Lux - 320ms"
S2 = 0b011,
//% blockIdentity=calliopeBunt.sensitivity
//% block="1031 Lux - 640ms"
S1 = 0b100,
//% blockIdentity=calliopeBunt.sensitivity
//% block="515 Lux - 1280ms"
S0 = 0b101,
}
/**
* Read data from an Calliope mini RGB sensor.
*/
//% color=#A80000
namespace sensors {
const ADDRESS = 0x10;
let colorSensitivity = Sensitivity.S4;
/**
* Converts the sensitivity name to a number.
*/
//% blockId=sensitivity_id block="%c" shim=TD_ID
export function sensitivity(c: Sensitivity): number {
return c;
}
/**
* Set sensivity for the Calliope mini RGB sensor.
*/
//% blockId=calliopeBunt_sensitivity block="set light sensitivity to %sensitivity=sensitivity_id"
//% parts="calliope-bunt"
//% trackArgs=0
export function calliopeBuntSetSensitivity(sensitivity: number): void {
colorSensitivity = sensitivity > Sensitivity.S0 ? Sensitivity.S0 : sensitivity;
}
/**
* Get RGBW values from Calliope mini RGB sensor.
*/
//% blockId=calliopeBunt_rgb block="read light color"
//% parts="calliope-bunt"
//% trackArgs=0
export function calliopeBuntColor(): number {
// enable the sensor, writing to the register
pins.i2cWriteNumber(ADDRESS, (0x00 << 8) | (colorSensitivity << 4), NumberFormat.UInt16BE);
// red
pins.i2cWriteNumber(ADDRESS, 0x08, NumberFormat.Int8LE, true);
let red = pins.map(pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE), 0, 65535, 0, 255);
// green
pins.i2cWriteNumber(ADDRESS, 0x09, NumberFormat.Int8LE, true);
let green = pins.map(pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE), 0, 65535, 0, 255);
// blue
pins.i2cWriteNumber(ADDRESS, 0x0A, NumberFormat.Int8LE, true);
let blue = pins.map(pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE), 0, 65535, 0, 255);
// white
pins.i2cWriteNumber(ADDRESS, 0x0B, NumberFormat.Int8LE, true);
let white = pins.map(pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE), 0, 65535, 0, 255);
// -- enable for debugging
// serial.writeString("RGB[");
// serial.writeNumber(red);
// serial.writeString(",");
// serial.writeNumber(green);
// serial.writeString(",");
// serial.writeNumber(blue);
// serial.writeString("](");
// serial.writeNumber(white);
// serial.writeString(")\r\n");
// shift colors down (only need the high byte)
return basic.rgbw(red, green, blue, white);
}
/**
* Get RGBW values from Calliope mini RGB sensor.
*/
//% blockId=calliopeBunt_green block="erkannte Grün Farbe"
//% parts="calliope-bunt"
//% trackArgs=0
export function calliopeBuntGreen(): number {
// green
pins.i2cWriteNumber(ADDRESS, 0x09, NumberFormat.Int8LE, true);
let green = pins.map(pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE), 0, 65535, 0, 255);
// -- enable for debugging
// serial.writeString("G[");
// serial.writeNumber(green);
// serial.writeString("]\r\n");
return green;
}
/**
* Get RGBW values from Calliope mini RGB sensor.
*/
//% blockId=calliopeBunt_red block="erkannte Rot Farbe"
//% parts="calliope-bunt"
//% trackArgs=0
export function calliopeBuntRed(): number {
// red
pins.i2cWriteNumber(ADDRESS, 0x08, NumberFormat.Int8LE, true);
let red = pins.map(pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE), 0, 65535, 0, 255);
// -- enable for debugging
// serial.writeString("R[");
// serial.writeNumber(red);
// serial.writeString("]\r\n");
return red;
}
/**
* Get RGBW values from Calliope mini RGB sensor.
*/
//% blockId=calliopeBunt_blue block="erkannte Blau Farbe"
//% parts="calliope-bunt"
//% trackArgs=0
export function calliopeBuntBlue(): number {
// blue
pins.i2cWriteNumber(ADDRESS, 0x0A, NumberFormat.Int8LE, true);
let blue = pins.map(pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE), 0, 65535, 0, 255);
// -- enable for debugging
// serial.writeString("B[");
// serial.writeNumber(blue);
// serial.writeString("]\r\n");
return blue;
}
/**
* Get RGBW values from Calliope mini RGB sensor.
*/
//% blockId=calliopeBunt_white block="erkannte Helligkeit"
//% parts="calliope-bunt"
//% trackArgs=0
export function calliopeBuntWhite(): number {
// white
pins.i2cWriteNumber(ADDRESS, 0x0B, NumberFormat.Int8LE, true);
let white = pins.map(pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE), 0, 65535, 0, 255);
// -- enable for debugging
// serial.writeString("W[");
// serial.writeNumber(white);
// serial.writeString("]\r\n");
return white;
}
}