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:
7:e5732637dfd0
Parent:
6:6c61186c8739
Child:
8:6f30f477fa23
--- a/main.cpp	Mon Feb 22 19:09:17 2016 +0000
+++ b/main.cpp	Tue Feb 23 16:57:15 2016 +0000
@@ -1,7 +1,8 @@
 /**
  *  This program is an advanced "IoT" thermometer using LM75B component library.
- *  It displays the current value to the Serial host in Celcius or Fahrenheit.
+ *  It displays the current value to the Serial host in Celcius (BLUE) or Fahrenheit (GREEN).
  *  It is possible to change units using SW2 switch or computer's 'c' and 'f' keys.
+ *  You can use SW3 to pause/resume to data collection or computer's 'p' key.
  *  It also stores a small 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.
  *
@@ -17,8 +18,9 @@
 #include "MyTimeout.hpp"
 
 Serial              pc(USBTX, USBRX);           // Serial Host
-LM75B               lm_temp(D14, D15);          // Thermal Sensor
+LM75B               lm_temp(D14, D15);          // Temperature Sensor
 InterruptIn         sw2_int(SW2);               // SW2
+InterruptIn         sw3_int(SW3);               // SW3
 DigitalOut          tilt_led(LED1);             // Red LED
 DigitalOut          r_led(PTA2);                // RGB LED
 DigitalOut          g_led(PTC4);
@@ -26,15 +28,17 @@
 AnalogIn            pot(A0);                    // Potentiometer POT1
 MyTimeout           process_timeout;            // Main timeout used for the whole process
 
-volatile bool       celcius = true;             // Global boolean for unit Celcius / Fahrenheit
+volatile bool       celcius;                    // Global boolean for unit Celcius / Fahrenheit
+volatile bool       running;                    // Either the program is running or paused
 volatile int        refresh_time = 3000;
 
 ThermalDisplayer    temp_display = ThermalDisplayer();
 
 /*
  *  SW2 handler
+ *  Changes the unit and adapts LED to the new one.
  */
-void                sw_interrupt(void)
+void                sw2_interrupt(void)
 {
     celcius = !celcius;
 
@@ -43,30 +47,47 @@
         g_led = 1.0;
         b_led = 0.0;
     } else {
-        r_led = 0.0;
-        g_led = 1.0;
+        r_led = 1.0;
+        g_led = 0.0;
         b_led = 1.0;
     }
 }
 
 /*
- *  Interrupt that does the same as above but using keyboard of host
+ *  SW3 handler
+ *  Used to pause / resume the data collection and display.
+ */
+void                sw3_interrupt(void)
+{
+    running = !running;
+    if (!running) {
+        process_timeout.detach();
+        r_led = 0.0;
+        g_led = 1.0;
+        b_led = 1.0;
+    } else {
+        celcius = !celcius;
+        sw2_interrupt();
+    }
+}
+
+/*
+ *  Interrupt that does the same as SW2 handler above but using keyboard of host
  */
 void                host_interrupt(void)
 {
-#if defined _DEBUG
-    pc.printf("Check if pc key as been pressed\r\n");
-#endif
     if (pc.readable()) {
-        char c = pc.getc();
-        switch(c) {
+        switch(pc.getc()) {
             case 'c':
                 celcius = false;
-                sw_interrupt();
+                sw2_interrupt();
                 break;
             case 'f':
                 celcius = true;
-                sw_interrupt();
+                sw2_interrupt();
+                break;
+            case 'p':
+                sw3_interrupt();
                 break;
             default:
                 break;
@@ -114,28 +135,32 @@
 {
     pc.baud(38400);
     if (pc.writeable()) {
-        pc.printf("Hello gtvl2, from FRDM-K64F!\r\nUse POT1 to change the refresh rate, POT2 to change the scale of the graphic and SW2 or keyboard to change the unit.\r\n");
+        pc.printf("Hello gtvl2, from FRDM-K64F!\r\nUse POT1 to change the refresh rate, POT2 to change the scale of the graphic, SW2 or keyboard to change the unit and SW3 to pause.\r\n");
     }
 
     /*
      * Thermal unit control
      */
     sw2_int.mode(PullUp);
-    sw2_int.fall(&sw_interrupt);
+    sw2_int.fall(&sw2_interrupt);
     pc.attach(&host_interrupt);
-
+    sw3_int.mode(PullUp);
+    sw3_int.fall(&sw3_interrupt);
+    
     r_led = 1.0;
     g_led = 1.0;
     b_led = 0.0;
+    celcius = true;
+    running = true;
 
     // Do once
     process_function();
     wait_ms(1);
     // Then forever
     while (true) {
-        if (!process_timeout.hasAttachment()) {
+        if (running && !process_timeout.hasAttachment()) {
 #if defined _DEBUG
-            pc.printf("Next value in %.3f seconds\r\n", refresh_time / 1000.0f);
+            pc.printf("Next cycle in %.3f seconds\r\n", refresh_time / 1000.0f);
 #endif
             process_timeout.attach_us(&process_function, (float) refresh_time * 1000.0f);
         }