forked from CytronTechnologies/pxt-edubit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic_light_bit.ts
128 lines (103 loc) · 3.13 KB
/
traffic_light_bit.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
/*******************************************************************************
* Functions for edu:bit - Traffic Light Bit.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Color for traffic light LED.
enum LedColor {
//% block="red"
Red = DigitalPin.P14,
//% block="yellow"
Yellow = DigitalPin.P15,
//% block="green"
Green = DigitalPin.P16,
//% block="all"
All = 1000,
};
// IO state.
enum DigitalIoState {
//% block="off"
Off = 0,
//% block="on"
On = 1,
}
/**
* Blocks for Traffic Light Bit.
*/
//% weight=11 color=#ff8000 icon="\uf0eb" block="Traffic Light Bit"
namespace edubitTrafficLightBit {
// State for each pin.
let ledState = {
red: 0,
yellow: 0,
green: 0
};
/**
* Turn on/off the LED (On = 1, Off = 0).
* @param color LED color.
* @param state LED state.
*/
//% weight=20
//% blockGap=8
//% blockId=edubit_set_led
//% block="set LED %color to %state"
//% state.shadow=edubit_digital_state_picker
export function setLed(color: LedColor, state: number): void {
// Limit the number.
state = edubit.limit(state, 0, 1);
// Save the pin state.
switch (color) {
case LedColor.Red: ledState.red = state; break;
case LedColor.Yellow: ledState.yellow = state; break;
case LedColor.Green: ledState.green = state; break;
case LedColor.All:
ledState.red = state;
ledState.yellow = state;
ledState.green = state;
break;
}
// Write to pin.
pins.digitalWritePin(<number>LedColor.Red, ledState.red);
pins.digitalWritePin(<number>LedColor.Yellow, ledState.yellow);
pins.digitalWritePin(<number>LedColor.Green, ledState.green);
}
/**
* Toggle the LED.
* @param color LED color.
*/
//% weight=19
//% blockGap=8
//% blockId=edubit_toggle_led
//% block="Toggle LED %color"
export function toggleLed(color: LedColor): void {
// Toggle the state.
let state = 0;
switch (color) {
case LedColor.Red: ledState.red ^= 1; break;
case LedColor.Yellow: ledState.yellow ^= 1; break;
case LedColor.Green: ledState.green ^= 1; break;
case LedColor.All:
ledState.red ^= 1;
ledState.yellow ^= 1;
ledState.green ^= 1;
break;
}
// Write to pin.
setLed(LedColor.Red, ledState.red);
setLed(LedColor.Yellow, ledState.yellow);
setLed(LedColor.Green, ledState.green);
}
/**
* Get the digital IO state field editor.
* @param state Digital IO state. eg: DigitalIoState.On
*/
//% blockHidden=true
//% colorSecondary="#ff8000"
//% blockId="edubit_digital_state_picker"
//% block="%state"
export function digitalStatePicker(state: DigitalIoState): number {
return <number>state;
}
}