forked from CytronTechnologies/pxt-edubit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput
279 lines (229 loc) · 9.06 KB
/
input
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
/*******************************************************************************
* Functions for edu:bit input.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Possible pins for digital input.
enum DigitalInputPin {
//% block="IR Sensor"
IR = DigitalPin.P8,
//% block="Sound Sensor"
SOUND = DigitalPin.P16,
P0 = DigitalPin.P0,
P1 = DigitalPin.P1,
P2 = DigitalPin.P2,
P8 = DigitalPin.P8,
P12 = DigitalPin.P12,
P13 = DigitalPin.P13,
P14 = DigitalPin.P14,
P15 = DigitalPin.P15,
P16 = DigitalPin.P16,
}
// Possible pins for analog input.
enum AnalogInputPin {
//% block="Potentiometer"
POT = AnalogPin.P1,
//% block="Sound Sensor"
SOUND = AnalogPin.P2,
P0 = AnalogPin.P0,
P1 = AnalogPin.P1,
P2 = AnalogPin.P2,
}
// Analog input comparison type.
enum AnalogInputCompareType {
//% block=">"
MoreThan = 0,
//% block="<"
LessThan = 1
};
// Digital input trigger state.
enum DigitalInputTriggerState {
//% block="High"
High = 1,
//% block="Low"
Low = 0,
}
/**
* Blocks for input
*/
//% weight=21 color=#ff8000 icon="\uf060" block="Input"
//% groups=['Analog Input', 'Digital Input']
namespace edubit_input {
// Indicate whether background function has been created.
let bgFunctionCreated = false;
// Event type.
let AnalogEventType = 0;
// Array for compare type, threshold and pin number.
let compareTypesArray: AnalogInputCompareType[] = [];
let thresholdsArray: number[] = [];
let pinsArray: AnalogInputPin[] = [];
// Array for old compare result.
let oldCompareResult: boolean[] = [];
/**
* Return analog input value (0-1023).
* @param pin Pin number for analog input. eg: AnalogInputPin.POT
*/
//% group="Analog Input"
//% blockGap=8
//% blockId=edubit_read_analog_input
//% block="Read analog input %pin"
export function readAnalogInput(pin: AnalogInputPin): number {
return pins.analogReadPin(<number>pin);
}
/**
* Compare the analog input value (0-1023) with a number and return the result (true/false).
* @param pin Pin number for analog input. eg: AnalogInputPin.POT
* @param compareType More than or less than. eg: AnalogInputCompareType.MoreThan
* @param threshold The value to compare with. eg: 0, 512, 1023
*/
//% group="Analog Input"
//% blockGap=30
//% blockId=edubit_compare_analog_input
//% block="Analog input %pin %compareType %threshold"
//% threshold.min=0 threshold.max=1023
export function compareAnalogInput(pin: AnalogInputPin, compareType: AnalogInputCompareType, threshold: number): boolean {
let result = false;
switch (compareType) {
case AnalogInputCompareType.MoreThan:
if (readAnalogInput(pin) > threshold) {
result = true;
}
break;
case AnalogInputCompareType.LessThan:
if (readAnalogInput(pin) < threshold) {
result = true;
}
break;
}
return result;
}
/**
* Compare the analog input value (0-1023) with a number and do something when true.
* @param pin Pin number for analog input. eg: AnalogInputPin.POT
* @param compareType More than or less than. eg: AnalogInputCompareType.MoreThan
* @param threshold The value to compare with. eg: 0, 512, 1023
*/
//% group="Analog Input"
//% blockGap=8
//% blockId=edubit_on_analog_input_event
//% block="On analog input %pin %compareType %threshold"
//% threshold.min=0 threshold.max=1023
export function onAnalogInputEvent(pin: AnalogInputPin, compareType: AnalogInputCompareType, threshold: number, handler: Action): void {
// Use a new event type everytime a new event is create.
AnalogEventType++;
// Add the event info to the arrays.
compareTypesArray.push(compareType);
thresholdsArray.push(threshold);
pinsArray.push(pin);
// Create a placeholder for the old compare result.
oldCompareResult.push(false);
// Register the event.
control.onEvent(getAnalogEventSource(pin), AnalogEventType, handler);
// Create a function in background if haven't done so.
// This function will check for pot value and raise the event if the condition is met.
if (bgFunctionCreated == false) {
control.inBackground(function () {
while (true) {
// Loop for all the event created.
for (let i = 0; i < AnalogEventType; i++) {
// Check if the condition is met.
if (compareAnalogInput(pinsArray[i], compareTypesArray[i], thresholdsArray[i]) == true) {
// Raise the event if the compare result changed from false to true.
if (oldCompareResult[i] == false) {
control.raiseEvent(getAnalogEventSource(pinsArray[i]), i + 1);
}
// Save old compare result.
oldCompareResult[i] = true;
}
else {
// Save old compare result.
oldCompareResult[i] = false;
}
basic.pause(20)
}
}
});
bgFunctionCreated = true;
}
}
/**
* Get the analog input event source based on pin number.
*/
function getAnalogEventSource(pin: AnalogInputPin): EventBusSource {
// Get the event source based on pin number.
switch (<number>pin) {
case <number>AnalogPin.P0: return EventBusSource.MICROBIT_ID_IO_P0;
case <number>AnalogPin.P1: return EventBusSource.MICROBIT_ID_IO_P1;
case <number>AnalogPin.P2: return EventBusSource.MICROBIT_ID_IO_P2;
}
return null;
}
/**
* Return digital input state (0 or 1).
* @param pin Pin number for digital input. eg: DigitalInputPin.IR
*/
//% group="Digital Input"
//% blockGap=8
//% blockId=edubit_read_digital_input
//% block="Read digital input %pin"
export function readDigitalInput(pin: DigitalInputPin): number {
return pins.digitalReadPin(<number>pin);
}
/**
* Return true if digital input is triggered.
* @param pin Pin number for digital input. eg: DigitalInputPin.IR
* @param triggerState Digital input trigger state. eg: DigitalInputTriggerState.High
*/
//% group="Digital Input"
//% blockGap=30
//% blockId=edubit_is_digital_input_triggered
//% block="Digital input %pin is %triggerState"
export function isDigitalInputHigh(pin: DigitalInputPin, triggerState: DigitalInputTriggerState): boolean {
if (pins.digitalReadPin(<number>pin) == <number>triggerState) {
return true;
}
else {
return false;
}
}
/**
* Do something when digital input is triggered.
* @param pin Pin number for digital input. eg: DigitalInputPin.IR
* @param triggerState Digital input trigger state. eg: DigitalInputTriggerState.High
*/
//% group="Digital Input"
//% blockGap=8
//% blockId=edubit_on_digital_input_event
//% block="On digital Input %pin is %triggerState"
export function onDigitalInputEvent(pin: DigitalInputPin, triggerState: DigitalInputTriggerState, handler: Action) {
// Set the event type as edge triggered.
pins.setEvents(<number>pin, PinEventType.Edge);
// Get the event type.
let digitalEventType = EventBusValue.MICROBIT_PIN_EVT_FALL;
if (triggerState == DigitalInputTriggerState.High) {
digitalEventType = EventBusValue.MICROBIT_PIN_EVT_RISE;
}
// Register the event.
control.onEvent(getDigitalEventSource(pin), digitalEventType, handler);
}
/**
* Get the digital input event source based on pin number.
*/
function getDigitalEventSource(pin: DigitalInputPin): EventBusSource {
// Get the event source based on pin number.
switch (<number>pin) {
case <number>DigitalPin.P0: return EventBusSource.MICROBIT_ID_IO_P0;
case <number>DigitalPin.P1: return EventBusSource.MICROBIT_ID_IO_P1;
case <number>DigitalPin.P2: return EventBusSource.MICROBIT_ID_IO_P2;
case <number>DigitalPin.P8: return EventBusSource.MICROBIT_ID_IO_P8;
case <number>DigitalPin.P12: return EventBusSource.MICROBIT_ID_IO_P12;
case <number>DigitalPin.P13: return EventBusSource.MICROBIT_ID_IO_P13;
case <number>DigitalPin.P14: return EventBusSource.MICROBIT_ID_IO_P14;
case <number>DigitalPin.P15: return EventBusSource.MICROBIT_ID_IO_P15;
case <number>DigitalPin.P16: return EventBusSource.MICROBIT_ID_IO_P16;
}
return null;
}
}