|
1 | 1 | #include "MicroBit.h" |
2 | | -#include "samples/Tests.h" |
3 | 2 |
|
4 | 3 | MicroBit uBit; |
5 | 4 |
|
6 | | -int main() |
7 | | -{ |
| 5 | +class RawMicAdcSink : public CodalComponent, public DataSink { |
| 6 | + public: |
| 7 | + DataSource &upstream; |
| 8 | + int NrOfPullRequests = 0; |
| 9 | + |
| 10 | + RawMicAdcSink(DataSource &source): upstream(source) { |
| 11 | + upstream.connect(*this); |
| 12 | + } |
| 13 | + |
| 14 | + virtual int pullRequest() { |
| 15 | + ManagedBuffer b = upstream.pull(); |
| 16 | + |
| 17 | + uint8_t *data = &b[0]; |
| 18 | + int SampleArrRaw[128]; |
| 19 | + int format = upstream.getFormat(); |
| 20 | + int skip = 2; |
| 21 | + int windowSize = 128; |
| 22 | + |
| 23 | + if (format != DATASTREAM_FORMAT_16BIT_SIGNED) { |
| 24 | + uBit.serial.printf("Upstream data format enexpected: %d", format); |
| 25 | + uBit.sleep(100); |
| 26 | + uBit.serial.send("\r\n", SYNC_SPINWAIT); |
| 27 | + uBit.panic(123); |
| 28 | + } |
| 29 | + |
| 30 | + int samples = b.length() / skip; |
| 31 | + |
| 32 | + while(samples){ |
| 33 | + // ensure we use at least windowSize number of samples (128) |
| 34 | + if (samples < windowSize) { |
| 35 | + break; |
| 36 | + } |
| 37 | + |
| 38 | + uint8_t *ptr = data; |
| 39 | + uint8_t *end = data + windowSize; |
| 40 | + |
| 41 | + bool dumpToSerial = false; |
| 42 | + uint8_t SampCntr = 0; |
| 43 | + |
| 44 | + while (ptr < end) { |
| 45 | + int TempSample = StreamNormalizer::readSample[format](ptr); |
| 46 | + SampleArrRaw[SampCntr] = TempSample; |
| 47 | + SampCntr++; |
| 48 | + ptr += skip; |
| 49 | + |
| 50 | + if (TempSample == -30584) { |
| 51 | + dumpToSerial = true; |
| 52 | + } |
| 53 | + } |
| 54 | + if (dumpToSerial) { |
| 55 | + uBit.serial.printf("pullRequest() number %d:\r\n", NrOfPullRequests); |
| 56 | + for (int i = 0; i < SampCntr; i++) |
| 57 | + uBit.serial.printf("%d\t", SampleArrRaw[i]); |
| 58 | + uBit.serial.send("\r\n\r\n"); |
| 59 | + } |
| 60 | + |
| 61 | + samples -= windowSize; |
| 62 | + data += windowSize; |
| 63 | + } |
| 64 | + |
| 65 | + NrOfPullRequests++; |
| 66 | + |
| 67 | + return DEVICE_OK; |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | + |
| 72 | +int main() { |
8 | 73 | uBit.init(); |
9 | 74 |
|
10 | | - out_of_box_experience(); |
| 75 | + uBit.serial.send("Start:\r\n"); |
11 | 76 |
|
12 | | - microbit_panic( 999 ); |
13 | | -} |
| 77 | + RawMicAdcSink* micAdcSink = new RawMicAdcSink(*(uBit.audio.rawSplitter->createChannel())); |
14 | 78 |
|
| 79 | + uBit.io.P2.getAnalogValue(); |
| 80 | + uBit.audio.virtualOutputPin.setAnalogPeriodUs(1000000 / 2600); |
| 81 | + uBit.audio.setSpeakerEnabled(false); // This line is just to avoid the annoying tone being played |
| 82 | + |
| 83 | + while (true) { |
| 84 | + uBit.sleep(1); |
| 85 | + } |
| 86 | +} |
0 commit comments