28
28
#include " microbithal.h"
29
29
#include " MicroBitDevice.h"
30
30
31
- #define SOUND_LEVEL_MAXIMUM (20000 )
31
+ // Range of level values used by CODAL.
32
+ #define CODAL_LEVEL_MIN (52 )
33
+ #define CODAL_LEVEL_MAX (86 )
34
+ #define CODAL_LEVEL_RANGE (CODAL_LEVEL_MAX - CODAL_LEVEL_MIN)
35
+
36
+ // Range of level values used by this micro:bit HAL.
37
+ #define HAL_LEVEL_MIN (0 )
38
+ #define HAL_LEVEL_MAX (255 )
39
+ #define HAL_LEVEL_RANGE (HAL_LEVEL_MAX - HAL_LEVEL_MIN)
32
40
33
41
extern " C" void microbit_hal_level_detector_callback (int );
34
42
@@ -48,7 +56,8 @@ void microbit_hal_microphone_init(void) {
48
56
}
49
57
50
58
void microbit_hal_microphone_set_threshold (int kind, int value) {
51
- value = value * SOUND_LEVEL_MAXIMUM / 255 ;
59
+ value = (value - HAL_LEVEL_MIN) * CODAL_LEVEL_RANGE / HAL_LEVEL_RANGE + CODAL_LEVEL_MIN;
60
+ value = min (max (value, CODAL_LEVEL_MIN), CODAL_LEVEL_MAX);
52
61
if (kind == 0 ) {
53
62
uBit.audio .level ->setLowThreshold (value);
54
63
} else {
@@ -57,9 +66,10 @@ void microbit_hal_microphone_set_threshold(int kind, int value) {
57
66
}
58
67
59
68
int microbit_hal_microphone_get_level (void ) {
60
- int l = uBit.audio .level ->getValue ();
61
- l = min (255 , l * 255 / SOUND_LEVEL_MAXIMUM);
62
- return l;
69
+ int value = uBit.audio .levelSPL ->getValue ();
70
+ value = (value - CODAL_LEVEL_MIN) * HAL_LEVEL_RANGE / CODAL_LEVEL_RANGE + HAL_LEVEL_MIN;
71
+ value = min (max (value, HAL_LEVEL_MIN), HAL_LEVEL_MAX);
72
+ return value;
63
73
}
64
74
65
75
}
0 commit comments