1
+ #include " MicroBit.h"
2
+ #include " CodalAssert.h"
3
+ #include " LevelDetector.h"
4
+ #include " LevelDetectorSPL.h"
5
+ #include " Tests.h"
6
+
7
+ /* *
8
+ * Note - These tests use CodalAssert.h - so require the following codal.json options
9
+ * be enabled:
10
+ * CODAL_ENABLE_ASSERT: 1
11
+ *
12
+ * To cause the first assertion failure to halt the processor, also include:
13
+ * CODAL_ASSERT_PANIC: 1
14
+ */
15
+
16
+ extern MicroBit uBit;
17
+
18
+ /* *
19
+ * Tests if the mic auto activation (and ADC activation) works correctly
20
+ */
21
+ void stream_test_mic_activate () {
22
+ assert ( uBit.audio .isMicrophoneEnabled () == false , " Microphone was enabled on startup?" );
23
+ assert ( uBit.audio .mic ->output .isFlowing () == false , " isFlowing() should be false on startup." );
24
+ int level = uBit.audio .levelSPL ->getValue ();
25
+ assert ( uBit.audio .isMicrophoneEnabled (), " getValue() failed to turn the MIC on!" );
26
+ assert ( uBit.audio .mic ->output .isFlowing (), " isFlowing() was not true after mic activation" );
27
+ assert ( level > 0 , " Detected level appears to be zero? Defective hardware?" );
28
+ assert_pass ( NULL );
29
+ }
30
+
31
+ void stream_test_getValue_interval () {
32
+ uBit.audio .levelSPL ->getValue (); // Wake the stream :)
33
+ for ( int i=0 ; i<10 ; i++ ) {
34
+ assert ( uBit.audio .isMicrophoneEnabled (), " Microphone should still be active" );
35
+ assert ( uBit.audio .mic ->output .isFlowing (), " isFlowing() should still be true" );
36
+ int level = uBit.audio .levelSPL ->getValue ();
37
+ assert ( level > 0 , " Detected level appears to be zero? Defective hardware?" );
38
+ uBit.sleep ( CODAL_STREAM_IDLE_TIMEOUT_MS / 2 );
39
+ }
40
+ uBit.sleep ( CODAL_STREAM_IDLE_TIMEOUT_MS * 2 ); // Ensure we go quiscient
41
+ assert ( uBit.audio .isMicrophoneEnabled () == false , " Microphone should be shut down after 2 * CODAL_STREAM_IDLE_TIMEOUT_MS" );
42
+ assert ( uBit.audio .mic ->output .isFlowing () == false , " isFlowing() should be false after 2 * CODAL_STREAM_IDLE_TIMEOUT_MS" );
43
+ assert_pass ( NULL );
44
+ }
45
+
46
+ void stream_test_all () {
47
+ stream_test_mic_activate ();
48
+ stream_test_getValue_interval ();
49
+ assert_pass ( NULL );
50
+ }
0 commit comments