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