BLUETOOTH ADDED VERSION HEXIWEAR CLICK BOARD HDC1000
Dependencies: HDC1000 Hexi_KW40Z Hexi_OLED_SSD1351
main.cpp
00001 #include "mbed.h" 00002 #include "HDC1000.h" 00003 #include "Hexi_KW40Z.h" 00004 #include "Hexi_OLED_SSD1351.h" 00005 #include "string.h" 00006 00007 #define LED_ON 0 00008 #define LED_OFF 1 00009 00010 void StartHaptic(void); 00011 void StopHaptic(void const *n); 00012 void txTask(void); 00013 void read_task(void); 00014 00015 DigitalOut haptic(PTB9); 00016 DigitalOut redLed(LED1,1); 00017 DigitalOut greenLed(LED2,1); 00018 DigitalOut blueLed(LED3,1); 00019 00020 HDC1000 hdc(PTD9,PTD8); 00021 Serial pc(USBTX, USBRX); 00022 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ 00023 KW40Z kw40z_device(PTE24, PTE25); /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 00024 RtosTimer hapticTimer(StopHaptic, osTimerOnce); 00025 00026 Thread txThread; /*Create a Thread to handle sending BLE Sensor Data */ 00027 Thread readThread; 00028 00029 char text[20]; /* Text Buffer */ 00030 00031 void StartHaptic(void) 00032 { 00033 hapticTimer.start(50); 00034 haptic = 1; 00035 } 00036 00037 void StopHaptic(void const *n) 00038 { 00039 haptic = 0; 00040 hapticTimer.stop(); 00041 } 00042 00043 /****************************Call Back Functions*******************************/ 00044 void ButtonRight(void) 00045 { 00046 StartHaptic(); 00047 kw40z_device.ToggleAdvertisementMode(); 00048 } 00049 00050 void ButtonLeft(void) 00051 { 00052 StartHaptic(); 00053 kw40z_device.ToggleAdvertisementMode(); 00054 } 00055 00056 void PassKey(void) 00057 { 00058 StartHaptic(); 00059 strcpy((char *) text,"PAIR CODE"); 00060 oled.TextBox((uint8_t *)text,0,25,95,18); 00061 00062 /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ 00063 sprintf(text,"%d", kw40z_device.GetPassKey()); 00064 oled.TextBox((uint8_t *)text,0,40,95,18); 00065 } 00066 00067 00068 // main() runs in its own thread in the OS 00069 int main() 00070 { 00071 00072 00073 00074 oled_text_properties_t textProperties = {0}; 00075 oled.GetTextProperties(&textProperties); 00076 00077 /* Turn on the backlight of the OLED Display */ 00078 oled.DimScreenON(); 00079 00080 /* Fills the screen with solid black */ 00081 oled.FillScreen(COLOR_BLACK); 00082 00083 /* Register callbacks to application functions */ 00084 kw40z_device.attach_buttonLeft(&ButtonLeft); 00085 kw40z_device.attach_buttonRight(&ButtonRight); 00086 kw40z_device.attach_passkey(&PassKey); 00087 00088 /* Change font color to blue */ 00089 textProperties.fontColor = COLOR_BLUE; 00090 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00091 oled.SetTextProperties(&textProperties); 00092 00093 /* Display Text at (x=0,y=0) */ 00094 strcpy((char *) text,"HDC1000"); 00095 oled.TextBox((uint8_t *)text,0,0,96,15); //Increase textbox for more digits 00096 00097 textProperties.fontColor = COLOR_WHITE; 00098 oled.SetTextProperties(&textProperties); 00099 00100 /* Display Text at (x=7,y=30) */ 00101 strcpy((char *) text,"TEMP :"); 00102 oled.Label((uint8_t *)text,7,30); 00103 00104 /* Display Text at (x=7,y=45) */ 00105 strcpy((char *) text,"HUMI :"); 00106 oled.Label((uint8_t *)text,7,45); 00107 00108 /* Change font color to Blue */ 00109 textProperties.fontColor = COLOR_BLUE; 00110 oled.SetTextProperties(&textProperties); 00111 00112 /* Display Bluetooth Label at x=17,y=65 */ 00113 strcpy((char *) text,"BLUETOOTH"); 00114 oled.Label((uint8_t *)text,17,65); 00115 00116 /* Change font color to white */ 00117 textProperties.fontColor = COLOR_WHITE; 00118 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00119 oled.SetTextProperties(&textProperties); 00120 00121 /* Display Label at x=22,y=80 */ 00122 strcpy((char *) text,"Tap Below"); 00123 oled.Label((uint8_t *)text,22,80); 00124 00125 txThread.start(txTask); /*Start transmitting Sensor Tag Data */ 00126 readThread.start(read_task); 00127 00128 while (true) { 00129 00130 blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ 00131 Thread::wait(50); 00132 } 00133 } 00134 00135 /* txTask() transmits the sensor data */ 00136 void txTask(void) 00137 { 00138 while (true) { 00139 00140 hdc.get(); // Triger conversion 00141 00142 /*Notify Hexiwear App that it is running Sensor Tag mode*/ 00143 kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); 00144 /*Send weight using pressure service*/ 00145 kw40z_device.SendTemperature(hdc.send_temp()); 00146 kw40z_device.SendHumidity(hdc.send_humi()); 00147 Thread::wait(1000); 00148 } 00149 } 00150 00151 void read_task(void) 00152 { 00153 00154 char t_temp[10] = {0}, s_temp[10] = {0}; 00155 char t_humi[10] = {0}, s_humi[10] = {0}; 00156 float temp = 0.0f, humi = 0.0f; 00157 int16_t i_temp = 0; 00158 uint16_t u_temp = 0, u_humi = 0; 00159 00160 while(true) { 00161 00162 hdc.get(); // Triger conversion 00163 pc.printf("Temp: %+4.1fC, Humid: %4.1f%%\r\n", hdc.temperature(), hdc.humidity()); 00164 temp = hdc.temperature(); 00165 humi = hdc.humidity(); 00166 sprintf(t_temp,"%4.1fC",temp); 00167 sprintf(t_humi,"%4.1f%%",humi); 00168 i_temp = (int)(hdc.temperature()); 00169 u_temp = hdc.send_temp()/ 1000; 00170 u_humi = hdc.send_humi()/ 1000; 00171 pc.printf("int Temp: %d \r\n", i_temp); 00172 pc.printf("unsigned Temp: %d, Humid: %u\r\n", u_temp, u_humi); 00173 00174 oled.TextBox((uint8_t *)t_temp,40,30,46,15); //Increase textbox for more digits 00175 oled.TextBox((uint8_t *)t_humi,40,45,46,15); //Increase textbox for more digits 00176 00177 Thread::wait(2000); 00178 } 00179 }
Generated on Mon Jul 18 2022 18:18:00 by
1.7.2