Very advanced Click IR Thermo example for Hexiwear featuring OLED Display, Bluetooth, Cloud and Touch

Dependencies:   Hexi_KW40Z Hexi_MLX90614 Hexi_OLED_SSD1351

Fork of Hexi_Click_IRThermo_Example by Hexiwear

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /******************************************************************************
00003 * Includes
00004 *******************************************************************************/
00005 
00006 #include "mbed.h"
00007 #include "mlx90614.h"
00008 #include "Hexi_KW40Z.h"
00009 #include "Hexi_OLED_SSD1351.h"
00010 #include "OLED_types.h"
00011 #include "OpenSans_Font.h"
00012 #include "string.h"
00013 #include "irimages_c.h"
00014 
00015  
00016 
00017 DigitalOut blueLed(LED3,1);
00018 DigitalOut haptic(PTB9);
00019 
00020 I2C i2c(PTD9,PTD8);         //sda,scl (start i2c interace)
00021 
00022 Serial pc(USBTX,USBRX);     //serial usb config for debug
00023 
00024 MLX90614 IR_thermometer(&i2c); // instantiate IR Thermometer
00025 
00026 
00027 /* prototypes */
00028 void Sys_Init(void);
00029 void readTemp(void);
00030 void StartHaptic(void);
00031 void StopHaptic(void const *n);
00032 void txTask(void);
00033 void animateDisplay(void);
00034 void displayTemp(void);
00035 
00036 /* Define timer for haptic feedback */
00037 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
00038 
00039 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 
00040 KW40Z kw40z_device(PTE24, PTE25);
00041 
00042 
00043 /* Instantiate the SSD1351 OLED Driver */ 
00044 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
00045 
00046 /*Create a Thread to handle sending BLE Sensor Data */ 
00047 Thread txThread;
00048 
00049 /*Create a Thread to handle reading the sensor and display*/ 
00050 Thread dispThread;
00051 
00052  /* Text Buffer */ 
00053 char text[20];
00054 
00055 /* Temperature images */
00056 const uint8_t *blue1;       // Pointer for the image to be displayed
00057 const uint8_t *orange2;     // Pointer for the image to be displayed
00058 const uint8_t *red3;        // Pointer for the image to be displayed
00059 
00060 float tempC; //temperature in degrees C
00061 float tempF; //temperature in degrees F
00062 
00063 
00064 /****************************Call Back Functions*******************************/
00065 
00066 void ButtonRight(void)
00067 {
00068     StartHaptic();
00069     kw40z_device.ToggleAdvertisementMode();
00070 }
00071 
00072 void ButtonLeft(void)
00073 {
00074     StartHaptic();
00075     kw40z_device.ToggleAdvertisementMode();
00076 }
00077 
00078 void PassKey(void)
00079 {
00080     StartHaptic();
00081   
00082     /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
00083     sprintf(text,"%d", kw40z_device.GetPassKey());
00084     oled.TextBox((uint8_t *)text,0,72,95,18);
00085 }
00086 
00087 /***********************End of Call Back Functions*****************************/
00088 
00089 /********************************Main******************************************/
00090 
00091 int main()
00092 {    
00093     Sys_Init(); // system initial values
00094     
00095     while (true) 
00096     {
00097         blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
00098         Thread::wait(50);
00099         
00100     }
00101 }
00102 
00103 /******************************End of Main*************************************/
00104 
00105 
00106 /* txTask() transmits the sensor data */
00107 void txTask(void){
00108    
00109    while (true) 
00110    {
00111         
00112         /*Notify Hexiwear App that it is running Sensor Tag mode*/
00113         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
00114       
00115         kw40z_device.SendTemperature(tempC*100); //send IR Click Temperature
00116       
00117         Thread::wait(1000);                 
00118     }
00119 }
00120 
00121 
00122 void StartHaptic(void)  {
00123     hapticTimer.start(50);
00124     haptic = 1;
00125 }
00126 
00127 void StopHaptic(void const *n) {
00128     haptic = 0;
00129     hapticTimer.stop();
00130 }
00131 
00132 void readTemp(void) {
00133     if (IR_thermometer.getTemp(&tempC)) {
00134     //gets temperature from sensor via I2C bus
00135         
00136         tempF = tempC * 9.0 / 5 + 32; // convert C to F
00137         
00138         //print temperature on PC
00139         printf("Temperature is %4.2F degrees C\r\n",tempC);
00140         printf("Temperature is %4.2F degrees F\r\n",tempF);
00141         }
00142 }
00143 
00144 void Sys_Init(void){
00145     
00146     blue1 = blue1s_bmp;      // Pointer for the image to be displayed
00147     orange2 = orange2s_bmp;  // Pointer for the image to be displayed
00148     red3 = red3s_bmp;        // Pointer for the image to be displayed
00149     
00150     /* Fills the screen with solid black */         
00151     oled.FillScreen(COLOR_BLACK);
00152     
00153      /* Turn on the backlight of the OLED Display */
00154 //    oled.DimScreenON();
00155        
00156     oled.DrawImage(blue1,0,0);          // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0
00157     wait(0.5);
00158     oled.DrawImage(orange2,0,0);        // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0
00159     wait(0.5);
00160     oled.DrawImage(red3,0,0);           // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0
00161     wait(0.5);  
00162     
00163     /* Get OLED Class Default Text Properties */
00164     oled_text_properties_t textProperties = {0};
00165     oled.GetTextProperties(&textProperties);    
00166     
00167     /* Register callbacks to application functions */
00168     kw40z_device.attach_buttonLeft(&ButtonLeft);
00169     kw40z_device.attach_buttonRight(&ButtonRight);
00170     kw40z_device.attach_passkey(&PassKey);
00171 
00172     /* Change font color to white */ 
00173     textProperties.font = OpenSans_12x18_Regular;
00174     textProperties.fontColor   = COLOR_WHITE;
00175     textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
00176     oled.SetTextProperties(&textProperties);
00177     
00178     strcpy((char *) text,"C");
00179     /* Display Label at x=60,y=72 */ 
00180     oled.Label((uint8_t *)text,60,72);
00181         
00182     txThread.start(txTask); /*Start transmitting Sensor Tag Data */
00183     dispThread.start(animateDisplay); /* start reading the temperature */
00184 }
00185 
00186  
00187 
00188 void animateDisplay(void){
00189             
00190     while(true){
00191                 
00192         readTemp();
00193         
00194         if (tempC > 30) // for temps over 30 display red
00195              oled.DrawImage(red3,0,0);
00196         else if (tempC < 30 || tempC > 10) // for temps between 10 and 30 display orange
00197              oled.DrawImage(orange2,0,0);
00198         else if (tempC < 10)  // for temps under 10 display blue       
00199              oled.DrawImage(blue1,0,0);
00200         displayTemp(); // show temp on display
00201         
00202         Thread::wait(500);
00203     }    
00204 }            
00205 
00206 void displayTemp(void){
00207  
00208      /* format the text */       
00209      sprintf(text,"%.1f", tempC);
00210     /* Display Label at x=22,y=80 */ 
00211      oled.TextBox((uint8_t *)text,22,72,30,18);
00212 }