BME SmartLab / Mbed 2 deprecated CHEAP

Dependencies:   SmartSwitch TMRh20 mbed

main.cpp

Committer:
gume
Date:
2016-03-21
Revision:
2:5d075b76930a
Parent:
1:2b938172cef5
Child:
3:f76bcafbbdd0

File content as of revision 2:5d075b76930a:

#include "mbed.h"
#include "DeafNode.h"

AnalogIn adc0(A0);
AnalogIn adc1(A1);
AnalogIn adc2(A2);
AnalogIn adc3(A3);

DigitalOut led(PA_8);
DeafNode *deafNode;
Serial pc(USBTX, USBRX);

#define SAMPLE_SIZE 720
#define PRESAMPLE_SIZE 60
// Buffers to store the samples of the 4 channels
uint8_t buffer0[SAMPLE_SIZE];
uint8_t buffer1[SAMPLE_SIZE];
uint8_t buffer2[SAMPLE_SIZE];
uint8_t buffer3[SAMPLE_SIZE];

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;
}

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;

    return threshold;
}

void sendSamples(uint8_t id, uint8_t *samples, int length, int pointer)
{
    datapacket[0] = 0x00;
    datapacket[1] = 0x10;
    datapacket[2] = id;

    int c = 0;
    while (c < length) {
        int j;
        //datapacket[3] = (uint8_t)(c & 0xff);
        //datapacket[4] = (uint8_t)(c >> 8);
        datapacket[3] = c;
        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 = D8;
    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();

    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++) {
                    int sample_start2 = sample_start + SAMPLE_SIZE / 3;
                    if (sample_start2 >= SAMPLE_SIZE) sample_start2 -= SAMPLE_SIZE;
                    int sample_start3 = sample_start + 2* SAMPLE_SIZE / 3;
                    if (sample_start3 >= SAMPLE_SIZE) sample_start3 -= SAMPLE_SIZE;
                    sendSamples(0x00, buffer0, SAMPLE_SIZE / 3, sample_start);
                    sendSamples(0x10, buffer0, SAMPLE_SIZE / 3, sample_start2);
                    sendSamples(0x20, buffer0, SAMPLE_SIZE / 3, sample_start3);
                    sendSamples(0x01, buffer1, SAMPLE_SIZE / 3, sample_start);
                    sendSamples(0x11, buffer1, SAMPLE_SIZE / 3, sample_start2);
                    sendSamples(0x21, buffer1, SAMPLE_SIZE / 3, sample_start3);
                    sendSamples(0x02, buffer2, SAMPLE_SIZE / 3, sample_start);
                    sendSamples(0x12, buffer2, SAMPLE_SIZE / 3, sample_start2);
                    sendSamples(0x22, buffer2, SAMPLE_SIZE / 3, sample_start3);
                    sendSamples(0x03, buffer3, SAMPLE_SIZE / 3, sample_start);
                    sendSamples(0x13, buffer3, SAMPLE_SIZE / 3, sample_start2);
                    sendSamples(0x23, buffer3, SAMPLE_SIZE / 3, sample_start3);
                    printf("\nSamples were sent (%d).\n", r);
                }
                sample_size = 0;
                sample_pointer = 0;
            }
        }
    }
}