Latest

Dependencies:   serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice

Committer:
Swabey89
Date:
Fri Nov 30 13:52:44 2018 +0000
Revision:
17:ead43c1b729d
Parent:
16:15588fdc5d11
Child:
18:3edfb9152b05
Code tidy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Swabey89 7:e2bf2d703867 1 #include "mbed.h"
Swabey89 1:31c7b4ab552b 2 #include "sample_hardware.hpp"
Swabey89 1:31c7b4ab552b 3 #include "Networkbits.hpp"
Swabey89 5:956984cbe447 4 #include "serial_terminal.hpp"
Swabey89 6:a5394c9e5927 5 #include "SDCard.hpp"
Swabey89 5:956984cbe447 6 #include "rtos.h"
Swabey89 11:3ad0327e8c9f 7 #include "events/mbed_events.h"
Swabey89 8:c81b0ff8b822 8 #include "LCDdisplay.hpp"
Swabey89 5:956984cbe447 9
Swabey89 10:0fffc988d325 10 //Signals
Swabey89 8:c81b0ff8b822 11 #define TAKE_SAMPLE 1
Swabey89 10:0fffc988d325 12
Swabey89 17:ead43c1b729d 13 //Global variables
Swabey89 17:ead43c1b729d 14 float sample_rate = 15;
Swabey89 17:ead43c1b729d 15 FILE* fp;
Swabey89 17:ead43c1b729d 16 FATFileSystem* fs;
Swabey89 10:0fffc988d325 17
Swabey89 17:ead43c1b729d 18 //Shared mutable variables
Swabey89 17:ead43c1b729d 19 struct tm* timeData;
Swabey89 17:ead43c1b729d 20 char buffer[256];
Swabey89 17:ead43c1b729d 21 RawSerial* pc;
Swabey89 8:c81b0ff8b822 22
Swabey89 8:c81b0ff8b822 23 //Queues
Swabey89 11:3ad0327e8c9f 24 EventQueue SDqueue(32*EVENTS_EVENT_SIZE);
Swabey89 11:3ad0327e8c9f 25 EventQueue LCDqueue(32*EVENTS_EVENT_SIZE);
Swabey89 17:ead43c1b729d 26 EventQueue serialqueue(32*EVENTS_EVENT_SIZE);
Swabey89 1:31c7b4ab552b 27
Swabey89 1:31c7b4ab552b 28 //Threads
Swabey89 17:ead43c1b729d 29 Thread sample_thread(osPriorityHigh);
Swabey89 17:ead43c1b729d 30 Thread serialthread(osPriorityAboveNormal);
Swabey89 11:3ad0327e8c9f 31 Thread SDqueue_thread;
Swabey89 11:3ad0327e8c9f 32 Thread LCDqueue_thread;
Swabey89 0:4afd4940a189 33
Swabey89 17:ead43c1b729d 34 //Timers
Swabey89 8:c81b0ff8b822 35 Ticker sample;
Swabey89 10:0fffc988d325 36
Swabey89 17:ead43c1b729d 37 //Function prototypes
Swabey89 8:c81b0ff8b822 38 void sampleISR(void);
Swabey89 8:c81b0ff8b822 39 void takesample(void);
Swabey89 8:c81b0ff8b822 40 void samples(void);
Swabey89 11:3ad0327e8c9f 41 void serialISR(void);
Swabey89 11:3ad0327e8c9f 42 void serialiser(void);
Swabey89 11:3ad0327e8c9f 43
Swabey89 15:8af9672a6778 44 int main() {
Swabey89 17:ead43c1b729d 45 timeData = new tm;
Swabey89 11:3ad0327e8c9f 46 pc = new RawSerial(USBTX, USBRX);
Swabey89 11:3ad0327e8c9f 47
Swabey89 8:c81b0ff8b822 48 //Move threads into a thread init function?
Swabey89 8:c81b0ff8b822 49 SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever));
Swabey89 8:c81b0ff8b822 50 LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever));
Swabey89 8:c81b0ff8b822 51
Swabey89 11:3ad0327e8c9f 52 //sterm_thread.start(serialterm);
Swabey89 8:c81b0ff8b822 53 sample_thread.start(samples);
Swabey89 8:c81b0ff8b822 54
Swabey89 8:c81b0ff8b822 55 //Initialise, move into initialise function
Swabey89 17:ead43c1b729d 56 SDcard();
Swabey89 9:fa8a35d9d6c0 57
Swabey89 1:31c7b4ab552b 58 //Power on self test
Swabey89 1:31c7b4ab552b 59 post();
Swabey89 1:31c7b4ab552b 60
Swabey89 11:3ad0327e8c9f 61 pc->printf("Send commands\n\r");
Swabey89 3:b1583f309b43 62
Swabey89 10:0fffc988d325 63 sample.attach(&sampleISR, sample_rate);
Swabey89 11:3ad0327e8c9f 64
Swabey89 11:3ad0327e8c9f 65 serialthread.start(callback(&serialqueue, &EventQueue::dispatch_forever));
Swabey89 11:3ad0327e8c9f 66 pc->attach(serialISR, Serial::RxIrq);
Swabey89 15:8af9672a6778 67
Swabey89 1:31c7b4ab552b 68 //Flash to indicate goodness
Swabey89 1:31c7b4ab552b 69 while(true) {
Swabey89 5:956984cbe447 70 greenLED = !greenLED;
Swabey89 5:956984cbe447 71 Thread::wait(500);
Swabey89 0:4afd4940a189 72 }
Swabey89 0:4afd4940a189 73 }
Swabey89 1:31c7b4ab552b 74
Swabey89 8:c81b0ff8b822 75 void sampleISR()
Swabey89 8:c81b0ff8b822 76 {
Swabey89 8:c81b0ff8b822 77 sample_thread.signal_set(TAKE_SAMPLE);
Swabey89 8:c81b0ff8b822 78 }
Swabey89 8:c81b0ff8b822 79
Swabey89 8:c81b0ff8b822 80 void samples()
Swabey89 8:c81b0ff8b822 81 {
Swabey89 8:c81b0ff8b822 82 while(true)
Swabey89 8:c81b0ff8b822 83 {
Swabey89 8:c81b0ff8b822 84 //High priority thread
Swabey89 8:c81b0ff8b822 85 Thread::signal_wait(TAKE_SAMPLE);
Swabey89 8:c81b0ff8b822 86
Swabey89 11:3ad0327e8c9f 87 //needs to be a class at some point
Swabey89 12:3a54cbaa714c 88 //read time also
Swabey89 8:c81b0ff8b822 89 double temp = sensor.getTemperature();
Swabey89 8:c81b0ff8b822 90 double pressure = sensor.getPressure();
Swabey89 12:3a54cbaa714c 91 float light = adcIn.read();
Swabey89 8:c81b0ff8b822 92
Swabey89 8:c81b0ff8b822 93 //Pass onto queues
Swabey89 12:3a54cbaa714c 94 LCDqueue.call(LCD_display,temp,pressure,light);
Swabey89 8:c81b0ff8b822 95 SDqueue.call(SDaddSample,temp,pressure);
Swabey89 8:c81b0ff8b822 96 }
Swabey89 8:c81b0ff8b822 97 }
Swabey89 1:31c7b4ab552b 98
Swabey89 11:3ad0327e8c9f 99 void serialISR()
Swabey89 11:3ad0327e8c9f 100 {
Swabey89 11:3ad0327e8c9f 101 pc->attach(NULL, Serial::RxIrq);
Swabey89 11:3ad0327e8c9f 102 serialqueue.call(serialiser);
Swabey89 11:3ad0327e8c9f 103 }
Swabey89 1:31c7b4ab552b 104
Swabey89 11:3ad0327e8c9f 105 void serialiser()
Swabey89 12:3a54cbaa714c 106 {
Swabey89 17:ead43c1b729d 107 static int i = 0;
Swabey89 11:3ad0327e8c9f 108 if (pc->readable())
Swabey89 11:3ad0327e8c9f 109 {
Swabey89 11:3ad0327e8c9f 110 buffer[i] = pc->getc();
Swabey89 11:3ad0327e8c9f 111 if (buffer[i] == '\r')
Swabey89 11:3ad0327e8c9f 112 {
Swabey89 11:3ad0327e8c9f 113 i = 0;
Swabey89 11:3ad0327e8c9f 114 serialqueue.call(serialterm);
Swabey89 11:3ad0327e8c9f 115
Swabey89 11:3ad0327e8c9f 116 }
Swabey89 11:3ad0327e8c9f 117 else i++;
Swabey89 11:3ad0327e8c9f 118 }
Swabey89 11:3ad0327e8c9f 119 pc->attach(serialISR, Serial::RxIrq);
Swabey89 11:3ad0327e8c9f 120 }