Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SmartSwitch TMRh20 mbed
main.cpp
- Committer:
- gume
- Date:
- 2016-03-22
- Revision:
- 3:f76bcafbbdd0
- Parent:
- 2:5d075b76930a
- Child:
- 4:6efe063cdbb2
File content as of revision 3:f76bcafbbdd0:
#include "mbed.h"
#include "DeafNode.h"
AnalogIn adc0(A0);
AnalogIn adc1(A1);
AnalogIn adc2(A2);
AnalogIn adc3(A3);
AnalogIn adc4(A4);
AnalogIn adc5(A5);
DigitalOut led(PA_8);
DeafNode *deafNode;
Serial pc(USBTX, USBRX);
#define SAMPLE_SIZE 480
#define PRESAMPLE_SIZE 60
// Buffers to store the samples of the 6 channels
uint8_t buffer0[SAMPLE_SIZE];
uint8_t buffer1[SAMPLE_SIZE];
uint8_t buffer2[SAMPLE_SIZE];
uint8_t buffer3[SAMPLE_SIZE];
uint8_t buffer4[SAMPLE_SIZE];
uint8_t buffer5[SAMPLE_SIZE];
uint8_t sid;
int sample_start;
int sample_size;
int sample_pointer;
#define DCVAL 118
#define THRESHOLD 34
uint8_t datapacket[32];
volatile unsigned int TickCount;
extern "C" void SysTick_Handler (void)
{
TickCount+= 1;
}
void readMics()
{
buffer0[sample_pointer] = adc0.read_u16() >> 8;
buffer1[sample_pointer] = adc1.read_u16() >> 8;
buffer2[sample_pointer] = adc2.read_u16() >> 8;
buffer3[sample_pointer] = adc3.read_u16() >> 8;
buffer4[sample_pointer] = adc3.read_u16() >> 8;
buffer5[sample_pointer] = adc3.read_u16() >> 8;
}
bool checkThreshold()
{
bool threshold = false;
if (buffer0[sample_pointer] > DCVAL + THRESHOLD || buffer0[sample_pointer] < DCVAL - THRESHOLD) threshold = true;
else if (buffer1[sample_pointer] > DCVAL + THRESHOLD || buffer1[sample_pointer] < DCVAL - THRESHOLD) threshold = true;
else if (buffer2[sample_pointer] > DCVAL + THRESHOLD || buffer2[sample_pointer] < DCVAL - THRESHOLD) threshold = true;
else if (buffer3[sample_pointer] > DCVAL + THRESHOLD || buffer3[sample_pointer] < DCVAL - THRESHOLD) threshold = true;
else if (buffer4[sample_pointer] > DCVAL + THRESHOLD || buffer4[sample_pointer] < DCVAL - THRESHOLD) threshold = true;
else if (buffer5[sample_pointer] > DCVAL + THRESHOLD || buffer5[sample_pointer] < DCVAL - THRESHOLD) threshold = true;
return threshold;
}
void sendSamples(uint8_t id, uint8_t *samples, int length, int pointer)
{
// 0-1 bytes: type ID
datapacket[0] = 0x00;
datapacket[1] = 0x11;
int c = 0;
while (c < length) {
int j;
// 2-3 bytes: 4 bit sample ID, 12 bit offset (in 20 bytes)
datapacket[2] = id << 4 | (((c / 20) >> 8) & 0x0f);
datapacket[3] = (c / 20) & 0xff;
for (j = 0; j < 20; j++) {
datapacket[4 + j] = samples[pointer];
pointer++;
if (pointer >= SAMPLE_SIZE) pointer = 0;
c++;
if (c > length) break;
}
deafNode->sendData(0, datapacket, j + 4);
}
}
int main()
{
SysTick_Config(SystemCoreClock / 1000); // Systick in ms
pc.baud(115200);
pc.printf("Node started (%d MHz).\n", SystemCoreClock / 1000000);
NodeConfig nc;
nc.address = 0x4D31;
nc.netprefix = 0x424D45L;
nc.radioCE = D7;
nc.radioCS = D10;
nc.spi_sck = SPI_SCK;
nc.spi_miso = SPI_MISO;
nc.spi_mosi = SPI_MOSI;
nc.radioChannel = 96;
nc.radioSpeed = RF24_2MBPS;
deafNode = new DeafNode(&nc);
deafNode->setup();
sid = 0;
sample_start = 0;
sample_size = 0;
sample_pointer = 0;
bool action = false;
while(1) {
readMics();
//pc.puts(".");
if (!action) {
// Not in action, just listening
if (checkThreshold()) {
//pc.puts("\nThreshold reached!\n");
action = true;
if (sample_size > PRESAMPLE_SIZE) {
sample_size = PRESAMPLE_SIZE;
}
sample_start = sample_pointer - sample_size;
if (sample_start < 0) sample_start += SAMPLE_SIZE;
}
sample_pointer++;
if (sample_pointer >= SAMPLE_SIZE) sample_pointer = 0;
if (sample_size < SAMPLE_SIZE) sample_size++;
} else {
// In action, collecting
sample_pointer++;
if (sample_pointer >= SAMPLE_SIZE) sample_pointer = 0;
sample_size++;
if (sample_size == SAMPLE_SIZE) {
// Stop collecting, send samples
action = false;
for (int r = 1; r <= 2; r++) {
sendSamples(sid, buffer0, SAMPLE_SIZE, sample_start);
sendSamples(sid, buffer1, SAMPLE_SIZE, sample_start);
sendSamples(sid, buffer2, SAMPLE_SIZE, sample_start);
sendSamples(sid, buffer3, SAMPLE_SIZE, sample_start);
sendSamples(sid, buffer4, SAMPLE_SIZE, sample_start);
sendSamples(sid, buffer5, SAMPLE_SIZE, sample_start);
printf("\nSamples were sent (%d).\n", r);
}
sid++;
sample_size = 0;
sample_pointer = 0;
}
}
}
}