BLUETOOTH ADDED VERSION HEXIWEAR CLICK BOARD HDC1000
Dependencies: HDC1000 Hexi_KW40Z Hexi_OLED_SSD1351
main.cpp@0:13d7607a7678, 2017-09-19 (annotated)
- Committer:
- i_am_kitsune
- Date:
- Tue Sep 19 08:24:57 2017 +0000
- Revision:
- 0:13d7607a7678
HEXIWEAR HDC1000 CLICK BOARD ; LAB TIME ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
i_am_kitsune | 0:13d7607a7678 | 1 | #include "mbed.h" |
i_am_kitsune | 0:13d7607a7678 | 2 | #include "HDC1000.h" |
i_am_kitsune | 0:13d7607a7678 | 3 | #include "Hexi_KW40Z.h" |
i_am_kitsune | 0:13d7607a7678 | 4 | #include "Hexi_OLED_SSD1351.h" |
i_am_kitsune | 0:13d7607a7678 | 5 | #include "string.h" |
i_am_kitsune | 0:13d7607a7678 | 6 | |
i_am_kitsune | 0:13d7607a7678 | 7 | #define LED_ON 0 |
i_am_kitsune | 0:13d7607a7678 | 8 | #define LED_OFF 1 |
i_am_kitsune | 0:13d7607a7678 | 9 | |
i_am_kitsune | 0:13d7607a7678 | 10 | void StartHaptic(void); |
i_am_kitsune | 0:13d7607a7678 | 11 | void StopHaptic(void const *n); |
i_am_kitsune | 0:13d7607a7678 | 12 | void txTask(void); |
i_am_kitsune | 0:13d7607a7678 | 13 | void read_task(void); |
i_am_kitsune | 0:13d7607a7678 | 14 | |
i_am_kitsune | 0:13d7607a7678 | 15 | DigitalOut haptic(PTB9); |
i_am_kitsune | 0:13d7607a7678 | 16 | DigitalOut redLed(LED1,1); |
i_am_kitsune | 0:13d7607a7678 | 17 | DigitalOut greenLed(LED2,1); |
i_am_kitsune | 0:13d7607a7678 | 18 | DigitalOut blueLed(LED3,1); |
i_am_kitsune | 0:13d7607a7678 | 19 | |
i_am_kitsune | 0:13d7607a7678 | 20 | HDC1000 hdc(PTD9,PTD8); |
i_am_kitsune | 0:13d7607a7678 | 21 | Serial pc(USBTX, USBRX); |
i_am_kitsune | 0:13d7607a7678 | 22 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ |
i_am_kitsune | 0:13d7607a7678 | 23 | KW40Z kw40z_device(PTE24, PTE25); /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
i_am_kitsune | 0:13d7607a7678 | 24 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
i_am_kitsune | 0:13d7607a7678 | 25 | |
i_am_kitsune | 0:13d7607a7678 | 26 | Thread txThread; /*Create a Thread to handle sending BLE Sensor Data */ |
i_am_kitsune | 0:13d7607a7678 | 27 | Thread readThread; |
i_am_kitsune | 0:13d7607a7678 | 28 | |
i_am_kitsune | 0:13d7607a7678 | 29 | char text[20]; /* Text Buffer */ |
i_am_kitsune | 0:13d7607a7678 | 30 | |
i_am_kitsune | 0:13d7607a7678 | 31 | void StartHaptic(void) |
i_am_kitsune | 0:13d7607a7678 | 32 | { |
i_am_kitsune | 0:13d7607a7678 | 33 | hapticTimer.start(50); |
i_am_kitsune | 0:13d7607a7678 | 34 | haptic = 1; |
i_am_kitsune | 0:13d7607a7678 | 35 | } |
i_am_kitsune | 0:13d7607a7678 | 36 | |
i_am_kitsune | 0:13d7607a7678 | 37 | void StopHaptic(void const *n) |
i_am_kitsune | 0:13d7607a7678 | 38 | { |
i_am_kitsune | 0:13d7607a7678 | 39 | haptic = 0; |
i_am_kitsune | 0:13d7607a7678 | 40 | hapticTimer.stop(); |
i_am_kitsune | 0:13d7607a7678 | 41 | } |
i_am_kitsune | 0:13d7607a7678 | 42 | |
i_am_kitsune | 0:13d7607a7678 | 43 | /****************************Call Back Functions*******************************/ |
i_am_kitsune | 0:13d7607a7678 | 44 | void ButtonRight(void) |
i_am_kitsune | 0:13d7607a7678 | 45 | { |
i_am_kitsune | 0:13d7607a7678 | 46 | StartHaptic(); |
i_am_kitsune | 0:13d7607a7678 | 47 | kw40z_device.ToggleAdvertisementMode(); |
i_am_kitsune | 0:13d7607a7678 | 48 | } |
i_am_kitsune | 0:13d7607a7678 | 49 | |
i_am_kitsune | 0:13d7607a7678 | 50 | void ButtonLeft(void) |
i_am_kitsune | 0:13d7607a7678 | 51 | { |
i_am_kitsune | 0:13d7607a7678 | 52 | StartHaptic(); |
i_am_kitsune | 0:13d7607a7678 | 53 | kw40z_device.ToggleAdvertisementMode(); |
i_am_kitsune | 0:13d7607a7678 | 54 | } |
i_am_kitsune | 0:13d7607a7678 | 55 | |
i_am_kitsune | 0:13d7607a7678 | 56 | void PassKey(void) |
i_am_kitsune | 0:13d7607a7678 | 57 | { |
i_am_kitsune | 0:13d7607a7678 | 58 | StartHaptic(); |
i_am_kitsune | 0:13d7607a7678 | 59 | strcpy((char *) text,"PAIR CODE"); |
i_am_kitsune | 0:13d7607a7678 | 60 | oled.TextBox((uint8_t *)text,0,25,95,18); |
i_am_kitsune | 0:13d7607a7678 | 61 | |
i_am_kitsune | 0:13d7607a7678 | 62 | /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ |
i_am_kitsune | 0:13d7607a7678 | 63 | sprintf(text,"%d", kw40z_device.GetPassKey()); |
i_am_kitsune | 0:13d7607a7678 | 64 | oled.TextBox((uint8_t *)text,0,40,95,18); |
i_am_kitsune | 0:13d7607a7678 | 65 | } |
i_am_kitsune | 0:13d7607a7678 | 66 | |
i_am_kitsune | 0:13d7607a7678 | 67 | |
i_am_kitsune | 0:13d7607a7678 | 68 | // main() runs in its own thread in the OS |
i_am_kitsune | 0:13d7607a7678 | 69 | int main() |
i_am_kitsune | 0:13d7607a7678 | 70 | { |
i_am_kitsune | 0:13d7607a7678 | 71 | |
i_am_kitsune | 0:13d7607a7678 | 72 | |
i_am_kitsune | 0:13d7607a7678 | 73 | |
i_am_kitsune | 0:13d7607a7678 | 74 | oled_text_properties_t textProperties = {0}; |
i_am_kitsune | 0:13d7607a7678 | 75 | oled.GetTextProperties(&textProperties); |
i_am_kitsune | 0:13d7607a7678 | 76 | |
i_am_kitsune | 0:13d7607a7678 | 77 | /* Turn on the backlight of the OLED Display */ |
i_am_kitsune | 0:13d7607a7678 | 78 | oled.DimScreenON(); |
i_am_kitsune | 0:13d7607a7678 | 79 | |
i_am_kitsune | 0:13d7607a7678 | 80 | /* Fills the screen with solid black */ |
i_am_kitsune | 0:13d7607a7678 | 81 | oled.FillScreen(COLOR_BLACK); |
i_am_kitsune | 0:13d7607a7678 | 82 | |
i_am_kitsune | 0:13d7607a7678 | 83 | /* Register callbacks to application functions */ |
i_am_kitsune | 0:13d7607a7678 | 84 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
i_am_kitsune | 0:13d7607a7678 | 85 | kw40z_device.attach_buttonRight(&ButtonRight); |
i_am_kitsune | 0:13d7607a7678 | 86 | kw40z_device.attach_passkey(&PassKey); |
i_am_kitsune | 0:13d7607a7678 | 87 | |
i_am_kitsune | 0:13d7607a7678 | 88 | /* Change font color to blue */ |
i_am_kitsune | 0:13d7607a7678 | 89 | textProperties.fontColor = COLOR_BLUE; |
i_am_kitsune | 0:13d7607a7678 | 90 | textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; |
i_am_kitsune | 0:13d7607a7678 | 91 | oled.SetTextProperties(&textProperties); |
i_am_kitsune | 0:13d7607a7678 | 92 | |
i_am_kitsune | 0:13d7607a7678 | 93 | /* Display Text at (x=0,y=0) */ |
i_am_kitsune | 0:13d7607a7678 | 94 | strcpy((char *) text,"HDC1000"); |
i_am_kitsune | 0:13d7607a7678 | 95 | oled.TextBox((uint8_t *)text,0,0,96,15); //Increase textbox for more digits |
i_am_kitsune | 0:13d7607a7678 | 96 | |
i_am_kitsune | 0:13d7607a7678 | 97 | textProperties.fontColor = COLOR_WHITE; |
i_am_kitsune | 0:13d7607a7678 | 98 | oled.SetTextProperties(&textProperties); |
i_am_kitsune | 0:13d7607a7678 | 99 | |
i_am_kitsune | 0:13d7607a7678 | 100 | /* Display Text at (x=7,y=30) */ |
i_am_kitsune | 0:13d7607a7678 | 101 | strcpy((char *) text,"TEMP :"); |
i_am_kitsune | 0:13d7607a7678 | 102 | oled.Label((uint8_t *)text,7,30); |
i_am_kitsune | 0:13d7607a7678 | 103 | |
i_am_kitsune | 0:13d7607a7678 | 104 | /* Display Text at (x=7,y=45) */ |
i_am_kitsune | 0:13d7607a7678 | 105 | strcpy((char *) text,"HUMI :"); |
i_am_kitsune | 0:13d7607a7678 | 106 | oled.Label((uint8_t *)text,7,45); |
i_am_kitsune | 0:13d7607a7678 | 107 | |
i_am_kitsune | 0:13d7607a7678 | 108 | /* Change font color to Blue */ |
i_am_kitsune | 0:13d7607a7678 | 109 | textProperties.fontColor = COLOR_BLUE; |
i_am_kitsune | 0:13d7607a7678 | 110 | oled.SetTextProperties(&textProperties); |
i_am_kitsune | 0:13d7607a7678 | 111 | |
i_am_kitsune | 0:13d7607a7678 | 112 | /* Display Bluetooth Label at x=17,y=65 */ |
i_am_kitsune | 0:13d7607a7678 | 113 | strcpy((char *) text,"BLUETOOTH"); |
i_am_kitsune | 0:13d7607a7678 | 114 | oled.Label((uint8_t *)text,17,65); |
i_am_kitsune | 0:13d7607a7678 | 115 | |
i_am_kitsune | 0:13d7607a7678 | 116 | /* Change font color to white */ |
i_am_kitsune | 0:13d7607a7678 | 117 | textProperties.fontColor = COLOR_WHITE; |
i_am_kitsune | 0:13d7607a7678 | 118 | textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; |
i_am_kitsune | 0:13d7607a7678 | 119 | oled.SetTextProperties(&textProperties); |
i_am_kitsune | 0:13d7607a7678 | 120 | |
i_am_kitsune | 0:13d7607a7678 | 121 | /* Display Label at x=22,y=80 */ |
i_am_kitsune | 0:13d7607a7678 | 122 | strcpy((char *) text,"Tap Below"); |
i_am_kitsune | 0:13d7607a7678 | 123 | oled.Label((uint8_t *)text,22,80); |
i_am_kitsune | 0:13d7607a7678 | 124 | |
i_am_kitsune | 0:13d7607a7678 | 125 | txThread.start(txTask); /*Start transmitting Sensor Tag Data */ |
i_am_kitsune | 0:13d7607a7678 | 126 | readThread.start(read_task); |
i_am_kitsune | 0:13d7607a7678 | 127 | |
i_am_kitsune | 0:13d7607a7678 | 128 | while (true) { |
i_am_kitsune | 0:13d7607a7678 | 129 | |
i_am_kitsune | 0:13d7607a7678 | 130 | blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
i_am_kitsune | 0:13d7607a7678 | 131 | Thread::wait(50); |
i_am_kitsune | 0:13d7607a7678 | 132 | } |
i_am_kitsune | 0:13d7607a7678 | 133 | } |
i_am_kitsune | 0:13d7607a7678 | 134 | |
i_am_kitsune | 0:13d7607a7678 | 135 | /* txTask() transmits the sensor data */ |
i_am_kitsune | 0:13d7607a7678 | 136 | void txTask(void) |
i_am_kitsune | 0:13d7607a7678 | 137 | { |
i_am_kitsune | 0:13d7607a7678 | 138 | while (true) { |
i_am_kitsune | 0:13d7607a7678 | 139 | |
i_am_kitsune | 0:13d7607a7678 | 140 | hdc.get(); // Triger conversion |
i_am_kitsune | 0:13d7607a7678 | 141 | |
i_am_kitsune | 0:13d7607a7678 | 142 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
i_am_kitsune | 0:13d7607a7678 | 143 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
i_am_kitsune | 0:13d7607a7678 | 144 | /*Send weight using pressure service*/ |
i_am_kitsune | 0:13d7607a7678 | 145 | kw40z_device.SendTemperature(hdc.send_temp()); |
i_am_kitsune | 0:13d7607a7678 | 146 | kw40z_device.SendHumidity(hdc.send_humi()); |
i_am_kitsune | 0:13d7607a7678 | 147 | Thread::wait(1000); |
i_am_kitsune | 0:13d7607a7678 | 148 | } |
i_am_kitsune | 0:13d7607a7678 | 149 | } |
i_am_kitsune | 0:13d7607a7678 | 150 | |
i_am_kitsune | 0:13d7607a7678 | 151 | void read_task(void) |
i_am_kitsune | 0:13d7607a7678 | 152 | { |
i_am_kitsune | 0:13d7607a7678 | 153 | |
i_am_kitsune | 0:13d7607a7678 | 154 | char t_temp[10] = {0}, s_temp[10] = {0}; |
i_am_kitsune | 0:13d7607a7678 | 155 | char t_humi[10] = {0}, s_humi[10] = {0}; |
i_am_kitsune | 0:13d7607a7678 | 156 | float temp = 0.0f, humi = 0.0f; |
i_am_kitsune | 0:13d7607a7678 | 157 | int16_t i_temp = 0; |
i_am_kitsune | 0:13d7607a7678 | 158 | uint16_t u_temp = 0, u_humi = 0; |
i_am_kitsune | 0:13d7607a7678 | 159 | |
i_am_kitsune | 0:13d7607a7678 | 160 | while(true) { |
i_am_kitsune | 0:13d7607a7678 | 161 | |
i_am_kitsune | 0:13d7607a7678 | 162 | hdc.get(); // Triger conversion |
i_am_kitsune | 0:13d7607a7678 | 163 | pc.printf("Temp: %+4.1fC, Humid: %4.1f%%\r\n", hdc.temperature(), hdc.humidity()); |
i_am_kitsune | 0:13d7607a7678 | 164 | temp = hdc.temperature(); |
i_am_kitsune | 0:13d7607a7678 | 165 | humi = hdc.humidity(); |
i_am_kitsune | 0:13d7607a7678 | 166 | sprintf(t_temp,"%4.1fC",temp); |
i_am_kitsune | 0:13d7607a7678 | 167 | sprintf(t_humi,"%4.1f%%",humi); |
i_am_kitsune | 0:13d7607a7678 | 168 | i_temp = (int)(hdc.temperature()); |
i_am_kitsune | 0:13d7607a7678 | 169 | u_temp = hdc.send_temp()/ 1000; |
i_am_kitsune | 0:13d7607a7678 | 170 | u_humi = hdc.send_humi()/ 1000; |
i_am_kitsune | 0:13d7607a7678 | 171 | pc.printf("int Temp: %d \r\n", i_temp); |
i_am_kitsune | 0:13d7607a7678 | 172 | pc.printf("unsigned Temp: %d, Humid: %u\r\n", u_temp, u_humi); |
i_am_kitsune | 0:13d7607a7678 | 173 | |
i_am_kitsune | 0:13d7607a7678 | 174 | oled.TextBox((uint8_t *)t_temp,40,30,46,15); //Increase textbox for more digits |
i_am_kitsune | 0:13d7607a7678 | 175 | oled.TextBox((uint8_t *)t_humi,40,45,46,15); //Increase textbox for more digits |
i_am_kitsune | 0:13d7607a7678 | 176 | |
i_am_kitsune | 0:13d7607a7678 | 177 | Thread::wait(2000); |
i_am_kitsune | 0:13d7607a7678 | 178 | } |
i_am_kitsune | 0:13d7607a7678 | 179 | } |