MemLCD-Temperature-Humidity-Demo: This is an example program for the Happy Gecko starter kit, which includes an on-board Sharp memory LCD, and a Silicon Labs si7021 relative humidity and temperature sensor.

Dependencies:   MemoryLCD SILABS_RHT mbed

Demo of memory LCD and RHT library

Demo running on Happy Gecko starter kit

This demo is meant to run on a Silicon Labs EFM32 Happy Gecko Starter Kit, and will demonstrate the use of the Memory LCD and si70xx sensor libraries. It can exclusively be used with the low-power version of mbed.

For documentation on the libraries, please refer to the respective library pages.

Committer:
stevew817
Date:
Mon Sep 12 10:47:12 2016 +0000
Revision:
7:90fb154b57f7
Parent:
6:2186477a8aa2
Update libraries to latest version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stevew817 0:42249df8c223 1 #include "LS013B7DH03.h"
stevew817 0:42249df8c223 2 #include "SILABS_RHT.h"
stevew817 6:2186477a8aa2 3 #include "mbed_logo_thermo.h"
stevew817 0:42249df8c223 4 #include "Animation.h"
stevew817 0:42249df8c223 5
stevew817 0:42249df8c223 6 /******************** Define I/O *****************************/
stevew817 0:42249df8c223 7
stevew817 0:42249df8c223 8 InterruptIn in(SW0);
stevew817 0:42249df8c223 9
stevew817 0:42249df8c223 10 #define SCK PE12
stevew817 0:42249df8c223 11 #define MOSI PE10
stevew817 0:42249df8c223 12
stevew817 0:42249df8c223 13 DigitalOut CS(PA10);
stevew817 0:42249df8c223 14 DigitalOut EXTCOM(PF3);
stevew817 0:42249df8c223 15 DigitalOut DISP(PA8);
stevew817 0:42249df8c223 16
stevew817 0:42249df8c223 17 SPI displaySPI(MOSI, NC, SCK);
stevew817 0:42249df8c223 18 silabs::LS013B7DH03 display(&displaySPI, &CS, &EXTCOM);
stevew817 0:42249df8c223 19
stevew817 0:42249df8c223 20 I2C sensorI2C(PD6, PD7);
stevew817 0:42249df8c223 21 DigitalOut SENS_EN(PC8);
stevew817 0:42249df8c223 22 silabs::SILABS_RHT rhtSensor(&sensorI2C);
stevew817 0:42249df8c223 23
stevew817 0:42249df8c223 24 /******************** Define Timers *****************************/
stevew817 0:42249df8c223 25
stevew817 0:42249df8c223 26 LowPowerTicker timeKeeping;
stevew817 0:42249df8c223 27
stevew817 0:42249df8c223 28 /***************** Define global variables **********************/
stevew817 0:42249df8c223 29 #define INIT_SECONDS 17600
stevew817 0:42249df8c223 30 #define TEST_DURATION 10
stevew817 0:42249df8c223 31
stevew817 0:42249df8c223 32 volatile uint32_t prevSeconds = INIT_SECONDS, seconds = INIT_SECONDS;
stevew817 0:42249df8c223 33 volatile bool refreshed = false;
stevew817 0:42249df8c223 34 volatile bool clockShown = false;
stevew817 0:42249df8c223 35 volatile bool measured = false;
stevew817 0:42249df8c223 36
stevew817 0:42249df8c223 37 typedef enum {
stevew817 0:42249df8c223 38 CLOCK,
stevew817 0:42249df8c223 39 DODECA
stevew817 0:42249df8c223 40 } display_mode_t;
stevew817 0:42249df8c223 41
stevew817 0:42249df8c223 42 volatile display_mode_t currentMode = CLOCK;
stevew817 0:42249df8c223 43 uint8_t currentFrame = 0;
stevew817 0:42249df8c223 44
stevew817 0:42249df8c223 45 /***************** Define callback handlers *********************/
stevew817 0:42249df8c223 46 void secondsCallback(void);
stevew817 0:42249df8c223 47 void in_handler();
stevew817 0:42249df8c223 48 void toggleCallback(void);
stevew817 0:42249df8c223 49
stevew817 0:42249df8c223 50 /***************** Define helper functions ***********************/
stevew817 0:42249df8c223 51 void drawTemperature(int32_t temperature);
stevew817 0:42249df8c223 52
stevew817 0:42249df8c223 53 void drawTemperature(int32_t temperature) {
stevew817 0:42249df8c223 54 uint32_t line = 20;
stevew817 0:42249df8c223 55 int8_t int_temp = temperature / 500;
stevew817 0:42249df8c223 56
stevew817 0:42249df8c223 57 for(int8_t iterator = 80; iterator > -6; iterator--) {
stevew817 0:42249df8c223 58 if(int_temp >= iterator) display.fill(108,line,10,1,Black);
stevew817 0:42249df8c223 59 else display.fill(108,line,10,1,White);
stevew817 0:42249df8c223 60
stevew817 0:42249df8c223 61 line += 1;
stevew817 0:42249df8c223 62 }
stevew817 0:42249df8c223 63 }
stevew817 0:42249df8c223 64
stevew817 0:42249df8c223 65 /**
stevew817 0:42249df8c223 66 * Callback for pushbutton interrupt
stevew817 0:42249df8c223 67 */
stevew817 0:42249df8c223 68 void in_handler() {
stevew817 0:42249df8c223 69 if(currentMode == CLOCK) {
stevew817 0:42249df8c223 70 currentMode = DODECA;
stevew817 0:42249df8c223 71 } else {
stevew817 0:42249df8c223 72 currentMode = CLOCK;
stevew817 0:42249df8c223 73 clockShown = false;
stevew817 0:42249df8c223 74 }
stevew817 0:42249df8c223 75 }
stevew817 0:42249df8c223 76
stevew817 0:42249df8c223 77 void secondsCallback(void) {
stevew817 0:42249df8c223 78 seconds++;
stevew817 0:42249df8c223 79 }
stevew817 0:42249df8c223 80
stevew817 0:42249df8c223 81 /**
stevew817 0:42249df8c223 82 * Callback for refresh completion
stevew817 0:42249df8c223 83 */
stevew817 0:42249df8c223 84 void refreshCallback(void) {
stevew817 0:42249df8c223 85 refreshed = true;
stevew817 0:42249df8c223 86 }
stevew817 0:42249df8c223 87
stevew817 0:42249df8c223 88 /**
stevew817 0:42249df8c223 89 * Callback for measurement completion
stevew817 0:42249df8c223 90 */
stevew817 0:42249df8c223 91 void measureCallback(void) {
stevew817 0:42249df8c223 92 if(rhtSensor.get_active()) {
stevew817 0:42249df8c223 93 measured = true;
stevew817 0:42249df8c223 94 }
stevew817 0:42249df8c223 95 }
stevew817 0:42249df8c223 96
stevew817 0:42249df8c223 97 /*************************** MAIN *******************************/
stevew817 0:42249df8c223 98 int main() {
stevew817 0:42249df8c223 99 // Initialize pushbutton handler
stevew817 0:42249df8c223 100 in.rise(NULL);
stevew817 0:42249df8c223 101 in.fall(in_handler);
stevew817 0:42249df8c223 102
stevew817 0:42249df8c223 103 // Enable the LCD
stevew817 0:42249df8c223 104 DISP = 1;
stevew817 0:42249df8c223 105
stevew817 0:42249df8c223 106 // Enable the I2C RHT sensor
stevew817 0:42249df8c223 107 SENS_EN = 1;
stevew817 0:42249df8c223 108
stevew817 0:42249df8c223 109 // Start generating the 1Hz call for the timekeeping
stevew817 0:42249df8c223 110 timeKeeping.attach(&secondsCallback, 1.0f);
stevew817 0:42249df8c223 111
stevew817 0:42249df8c223 112 // Reset the LCD to a blank state. (All white)
stevew817 0:42249df8c223 113 refreshed = false;
stevew817 0:42249df8c223 114 display.clearImmediate(refreshCallback);
stevew817 0:42249df8c223 115 while(refreshed == false) sleep();
stevew817 0:42249df8c223 116
stevew817 0:42249df8c223 117 printf("Initialization done! \n");
stevew817 0:42249df8c223 118
stevew817 0:42249df8c223 119 // Apply mbed logo bitmap to the pixel buffer
stevew817 6:2186477a8aa2 120 display.showBMP((uint8_t*)mbed_logo_thermometer, 128, 128, 0, 0);
stevew817 0:42249df8c223 121 display.background(Black);
stevew817 0:42249df8c223 122 display.foreground(White);
stevew817 0:42249df8c223 123 display.printf("I like MBED!");
stevew817 0:42249df8c223 124
stevew817 0:42249df8c223 125 // Push update to the display
stevew817 0:42249df8c223 126 uint32_t refreshCount = display.getRefreshTicks();
stevew817 0:42249df8c223 127 refreshed = false;
stevew817 0:42249df8c223 128 display.update(refreshCallback);
stevew817 0:42249df8c223 129
stevew817 0:42249df8c223 130 // Sleep while doing the transmit
stevew817 0:42249df8c223 131 while(refreshed == false) sleep();
stevew817 0:42249df8c223 132
stevew817 0:42249df8c223 133 // Calculate and print refresh duration
stevew817 0:42249df8c223 134 refreshCount = display.getRefreshTicks() - refreshCount;
stevew817 0:42249df8c223 135 printf("Refresh duration: %d cycles @ 125Hz \n", (int)refreshCount);
stevew817 0:42249df8c223 136
stevew817 0:42249df8c223 137 // Perform a measurement
stevew817 0:42249df8c223 138 rhtSensor.check_availability(si7021, measureCallback);
stevew817 0:42249df8c223 139 while(measured == false);
stevew817 0:42249df8c223 140
stevew817 0:42249df8c223 141 /* Main loop */
stevew817 0:42249df8c223 142 while(1) {
stevew817 0:42249df8c223 143 sleep();
stevew817 0:42249df8c223 144
stevew817 0:42249df8c223 145 // In clock mode, only update once per second
stevew817 0:42249df8c223 146 if((currentMode == CLOCK) && (prevSeconds != seconds)) {
stevew817 0:42249df8c223 147 /* Redraw background when coming from video mode */
stevew817 0:42249df8c223 148 if(clockShown == false) {
stevew817 6:2186477a8aa2 149 display.showBMP((uint8_t*)mbed_logo_thermometer, 128, 128, 0, 0);
stevew817 0:42249df8c223 150 clockShown = true;
stevew817 0:42249df8c223 151 }
stevew817 0:42249df8c223 152
stevew817 0:42249df8c223 153 /* Show numeric measurements */
stevew817 0:42249df8c223 154 display.locate(0,13);
stevew817 0:42249df8c223 155 display.printf("%02d.%01d degC", rhtSensor.get_temperature() / 1000, (rhtSensor.get_temperature() % 1000) / 100);
stevew817 0:42249df8c223 156 display.locate(0,14);
stevew817 0:42249df8c223 157 display.printf("%03d.%03d%%H", rhtSensor.get_humidity() / 1000, rhtSensor.get_humidity() % 1000);
stevew817 0:42249df8c223 158
stevew817 0:42249df8c223 159 /* Show clock */
stevew817 0:42249df8c223 160 display.locate(4,15);
stevew817 0:42249df8c223 161 display.printf("%02d:%02d:%02d", (seconds / 1200) % 24, (seconds / 60) % 60, seconds % 60);
stevew817 0:42249df8c223 162
stevew817 0:42249df8c223 163 /* Update the graphical thermometer */
stevew817 0:42249df8c223 164 drawTemperature(rhtSensor.get_temperature());
stevew817 0:42249df8c223 165
stevew817 0:42249df8c223 166 if(refreshed == true) {
stevew817 0:42249df8c223 167 prevSeconds = seconds;
stevew817 0:42249df8c223 168 refreshed = false;
stevew817 0:42249df8c223 169
stevew817 0:42249df8c223 170 /* Perform both I/O tasks simultaneously */
stevew817 0:42249df8c223 171 display.update(refreshCallback);
stevew817 0:42249df8c223 172 rhtSensor.measure(si7021);
stevew817 0:42249df8c223 173 }
stevew817 0:42249df8c223 174 }
stevew817 0:42249df8c223 175 // In dodecaeder mode, show frames as fast as possible
stevew817 0:42249df8c223 176 else if(currentMode == DODECA) {
stevew817 0:42249df8c223 177 if(refreshed == true) {
stevew817 0:42249df8c223 178 display.showBMP((uint8_t*)(frames[currentFrame]->pData), 128, 128, 0, 0);
stevew817 0:42249df8c223 179 currentFrame++;
stevew817 0:42249df8c223 180 if( currentFrame >= NUM_FRAMES ) currentFrame = 0;
stevew817 0:42249df8c223 181 refreshed = false;
stevew817 0:42249df8c223 182 display.update(refreshCallback);
stevew817 0:42249df8c223 183 }
stevew817 0:42249df8c223 184 }
stevew817 0:42249df8c223 185 }
stevew817 0:42249df8c223 186
stevew817 0:42249df8c223 187 //notify_completion(true);
stevew817 0:42249df8c223 188 }
stevew817 0:42249df8c223 189