Latest
Dependencies: serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice
main.cpp@18:3edfb9152b05, 2018-11-30 (annotated)
- Committer:
- Swabey89
- Date:
- Fri Nov 30 14:39:10 2018 +0000
- Revision:
- 18:3edfb9152b05
- Parent:
- 17:ead43c1b729d
- Child:
- 19:88d8359306a4
pre-class
Who changed what in which revision?
User | Revision | Line number | New 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 | 18:3edfb9152b05 | 20 | char cmdBuffer[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 | 8:c81b0ff8b822 | 47 | |
Swabey89 | 18:3edfb9152b05 | 48 | //Initialisations |
Swabey89 | 17:ead43c1b729d | 49 | SDcard(); |
Swabey89 | 9:fa8a35d9d6c0 | 50 | |
Swabey89 | 1:31c7b4ab552b | 51 | //Power on self test |
Swabey89 | 1:31c7b4ab552b | 52 | post(); |
Swabey89 | 18:3edfb9152b05 | 53 | |
Swabey89 | 18:3edfb9152b05 | 54 | //Start threads |
Swabey89 | 18:3edfb9152b05 | 55 | SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 18:3edfb9152b05 | 56 | LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 18:3edfb9152b05 | 57 | serialthread.start(callback(&serialqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 18:3edfb9152b05 | 58 | sample_thread.start(samples); |
Swabey89 | 3:b1583f309b43 | 59 | |
Swabey89 | 18:3edfb9152b05 | 60 | //Attach ISRs |
Swabey89 | 18:3edfb9152b05 | 61 | sample.attach(&sampleISR, sample_rate); |
Swabey89 | 11:3ad0327e8c9f | 62 | pc->attach(serialISR, Serial::RxIrq); |
Swabey89 | 15:8af9672a6778 | 63 | |
Swabey89 | 1:31c7b4ab552b | 64 | //Flash to indicate goodness |
Swabey89 | 1:31c7b4ab552b | 65 | while(true) { |
Swabey89 | 5:956984cbe447 | 66 | greenLED = !greenLED; |
Swabey89 | 5:956984cbe447 | 67 | Thread::wait(500); |
Swabey89 | 0:4afd4940a189 | 68 | } |
Swabey89 | 0:4afd4940a189 | 69 | } |
Swabey89 | 1:31c7b4ab552b | 70 | |
Swabey89 | 8:c81b0ff8b822 | 71 | void sampleISR() |
Swabey89 | 8:c81b0ff8b822 | 72 | { |
Swabey89 | 8:c81b0ff8b822 | 73 | sample_thread.signal_set(TAKE_SAMPLE); |
Swabey89 | 8:c81b0ff8b822 | 74 | } |
Swabey89 | 8:c81b0ff8b822 | 75 | |
Swabey89 | 8:c81b0ff8b822 | 76 | void samples() |
Swabey89 | 8:c81b0ff8b822 | 77 | { |
Swabey89 | 8:c81b0ff8b822 | 78 | while(true) |
Swabey89 | 8:c81b0ff8b822 | 79 | { |
Swabey89 | 8:c81b0ff8b822 | 80 | //High priority thread |
Swabey89 | 8:c81b0ff8b822 | 81 | Thread::signal_wait(TAKE_SAMPLE); |
Swabey89 | 8:c81b0ff8b822 | 82 | |
Swabey89 | 11:3ad0327e8c9f | 83 | //needs to be a class at some point |
Swabey89 | 12:3a54cbaa714c | 84 | //read time also |
Swabey89 | 8:c81b0ff8b822 | 85 | double temp = sensor.getTemperature(); |
Swabey89 | 8:c81b0ff8b822 | 86 | double pressure = sensor.getPressure(); |
Swabey89 | 12:3a54cbaa714c | 87 | float light = adcIn.read(); |
Swabey89 | 8:c81b0ff8b822 | 88 | |
Swabey89 | 8:c81b0ff8b822 | 89 | //Pass onto queues |
Swabey89 | 12:3a54cbaa714c | 90 | LCDqueue.call(LCD_display,temp,pressure,light); |
Swabey89 | 8:c81b0ff8b822 | 91 | SDqueue.call(SDaddSample,temp,pressure); |
Swabey89 | 8:c81b0ff8b822 | 92 | } |
Swabey89 | 8:c81b0ff8b822 | 93 | } |
Swabey89 | 1:31c7b4ab552b | 94 | |
Swabey89 | 11:3ad0327e8c9f | 95 | void serialISR() |
Swabey89 | 11:3ad0327e8c9f | 96 | { |
Swabey89 | 11:3ad0327e8c9f | 97 | pc->attach(NULL, Serial::RxIrq); |
Swabey89 | 11:3ad0327e8c9f | 98 | serialqueue.call(serialiser); |
Swabey89 | 11:3ad0327e8c9f | 99 | } |
Swabey89 | 1:31c7b4ab552b | 100 | |
Swabey89 | 11:3ad0327e8c9f | 101 | void serialiser() |
Swabey89 | 12:3a54cbaa714c | 102 | { |
Swabey89 | 17:ead43c1b729d | 103 | static int i = 0; |
Swabey89 | 11:3ad0327e8c9f | 104 | if (pc->readable()) |
Swabey89 | 11:3ad0327e8c9f | 105 | { |
Swabey89 | 18:3edfb9152b05 | 106 | cmdBuffer[i] = pc->getc(); |
Swabey89 | 18:3edfb9152b05 | 107 | if (cmdBuffer[i] == '\r') |
Swabey89 | 11:3ad0327e8c9f | 108 | { |
Swabey89 | 11:3ad0327e8c9f | 109 | i = 0; |
Swabey89 | 18:3edfb9152b05 | 110 | serialqueue.call(serialterm); |
Swabey89 | 11:3ad0327e8c9f | 111 | } |
Swabey89 | 11:3ad0327e8c9f | 112 | else i++; |
Swabey89 | 11:3ad0327e8c9f | 113 | } |
Swabey89 | 11:3ad0327e8c9f | 114 | pc->attach(serialISR, Serial::RxIrq); |
Swabey89 | 11:3ad0327e8c9f | 115 | } |