Very advanced Click Air Quality example for Hexiwear featuring OLED Display, Bluetooth, Cloud and Touch

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 
00003 /******************************************************************************
00004 * Includes
00005 *******************************************************************************/
00006 
00007 #include "mbed.h"
00008 #include "Hexi_OLED_SSD1351.h"
00009 #include "Hexi_KW40Z.h"
00010 #include "images.h"
00011 #include "new_images.h"
00012 #include "string.h"
00013 #include "stdbool.h"
00014 
00015 /******************************************************************************
00016 * Input and Output Pinout
00017 *******************************************************************************/
00018 
00019 AnalogIn ain(PTB2);
00020 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)
00021 KW40Z kw40z_device(PTE24, PTE25); // Instantiate the Hexi KW40Z Driver (UART TX, UART RX)
00022 DigitalOut blueLed(LED3,1);
00023 Serial pc(USBTX, USBRX);
00024 
00025 Thread txThread;
00026 
00027 /******************************************************************************
00028 * prototypes
00029 *******************************************************************************/
00030 void InitModules();
00031 void ReadSensor();
00032 void CalculatePPM();
00033 void DisplayAirQValue(uint16_t value);
00034 
00035 
00036 /******************************************************************************
00037 * Module Variable Definitions
00038 *******************************************************************************/
00039                 
00040 uint16_t ppm;
00041 
00042 uint16_t value = 0;
00043 uint16_t value_old = 0;
00044 uint16_t adc_rd;
00045     
00046 //const uint8_t *image1;          // old cover image
00047 const uint8_t *green_arrow;     // Pointer for the image to be displayed
00048 const uint8_t *yellow_arrow;    // Pointer for the image to be displayed
00049 const uint8_t *orange_arrow;    // Pointer for the image to be displayed
00050 const uint8_t *red_arrow;       // Pointer for the image to be displayed
00051 const uint8_t *purple_arrow;    // Pointer for the image to be displayed
00052 
00053 char text[20];                  // Text Buffer for dynamic value displayed
00054 
00055 bool test = false; // used to put the program in text mode to check images    
00056 
00057 /**************************************************************************************************
00058 * Call Back Functions
00059 **************************************************************************************************/
00060 
00061 void ButtonRight(void)
00062 {
00063     kw40z_device.ToggleAdvertisementMode();
00064 }
00065 
00066 void ButtonLeft(void)
00067 {
00068     kw40z_device.ToggleAdvertisementMode();
00069 }
00070 
00071 void PassKey(void)
00072 {
00073     /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
00074     sprintf(text,"%d", kw40z_device.GetPassKey());
00075     oled.TextBox((uint8_t *)text,0,72,95,18);
00076 }
00077 
00078 
00079 
00080 /**************************************************************************************************
00081 * Function Main()
00082 * -------------------------------------------------------------------------------------------------
00083 * Overview:
00084 * Input: None
00085 * Output: None
00086 **************************************************************************************************/
00087 
00088 int main() 
00089 {
00090 
00091     InitModules();
00092    
00093     while (true) 
00094     {
00095        
00096         ReadSensor();
00097         pc.printf("Sensor Value 0x%04X\n\r",ain.read_u16());
00098 
00099         blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
00100        
00101   
00102         CalculatePPM();
00103         DisplayAirQValue(ppm);
00104         
00105         Thread::wait(500); 
00106 
00107     }
00108 }
00109 
00110 /**************************************************************************************************
00111 * Function TxTask()
00112 **************************************************************************************************/
00113 
00114 void txTask(void){
00115    
00116    while (true) 
00117    {
00118         
00119         /*Notify Hexiwear App that it is running Sensor Tag mode*/
00120         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
00121       
00122         kw40z_device.SendPressure(10*ppm); //send ppm Air Quality Click value
00123       
00124         Thread::wait(1000);                 
00125     }
00126 }
00127 
00128 /**************************************************************************************************
00129 * Function InitModules()
00130 * -------------------------------------------------------------------------------------------------
00131 * Overview:
00132 * Input: None
00133 * Output: None
00134 **************************************************************************************************/
00135 
00136 void InitModules()
00137 {
00138     //image1  = AirQuality;         // Setting pointer location of the 96 by 96 pixel bitmap
00139     //oled.DrawImage(image1,0,0);     // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0  
00140     
00141     green_arrow = Green_arrow_bmp;      // Setting pointer location of the 96 by 96 pixel bitmap Arrow Point to Green To begin
00142     yellow_arrow = Yellow_arrow_bmp;    // Setting pointer location of the 96 by 96 pixel bitmap
00143     orange_arrow = Orange_arrow_bmp;    // Setting pointer location of the 96 by 96 pixel bitmap 
00144     red_arrow = Red_arrow_bmp;          // Setting pointer location of the 96 by 96 pixel bitmap 
00145     purple_arrow = Purple_arrow_bmp;    // Setting pointer location of the 96 by 96 pixel bitmap 
00146     
00147     oled.DrawImage(green_arrow,0,0);     // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 
00148     
00149     oled_text_properties_t textProperties = {0};    // Get OLED Class Default Text Properties
00150     oled.GetTextProperties(&textProperties); 
00151     
00152     /* Register callbacks to application functions */
00153     kw40z_device.attach_buttonLeft(&ButtonLeft);
00154     kw40z_device.attach_buttonRight(&ButtonRight);
00155     kw40z_device.attach_passkey(&PassKey);
00156       
00157     //PPM text now in the Bitmap
00158    
00159     //textProperties.fontColor = COLOR_BLUE;          // Set text properties to white and right aligned for the dynamic text
00160     //textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
00161     //oled.SetTextProperties(&textProperties);  
00162       
00163     //strcpy((char *) text,"ppm:");       // Display Legends
00164    // oled.Label((uint8_t *)text,15,75);      
00165     
00166     textProperties.fontColor = COLOR_WHITE;          // Set text properties to white and right aligned for the dynamic text
00167     textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
00168     oled.SetTextProperties(&textProperties);  
00169 
00170     sprintf(text,"%i",value);                       // Format the value
00171     oled.TextBox((uint8_t *)text,55,76,20,15);   //Increase textbox for more digits
00172 
00173     txThread.start(txTask); /*Start transmitting Sensor Tag Data */      
00174 
00175 }
00176 
00177 
00178 /**************************************************************************************************
00179 * Function ReadSensor()
00180 * -------------------------------------------------------------------------------------------------
00181 * Overview: Read sensor.
00182 * Input: None
00183 * Output: None
00184 **************************************************************************************************/
00185 
00186 void ReadSensor()
00187 {
00188     adc_rd = ain.read_u16();
00189     wait_ms(10);
00190 }
00191 
00192 
00193 /**************************************************************************************************
00194 * Function CalculatePPM()
00195 * -------------------------------------------------------------------------------------------------
00196 * Overview: Calculation of PPM.
00197 * Input: None
00198 * Output: None
00199 **************************************************************************************************/
00200     
00201 void CalculatePPM()
00202 {
00203     const double Rl      = 5000.0;               // Rl (Ohm) - Load resistance
00204     const double Vadc_33 = 0.0000503548;         // ADC step 3,3V/65535 0.00503mV (16bit ADC)
00205     double Vrl;                                  // Output voltage
00206     double Rs;                                   // Rs (Ohm) - Sensor resistance
00207     double ratio;                                // Rs/Rl ratio
00208     double lgPPM;
00209 
00210     Vrl = (double)adc_rd * Vadc_33;            // For 3.3V Vcc use Vadc_33
00211     Rs = Rl * (3.3 - Vrl)/Vrl;                 // Calculate sensor resistance
00212     ratio = Rs/Rl;                             // Calculate ratio
00213     lgPPM = (log10(ratio) * -0.8) + 0.9;       // Calculate ppm
00214     ppm = 6* pow(10,lgPPM);                       // Calculate ppm
00215 }
00216 
00217 
00218 
00219 /**************************************************************************************************
00220 * Function DisplayAirQValue()
00221 * -------------------------------------------------------------------------------------------------
00222 * Overview:
00223 * Input: None
00224 * Output: None
00225 **************************************************************************************************/
00226 
00227 void DisplayAirQValue(uint16_t value )
00228 {
00229     if (value_old != value)
00230     {
00231 
00232         if( value > 200) 
00233             oled.DrawImage(purple_arrow,0,0);     // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0
00234         else if (value < 200 && value > 150) 
00235             oled.DrawImage(red_arrow,0,0);
00236         else if (value < 150 && value > 100) 
00237             oled.DrawImage(orange_arrow,0,0);
00238         else if (value < 100 && value > 50) 
00239             oled.DrawImage(yellow_arrow,0,0); 
00240         else  
00241             oled.DrawImage(green_arrow,0,0);
00242         
00243         oled_text_properties_t textProperties = {0};    // Get OLED Class Default Text Properties
00244         oled.GetTextProperties(&textProperties); 
00245 
00246         // clear the previous value
00247         textProperties.fontColor = COLOR_WHITE;          // Set text properties to white and right aligned for the dynamic text
00248         textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
00249         oled.SetTextProperties(&textProperties);  
00250 
00251         sprintf(text,"%i",value_old);                       // Format the value
00252         oled.TextBox((uint8_t *)text,55,76,20,15);   //Increase textbox for more digits
00253 
00254         // display the new value
00255         textProperties.fontColor = COLOR_WHITE;          // Set text properties to white and right aligned for the dynamic text
00256         textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
00257         oled.SetTextProperties(&textProperties);  
00258 
00259         sprintf(text,"%i",value);                       // Format the value
00260         oled.TextBox((uint8_t *)text,55,76,20,15);   //Increase textbox for more digits
00261     }
00262     value_old = value;
00263 }