2828#include " microbithal.h"
2929#include " MicroBitDevice.h"
3030
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)
40-
4131extern " C" void microbit_hal_level_detector_callback (int );
4232
4333static void level_detector_event_handler (Event evt) {
@@ -51,24 +41,21 @@ static bool microphone_init_done = false;
5141void microbit_hal_microphone_init (void ) {
5242 if (!microphone_init_done) {
5343 microphone_init_done = true ;
44+ uBit.audio .levelSPL ->setUnit (LEVEL_DETECTOR_SPL_8BIT);
5445 uBit.messageBus .listen (DEVICE_ID_SYSTEM_LEVEL_DETECTOR, DEVICE_EVT_ANY, level_detector_event_handler);
5546 }
5647}
5748
5849void microbit_hal_microphone_set_threshold (int kind, int value) {
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);
6150 if (kind == MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW) {
62- uBit.audio .level ->setLowThreshold (value);
51+ uBit.audio .levelSPL ->setLowThreshold (value);
6352 } else {
64- uBit.audio .level ->setHighThreshold (value);
53+ uBit.audio .levelSPL ->setHighThreshold (value);
6554 }
6655}
6756
6857int microbit_hal_microphone_get_level (void ) {
6958 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);
7259 return value;
7360}
7461
0 commit comments