forked from CytronTechnologies/pxt-edubit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound_bit.ts
150 lines (118 loc) · 4.47 KB
/
sound_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
* Functions for edu:bit - Sound Bit.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Default pin.
const SOUND_BIT_PIN = AnalogPin.P1;
// Event source.
const SOUND_BIT_EVENT_SOURCE = EventBusSource.MICROBIT_ID_IO_P1;
// Comparison type.
enum SoundSensorCompareType {
//% block=">"
MoreThan = 0,
//% block="<"
LessThan = 1
};
/**
* Blocks for Sound Bit.
*/
//% weight=15 color=#ff8000 icon="\uf130" block="Sound Bit"
namespace edubitSoundBit {
// Indicate whether background function has been created.
let bgFunctionCreated = false;
// Event type.
let eventType = 0;
// Array for compare type and threshold.
let compareTypesArray: SoundSensorCompareType[] = [];
let thresholdsArray: number[] = [];
// Array for old compare result.
let oldCompareResult: boolean[] = [];
/**
* Return sound level (0-1023).
* @param pin Pin number for sound sensor.
*/
//% weight=20
//% blockGap=8
//% blockId=edubit_read_sound_sensor
//% block="sound level"
export function readSoundSensor(): number {
return pins.analogReadPin(SOUND_BIT_PIN);
}
/**
* Compare the sound level (0-1023) with a number and return the result (true/false).
* @param compareType More than or less than.
* @param threshold The value to compare with. eg: 200
*/
//% weight=19
//% blockGap=40
//% blockId=edubit_compare_sound_sensor
//% block="sound level %compareType %threshold"
//% threshold.min=0 threshold.max=1023
export function compareSoundSensor(compareType: SoundSensorCompareType, threshold: number): boolean {
let result = false;
switch (compareType) {
case SoundSensorCompareType.MoreThan:
if (readSoundSensor() > threshold) {
result = true;
}
break;
case SoundSensorCompareType.LessThan:
if (readSoundSensor() < threshold) {
result = true;
}
break;
}
return result;
}
/**
* Compare the sound level value with a number and do something when true.
* @param compareType More than or less than.
* @param threshold The value to compare with. eg: 200
* @param handler Code to run when the event is raised.
*/
//% weight=18
//% blockGap=8
//% blockId=edubit_sound_sensor_event
//% block="on sound level %compareType %threshold"
//% threshold.min=0 threshold.max=1023
export function onEvent(compareType: SoundSensorCompareType, threshold: number, handler: Action): void {
// Use a new event type everytime a new event is create.
eventType++;
// Add the event info to the arrays.
compareTypesArray.push(compareType);
thresholdsArray.push(threshold);
// Create a placeholder for the old compare result.
oldCompareResult.push(false);
// Register the event.
control.onEvent(SOUND_BIT_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 (compareSoundSensor(compareTypesArray[i], thresholdsArray[i]) == true) {
// Raise the event if the compare result changed from false to true.
if (oldCompareResult[i] == false) {
control.raiseEvent(SOUND_BIT_EVENT_SOURCE, i + 1);
}
// Save old compare result.
oldCompareResult[i] = true;
}
else {
// Save old compare result.
oldCompareResult[i] = false;
}
basic.pause(20)
}
}
});
bgFunctionCreated = true;
}
}
}