Michael Knudson / Mbed 2 deprecated Thermostat

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE

Revision:
6:65bbd44cabd7
Parent:
5:568a15151d11
Child:
7:58575c249736
--- a/main.cpp	Mon Apr 13 15:00:47 2020 +0000
+++ b/main.cpp	Mon Apr 27 00:56:36 2020 +0000
@@ -10,9 +10,88 @@
 DigitalOut relay(p21);
 AnalogIn tmp36(p19);
 AnalogIn pot(p20);
+RawSerial  tooth(p28,p27);
 
-int currTemp, desiredTemp;
+volatile int currTemp, desiredTemp;
 bool isHeating;
+Mutex desiredTempLock;
+
+bool setOrange;
+bool setGray;
+
+void setLCDGraphics(int temp) {
+    if(setOrange) {
+        uLCD.circle(64,64,61,WHITE);
+        uLCD.filled_circle(64,64,60,0xF2A007);
+        uLCD.locate(2,5);
+        uLCD.text_width(1);
+        uLCD.text_height(1);
+        uLCD.text_mode(TRANSPARENT);
+        uLCD.color(WHITE);
+        uLCD.printf("mbed Thermostat\r\n\r\n     ");
+        uLCD.text_width(4);
+        uLCD.text_height(4);
+        uLCD.printf("%d\r\n",currTemp);
+        uLCD.text_width(1);
+        uLCD.text_height(1);
+        uLCD.locate(6,10);
+        uLCD.printf("\r\n\r\n   Heating to:%d", desiredTemp);
+    }
+    else if(setGray) {
+        uLCD.circle(64,64,61,WHITE);
+        uLCD.filled_circle(64,64,60,0x969AE0);
+        uLCD.locate(2,5);
+        uLCD.text_width(1);
+        uLCD.text_height(1);
+        uLCD.text_mode(TRANSPARENT);
+        uLCD.color(WHITE);
+        uLCD.printf("mbed Thermostat\r\n\r\n     ");
+        uLCD.text_width(4);
+        uLCD.text_height(4);
+        uLCD.printf("%d\r\n",currTemp);
+        uLCD.text_width(1);
+        uLCD.text_height(1);
+        uLCD.locate(6,10);
+        uLCD.printf("\r\n\r\n   Cooling to:%d", desiredTemp);
+    }
+    else if(!setOrange && !setGray) {
+        uLCD.circle(64,64,61,WHITE);
+        uLCD.filled_circle(64,64,60,0x255ECF);
+        uLCD.locate(2,5);
+        uLCD.text_width(1);
+        uLCD.text_height(1);
+        uLCD.text_mode(TRANSPARENT);
+        uLCD.color(WHITE);
+        uLCD.printf("mbed Thermostat\r\n\r\n     ");
+        uLCD.text_width(4);
+        uLCD.text_height(4);
+        uLCD.printf("%d\r\n",currTemp);
+        uLCD.text_width(1);
+        uLCD.text_height(1);
+        uLCD.locate(6,10);
+        uLCD.printf("\r\n\r\n     Set to:%d", desiredTemp);
+    }
+}
+
+//void compareTemp(void const *args) {
+void compareTemp() {
+    while(true) {
+        if(currTemp > desiredTemp) {
+            setGray = true;
+            setOrange = false;
+        }
+        else if(currTemp < desiredTemp) {
+            setOrange = true;
+            setGray = true;
+        }
+        else if(currTemp == desiredTemp) {
+            setOrange = false;
+            setGray = false;
+        }
+        setLCDGraphics(currTemp);
+        Thread::wait(3000);
+    }
+}
 
 // Helper function
 // convert tmp36 reading to degrees fahrenheit
@@ -32,29 +111,65 @@
         isHeating = !isHeating;
     }
 }
+void bluetooth() {
+    while(1){
+        if (!tooth.readable()) Thread::yield();
+        char bnum=0;
+        char bhit=0;
+        if (tooth.getc()=='!') {
+            if (tooth.getc()=='B') { //button data packet
+                bnum = tooth.getc(); //button number
+                bhit = tooth.getc(); //1=hit, 0=release
+                if (tooth.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
+                    switch (bnum) {
+                        case '5': //up arrow
+                            if (bhit=='1') {
+                                desiredTempLock.lock();
+                                desiredTemp++;
+                                desiredTempLock.unlock();
+                            }
+                            break;
+                        case '6': //button 6 down arrow
+                            if (bhit=='1') {
+                                desiredTempLock.lock();
+                                desiredTemp--;
+                                desiredTempLock.unlock();
+                            }
+                            break;
+                    }
+                }
+            }
+        }
+        Thread::wait(10);
+    }
+}
 
+Thread bluetooth_thread;
+Thread uLCD_thread;
 int main() {
     
-    uLCD.printf("\nCurrent temp:\n\nDesired temp:");
-            
+    desiredTemp = 72;
+    
+    bluetooth_thread.start(bluetooth);
+
+    uLCD.display_control(LANDSCAPE);
+    uLCD.cls();
+    uLCD.baudrate(30000); //jack up baud rate
+    wait(1.0);
+    uLCD.cls();
+
+    uLCD_thread.start(compareTemp);
+    
     bool shouldHeat = false;
     isHeating = false;
     
-    Timer main, on, off;
-    main.start();
+    Timer tMain, on, off;
+    tMain.start();
     while(1) {
-        if (main.read_ms() > 500) {
-            main.reset();
+        if (tMain.read_ms() > 500) {
+            tMain.reset();
             currTemp = tmp2f();
-            desiredTemp = 90-pot.read()*35;
             shouldHeat = currTemp < desiredTemp;
-    
-            uLCD.locate(14, 1);
-            uLCD.printf("%d", currTemp);
-            uLCD.locate(14, 3);
-            uLCD.printf("%d", desiredTemp);
-            
-            
             if (isHeating) attemptToggle(&off, !shouldHeat);
             if (!isHeating) attemptToggle(&on, shouldHeat);
             
@@ -62,3 +177,4 @@
         }
     }
 }
+