Happy Gecko demo

Dependencies:   Fonts Images SILABS_RHT mbed

Fork of MemLCD-Temperature-Humidity-Demo by Paul Staron

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "MemoryLCD.h"
00002 #include "SILABS_RHT.h"
00003 #include "mbed_EFM32_logo.h"
00004 #include "mbed_enabled_logo.h"
00005 #include "therm32x120.h"
00006 #include "LCD19x28.h"
00007 #include "Arial6x9.h"
00008 #include "ArialR16x17.h"
00009 #include "silabs_logo.h"
00010 
00011 /******************** Define I/O *****************************/
00012 
00013 DigitalOut led1(LED1);
00014 
00015 #define SCK     PE12
00016 #define MOSI    PE10
00017 DigitalOut CS(PA10);
00018 DigitalOut EXTCOM(PF3);
00019 DigitalOut DISP(PA8);
00020 
00021 SPI displaySPI(MOSI, NC, SCK);
00022 silabs::MemoryLCD display(&displaySPI, &CS, &EXTCOM);
00023 
00024 I2C sensorI2C(PD6, PD7);
00025 DigitalOut SENS_EN(PC8);
00026 silabs::SILABS_RHT rhtSensor(&sensorI2C);
00027 
00028 /******************** Define Timers *****************************/
00029 
00030 LowPowerTicker timeKeeping;
00031 
00032 /***************** Define global variables **********************/
00033 #define INIT_SECONDS        0
00034 
00035 volatile uint32_t prevSeconds, seconds = INIT_SECONDS;
00036 volatile bool refreshed = false;
00037 volatile bool measured = false;
00038 
00039 float temperature,humidity;
00040 int update,n,t,pt;
00041 char tempbuffer[62];
00042 
00043 void tempupdate();
00044 
00045 // Callback for temperature upadte 
00046 void updateCallback(void) {
00047     update = true;
00048 }
00049 // Callback for refresh completion
00050 void refreshCallback(void) {
00051     refreshed = true;
00052 }
00053 // Callback for measurement completion
00054 void measureCallback(void) {
00055     if(rhtSensor.get_active()) {
00056         measured = true;
00057     }
00058 }
00059 
00060 /*************************** MAIN *******************************/
00061 int main() {
00062 
00063     // Enable the LCD
00064     DISP = 1;
00065     EXTCOM = 1;
00066     // Enable the I2C RHT sensor
00067     SENS_EN = 1;
00068 
00069     // Reset the LCD to a blank state. (All white)  
00070     refreshed = false;
00071     display.clearImmediate(refreshCallback);
00072     while(refreshed == false) sleep();
00073     
00074     // Perform a measurement
00075     rhtSensor.check_availability(si7021, measureCallback);
00076     while(measured == false);
00077 
00078     rhtSensor.measure(si7021);
00079     
00080     //while(measured == false); //This did not work here.   
00081     wait(.1);                   // but this delay does to finish the initial measurement function.
00082     
00083     // initialise some screen graphics
00084     display.set_font(Arial6x9);
00085     display.locate(28,114);
00086     display.printf("12 Hours");
00087     display.locate(2,57);
00088     display.printf("40-");
00089     display.locate(2,69);
00090     display.printf("30-");
00091     display.locate(2,81);
00092     display.printf("20-");
00093     display.locate(3,93);
00094     display.printf("10-");
00095     display.vline(20,60,110,Black);
00096     display.hline(20,80,110,Black);
00097     display.showBMP((uint8_t*)therm32x120, 32, 120, 96, 0);
00098     
00099     // Start generating the 10 second call for updating measurments
00100     timeKeeping.attach(&updateCallback, 10.0f);
00101     
00102     // Get initial screen display before sleep function
00103     tempupdate();   
00104     
00105     // Main loop 
00106     while(1) {
00107         
00108         sleep();    // low power sleep, wake and update display every 10 seconds.
00109                     // 28uA average power consumption, 334 days with a 225mAH CR2032 cell
00110 
00111         // update once every 10 seconds
00112         if(update==true) {  
00113             tempupdate();
00114         }
00115         update=false;
00116     }
00117 }
00118 
00119 void tempupdate()
00120 {
00121 // update temperature display
00122     led1=1;
00123     rhtSensor.measure(si7021);      
00124     temperature = rhtSensor.get_temperature();
00125     humidity = rhtSensor.get_humidity();
00126     display.set_font(LCD19x28);
00127     display.locate(5,6);
00128     display.printf("%2.1f'c", temperature/1000);
00129     display.set_font(ArialR16x17);
00130     display.locate(6,38);
00131     display.printf("%02.2f%%rH", humidity/1000);
00132     
00133     // Update rolling temperature histogram every 720 seconds  
00134     if(t>71){       
00135         if(temperature<41000){  // don't overshoot plot display     
00136             int xpos=21; // plot start position    
00137             if (pt<61){  // keep filling temperature buffer until full, 60 values 
00138                 tempbuffer[pt] = 109-(temperature/1000*1.2);      
00139                 pt++;
00140             }
00141             else{  // when buffer full drop last value and shift other values down one position
00142                 for(int nn = 1; nn < 61; nn++) {
00143                     tempbuffer[nn-1] = tempbuffer[nn];
00144                 }
00145                 tempbuffer[60] = 109-(temperature/1000*1.2);  // when buffer full, add current value to end of buffer
00146             }   
00147             display.fillrect(21,56,61,54,White); // clear plot display field  
00148             for(int nn = 0; nn < (pt-1); nn++) {  // display to current pt value or complete field if buffer full
00149                 if(tempbuffer[nn]!=0){display.vline(nn+xpos, tempbuffer[nn], 109, Black);}  // only display valid values starting at plot start position
00150             }
00151         t=0;
00152         }           
00153     }
00154     t++;    
00155     
00156     // Update the graphical thermometer 
00157     uint32_t line = 20;
00158     int8_t int_temp = temperature / 500;
00159     for(int8_t iterator = 76; iterator > -6; iterator--) {
00160         if(int_temp >= iterator) display.fillrect(105,line,10,1,Black);
00161             else display.fillrect(105,line,10,1,White);
00162         line += 1;
00163     }    
00164     
00165     // Send buffer to display
00166     refreshed == false;
00167     if(refreshed == true) {
00168         display.update(refreshCallback);
00169     }
00170     led1=0; 
00171 }
00172 
00173