Skip to content

Commit

Permalink
DO NOT MERGE: Example to replicate issue codal-microbit-v2#377
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-carlos committed Sep 19, 2023
1 parent 95dba76 commit efec857
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
2 changes: 1 addition & 1 deletion codal.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"target": {
"name": "codal-microbit-v2",
"url": "https://github.com/lancaster-university/codal-microbit-v2",
"branch": "master",
"branch": "v0.2.59",
"type": "git"
},
"config":{
Expand Down
84 changes: 78 additions & 6 deletions source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,86 @@
#include "MicroBit.h"
#include "samples/Tests.h"

MicroBit uBit;

int main()
{
class RawMicAdcSink : public CodalComponent, public DataSink {
public:
DataSource &upstream;
int NrOfPullRequests = 0;

RawMicAdcSink(DataSource &source): upstream(source) {
upstream.connect(*this);
}

virtual int pullRequest() {
ManagedBuffer b = upstream.pull();

uint8_t *data = &b[0];
int SampleArrRaw[128];
int format = upstream.getFormat();
int skip = 2;
int windowSize = 128;

if (format != DATASTREAM_FORMAT_16BIT_SIGNED) {
uBit.serial.printf("Upstream data format enexpected: %d", format);
uBit.sleep(100);
uBit.serial.send("\r\n", SYNC_SPINWAIT);
uBit.panic(123);
}

int samples = b.length() / skip;

while(samples){
// ensure we use at least windowSize number of samples (128)
if (samples < windowSize) {
break;
}

uint8_t *ptr = data;
uint8_t *end = data + windowSize;

bool dumpToSerial = false;
uint8_t SampCntr = 0;

while (ptr < end) {
int TempSample = StreamNormalizer::readSample[format](ptr);
SampleArrRaw[SampCntr] = TempSample;
SampCntr++;
ptr += skip;

if (TempSample == -30584) {
dumpToSerial = true;
}
}
if (dumpToSerial) {
uBit.serial.printf("pullRequest() number %d:\r\n", NrOfPullRequests);
for (int i = 0; i < SampCntr; i++)
uBit.serial.printf("%d\t", SampleArrRaw[i]);
uBit.serial.send("\r\n\r\n");
}

samples -= windowSize;
data += windowSize;
}

NrOfPullRequests++;

return DEVICE_OK;
}
};


int main() {
uBit.init();

out_of_box_experience();
uBit.serial.send("Start:\r\n");

microbit_panic( 999 );
}
RawMicAdcSink* micAdcSink = new RawMicAdcSink(*(uBit.audio.rawSplitter->createChannel()));

uBit.io.P2.getAnalogValue();
uBit.audio.virtualOutputPin.setAnalogPeriodUs(1000000 / 2600);
uBit.audio.setSpeakerEnabled(false); // This line is just to avoid the annoying tone being played

while (true) {
uBit.sleep(1);
}
}
4 changes: 2 additions & 2 deletions utils/python/codal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def system(cmd):
def build(clean, verbose = False, parallelism = 10):
if platform.system() == "Windows":
# configure
system("cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo -G \"Ninja\"")
system("cmake .. -DCMAKE_BUILD_TYPE=Debug -G \"Ninja\"")

if clean:
system("ninja clean")
Expand All @@ -30,7 +30,7 @@ def build(clean, verbose = False, parallelism = 10):
system("ninja -j {}".format(parallelism))
else:
# configure
system("cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo -G \"Unix Makefiles\"")
system("cmake .. -DCMAKE_BUILD_TYPE=Debug -G \"Unix Makefiles\"")

if clean:
system("make clean")
Expand Down

0 comments on commit efec857

Please sign in to comment.