Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Hexi_KW40Z Hexi_MLX90614 Hexi_OLED_SSD1351
Fork of Hexi_Click_IRThermo_Example by
main.cpp@5:ae7682e17859, 2016-11-03 (annotated)
- Committer:
- GregC
- Date:
- Thu Nov 03 21:57:48 2016 +0000
- Revision:
- 5:ae7682e17859
- Parent:
- 2:14a1a79639db
Very advanced Click IR Thermo example for Hexiwear featuring OLED Display, Bluetooth, Cloud and Touch
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| daveyclk | 2:14a1a79639db | 1 | |
| daveyclk | 2:14a1a79639db | 2 | /****************************************************************************** |
| daveyclk | 2:14a1a79639db | 3 | * Includes |
| daveyclk | 2:14a1a79639db | 4 | *******************************************************************************/ |
| daveyclk | 2:14a1a79639db | 5 | |
| 4180_1 | 0:bbaf949d2a27 | 6 | #include "mbed.h" |
| 4180_1 | 0:bbaf949d2a27 | 7 | #include "mlx90614.h" |
| daveyclk | 2:14a1a79639db | 8 | #include "Hexi_KW40Z.h" |
| daveyclk | 2:14a1a79639db | 9 | #include "Hexi_OLED_SSD1351.h" |
| daveyclk | 2:14a1a79639db | 10 | #include "OLED_types.h" |
| daveyclk | 2:14a1a79639db | 11 | #include "OpenSans_Font.h" |
| daveyclk | 2:14a1a79639db | 12 | #include "string.h" |
| daveyclk | 2:14a1a79639db | 13 | #include "irimages_c.h" |
| 4180_1 | 0:bbaf949d2a27 | 14 | |
| daveyclk | 2:14a1a79639db | 15 | |
| daveyclk | 2:14a1a79639db | 16 | |
| daveyclk | 2:14a1a79639db | 17 | DigitalOut blueLed(LED3,1); |
| daveyclk | 2:14a1a79639db | 18 | DigitalOut haptic(PTB9); |
| daveyclk | 2:14a1a79639db | 19 | |
| daveyclk | 2:14a1a79639db | 20 | I2C i2c(PTD9,PTD8); //sda,scl (start i2c interace) |
| daveyclk | 2:14a1a79639db | 21 | |
| daveyclk | 2:14a1a79639db | 22 | Serial pc(USBTX,USBRX); //serial usb config for debug |
| daveyclk | 2:14a1a79639db | 23 | |
| daveyclk | 2:14a1a79639db | 24 | MLX90614 IR_thermometer(&i2c); // instantiate IR Thermometer |
| daveyclk | 2:14a1a79639db | 25 | |
| 4180_1 | 0:bbaf949d2a27 | 26 | |
| daveyclk | 2:14a1a79639db | 27 | /* prototypes */ |
| daveyclk | 2:14a1a79639db | 28 | void Sys_Init(void); |
| daveyclk | 2:14a1a79639db | 29 | void readTemp(void); |
| daveyclk | 2:14a1a79639db | 30 | void StartHaptic(void); |
| daveyclk | 2:14a1a79639db | 31 | void StopHaptic(void const *n); |
| daveyclk | 2:14a1a79639db | 32 | void txTask(void); |
| daveyclk | 2:14a1a79639db | 33 | void animateDisplay(void); |
| daveyclk | 2:14a1a79639db | 34 | void displayTemp(void); |
| daveyclk | 2:14a1a79639db | 35 | |
| daveyclk | 2:14a1a79639db | 36 | /* Define timer for haptic feedback */ |
| daveyclk | 2:14a1a79639db | 37 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
| 4180_1 | 0:bbaf949d2a27 | 38 | |
| daveyclk | 2:14a1a79639db | 39 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
| daveyclk | 2:14a1a79639db | 40 | KW40Z kw40z_device(PTE24, PTE25); |
| daveyclk | 2:14a1a79639db | 41 | |
| daveyclk | 2:14a1a79639db | 42 | |
| daveyclk | 2:14a1a79639db | 43 | /* Instantiate the SSD1351 OLED Driver */ |
| daveyclk | 2:14a1a79639db | 44 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ |
| daveyclk | 2:14a1a79639db | 45 | |
| daveyclk | 2:14a1a79639db | 46 | /*Create a Thread to handle sending BLE Sensor Data */ |
| daveyclk | 2:14a1a79639db | 47 | Thread txThread; |
| daveyclk | 2:14a1a79639db | 48 | |
| daveyclk | 2:14a1a79639db | 49 | /*Create a Thread to handle reading the sensor and display*/ |
| daveyclk | 2:14a1a79639db | 50 | Thread dispThread; |
| 4180_1 | 0:bbaf949d2a27 | 51 | |
| daveyclk | 2:14a1a79639db | 52 | /* Text Buffer */ |
| daveyclk | 2:14a1a79639db | 53 | char text[20]; |
| daveyclk | 2:14a1a79639db | 54 | |
| daveyclk | 2:14a1a79639db | 55 | /* Temperature images */ |
| daveyclk | 2:14a1a79639db | 56 | const uint8_t *blue1; // Pointer for the image to be displayed |
| daveyclk | 2:14a1a79639db | 57 | const uint8_t *orange2; // Pointer for the image to be displayed |
| daveyclk | 2:14a1a79639db | 58 | const uint8_t *red3; // Pointer for the image to be displayed |
| daveyclk | 2:14a1a79639db | 59 | |
| daveyclk | 2:14a1a79639db | 60 | float tempC; //temperature in degrees C |
| daveyclk | 2:14a1a79639db | 61 | float tempF; //temperature in degrees F |
| daveyclk | 2:14a1a79639db | 62 | |
| daveyclk | 2:14a1a79639db | 63 | |
| daveyclk | 2:14a1a79639db | 64 | /****************************Call Back Functions*******************************/ |
| daveyclk | 2:14a1a79639db | 65 | |
| daveyclk | 2:14a1a79639db | 66 | void ButtonRight(void) |
| daveyclk | 2:14a1a79639db | 67 | { |
| daveyclk | 2:14a1a79639db | 68 | StartHaptic(); |
| daveyclk | 2:14a1a79639db | 69 | kw40z_device.ToggleAdvertisementMode(); |
| daveyclk | 2:14a1a79639db | 70 | } |
| daveyclk | 2:14a1a79639db | 71 | |
| daveyclk | 2:14a1a79639db | 72 | void ButtonLeft(void) |
| daveyclk | 2:14a1a79639db | 73 | { |
| daveyclk | 2:14a1a79639db | 74 | StartHaptic(); |
| daveyclk | 2:14a1a79639db | 75 | kw40z_device.ToggleAdvertisementMode(); |
| daveyclk | 2:14a1a79639db | 76 | } |
| daveyclk | 2:14a1a79639db | 77 | |
| daveyclk | 2:14a1a79639db | 78 | void PassKey(void) |
| daveyclk | 2:14a1a79639db | 79 | { |
| daveyclk | 2:14a1a79639db | 80 | StartHaptic(); |
| daveyclk | 2:14a1a79639db | 81 | |
| daveyclk | 2:14a1a79639db | 82 | /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ |
| daveyclk | 2:14a1a79639db | 83 | sprintf(text,"%d", kw40z_device.GetPassKey()); |
| daveyclk | 2:14a1a79639db | 84 | oled.TextBox((uint8_t *)text,0,72,95,18); |
| daveyclk | 2:14a1a79639db | 85 | } |
| daveyclk | 2:14a1a79639db | 86 | |
| daveyclk | 2:14a1a79639db | 87 | /***********************End of Call Back Functions*****************************/ |
| daveyclk | 2:14a1a79639db | 88 | |
| daveyclk | 2:14a1a79639db | 89 | /********************************Main******************************************/ |
| daveyclk | 2:14a1a79639db | 90 | |
| daveyclk | 2:14a1a79639db | 91 | int main() |
| daveyclk | 2:14a1a79639db | 92 | { |
| daveyclk | 2:14a1a79639db | 93 | Sys_Init(); // system initial values |
| daveyclk | 2:14a1a79639db | 94 | |
| daveyclk | 2:14a1a79639db | 95 | while (true) |
| daveyclk | 2:14a1a79639db | 96 | { |
| daveyclk | 2:14a1a79639db | 97 | blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
| daveyclk | 2:14a1a79639db | 98 | Thread::wait(50); |
| daveyclk | 2:14a1a79639db | 99 | |
| 4180_1 | 0:bbaf949d2a27 | 100 | } |
| 4180_1 | 0:bbaf949d2a27 | 101 | } |
| daveyclk | 2:14a1a79639db | 102 | |
| daveyclk | 2:14a1a79639db | 103 | /******************************End of Main*************************************/ |
| daveyclk | 2:14a1a79639db | 104 | |
| daveyclk | 2:14a1a79639db | 105 | |
| daveyclk | 2:14a1a79639db | 106 | /* txTask() transmits the sensor data */ |
| daveyclk | 2:14a1a79639db | 107 | void txTask(void){ |
| daveyclk | 2:14a1a79639db | 108 | |
| daveyclk | 2:14a1a79639db | 109 | while (true) |
| daveyclk | 2:14a1a79639db | 110 | { |
| daveyclk | 2:14a1a79639db | 111 | |
| daveyclk | 2:14a1a79639db | 112 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
| daveyclk | 2:14a1a79639db | 113 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
| daveyclk | 2:14a1a79639db | 114 | |
| daveyclk | 2:14a1a79639db | 115 | kw40z_device.SendTemperature(tempC*100); //send IR Click Temperature |
| daveyclk | 2:14a1a79639db | 116 | |
| daveyclk | 2:14a1a79639db | 117 | Thread::wait(1000); |
| daveyclk | 2:14a1a79639db | 118 | } |
| daveyclk | 2:14a1a79639db | 119 | } |
| daveyclk | 2:14a1a79639db | 120 | |
| daveyclk | 2:14a1a79639db | 121 | |
| daveyclk | 2:14a1a79639db | 122 | void StartHaptic(void) { |
| daveyclk | 2:14a1a79639db | 123 | hapticTimer.start(50); |
| daveyclk | 2:14a1a79639db | 124 | haptic = 1; |
| daveyclk | 2:14a1a79639db | 125 | } |
| daveyclk | 2:14a1a79639db | 126 | |
| daveyclk | 2:14a1a79639db | 127 | void StopHaptic(void const *n) { |
| daveyclk | 2:14a1a79639db | 128 | haptic = 0; |
| daveyclk | 2:14a1a79639db | 129 | hapticTimer.stop(); |
| daveyclk | 2:14a1a79639db | 130 | } |
| daveyclk | 2:14a1a79639db | 131 | |
| daveyclk | 2:14a1a79639db | 132 | void readTemp(void) { |
| daveyclk | 2:14a1a79639db | 133 | if (IR_thermometer.getTemp(&tempC)) { |
| daveyclk | 2:14a1a79639db | 134 | //gets temperature from sensor via I2C bus |
| daveyclk | 2:14a1a79639db | 135 | |
| daveyclk | 2:14a1a79639db | 136 | tempF = tempC * 9.0 / 5 + 32; // convert C to F |
| daveyclk | 2:14a1a79639db | 137 | |
| daveyclk | 2:14a1a79639db | 138 | //print temperature on PC |
| daveyclk | 2:14a1a79639db | 139 | printf("Temperature is %4.2F degrees C\r\n",tempC); |
| daveyclk | 2:14a1a79639db | 140 | printf("Temperature is %4.2F degrees F\r\n",tempF); |
| daveyclk | 2:14a1a79639db | 141 | } |
| daveyclk | 2:14a1a79639db | 142 | } |
| daveyclk | 2:14a1a79639db | 143 | |
| daveyclk | 2:14a1a79639db | 144 | void Sys_Init(void){ |
| daveyclk | 2:14a1a79639db | 145 | |
| daveyclk | 2:14a1a79639db | 146 | blue1 = blue1s_bmp; // Pointer for the image to be displayed |
| daveyclk | 2:14a1a79639db | 147 | orange2 = orange2s_bmp; // Pointer for the image to be displayed |
| daveyclk | 2:14a1a79639db | 148 | red3 = red3s_bmp; // Pointer for the image to be displayed |
| daveyclk | 2:14a1a79639db | 149 | |
| daveyclk | 2:14a1a79639db | 150 | /* Fills the screen with solid black */ |
| daveyclk | 2:14a1a79639db | 151 | oled.FillScreen(COLOR_BLACK); |
| daveyclk | 2:14a1a79639db | 152 | |
| daveyclk | 2:14a1a79639db | 153 | /* Turn on the backlight of the OLED Display */ |
| GregC | 5:ae7682e17859 | 154 | // oled.DimScreenON(); |
| daveyclk | 2:14a1a79639db | 155 | |
| daveyclk | 2:14a1a79639db | 156 | oled.DrawImage(blue1,0,0); // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 |
| daveyclk | 2:14a1a79639db | 157 | wait(0.5); |
| daveyclk | 2:14a1a79639db | 158 | oled.DrawImage(orange2,0,0); // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 |
| daveyclk | 2:14a1a79639db | 159 | wait(0.5); |
| daveyclk | 2:14a1a79639db | 160 | oled.DrawImage(red3,0,0); // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 |
| daveyclk | 2:14a1a79639db | 161 | wait(0.5); |
| daveyclk | 2:14a1a79639db | 162 | |
| daveyclk | 2:14a1a79639db | 163 | /* Get OLED Class Default Text Properties */ |
| daveyclk | 2:14a1a79639db | 164 | oled_text_properties_t textProperties = {0}; |
| daveyclk | 2:14a1a79639db | 165 | oled.GetTextProperties(&textProperties); |
| daveyclk | 2:14a1a79639db | 166 | |
| daveyclk | 2:14a1a79639db | 167 | /* Register callbacks to application functions */ |
| daveyclk | 2:14a1a79639db | 168 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
| daveyclk | 2:14a1a79639db | 169 | kw40z_device.attach_buttonRight(&ButtonRight); |
| daveyclk | 2:14a1a79639db | 170 | kw40z_device.attach_passkey(&PassKey); |
| daveyclk | 2:14a1a79639db | 171 | |
| daveyclk | 2:14a1a79639db | 172 | /* Change font color to white */ |
| daveyclk | 2:14a1a79639db | 173 | textProperties.font = OpenSans_12x18_Regular; |
| daveyclk | 2:14a1a79639db | 174 | textProperties.fontColor = COLOR_WHITE; |
| daveyclk | 2:14a1a79639db | 175 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
| daveyclk | 2:14a1a79639db | 176 | oled.SetTextProperties(&textProperties); |
| daveyclk | 2:14a1a79639db | 177 | |
| daveyclk | 2:14a1a79639db | 178 | strcpy((char *) text,"C"); |
| daveyclk | 2:14a1a79639db | 179 | /* Display Label at x=60,y=72 */ |
| daveyclk | 2:14a1a79639db | 180 | oled.Label((uint8_t *)text,60,72); |
| daveyclk | 2:14a1a79639db | 181 | |
| daveyclk | 2:14a1a79639db | 182 | txThread.start(txTask); /*Start transmitting Sensor Tag Data */ |
| daveyclk | 2:14a1a79639db | 183 | dispThread.start(animateDisplay); /* start reading the temperature */ |
| daveyclk | 2:14a1a79639db | 184 | } |
| daveyclk | 2:14a1a79639db | 185 | |
| daveyclk | 2:14a1a79639db | 186 | |
| daveyclk | 2:14a1a79639db | 187 | |
| daveyclk | 2:14a1a79639db | 188 | void animateDisplay(void){ |
| daveyclk | 2:14a1a79639db | 189 | |
| daveyclk | 2:14a1a79639db | 190 | while(true){ |
| daveyclk | 2:14a1a79639db | 191 | |
| daveyclk | 2:14a1a79639db | 192 | readTemp(); |
| daveyclk | 2:14a1a79639db | 193 | |
| daveyclk | 2:14a1a79639db | 194 | if (tempC > 30) // for temps over 30 display red |
| daveyclk | 2:14a1a79639db | 195 | oled.DrawImage(red3,0,0); |
| daveyclk | 2:14a1a79639db | 196 | else if (tempC < 30 || tempC > 10) // for temps between 10 and 30 display orange |
| daveyclk | 2:14a1a79639db | 197 | oled.DrawImage(orange2,0,0); |
| daveyclk | 2:14a1a79639db | 198 | else if (tempC < 10) // for temps under 10 display blue |
| daveyclk | 2:14a1a79639db | 199 | oled.DrawImage(blue1,0,0); |
| daveyclk | 2:14a1a79639db | 200 | displayTemp(); // show temp on display |
| daveyclk | 2:14a1a79639db | 201 | |
| daveyclk | 2:14a1a79639db | 202 | Thread::wait(500); |
| daveyclk | 2:14a1a79639db | 203 | } |
| daveyclk | 2:14a1a79639db | 204 | } |
| daveyclk | 2:14a1a79639db | 205 | |
| daveyclk | 2:14a1a79639db | 206 | void displayTemp(void){ |
| daveyclk | 2:14a1a79639db | 207 | |
| daveyclk | 2:14a1a79639db | 208 | /* format the text */ |
| daveyclk | 2:14a1a79639db | 209 | sprintf(text,"%.1f", tempC); |
| daveyclk | 2:14a1a79639db | 210 | /* Display Label at x=22,y=80 */ |
| daveyclk | 2:14a1a79639db | 211 | oled.TextBox((uint8_t *)text,22,72,30,18); |
| daveyclk | 2:14a1a79639db | 212 | } |
