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
main.cpp@4:5bf99eb2d740, 2016-02-10 (annotated)
- Committer:
- co838_gtvl2
- Date:
- Wed Feb 10 16:48:55 2016 +0000
- Revision:
- 4:5bf99eb2d740
- Parent:
- 3:a4dac093a968
- Child:
- 5:c5d7fca4d111
WOW !; Speed of display x100 and ability to reverse screen using accelerometer !
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co838_gtvl2 | 4:5bf99eb2d740 | 1 | /** |
co838_gtvl2 | 4:5bf99eb2d740 | 2 | * This program is an advanced "IoT" thermometer using LM75B component library. |
co838_gtvl2 | 4:5bf99eb2d740 | 3 | * It displays the current value to the Serial host in Celcius or Fahrenheit (possible to change using a switch). |
co838_gtvl2 | 4:5bf99eb2d740 | 4 | * It also stores an historic and displays it to the user using the LCD screen. |
co838_gtvl2 | 4:5bf99eb2d740 | 5 | * Moreover, you can change the orientation of the screen by turning the device (just like a smartphone) |
co838_gtvl2 | 4:5bf99eb2d740 | 6 | * |
co838_gtvl2 | 4:5bf99eb2d740 | 7 | * Created by Guillaume Lefrant gtvl2 |
co838_gtvl2 | 4:5bf99eb2d740 | 8 | **/ |
co838_gtvl2 | 4:5bf99eb2d740 | 9 | |
co838_gtvl2 | 1:06713d1b69cf | 10 | #include "main.h" |
co838_gtvl2 | 1:06713d1b69cf | 11 | #include "ThermalDisplayer.h" |
co838_gtvl2 | 0:362773c24009 | 12 | |
co838_gtvl2 | 1:06713d1b69cf | 13 | Serial host(USBTX, USBRX); // Serial Host |
co838_gtvl2 | 1:06713d1b69cf | 14 | LM75B lm_temp(D14, D15); // Thermal Sensor |
co838_gtvl2 | 1:06713d1b69cf | 15 | InterruptIn sw2_int(SW2); // SW2 |
co838_gtvl2 | 4:5bf99eb2d740 | 16 | DigitalOut tilt_led(LED1); // Red LED |
co838_gtvl2 | 1:06713d1b69cf | 17 | DigitalOut r_led(PTA2); // RGB LED |
co838_gtvl2 | 1:06713d1b69cf | 18 | DigitalOut g_led(PTC4); |
co838_gtvl2 | 4:5bf99eb2d740 | 19 | DigitalOut b_led(PTC12); |
co838_gtvl2 | 1:06713d1b69cf | 20 | AnalogIn pot(A0); // Potentiometer POT1 |
co838_gtvl2 | 0:362773c24009 | 21 | |
co838_gtvl2 | 1:06713d1b69cf | 22 | Ticker rgb_tick; // Ticker for RGB LED control |
co838_gtvl2 | 1:06713d1b69cf | 23 | volatile bool celcius = true; // Global boolean for unit Celcius / Fahrenheit |
co838_gtvl2 | 0:362773c24009 | 24 | |
co838_gtvl2 | 1:06713d1b69cf | 25 | /* |
co838_gtvl2 | 1:06713d1b69cf | 26 | * RGB LED control |
co838_gtvl2 | 1:06713d1b69cf | 27 | */ |
co838_gtvl2 | 2:5d0c209e5c61 | 28 | void rgb_handler() |
co838_gtvl2 | 2:5d0c209e5c61 | 29 | { |
co838_gtvl2 | 0:362773c24009 | 30 | if (celcius) { |
co838_gtvl2 | 0:362773c24009 | 31 | r_led = 1.0; |
co838_gtvl2 | 4:5bf99eb2d740 | 32 | g_led = 1.0; |
co838_gtvl2 | 4:5bf99eb2d740 | 33 | b_led = 0.0; |
co838_gtvl2 | 0:362773c24009 | 34 | } else { |
co838_gtvl2 | 0:362773c24009 | 35 | r_led = 0.0; |
co838_gtvl2 | 0:362773c24009 | 36 | g_led = 1.0; |
co838_gtvl2 | 4:5bf99eb2d740 | 37 | b_led = 1.0; |
co838_gtvl2 | 2:5d0c209e5c61 | 38 | } |
co838_gtvl2 | 0:362773c24009 | 39 | } |
co838_gtvl2 | 0:362773c24009 | 40 | |
co838_gtvl2 | 1:06713d1b69cf | 41 | /* |
co838_gtvl2 | 1:06713d1b69cf | 42 | * SW2 handler |
co838_gtvl2 | 1:06713d1b69cf | 43 | */ |
co838_gtvl2 | 2:5d0c209e5c61 | 44 | void sw_handler (void) |
co838_gtvl2 | 2:5d0c209e5c61 | 45 | { |
co838_gtvl2 | 0:362773c24009 | 46 | celcius = !celcius; |
co838_gtvl2 | 0:362773c24009 | 47 | } |
co838_gtvl2 | 0:362773c24009 | 48 | |
co838_gtvl2 | 2:5d0c209e5c61 | 49 | int main(void) |
co838_gtvl2 | 2:5d0c209e5c61 | 50 | { |
co838_gtvl2 | 0:362773c24009 | 51 | host.baud(38400); |
co838_gtvl2 | 3:a4dac093a968 | 52 | 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"); |
co838_gtvl2 | 2:5d0c209e5c61 | 53 | |
co838_gtvl2 | 4:5bf99eb2d740 | 54 | tilt_led = 0.0; |
co838_gtvl2 | 4:5bf99eb2d740 | 55 | r_led = 1.0; |
co838_gtvl2 | 0:362773c24009 | 56 | g_led = 0.0; |
co838_gtvl2 | 2:5d0c209e5c61 | 57 | b_led = 1.0; |
co838_gtvl2 | 2:5d0c209e5c61 | 58 | |
co838_gtvl2 | 1:06713d1b69cf | 59 | int refresh_time = 3000; |
co838_gtvl2 | 2:5d0c209e5c61 | 60 | |
co838_gtvl2 | 1:06713d1b69cf | 61 | /* |
co838_gtvl2 | 1:06713d1b69cf | 62 | * Thermal unit control |
co838_gtvl2 | 1:06713d1b69cf | 63 | */ |
co838_gtvl2 | 2:5d0c209e5c61 | 64 | ThermalDisplayer *temp_display = new ThermalDisplayer(); |
co838_gtvl2 | 1:06713d1b69cf | 65 | rgb_tick.attach(&rgb_handler, 0.001); |
co838_gtvl2 | 0:362773c24009 | 66 | sw2_int.mode(PullUp); |
co838_gtvl2 | 2:5d0c209e5c61 | 67 | sw2_int.fall(&sw_handler); |
co838_gtvl2 | 2:5d0c209e5c61 | 68 | |
co838_gtvl2 | 2:5d0c209e5c61 | 69 | while (true) { |
co838_gtvl2 | 4:5bf99eb2d740 | 70 | tilt_led = 0.0; |
co838_gtvl2 | 1:06713d1b69cf | 71 | /* |
co838_gtvl2 | 1:06713d1b69cf | 72 | * Get temperature using LM75B |
co838_gtvl2 | 1:06713d1b69cf | 73 | * and display it to LCD screen |
co838_gtvl2 | 1:06713d1b69cf | 74 | */ |
co838_gtvl2 | 0:362773c24009 | 75 | float temperature = lm_temp.read(); |
co838_gtvl2 | 1:06713d1b69cf | 76 | temp_display->addTemp(temperature); |
co838_gtvl2 | 4:5bf99eb2d740 | 77 | temp_display->adjustScale(host); |
co838_gtvl2 | 1:06713d1b69cf | 78 | temp_display->display(); |
co838_gtvl2 | 2:5d0c209e5c61 | 79 | |
co838_gtvl2 | 2:5d0c209e5c61 | 80 | /* |
co838_gtvl2 | 2:5d0c209e5c61 | 81 | * Display value to host according to unit choosed with SW2. |
co838_gtvl2 | 2:5d0c209e5c61 | 82 | * Could have been a ternary, but it's easier to read like this |
co838_gtvl2 | 1:06713d1b69cf | 83 | */ |
co838_gtvl2 | 0:362773c24009 | 84 | if (celcius) { |
co838_gtvl2 | 0:362773c24009 | 85 | host.printf("Temp: %.2f deg Celcius.\r\n", temperature); |
co838_gtvl2 | 0:362773c24009 | 86 | } else { |
co838_gtvl2 | 4:5bf99eb2d740 | 87 | host.printf("Temp: %.2f deg Fahrenheit.\r\n", (temperature * 1.8f + 32.0f)); |
co838_gtvl2 | 0:362773c24009 | 88 | } |
co838_gtvl2 | 2:5d0c209e5c61 | 89 | |
co838_gtvl2 | 1:06713d1b69cf | 90 | /* |
co838_gtvl2 | 2:5d0c209e5c61 | 91 | * Change refresh_time according to POT1's value |
co838_gtvl2 | 1:06713d1b69cf | 92 | */ |
co838_gtvl2 | 4:5bf99eb2d740 | 93 | tilt_led = 1.0; |
co838_gtvl2 | 3:a4dac093a968 | 94 | refresh_time = (int) (MIN_REFRESH_TIME + ((1.0f - pot) * (MAX_REFRESH_TIME - MIN_REFRESH_TIME))); |
co838_gtvl2 | 1:06713d1b69cf | 95 | wait_ms(refresh_time); |
co838_gtvl2 | 0:362773c24009 | 96 | } |
co838_gtvl2 | 0:362773c24009 | 97 | } |