first compiled version

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Committer:
nleoni
Date:
Tue Mar 04 09:04:44 2014 +0000
Revision:
5:d40a563e2c3b
Parent:
4:6c17983d6192
Child:
6:45c3ad45a252
Child:
7:4992146d9872
Succesfully merged both versions (Rob and Napoleon's)

Who changed what in which revision?

UserRevisionLine numberNew 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
nleoni 5:d40a563e2c3b 10 //I also merged your changes with mine.....temproarily commented out the pc.printf meessage for pot read.
nleoni 5:d40a563e2c3b 11
robertwharrell 1:83bc9f91d154 12
nleoni 0:7ae4f23f84a3 13 #include "mbed.h"
nleoni 0:7ae4f23f84a3 14 #include "rtos.h"
nleoni 3:0945093f48ec 15 #include "C12832_lcd.h"
nleoni 3:0945093f48ec 16 #include <string>
nleoni 3:0945093f48ec 17
nleoni 3:0945093f48ec 18 #define BUFFER 17
nleoni 3:0945093f48ec 19 #define COOKIEQUEUE 30
nleoni 3:0945093f48ec 20
nleoni 3:0945093f48ec 21 C12832_LCD lcd;
nleoni 5:d40a563e2c3b 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 5:d40a563e2c3b 28
nleoni 3:0945093f48ec 29 MemoryPool<char[BUFFER], COOKIEQUEUE> cookieReadingBuffer;
nleoni 3:0945093f48ec 30 Queue<char[BUFFER],COOKIEQUEUE> cookieReadingQueue;
robertwharrell 2:43cd60e0e32f 31
robertwharrell 2:43cd60e0e32f 32 /******************************************************************
robertwharrell 2:43cd60e0e32f 33 *
robertwharrell 2:43cd60e0e32f 34 *An LCD thread that updates the LCD based on information
robertwharrell 2:43cd60e0e32f 35 *received from other threads via IPC
robertwharrell 2:43cd60e0e32f 36 *
robertwharrell 2:43cd60e0e32f 37 *Uses the top 3 lines of the LCD to reflect the pot, the
robertwharrell 2:43cd60e0e32f 38 *temperature, and the cookie. This task must use IPC (with
robertwharrell 2:43cd60e0e32f 39 *timeout) methods to get data from each of the previous threads
robertwharrell 2:43cd60e0e32f 40 *
robertwharrell 2:43cd60e0e32f 41 *******************************************************************/
nleoni 0:7ae4f23f84a3 42 void lcdUpdate(void const*){
nleoni 0:7ae4f23f84a3 43 while(1){
nleoni 3:0945093f48ec 44 osEvent evtPot = potReadingQueue.get(1200);
nleoni 3:0945093f48ec 45 if (evtPot.status == osEventMessage) {
nleoni 3:0945093f48ec 46 float *queuePot = (float*)evtPot.value.p;
nleoni 3:0945093f48ec 47 lcd.locate(0,3);
nleoni 3:0945093f48ec 48 lcd.printf("Voltage: %.2f V", *queuePot);
nleoni 3:0945093f48ec 49 potReadingBuffer.free(queuePot);
nleoni 0:7ae4f23f84a3 50 }
nleoni 3:0945093f48ec 51 osEvent evtCookie = cookieReadingQueue.get(1200);
nleoni 3:0945093f48ec 52 if (evtCookie.status == osEventMessage) {
nleoni 3:0945093f48ec 53 char (*queueCookie)[BUFFER] = (char (*)[BUFFER])evtCookie.value.p;
nleoni 3:0945093f48ec 54 lcd.locate(0,15);
nleoni 3:0945093f48ec 55 string str(*queueCookie);
nleoni 3:0945093f48ec 56 lcd.printf("F.Cookie: %s", str);
nleoni 3:0945093f48ec 57 cookieReadingBuffer.free(queueCookie);
nleoni 3:0945093f48ec 58 }
nleoni 3:0945093f48ec 59 Thread::wait(1000);
nleoni 0:7ae4f23f84a3 60 }
nleoni 0:7ae4f23f84a3 61 }
nleoni 0:7ae4f23f84a3 62
robertwharrell 2:43cd60e0e32f 63 /******************************************************************
robertwharrell 2:43cd60e0e32f 64 *
robertwharrell 2:43cd60e0e32f 65 *A POT thread that reads the pot value in a polling loop
robertwharrell 2:43cd60e0e32f 66 *every 10 seconds and sends value to LCD thread via IPC Queue
robertwharrell 2:43cd60e0e32f 67 *
robertwharrell 2:43cd60e0e32f 68 *******************************************************************/
nleoni 0:7ae4f23f84a3 69 void readPOT(void const*){
nleoni 0:7ae4f23f84a3 70 while(1){
nleoni 3:0945093f48ec 71 float *queue = potReadingBuffer.alloc();
nleoni 0:7ae4f23f84a3 72 *queue = pot1;
nleoni 3:0945093f48ec 73 potReadingQueue.put(queue,1200);
robertwharrell 2:43cd60e0e32f 74 pc.printf("POT read");
robertwharrell 2:43cd60e0e32f 75 Thread::wait(10000);
robertwharrell 2:43cd60e0e32f 76 }
robertwharrell 2:43cd60e0e32f 77
robertwharrell 2:43cd60e0e32f 78 }
robertwharrell 2:43cd60e0e32f 79
robertwharrell 2:43cd60e0e32f 80 /******************************************************************
robertwharrell 2:43cd60e0e32f 81 *
robertwharrell 2:43cd60e0e32f 82 *A TEMP thread that read the temperature every 60 seconds
robertwharrell 2:43cd60e0e32f 83 *and sends the value to the LCD task via IPC Queue
robertwharrell 2:43cd60e0e32f 84 *
robertwharrell 2:43cd60e0e32f 85 *******************************************************************/
nleoni 5:d40a563e2c3b 86
robertwharrell 2:43cd60e0e32f 87 void readTemp(void const*){
robertwharrell 2:43cd60e0e32f 88 while(1){
nleoni 5:d40a563e2c3b 89 //pc.printf("TEMP read");
robertwharrell 2:43cd60e0e32f 90 Thread::wait(60000);
nleoni 0:7ae4f23f84a3 91 }
nleoni 0:7ae4f23f84a3 92
nleoni 0:7ae4f23f84a3 93 }
nleoni 0:7ae4f23f84a3 94
robertwharrell 2:43cd60e0e32f 95 /******************************************************************
robertwharrell 2:43cd60e0e32f 96 *
robertwharrell 2:43cd60e0e32f 97 *A SCANF thread that reads in a fortune cookie from user
robertwharrell 2:43cd60e0e32f 98 *and sends it to the LCD task via IPC Memory Pool
robertwharrell 2:43cd60e0e32f 99 *
robertwharrell 2:43cd60e0e32f 100 *******************************************************************/
nleoni 5:d40a563e2c3b 101 void readCookie(void const*){
nleoni 5:d40a563e2c3b 102 pc.printf(">Enter your fortune cookie\n>");
nleoni 5:d40a563e2c3b 103 char (*ptrBuffer)[BUFFER] = cookieReadingBuffer.alloc();
nleoni 5:d40a563e2c3b 104 char *ptrChar;
nleoni 5:d40a563e2c3b 105 ptrChar=*ptrBuffer;
robertwharrell 2:43cd60e0e32f 106 while(1){
nleoni 5:d40a563e2c3b 107
nleoni 3:0945093f48ec 108 if(pc.readable()){
nleoni 3:0945093f48ec 109 *ptrChar=pc.getc();
nleoni 3:0945093f48ec 110 pc.putc(*ptrChar);
nleoni 3:0945093f48ec 111 if((*ptrChar=='\n') || ((ptrChar-*ptrBuffer)>=(BUFFER-1)) ){
nleoni 3:0945093f48ec 112 if((ptrChar-*ptrBuffer)>=(BUFFER-1)) *++ptrChar='\n';
nleoni 3:0945093f48ec 113 while((ptrChar-*ptrBuffer)<=(BUFFER-1)){
nleoni 3:0945093f48ec 114 *ptrChar++=' ';
nleoni 3:0945093f48ec 115 }
nleoni 3:0945093f48ec 116 *ptrChar='\0';
nleoni 3:0945093f48ec 117 cookieReadingQueue.put(ptrBuffer,500);
nleoni 3:0945093f48ec 118 pc.printf(">Enter your fortune cookie\n>");
nleoni 3:0945093f48ec 119 ptrChar=*ptrBuffer;
nleoni 3:0945093f48ec 120 } else {
nleoni 3:0945093f48ec 121 ptrChar++;
nleoni 3:0945093f48ec 122 }
nleoni 3:0945093f48ec 123 }
nleoni 3:0945093f48ec 124 Thread::wait(10);
robertwharrell 2:43cd60e0e32f 125 }
nleoni 0:7ae4f23f84a3 126 }
nleoni 0:7ae4f23f84a3 127
robertwharrell 2:43cd60e0e32f 128
robertwharrell 2:43cd60e0e32f 129 /******************************************************************
robertwharrell 2:43cd60e0e32f 130 *
robertwharrell 2:43cd60e0e32f 131 *A TOD thread that updates the 4th line of the LCD with time
robertwharrell 2:43cd60e0e32f 132 *of day once a minute. It shares the LCD with the LCD thread
robertwharrell 2:43cd60e0e32f 133 *using mutual exclusion
robertwharrell 2:43cd60e0e32f 134 *
robertwharrell 2:43cd60e0e32f 135 *******************************************************************/
robertwharrell 2:43cd60e0e32f 136 void threadTOD(void const*){
robertwharrell 2:43cd60e0e32f 137 while(1){
robertwharrell 2:43cd60e0e32f 138 pc.printf("TOD updated");
robertwharrell 2:43cd60e0e32f 139 Thread::wait(60000);
robertwharrell 2:43cd60e0e32f 140 }
nleoni 0:7ae4f23f84a3 141 }
nleoni 0:7ae4f23f84a3 142
nleoni 0:7ae4f23f84a3 143 DigitalOut myled(LED1);
nleoni 0:7ae4f23f84a3 144
nleoni 0:7ae4f23f84a3 145 int main() {
nleoni 3:0945093f48ec 146 lcd.cls();
nleoni 3:0945093f48ec 147
nleoni 0:7ae4f23f84a3 148 Thread threadLCD(lcdUpdate);
nleoni 0:7ae4f23f84a3 149 Thread threadPOT(readPOT);
nleoni 0:7ae4f23f84a3 150 Thread threadTemp(readTemp);
nleoni 3:0945093f48ec 151 Thread threadCookie(readCookie);
nleoni 0:7ae4f23f84a3 152
nleoni 0:7ae4f23f84a3 153 while(1) {
nleoni 3:0945093f48ec 154 Thread::wait(250);
nleoni 3:0945093f48ec 155
nleoni 0:7ae4f23f84a3 156 }
nleoni 0:7ae4f23f84a3 157 }