first compiled version
Dependencies: mbed-rtos mbed C12832_lcd LM75B
main.cpp@4:6c17983d6192, 2014-03-04 (annotated)
- Committer:
- nleoni
- Date:
- Tue Mar 04 08:56:46 2014 +0000
- Revision:
- 4:6c17983d6192
- Parent:
- 3:0945093f48ec
- Parent:
- 2:43cd60e0e32f
- Child:
- 5:d40a563e2c3b
merging changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nleoni | 3:0945093f48ec | 1 | //Rob I updated the LCD update and read pot thread and they are working |
nleoni | 3:0945093f48ec | 2 | //One key issues was adding timeout for the put and get methods of the queue |
nleoni | 3:0945093f48ec | 3 | //Maybe you can work on the TOD thread and MUTEX (you'll need to add it to the |
nleoni | 3:0945093f48ec | 4 | //LCD thread and I will finish the scan thread, sounds like Plan?? :-) |
nleoni | 3:0945093f48ec | 5 | //In the end I had to add the memory pool combined with the queue as otherwise |
nleoni | 3:0945093f48ec | 6 | //the queues just keep pointers to data and being that the putting and getting of |
nleoni | 3:0945093f48ec | 7 | //the pot values in the queue is asynchronous you need a buffer to keep the values |
nleoni | 3:0945093f48ec | 8 | //and this is provided by the MemoryPool. |
nleoni | 3:0945093f48ec | 9 | |
robertwharrell | 1:83bc9f91d154 | 10 | |
nleoni | 0:7ae4f23f84a3 | 11 | #include "mbed.h" |
nleoni | 0:7ae4f23f84a3 | 12 | #include "rtos.h" |
nleoni | 3:0945093f48ec | 13 | #include "C12832_lcd.h" |
nleoni | 3:0945093f48ec | 14 | #include <string> |
nleoni | 3:0945093f48ec | 15 | |
nleoni | 3:0945093f48ec | 16 | #define BUFFER 17 |
nleoni | 3:0945093f48ec | 17 | #define COOKIEQUEUE 30 |
nleoni | 3:0945093f48ec | 18 | |
nleoni | 3:0945093f48ec | 19 | C12832_LCD lcd; |
nleoni | 3:0945093f48ec | 20 | Serial pc(USBTX, USBRX); // tx, rx |
nleoni | 0:7ae4f23f84a3 | 21 | |
robertwharrell | 2:43cd60e0e32f | 22 | Serial pc(USBTX, USBRX); //To differentiate from LCD functions |
robertwharrell | 2:43cd60e0e32f | 23 | |
nleoni | 0:7ae4f23f84a3 | 24 | AnalogIn pot1(p19); |
nleoni | 3:0945093f48ec | 25 | MemoryPool<float, 10> potReadingBuffer; |
nleoni | 0:7ae4f23f84a3 | 26 | Queue<float,10> potReadingQueue; |
nleoni | 0:7ae4f23f84a3 | 27 | |
nleoni | 4:6c17983d6192 | 28 | <<<<<<< local |
nleoni | 3:0945093f48ec | 29 | MemoryPool<char[BUFFER], COOKIEQUEUE> cookieReadingBuffer; |
nleoni | 3:0945093f48ec | 30 | Queue<char[BUFFER],COOKIEQUEUE> cookieReadingQueue; |
nleoni | 4:6c17983d6192 | 31 | ======= |
nleoni | 4:6c17983d6192 | 32 | >>>>>>> other |
robertwharrell | 2:43cd60e0e32f | 33 | |
nleoni | 4:6c17983d6192 | 34 | <<<<<<< local |
nleoni | 4:6c17983d6192 | 35 | ======= |
robertwharrell | 2:43cd60e0e32f | 36 | /****************************************************************** |
robertwharrell | 2:43cd60e0e32f | 37 | * |
robertwharrell | 2:43cd60e0e32f | 38 | *An LCD thread that updates the LCD based on information |
robertwharrell | 2:43cd60e0e32f | 39 | *received from other threads via IPC |
robertwharrell | 2:43cd60e0e32f | 40 | * |
robertwharrell | 2:43cd60e0e32f | 41 | *Uses the top 3 lines of the LCD to reflect the pot, the |
robertwharrell | 2:43cd60e0e32f | 42 | *temperature, and the cookie. This task must use IPC (with |
robertwharrell | 2:43cd60e0e32f | 43 | *timeout) methods to get data from each of the previous threads |
robertwharrell | 2:43cd60e0e32f | 44 | * |
robertwharrell | 2:43cd60e0e32f | 45 | *******************************************************************/ |
nleoni | 4:6c17983d6192 | 46 | >>>>>>> other |
nleoni | 0:7ae4f23f84a3 | 47 | void lcdUpdate(void const*){ |
nleoni | 0:7ae4f23f84a3 | 48 | while(1){ |
nleoni | 4:6c17983d6192 | 49 | <<<<<<< local |
nleoni | 3:0945093f48ec | 50 | osEvent evtPot = potReadingQueue.get(1200); |
nleoni | 3:0945093f48ec | 51 | if (evtPot.status == osEventMessage) { |
nleoni | 3:0945093f48ec | 52 | float *queuePot = (float*)evtPot.value.p; |
nleoni | 3:0945093f48ec | 53 | lcd.locate(0,3); |
nleoni | 3:0945093f48ec | 54 | lcd.printf("Voltage: %.2f V", *queuePot); |
nleoni | 3:0945093f48ec | 55 | potReadingBuffer.free(queuePot); |
nleoni | 4:6c17983d6192 | 56 | ======= |
nleoni | 0:7ae4f23f84a3 | 57 | osEvent evt = potReadingQueue.get(); |
nleoni | 0:7ae4f23f84a3 | 58 | if (evt.status == osEventMail) { |
nleoni | 0:7ae4f23f84a3 | 59 | float *queue = (float*)evt.value.p; |
robertwharrell | 2:43cd60e0e32f | 60 | pc.printf("\nVoltage: %.2f V\n\r" , *queue); |
robertwharrell | 2:43cd60e0e32f | 61 | |
robertwharrell | 2:43cd60e0e32f | 62 | pc.printf("LCD updated"); |
nleoni | 0:7ae4f23f84a3 | 63 | Thread::wait(1000); |
nleoni | 4:6c17983d6192 | 64 | >>>>>>> other |
nleoni | 0:7ae4f23f84a3 | 65 | } |
nleoni | 3:0945093f48ec | 66 | osEvent evtCookie = cookieReadingQueue.get(1200); |
nleoni | 3:0945093f48ec | 67 | if (evtCookie.status == osEventMessage) { |
nleoni | 3:0945093f48ec | 68 | char (*queueCookie)[BUFFER] = (char (*)[BUFFER])evtCookie.value.p; |
nleoni | 3:0945093f48ec | 69 | lcd.locate(0,15); |
nleoni | 3:0945093f48ec | 70 | string str(*queueCookie); |
nleoni | 3:0945093f48ec | 71 | lcd.printf("F.Cookie: %s", str); |
nleoni | 3:0945093f48ec | 72 | cookieReadingBuffer.free(queueCookie); |
nleoni | 3:0945093f48ec | 73 | } |
nleoni | 3:0945093f48ec | 74 | Thread::wait(1000); |
nleoni | 0:7ae4f23f84a3 | 75 | } |
nleoni | 0:7ae4f23f84a3 | 76 | } |
nleoni | 0:7ae4f23f84a3 | 77 | |
robertwharrell | 2:43cd60e0e32f | 78 | /****************************************************************** |
robertwharrell | 2:43cd60e0e32f | 79 | * |
robertwharrell | 2:43cd60e0e32f | 80 | *A POT thread that reads the pot value in a polling loop |
robertwharrell | 2:43cd60e0e32f | 81 | *every 10 seconds and sends value to LCD thread via IPC Queue |
robertwharrell | 2:43cd60e0e32f | 82 | * |
robertwharrell | 2:43cd60e0e32f | 83 | *******************************************************************/ |
nleoni | 0:7ae4f23f84a3 | 84 | void readPOT(void const*){ |
nleoni | 0:7ae4f23f84a3 | 85 | while(1){ |
nleoni | 3:0945093f48ec | 86 | float *queue = potReadingBuffer.alloc(); |
nleoni | 0:7ae4f23f84a3 | 87 | *queue = pot1; |
nleoni | 4:6c17983d6192 | 88 | <<<<<<< local |
nleoni | 3:0945093f48ec | 89 | potReadingQueue.put(queue,1200); |
nleoni | 0:7ae4f23f84a3 | 90 | |
nleoni | 0:7ae4f23f84a3 | 91 | Thread::wait(10000); |
nleoni | 4:6c17983d6192 | 92 | ======= |
nleoni | 0:7ae4f23f84a3 | 93 | potReadingQueue.put(queue); |
robertwharrell | 2:43cd60e0e32f | 94 | pc.printf("POT read"); |
robertwharrell | 2:43cd60e0e32f | 95 | Thread::wait(10000); |
nleoni | 4:6c17983d6192 | 96 | >>>>>>> other |
robertwharrell | 2:43cd60e0e32f | 97 | } |
robertwharrell | 2:43cd60e0e32f | 98 | |
robertwharrell | 2:43cd60e0e32f | 99 | } |
robertwharrell | 2:43cd60e0e32f | 100 | |
nleoni | 4:6c17983d6192 | 101 | <<<<<<< local |
nleoni | 3:0945093f48ec | 102 | |
nleoni | 3:0945093f48ec | 103 | |
nleoni | 4:6c17983d6192 | 104 | ======= |
robertwharrell | 2:43cd60e0e32f | 105 | /****************************************************************** |
robertwharrell | 2:43cd60e0e32f | 106 | * |
robertwharrell | 2:43cd60e0e32f | 107 | *A TEMP thread that read the temperature every 60 seconds |
robertwharrell | 2:43cd60e0e32f | 108 | *and sends the value to the LCD task via IPC Queue |
robertwharrell | 2:43cd60e0e32f | 109 | * |
robertwharrell | 2:43cd60e0e32f | 110 | *******************************************************************/ |
nleoni | 4:6c17983d6192 | 111 | >>>>>>> other |
robertwharrell | 2:43cd60e0e32f | 112 | void readTemp(void const*){ |
robertwharrell | 2:43cd60e0e32f | 113 | while(1){ |
robertwharrell | 2:43cd60e0e32f | 114 | pc.printf("TEMP read"); |
robertwharrell | 2:43cd60e0e32f | 115 | Thread::wait(60000); |
nleoni | 0:7ae4f23f84a3 | 116 | } |
nleoni | 0:7ae4f23f84a3 | 117 | |
nleoni | 0:7ae4f23f84a3 | 118 | } |
nleoni | 0:7ae4f23f84a3 | 119 | |
nleoni | 4:6c17983d6192 | 120 | <<<<<<< local |
nleoni | 3:0945093f48ec | 121 | void readCookie(void const*){ |
nleoni | 3:0945093f48ec | 122 | pc.printf(">Enter your fortune cookie\n>"); |
nleoni | 3:0945093f48ec | 123 | char (*ptrBuffer)[BUFFER] = cookieReadingBuffer.alloc(); |
nleoni | 3:0945093f48ec | 124 | char *ptrChar; |
nleoni | 3:0945093f48ec | 125 | ptrChar=*ptrBuffer; |
nleoni | 4:6c17983d6192 | 126 | ======= |
robertwharrell | 2:43cd60e0e32f | 127 | /****************************************************************** |
robertwharrell | 2:43cd60e0e32f | 128 | * |
robertwharrell | 2:43cd60e0e32f | 129 | *A SCANF thread that reads in a fortune cookie from user |
robertwharrell | 2:43cd60e0e32f | 130 | *and sends it to the LCD task via IPC Memory Pool |
robertwharrell | 2:43cd60e0e32f | 131 | * |
robertwharrell | 2:43cd60e0e32f | 132 | *******************************************************************/ |
robertwharrell | 2:43cd60e0e32f | 133 | void threadCookie(void const*){ |
robertwharrell | 2:43cd60e0e32f | 134 | //Checks often but low priority? |
nleoni | 4:6c17983d6192 | 135 | >>>>>>> other |
robertwharrell | 2:43cd60e0e32f | 136 | while(1){ |
nleoni | 4:6c17983d6192 | 137 | <<<<<<< local |
nleoni | 3:0945093f48ec | 138 | if(pc.readable()){ |
nleoni | 3:0945093f48ec | 139 | *ptrChar=pc.getc(); |
nleoni | 3:0945093f48ec | 140 | pc.putc(*ptrChar); |
nleoni | 3:0945093f48ec | 141 | if((*ptrChar=='\n') || ((ptrChar-*ptrBuffer)>=(BUFFER-1)) ){ |
nleoni | 3:0945093f48ec | 142 | if((ptrChar-*ptrBuffer)>=(BUFFER-1)) *++ptrChar='\n'; |
nleoni | 3:0945093f48ec | 143 | while((ptrChar-*ptrBuffer)<=(BUFFER-1)){ |
nleoni | 3:0945093f48ec | 144 | *ptrChar++=' '; |
nleoni | 3:0945093f48ec | 145 | } |
nleoni | 3:0945093f48ec | 146 | *ptrChar='\0'; |
nleoni | 3:0945093f48ec | 147 | cookieReadingQueue.put(ptrBuffer,500); |
nleoni | 3:0945093f48ec | 148 | pc.printf(">Enter your fortune cookie\n>"); |
nleoni | 3:0945093f48ec | 149 | ptrChar=*ptrBuffer; |
nleoni | 3:0945093f48ec | 150 | } else { |
nleoni | 3:0945093f48ec | 151 | ptrChar++; |
nleoni | 3:0945093f48ec | 152 | } |
nleoni | 3:0945093f48ec | 153 | } |
nleoni | 3:0945093f48ec | 154 | Thread::wait(10); |
nleoni | 3:0945093f48ec | 155 | } |
nleoni | 4:6c17983d6192 | 156 | ======= |
robertwharrell | 2:43cd60e0e32f | 157 | Thread::wait(1000); |
robertwharrell | 2:43cd60e0e32f | 158 | } |
nleoni | 0:7ae4f23f84a3 | 159 | } |
nleoni | 0:7ae4f23f84a3 | 160 | |
robertwharrell | 2:43cd60e0e32f | 161 | |
robertwharrell | 2:43cd60e0e32f | 162 | /****************************************************************** |
robertwharrell | 2:43cd60e0e32f | 163 | * |
robertwharrell | 2:43cd60e0e32f | 164 | *A TOD thread that updates the 4th line of the LCD with time |
robertwharrell | 2:43cd60e0e32f | 165 | *of day once a minute. It shares the LCD with the LCD thread |
robertwharrell | 2:43cd60e0e32f | 166 | *using mutual exclusion |
robertwharrell | 2:43cd60e0e32f | 167 | * |
robertwharrell | 2:43cd60e0e32f | 168 | *******************************************************************/ |
robertwharrell | 2:43cd60e0e32f | 169 | void threadTOD(void const*){ |
robertwharrell | 2:43cd60e0e32f | 170 | while(1){ |
robertwharrell | 2:43cd60e0e32f | 171 | pc.printf("TOD updated"); |
robertwharrell | 2:43cd60e0e32f | 172 | Thread::wait(60000); |
robertwharrell | 2:43cd60e0e32f | 173 | } |
nleoni | 4:6c17983d6192 | 174 | >>>>>>> other |
nleoni | 0:7ae4f23f84a3 | 175 | } |
nleoni | 0:7ae4f23f84a3 | 176 | |
nleoni | 0:7ae4f23f84a3 | 177 | DigitalOut myled(LED1); |
nleoni | 0:7ae4f23f84a3 | 178 | |
nleoni | 0:7ae4f23f84a3 | 179 | int main() { |
nleoni | 3:0945093f48ec | 180 | lcd.cls(); |
nleoni | 3:0945093f48ec | 181 | |
nleoni | 0:7ae4f23f84a3 | 182 | Thread threadLCD(lcdUpdate); |
nleoni | 0:7ae4f23f84a3 | 183 | Thread threadPOT(readPOT); |
nleoni | 0:7ae4f23f84a3 | 184 | Thread threadTemp(readTemp); |
nleoni | 3:0945093f48ec | 185 | Thread threadCookie(readCookie); |
nleoni | 0:7ae4f23f84a3 | 186 | |
nleoni | 0:7ae4f23f84a3 | 187 | while(1) { |
nleoni | 3:0945093f48ec | 188 | Thread::wait(250); |
nleoni | 3:0945093f48ec | 189 | |
nleoni | 0:7ae4f23f84a3 | 190 | } |
nleoni | 0:7ae4f23f84a3 | 191 | } |