This program is an advanced "IoT" thermometer using LM75B component library. It displays the current value to the Serial host in Celcius or Fahrenheit (possible to change using a switch). It also stores an historic and displays it to the user using the LCD screen. Moreover, you can change the orientation of the screen by turning the device, just like a smartphone.

Dependencies:   C12832 FXOS8700CQ LM75B mbed

Revision:
5:c5d7fca4d111
Parent:
4:5bf99eb2d740
Child:
6:6c61186c8739
--- a/main.cpp	Wed Feb 10 16:48:55 2016 +0000
+++ b/main.cpp	Fri Feb 12 09:45:41 2016 +0000
@@ -1,6 +1,6 @@
 /**
  *  This program is an advanced "IoT" thermometer using LM75B component library.
- *  It displays the current value to the Serial host in Celcius or Fahrenheit (possible to change using a switch).
+ *  It displays the current value to the Serial host in Celcius or Fahrenheit (possible to change using a switch or computer's 'c' and 'f').
  *  It also stores an historic and displays it to the user using the LCD screen.
  *  Moreover, you can change the orientation of the screen by turning the device (just like a smartphone)
  *
@@ -25,7 +25,7 @@
 /*
  *  RGB LED control
  */
-void rgb_handler()
+void rgb_handler(void)
 {
     if (celcius) {
         r_led = 1.0;
@@ -46,10 +46,34 @@
     celcius = !celcius;
 }
 
+/*
+ *  Thread that does the same as above
+ */
+void host_thread(void const *args)
+{
+    while (true) {
+        if (host.readable()) {
+            switch(host.getc()) {
+                case 'c':
+                    celcius = true;
+                    break;
+                case 'f':
+                    celcius = false;
+                    break;
+                default:
+                    break;
+            }
+        }
+        Thread::wait(500);
+    }
+}
+
 int main(void)
 {
     host.baud(38400);
-    host.printf("Hello gtvl2, from FRDM-K64F!\r\nUse POT1 to change the refresh rate, POT2 to change the scale of the graphic and SW2 to change the unit.\r\n");
+    if (host.writeable()) {
+        host.printf("Hello gtvl2, from FRDM-K64F!\r\nUse POT1 to change the refresh rate, POT2 to change the scale of the graphic and SW2 to change the unit.\r\n");
+    }
 
     tilt_led = 0.0;
     r_led = 1.0;
@@ -65,6 +89,9 @@
     rgb_tick.attach(&rgb_handler, 0.001);
     sw2_int.mode(PullUp);
     sw2_int.fall(&sw_handler);
+    if (host.readable()) {
+        Thread thread(host_thread);
+    }
 
     while (true) {
         tilt_led = 0.0;
@@ -81,17 +108,19 @@
          * Display value to host according to unit choosed with SW2.
          * Could have been a ternary, but it's easier to read like this
          */
-        if (celcius) {
-            host.printf("Temp: %.2f deg Celcius.\r\n", temperature);
-        } else {
-            host.printf("Temp: %.2f deg Fahrenheit.\r\n", (temperature * 1.8f + 32.0f));
+        if (host.writeable()) {
+            if (celcius) {
+                host.printf("Temp: %.2f deg Celcius.\r\n", temperature);
+            } else {
+                host.printf("Temp: %.2f deg Fahrenheit.\r\n", (temperature * 1.8f + 32.0f));
+            }
         }
-
+        
         /*
          * Change refresh_time according to POT1's value
          */
         tilt_led = 1.0;
         refresh_time = (int) (MIN_REFRESH_TIME + ((1.0f - pot) * (MAX_REFRESH_TIME - MIN_REFRESH_TIME)));
-        wait_ms(refresh_time);
+        Thread::wait(refresh_time);
     }
 }
\ No newline at end of file