Latest

Dependencies:   serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice

main.cpp

Committer:
Swabey89
Date:
2018-11-18
Revision:
11:3ad0327e8c9f
Parent:
10:0fffc988d325
Child:
12:3a54cbaa714c

File content as of revision 11:3ad0327e8c9f:

#include "mbed.h"
#include "sample_hardware.hpp"
#include "Networkbits.hpp"
#include "serial_terminal.hpp"
#include "SDCard.hpp"
#include "rtos.h"
#include "events/mbed_events.h"
#include "LCDdisplay.hpp"


//Signals
#define TAKE_SAMPLE 1



float sample_rate = 15;
//bool sample_enable = true;

//Queues
EventQueue SDqueue(32*EVENTS_EVENT_SIZE);
EventQueue LCDqueue(32*EVENTS_EVENT_SIZE);


//Threads
Thread SDqueue_thread;
Thread LCDqueue_thread;
//Thread sterm_thread;
Thread sample_thread(osPriorityHigh);
//Thread ntwkthread

Ticker sample;

void sampleISR(void);
void takesample(void);
void samples(void);


FILE* fp;
FATFileSystem* fs;


//TEST
Thread serialthread(osPriorityAboveNormal);
EventQueue serialqueue(32*EVENTS_EVENT_SIZE);
char buffer[256];
RawSerial* pc;
void serialISR(void);
void serialiser(void);
int i = 0;
//TEST



int main() {     
    pc = new RawSerial(USBTX, USBRX);

    //Move threads into a thread init function?
    SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever));
    LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever));
    
    //sterm_thread.start(serialterm);
    sample_thread.start(samples);
    
    //Initialise, move into initialise function
    SDcard();
    
    
    //Greeting
    pc->printf("Testing\n\n");  
     
    
    //Power on self test
    post();
    
    pc->printf("Send commands\n\r");
    
    sample.attach(&sampleISR, sample_rate);
    
    //TEST
    serialthread.start(callback(&serialqueue, &EventQueue::dispatch_forever));
    pc->attach(serialISR, Serial::RxIrq);
    //TEST
        
    
    //Flash to indicate goodness
    while(true) {
        greenLED = !greenLED;
        Thread::wait(500);    
    }
}

void sampleISR()
{
    sample_thread.signal_set(TAKE_SAMPLE);   
}

void samples()
{
    while(true)
    {
        //High priority thread 
        Thread::signal_wait(TAKE_SAMPLE);
        
        //needs to be a class at some point
        
        double temp = sensor.getTemperature();
        double pressure = sensor.getPressure();
        
        //Pass onto queues
        LCDqueue.call(LCD_display,temp,pressure);
        SDqueue.call(SDaddSample,temp,pressure);
    }
}
    
void serialISR()
{
    pc->attach(NULL, Serial::RxIrq);
    yellowLED = !yellowLED;
    serialqueue.call(serialiser);
}

void serialiser()
{
    
    if (pc->readable())
    {
        buffer[i] = pc->getc();
        if (buffer[i] == '\r')
        {
            //call serialterm
            greenLED = !greenLED; 
            i = 0;
            serialqueue.call(serialterm); 
                     
        }
        else i++;
    }
    pc->attach(serialISR, Serial::RxIrq);
}

//