Buffer working with LCD, Serial and SD Card
Dependencies: BMP280
Fork of Thread_Communication by
main.cpp@2:28d12a3db239, 2017-12-14 (annotated)
- Committer:
- GeorgeJourneaux
- Date:
- Thu Dec 14 15:44:49 2017 +0000
- Revision:
- 2:28d12a3db239
- Parent:
- 1:bca9993a0df3
- Child:
- 3:73497379c0cb
Sample, LCD and Serial CMDs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benparkes | 0:cb3a5c15b01e | 1 | #include "mbed.h" |
benparkes | 0:cb3a5c15b01e | 2 | #include "main.h" |
benparkes | 0:cb3a5c15b01e | 3 | |
benparkes | 0:cb3a5c15b01e | 4 | |
benparkes | 0:cb3a5c15b01e | 5 | |
benparkes | 0:cb3a5c15b01e | 6 | |
benparkes | 0:cb3a5c15b01e | 7 | LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); |
benparkes | 0:cb3a5c15b01e | 8 | |
benparkes | 0:cb3a5c15b01e | 9 | BMP280 Sensor(D14, D15); |
benparkes | 0:cb3a5c15b01e | 10 | void PrintLCD (); |
GeorgeJourneaux | 2:28d12a3db239 | 11 | void Rx_interrupt(); |
GeorgeJourneaux | 2:28d12a3db239 | 12 | void Serial_CMD(); |
GeorgeJourneaux | 2:28d12a3db239 | 13 | |
GeorgeJourneaux | 2:28d12a3db239 | 14 | volatile int rx_in=0; |
GeorgeJourneaux | 2:28d12a3db239 | 15 | char rx_line[80]; |
GeorgeJourneaux | 2:28d12a3db239 | 16 | int s_cmd = 0; |
benparkes | 0:cb3a5c15b01e | 17 | |
benparkes | 1:bca9993a0df3 | 18 | /* Mail */ |
benparkes | 1:bca9993a0df3 | 19 | typedef struct { |
benparkes | 1:bca9993a0df3 | 20 | float LDR_Value; |
benparkes | 1:bca9993a0df3 | 21 | float temp_Value; |
benparkes | 1:bca9993a0df3 | 22 | float press_Value; |
benparkes | 1:bca9993a0df3 | 23 | } mail_t; |
benparkes | 1:bca9993a0df3 | 24 | |
benparkes | 1:bca9993a0df3 | 25 | Mail<mail_t, 16> mail_box; |
benparkes | 0:cb3a5c15b01e | 26 | |
benparkes | 0:cb3a5c15b01e | 27 | |
benparkes | 0:cb3a5c15b01e | 28 | Thread t1; |
benparkes | 0:cb3a5c15b01e | 29 | Thread t2; |
GeorgeJourneaux | 2:28d12a3db239 | 30 | Thread S_CMD; |
benparkes | 0:cb3a5c15b01e | 31 | |
benparkes | 0:cb3a5c15b01e | 32 | void PrintLCD () { |
benparkes | 0:cb3a5c15b01e | 33 | |
benparkes | 0:cb3a5c15b01e | 34 | int i = 0; |
benparkes | 0:cb3a5c15b01e | 35 | while(1){ |
benparkes | 1:bca9993a0df3 | 36 | char lightString[16]; |
benparkes | 1:bca9993a0df3 | 37 | char tempString[16]; |
benparkes | 1:bca9993a0df3 | 38 | char pressString[16]; |
benparkes | 1:bca9993a0df3 | 39 | |
benparkes | 1:bca9993a0df3 | 40 | |
benparkes | 0:cb3a5c15b01e | 41 | lcd.Clear(); |
benparkes | 0:cb3a5c15b01e | 42 | lcd.RowSelect(0); |
benparkes | 1:bca9993a0df3 | 43 | |
benparkes | 0:cb3a5c15b01e | 44 | switch (i){ |
GeorgeJourneaux | 2:28d12a3db239 | 45 | case 0: |
GeorgeJourneaux | 2:28d12a3db239 | 46 | osEvent evt = mail_box.get(); |
benparkes | 1:bca9993a0df3 | 47 | |
GeorgeJourneaux | 2:28d12a3db239 | 48 | if (evt.status == osEventMail) { |
GeorgeJourneaux | 2:28d12a3db239 | 49 | mail_t *mail = (mail_t*)evt.value.p; |
benparkes | 0:cb3a5c15b01e | 50 | |
GeorgeJourneaux | 2:28d12a3db239 | 51 | sprintf(lightString,"%.4f", mail->LDR_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 52 | sprintf(tempString,"%2.2f", mail->temp_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 53 | sprintf(pressString,"%4.2f", mail->press_Value); |
benparkes | 1:bca9993a0df3 | 54 | |
GeorgeJourneaux | 2:28d12a3db239 | 55 | mail_box.free(mail); |
GeorgeJourneaux | 2:28d12a3db239 | 56 | } |
benparkes | 1:bca9993a0df3 | 57 | |
GeorgeJourneaux | 2:28d12a3db239 | 58 | lcd.Write("Light Level:"); |
GeorgeJourneaux | 2:28d12a3db239 | 59 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 60 | lcd.Write(lightString); |
GeorgeJourneaux | 2:28d12a3db239 | 61 | i++; |
GeorgeJourneaux | 2:28d12a3db239 | 62 | |
benparkes | 0:cb3a5c15b01e | 63 | break; |
GeorgeJourneaux | 2:28d12a3db239 | 64 | |
GeorgeJourneaux | 2:28d12a3db239 | 65 | case 1: |
benparkes | 1:bca9993a0df3 | 66 | |
GeorgeJourneaux | 2:28d12a3db239 | 67 | lcd.Write("Temperature:"); |
GeorgeJourneaux | 2:28d12a3db239 | 68 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 69 | lcd.Write(tempString); |
GeorgeJourneaux | 2:28d12a3db239 | 70 | i++; |
benparkes | 0:cb3a5c15b01e | 71 | |
benparkes | 0:cb3a5c15b01e | 72 | break; |
benparkes | 0:cb3a5c15b01e | 73 | |
GeorgeJourneaux | 2:28d12a3db239 | 74 | case 2: |
benparkes | 1:bca9993a0df3 | 75 | |
GeorgeJourneaux | 2:28d12a3db239 | 76 | lcd.Write("Pressure:"); |
GeorgeJourneaux | 2:28d12a3db239 | 77 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 78 | lcd.Write(pressString); |
GeorgeJourneaux | 2:28d12a3db239 | 79 | i =0; |
GeorgeJourneaux | 2:28d12a3db239 | 80 | |
benparkes | 0:cb3a5c15b01e | 81 | break; |
GeorgeJourneaux | 2:28d12a3db239 | 82 | |
GeorgeJourneaux | 2:28d12a3db239 | 83 | default: |
GeorgeJourneaux | 2:28d12a3db239 | 84 | |
GeorgeJourneaux | 2:28d12a3db239 | 85 | i = 0; |
GeorgeJourneaux | 2:28d12a3db239 | 86 | |
benparkes | 0:cb3a5c15b01e | 87 | break; |
benparkes | 0:cb3a5c15b01e | 88 | } |
benparkes | 0:cb3a5c15b01e | 89 | |
GeorgeJourneaux | 2:28d12a3db239 | 90 | Red_int = !Red_int; |
benparkes | 1:bca9993a0df3 | 91 | |
benparkes | 0:cb3a5c15b01e | 92 | Thread::wait (5000); |
GeorgeJourneaux | 2:28d12a3db239 | 93 | } |
benparkes | 0:cb3a5c15b01e | 94 | } |
benparkes | 0:cb3a5c15b01e | 95 | |
benparkes | 0:cb3a5c15b01e | 96 | |
benparkes | 0:cb3a5c15b01e | 97 | int main() { |
benparkes | 0:cb3a5c15b01e | 98 | |
benparkes | 0:cb3a5c15b01e | 99 | t1.start(PrintLCD); |
GeorgeJourneaux | 2:28d12a3db239 | 100 | |
GeorgeJourneaux | 2:28d12a3db239 | 101 | pc.baud(9600); |
GeorgeJourneaux | 2:28d12a3db239 | 102 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
GeorgeJourneaux | 2:28d12a3db239 | 103 | S_CMD.start(Serial_CMD); |
benparkes | 1:bca9993a0df3 | 104 | |
benparkes | 0:cb3a5c15b01e | 105 | while(1) { |
benparkes | 1:bca9993a0df3 | 106 | Green_int = !Green_int; |
benparkes | 1:bca9993a0df3 | 107 | |
benparkes | 1:bca9993a0df3 | 108 | mail_t *mail = mail_box.alloc(); |
benparkes | 1:bca9993a0df3 | 109 | mail->LDR_Value = LDR_In.read(); |
benparkes | 1:bca9993a0df3 | 110 | mail->temp_Value = Sensor.getTemperature(); |
benparkes | 1:bca9993a0df3 | 111 | mail->press_Value = Sensor.getPressure(); |
GeorgeJourneaux | 2:28d12a3db239 | 112 | mail_box.put(mail); |
benparkes | 0:cb3a5c15b01e | 113 | |
benparkes | 1:bca9993a0df3 | 114 | Thread::wait (15000); |
benparkes | 0:cb3a5c15b01e | 115 | } |
benparkes | 0:cb3a5c15b01e | 116 | |
benparkes | 0:cb3a5c15b01e | 117 | } |
benparkes | 0:cb3a5c15b01e | 118 | |
GeorgeJourneaux | 2:28d12a3db239 | 119 | void Rx_interrupt() { |
GeorgeJourneaux | 2:28d12a3db239 | 120 | |
GeorgeJourneaux | 2:28d12a3db239 | 121 | while (pc.readable()) { |
GeorgeJourneaux | 2:28d12a3db239 | 122 | rx_line[rx_in] = pc.getc(); |
GeorgeJourneaux | 2:28d12a3db239 | 123 | pc.putc(rx_line[rx_in]); |
GeorgeJourneaux | 2:28d12a3db239 | 124 | |
GeorgeJourneaux | 2:28d12a3db239 | 125 | if(rx_line[rx_in] == 0xD){ |
GeorgeJourneaux | 2:28d12a3db239 | 126 | s_cmd = 1; |
GeorgeJourneaux | 2:28d12a3db239 | 127 | } |
GeorgeJourneaux | 2:28d12a3db239 | 128 | else{ |
GeorgeJourneaux | 2:28d12a3db239 | 129 | rx_in = (rx_in + 1); |
GeorgeJourneaux | 2:28d12a3db239 | 130 | } |
GeorgeJourneaux | 2:28d12a3db239 | 131 | } |
GeorgeJourneaux | 2:28d12a3db239 | 132 | } |
benparkes | 0:cb3a5c15b01e | 133 | |
GeorgeJourneaux | 2:28d12a3db239 | 134 | void Serial_CMD(){ |
GeorgeJourneaux | 2:28d12a3db239 | 135 | |
GeorgeJourneaux | 2:28d12a3db239 | 136 | while(1){ |
GeorgeJourneaux | 2:28d12a3db239 | 137 | if(s_cmd == 1){ |
GeorgeJourneaux | 2:28d12a3db239 | 138 | pc.attach(NULL, Serial::RxIrq); |
benparkes | 0:cb3a5c15b01e | 139 | |
GeorgeJourneaux | 2:28d12a3db239 | 140 | if(strstr(rx_line, "READ ALL")){ |
GeorgeJourneaux | 2:28d12a3db239 | 141 | pc.printf("READ ALL\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 142 | } |
GeorgeJourneaux | 2:28d12a3db239 | 143 | else if(strstr(rx_line, "DELETE ALL")){ |
GeorgeJourneaux | 2:28d12a3db239 | 144 | pc.printf("DELETE ALL\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 145 | } |
GeorgeJourneaux | 2:28d12a3db239 | 146 | else if(strstr(rx_line, "READ")){ |
GeorgeJourneaux | 2:28d12a3db239 | 147 | pc.printf("READ\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 148 | } |
GeorgeJourneaux | 2:28d12a3db239 | 149 | else if(strstr(rx_line, "DELETE")){ |
GeorgeJourneaux | 2:28d12a3db239 | 150 | pc.printf("SETDATE\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 151 | } |
GeorgeJourneaux | 2:28d12a3db239 | 152 | else if(strstr(rx_line, "SETTIME")){ |
GeorgeJourneaux | 2:28d12a3db239 | 153 | pc.printf("SETTIME\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 154 | } |
GeorgeJourneaux | 2:28d12a3db239 | 155 | else if(strstr(rx_line, "SETT")){ |
GeorgeJourneaux | 2:28d12a3db239 | 156 | pc.printf("SETT\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 157 | } |
GeorgeJourneaux | 2:28d12a3db239 | 158 | else if(strstr(rx_line, "STATE")){ |
GeorgeJourneaux | 2:28d12a3db239 | 159 | pc.printf("STATE\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 160 | } |
GeorgeJourneaux | 2:28d12a3db239 | 161 | else if(strstr(rx_line, "LOGGING")){ |
GeorgeJourneaux | 2:28d12a3db239 | 162 | pc.printf("LOGGING\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 163 | } |
GeorgeJourneaux | 2:28d12a3db239 | 164 | else{ |
GeorgeJourneaux | 2:28d12a3db239 | 165 | pc.printf("ERROR\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 166 | } |
GeorgeJourneaux | 2:28d12a3db239 | 167 | |
GeorgeJourneaux | 2:28d12a3db239 | 168 | memset(rx_line, NULL, 80); |
GeorgeJourneaux | 2:28d12a3db239 | 169 | rx_in = 0; |
GeorgeJourneaux | 2:28d12a3db239 | 170 | |
GeorgeJourneaux | 2:28d12a3db239 | 171 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
GeorgeJourneaux | 2:28d12a3db239 | 172 | s_cmd = 0; |
GeorgeJourneaux | 2:28d12a3db239 | 173 | } |
GeorgeJourneaux | 2:28d12a3db239 | 174 | Thread::wait(5); |
GeorgeJourneaux | 2:28d12a3db239 | 175 | } |
GeorgeJourneaux | 2:28d12a3db239 | 176 | } |