BME SmartLab / Mbed 2 deprecated CHEAP

Dependencies:   SmartSwitch TMRh20 mbed

main.cpp

Committer:
gume
Date:
2016-03-18
Revision:
1:2b938172cef5
Parent:
0:7f49ac69d623
Child:
2:5d075b76930a

File content as of revision 1:2b938172cef5:

#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 240
#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] = id;
    int c = 0;
    while (c < length) {
        int j;
        for (j = 0; j < 24; j++) {
            datapacket[1 + j] = samples[pointer];
            pointer++;
            if (pointer > length) pointer = 0;
            c++;
            if (c > length) break;
        }
        deafNode->sendData(0, datapacket, j + 1);
    }
}

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;
                sendSamples(0, buffer0, SAMPLE_SIZE, sample_start);
                sendSamples(1, buffer1, SAMPLE_SIZE, sample_start);
                sendSamples(2, buffer2, SAMPLE_SIZE, sample_start);
                sendSamples(3, buffer3, SAMPLE_SIZE, sample_start);
                printf("\nSamples were sent.\n");
                sample_size = 0;
                sample_pointer = 0;
            }
        }
    }
}