Skip to content

Commit 490a890

Browse files
committed
Added some basic tests for the Streams API, using the new CodalAssert style
1 parent c576ffd commit 490a890

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

source/samples/StreamAPITest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

source/samples/Tests.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,8 @@ void init_clap_detect();
100100
void ble_test();
101101
void deepsleep_test( int test);
102102
void neopixel_test();
103+
void stream_test_mic_activate();
104+
void stream_test_getValue_interval();
105+
void stream_test_all();
103106

104107
#endif

0 commit comments

Comments
 (0)