-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbitcommander.ts
More file actions
348 lines (316 loc) · 8.08 KB
/
bitcommander.ts
File metadata and controls
348 lines (316 loc) · 8.08 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Pins used to generate events
*/
enum BCPins
{
//% block="red"
Red = DigitalPin.P12,
//% block="yellow"
Yellow = DigitalPin.P16,
//% block="green"
Green = DigitalPin.P14,
//% block="blue"
Blue = DigitalPin.P15,
//% block="joystick"
Joystick = DigitalPin.P8
}
/**
* Button events
*/
enum BCEvents
{
//% block="down"
Down = DAL.MICROBIT_BUTTON_EVT_UP,
//% block="up"
Up = DAL.MICROBIT_BUTTON_EVT_DOWN
}
/**
* Enumeration of buttons
*/
enum BCButtons
{
//% block="red"
Red,
//% block="yellow"
Yellow,
//% block="green"
Green,
//% block="blue"
Blue,
//% block="joystick"
Joystick
}
/**
* Enumeration of joystick axes
*/
enum BCJoystick
{
//% block="x"
X,
//% block="y"
Y
}
/**
* Update mode for LEDs
* setting to Manual requires show LED changes blocks
* setting to Auto will update the LEDs everytime they change
*/
enum BCMode
{
Manual,
Auto
}
/**
* Pre-Defined LED colours
*/
enum BCColors
{
//% block=red
Red = 0xff0000,
//% block=orange
Orange = 0xffa500,
//% block=yellow
Yellow = 0xffff00,
//% block=green
Green = 0x00ff00,
//% block=blue
Blue = 0x0000ff,
//% block=indigo
Indigo = 0x4b0082,
//% block=violet
Violet = 0x8a2be2,
//% block=purple
Purple = 0xff00ff,
//% block=white
White = 0xffffff,
//% block=black
Black = 0x000000
}
/**
* Custom blocks
*/
//% weight=10 color=#e7660b icon="\uf11b"
namespace bitcommander
{
let band: fireled.Band;
let ledPin = DigitalPin.P13;
let ledCount = 6;
let _updateMode = BCMode.Auto;
let btEnabled = false;
let _initEvents = true;
// Inputs. Buttons, Dial and Joystick
//% shim=bitcommander::init
function init(): void
{
return;
}
/**
* Registers event code
*/
//% weight=90
//% blockId=bcOnEvent block="on button%button|%event"
//% subcategory=Inputs
export function onEvent(button: BCPins, event: BCEvents, handler: Action)
{
init();
control.onEvent(<number>button, <number>event, handler); // register handler
}
/**
* check button states
* @param buttonID Button to check
*/
//% blockId="bcCheckButton" block="button %buttonID|pressed"
//% weight=85
//% subcategory=Inputs
export function readButton(buttonID: BCButtons): boolean
{
switch (buttonID)
{
case BCButtons.Red: return pins.digitalReadPin(DigitalPin.P12)==1; break;
case BCButtons.Yellow: return pins.digitalReadPin(DigitalPin.P16)==1; break;
case BCButtons.Green: return pins.digitalReadPin(DigitalPin.P14)==1; break;
case BCButtons.Blue: return pins.digitalReadPin(DigitalPin.P15)==1; break;
case BCButtons.Joystick: return pins.digitalReadPin(DigitalPin.P8)==1; break;
default: return false;
}
}
/**
* Read dial
*/
//% blockId="bcReadDial" block="dial"
//% weight=90
//% subcategory=Inputs
export function readDial( ): number
{
return pins.analogReadPin(AnalogPin.P0);
}
/**
* Read joystick values
* @param axis Axis to read
*/
//% blockId="bcReadJoystick" block="joystick %axis"
//% weight=90
//% subcategory=Inputs
export function readJoystick(axis: BCJoystick): number
{
if (axis == BCJoystick.X)
return pins.analogReadPin(AnalogPin.P1);
else
return pins.analogReadPin(AnalogPin.P2);
}
// Fireled Helper Blocks
// create a FireLed band if not got one already. Default to brightness 40
// defaults to P13 and 50 LEDs if not specified
function fire(): fireled.Band
{
if (!band)
{
band = fireled.newBand(ledPin, ledCount);
band.setBrightness(40);
}
return band;
}
// update LEDs if _updateMode set to Auto
function updateLEDs(): void
{
if (_updateMode == BCMode.Auto)
ledShow();
}
/**
* Sets all LEDs to a given color (range 0-255 for r, g, b).
* @param rgb RGB color of the LED
*/
//% blockId="bcSetLedColor" block="set all LEDs to%rgb=FireColours"
//% subcategory=Leds
//% weight=100
export function setLedColor(rgb: number)
{
fire().setBand(rgb);
updateLEDs();
}
/**
* Clear all leds.
*/
//% blockId="bcLedClear" block="clear all LEDs"
//% subcategory=Leds
//% weight=90
export function ledClear()
{
fire().clearBand();
updateLEDs();
}
/**
* Set single LED to a given color (range 0-255 for r, g, b).
* @param ledId position of the LED (0 to 5)
* @param rgb RGB color of the LED
*/
//% blockId="bcSetPixelColor" block="set LED at%ledId|to%rgb=FireColours"
//% subcategory=Leds
//% weight=80
export function setPixelColor(ledId: number, rgb: number)
{
fire().setPixel(ledId, rgb);
updateLEDs();
}
/**
* Shows a rainbow pattern on all LEDs.
*/
//% blockId="bcLedRainbow" block="set LED rainbow"
//% subcategory=Leds
//% weight=70
export function ledRainbow()
{
fire().setRainbow();
updateLEDs()
}
/**
* Shift LEDs forward and clear with zeros.
*/
//% blockId="bcLedShift" block="shift LEDs"
//% subcategory=Leds
//% weight=60
export function ledShift()
{
fire().shiftBand();
updateLEDs()
}
/**
* Rotate LEDs forward.
*/
//% blockId="bcLedRotate" block="rotate LEDs"
//% subcategory=Leds
//% weight=50
export function ledRotate()
{
fire().rotateBand();
updateLEDs()
}
// Advanced Fireled blocks
/**
* Set the brightness of the FireLed band
* @param brightness a measure of LED brightness in 0-255. eg: 40
*/
//% blockId="bcLedBrightness" block="set LED brightness%brightness"
//% brightness.min=0 brightness.max=255
//% weight=100
//% advanced=true
export function ledBrightness(brightness: number)
{
fire().setBrightness(brightness);
updateLEDs();
}
/**
* Set LED update mode (Manual or Automatic)
* @param updateMode setting automatic will show LED changes automatically
*/
//% blockId="bcSetUpdateMode" block="set %updateMode|update mode"
//% weight=90
//% advanced=true
export function setUpdateMode(updateMode: BCMode): void
{
_updateMode = updateMode;
}
/**
* Show LED changes
*/
//% blockId="bcLedShow" block="show LED changes"
//% weight=80
//% advanced=true
export function ledShow(): void
{
if (! btEnabled)
fire().updateBand();
}
/**
* Get numeric value of colour
* @param colour Standard RGB Led Colours eg: #ff0000
*/
//% blockId="FireColours" block=%colour
//% advanced=true
//% blockHidden=false
//% weight=70
//% shim=TD_ID colorSecondary="#e7660b"
//% colour.fieldEditor="colornumber"
//% colour.fieldOptions.decompileLiterals=true
//% colour.defl='#ff0000'
//% colour.fieldOptions.colours='["#FF0000","#659900","#18E600","#80FF00","#00FF00","#FF8000","#D82600","#B24C00","#00FFC0","#00FF80","#FFC000","#FF0080","#FF00FF","#B09EFF","#00FFFF","#FFFF00","#8000FF","#0080FF","#0000FF","#FFFFFF","#FF8080","#80FF80","#40C0FF","#999999","#000000"]'
//% colour.fieldOptions.columns=5
//% colour.fieldOptions.className='rgbColorPicker'
export function fireColours(colour: number): number
{
return colour;
}
/**
* Convert from RGB values to colour number
* @param red Red value of the LED (0 to 255)
* @param green Green value of the LED (0 to 255)
* @param blue Blue value of the LED (0 to 255)
*/
//% blockId="bcConvertRGB" block="convert from red%red|green%green|blue%blue"
//% weight=60
//% advanced=true
export function convertRGB(r: number, g: number, b: number): number
{
return ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF);
}
}