-
Notifications
You must be signed in to change notification settings - Fork 1
/
mode.ts
120 lines (96 loc) · 3.44 KB
/
mode.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
/*******************************************************************************
* Functions for sumo:bit mode.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
namespace sumobit {
// Mode event source flag.
const MODE_EVENT_SOURCE = 0x01;
// Flag to indicate whether the background function has been created.
let bgFunctionCreated = false;
// Event type.
let eventType = 0;
// Array for mode value.
let modevalueArray: number[] = [];
// Array for old compare result.
let oldCompareResult: boolean[] = [];
/**
* Get the current mode number(0-15).
*/
//% group="Mode"
//% weight=69
//% blockGap=8
//% blockId=sumobit_mode_read_value
//% block="mode"
export function readModeValue(): number {
return sumobit.i2cRead(SUMOBIT_REG_ADD_DIP);
}
/**
* Returns true if the mode matches the specified value.
* @param mode The mode number to compare. eg: 7
*/
//% group="Mode"
//% weight=68
//% blockGap=40
//% blockId=sumobit_mode_compare_value
//% block="mode %mode"
//% mode.min=0 mode.max=15
//% mode.defl=7
export function checkMode(mode: number): boolean {
let result = false;
if (readModeValue() === mode) {
result = true;
}
return result;
}
/**
* Check current mode value and do something when true.
* @param modevalue The current mode value. eg: 7
* @param handler Code to run when the event is raised.
*/
//% group="Mode"
//% weight=67
//% blockGap=8
//% blockId=sumobit_mode_event
//% block="on mode value %modevalue"
//% modevalue.min=0 modevalue.max=15
//% blockHidden=true
export function onModeEvent(modevalue: number, handler: Action): void {
// Use a new event type everytime a new event is create.
eventType++;
// Add the event info to the arrays.
modevalueArray.push(modevalue);
// Create a placeholder for the old compare result.
oldCompareResult.push(false);
// Register the event.
control.onEvent(MODE_EVENT_SOURCE, eventType, 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 < eventType; i++) {
// Check if the condition is met.
if (checkMode(modevalueArray[i]) == true) {
// Raise the event if the compare result changed from false to true.
if (oldCompareResult[i] == false) {
control.raiseEvent(MODE_EVENT_SOURCE, i + 1);
}
// Save old compare result.
oldCompareResult[i] = true;
}
else {
// Save old compare result.
oldCompareResult[i] = false;
}
basic.pause(20)
}
}
});
bgFunctionCreated = true;
}
}
}