![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Swim-It, is a waterproof counter for arm movement
Dependencies: Hexi_KW40Z Hexi_OLED_SSD1351
Fork of Hexi_BLE_Example by
main.cpp@4:c2ab3a0de448, 2016-09-26 (annotated)
- Committer:
- cotigac
- Date:
- Mon Sep 26 05:29:51 2016 +0000
- Revision:
- 4:c2ab3a0de448
- Parent:
- 1:a0d9eeedb771
Added dummy data generation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
khuang | 0:c80666325948 | 1 | #include "mbed.h" |
khuang | 0:c80666325948 | 2 | #include "Hexi_KW40Z.h" |
khuang | 0:c80666325948 | 3 | #include "Hexi_OLED_SSD1351.h" |
khuang | 0:c80666325948 | 4 | #include "OLED_types.h" |
khuang | 0:c80666325948 | 5 | #include "OpenSans_Font.h" |
khuang | 0:c80666325948 | 6 | #include "string.h" |
khuang | 0:c80666325948 | 7 | |
khuang | 0:c80666325948 | 8 | #define LED_ON 0 |
khuang | 0:c80666325948 | 9 | #define LED_OFF 1 |
cotigac | 4:c2ab3a0de448 | 10 | |
cotigac | 4:c2ab3a0de448 | 11 | void UpdateSensorData(void); |
khuang | 0:c80666325948 | 12 | void StartHaptic(void); |
khuang | 0:c80666325948 | 13 | void StopHaptic(void const *n); |
khuang | 1:a0d9eeedb771 | 14 | void txTask(void); |
khuang | 0:c80666325948 | 15 | |
khuang | 0:c80666325948 | 16 | DigitalOut redLed(LED1,1); |
khuang | 0:c80666325948 | 17 | DigitalOut greenLed(LED2,1); |
khuang | 0:c80666325948 | 18 | DigitalOut blueLed(LED3,1); |
khuang | 0:c80666325948 | 19 | DigitalOut haptic(PTB9); |
khuang | 0:c80666325948 | 20 | |
khuang | 0:c80666325948 | 21 | /* Define timer for haptic feedback */ |
khuang | 0:c80666325948 | 22 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
khuang | 0:c80666325948 | 23 | |
khuang | 0:c80666325948 | 24 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
khuang | 0:c80666325948 | 25 | KW40Z kw40z_device(PTE24, PTE25); |
khuang | 0:c80666325948 | 26 | |
khuang | 0:c80666325948 | 27 | /* Instantiate the SSD1351 OLED Driver */ |
khuang | 0:c80666325948 | 28 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ |
khuang | 0:c80666325948 | 29 | |
khuang | 1:a0d9eeedb771 | 30 | /*Create a Thread to handle sending BLE Sensor Data */ |
khuang | 1:a0d9eeedb771 | 31 | Thread txThread; |
khuang | 0:c80666325948 | 32 | |
khuang | 1:a0d9eeedb771 | 33 | /* Text Buffer */ |
khuang | 1:a0d9eeedb771 | 34 | char text[20]; |
khuang | 1:a0d9eeedb771 | 35 | |
cotigac | 4:c2ab3a0de448 | 36 | uint8_t battery = 100; |
cotigac | 4:c2ab3a0de448 | 37 | uint8_t light = 0; |
cotigac | 4:c2ab3a0de448 | 38 | uint16_t humidity = 4500; |
cotigac | 4:c2ab3a0de448 | 39 | uint16_t temperature = 2000; |
cotigac | 4:c2ab3a0de448 | 40 | uint16_t pressure = 9000; |
cotigac | 4:c2ab3a0de448 | 41 | uint16_t x = 0; |
cotigac | 4:c2ab3a0de448 | 42 | uint16_t y = 5000; |
cotigac | 4:c2ab3a0de448 | 43 | uint16_t z = 10000; |
cotigac | 4:c2ab3a0de448 | 44 | |
khuang | 1:a0d9eeedb771 | 45 | /****************************Call Back Functions*******************************/ |
khuang | 0:c80666325948 | 46 | void ButtonRight(void) |
khuang | 0:c80666325948 | 47 | { |
khuang | 0:c80666325948 | 48 | StartHaptic(); |
khuang | 0:c80666325948 | 49 | kw40z_device.ToggleAdvertisementMode(); |
khuang | 0:c80666325948 | 50 | } |
khuang | 0:c80666325948 | 51 | |
khuang | 0:c80666325948 | 52 | void ButtonLeft(void) |
khuang | 0:c80666325948 | 53 | { |
khuang | 0:c80666325948 | 54 | StartHaptic(); |
khuang | 0:c80666325948 | 55 | kw40z_device.ToggleAdvertisementMode(); |
khuang | 0:c80666325948 | 56 | } |
khuang | 0:c80666325948 | 57 | |
khuang | 0:c80666325948 | 58 | void PassKey(void) |
khuang | 0:c80666325948 | 59 | { |
khuang | 0:c80666325948 | 60 | StartHaptic(); |
khuang | 0:c80666325948 | 61 | strcpy((char *) text,"PAIR CODE"); |
khuang | 1:a0d9eeedb771 | 62 | oled.TextBox((uint8_t *)text,0,25,95,18); |
khuang | 0:c80666325948 | 63 | |
khuang | 0:c80666325948 | 64 | /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ |
khuang | 0:c80666325948 | 65 | sprintf(text,"%d", kw40z_device.GetPassKey()); |
khuang | 0:c80666325948 | 66 | oled.TextBox((uint8_t *)text,0,40,95,18); |
khuang | 0:c80666325948 | 67 | } |
khuang | 1:a0d9eeedb771 | 68 | |
khuang | 1:a0d9eeedb771 | 69 | /***********************End of Call Back Functions*****************************/ |
khuang | 1:a0d9eeedb771 | 70 | |
khuang | 1:a0d9eeedb771 | 71 | /********************************Main******************************************/ |
khuang | 1:a0d9eeedb771 | 72 | |
khuang | 0:c80666325948 | 73 | int main() |
khuang | 0:c80666325948 | 74 | { |
cotigac | 4:c2ab3a0de448 | 75 | /* Register callbacks to application functions */ |
cotigac | 4:c2ab3a0de448 | 76 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
cotigac | 4:c2ab3a0de448 | 77 | kw40z_device.attach_buttonRight(&ButtonRight); |
cotigac | 4:c2ab3a0de448 | 78 | kw40z_device.attach_passkey(&PassKey); |
khuang | 0:c80666325948 | 79 | |
khuang | 0:c80666325948 | 80 | /* Turn on the backlight of the OLED Display */ |
khuang | 0:c80666325948 | 81 | oled.DimScreenON(); |
khuang | 0:c80666325948 | 82 | |
khuang | 0:c80666325948 | 83 | /* Fills the screen with solid black */ |
khuang | 0:c80666325948 | 84 | oled.FillScreen(COLOR_BLACK); |
cotigac | 4:c2ab3a0de448 | 85 | |
cotigac | 4:c2ab3a0de448 | 86 | /* Get OLED Class Default Text Properties */ |
cotigac | 4:c2ab3a0de448 | 87 | oled_text_properties_t textProperties = {0}; |
cotigac | 4:c2ab3a0de448 | 88 | oled.GetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 89 | |
khuang | 0:c80666325948 | 90 | /* Change font color to Blue */ |
khuang | 0:c80666325948 | 91 | textProperties.fontColor = COLOR_BLUE; |
khuang | 0:c80666325948 | 92 | oled.SetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 93 | |
khuang | 0:c80666325948 | 94 | /* Display Bluetooth Label at x=17,y=65 */ |
khuang | 0:c80666325948 | 95 | strcpy((char *) text,"BLUETOOTH"); |
khuang | 0:c80666325948 | 96 | oled.Label((uint8_t *)text,17,65); |
khuang | 0:c80666325948 | 97 | |
khuang | 0:c80666325948 | 98 | /* Change font color to white */ |
khuang | 0:c80666325948 | 99 | textProperties.fontColor = COLOR_WHITE; |
khuang | 0:c80666325948 | 100 | textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; |
khuang | 0:c80666325948 | 101 | oled.SetTextProperties(&textProperties); |
khuang | 0:c80666325948 | 102 | |
khuang | 1:a0d9eeedb771 | 103 | /* Display Label at x=22,y=80 */ |
khuang | 0:c80666325948 | 104 | strcpy((char *) text,"Tap Below"); |
khuang | 0:c80666325948 | 105 | oled.Label((uint8_t *)text,22,80); |
khuang | 0:c80666325948 | 106 | |
khuang | 1:a0d9eeedb771 | 107 | uint8_t prevLinkState = 0; |
cotigac | 4:c2ab3a0de448 | 108 | uint8_t currLinkState = 0; |
cotigac | 4:c2ab3a0de448 | 109 | |
khuang | 1:a0d9eeedb771 | 110 | txThread.start(txTask); /*Start transmitting Sensor Tag Data */ |
khuang | 0:c80666325948 | 111 | |
khuang | 1:a0d9eeedb771 | 112 | while (true) |
khuang | 1:a0d9eeedb771 | 113 | { |
khuang | 1:a0d9eeedb771 | 114 | blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
khuang | 1:a0d9eeedb771 | 115 | Thread::wait(50); |
khuang | 1:a0d9eeedb771 | 116 | } |
khuang | 1:a0d9eeedb771 | 117 | } |
khuang | 1:a0d9eeedb771 | 118 | |
khuang | 1:a0d9eeedb771 | 119 | /******************************End of Main*************************************/ |
khuang | 1:a0d9eeedb771 | 120 | |
khuang | 1:a0d9eeedb771 | 121 | |
khuang | 1:a0d9eeedb771 | 122 | /* txTask() transmits the sensor data */ |
khuang | 1:a0d9eeedb771 | 123 | void txTask(void){ |
khuang | 1:a0d9eeedb771 | 124 | |
khuang | 1:a0d9eeedb771 | 125 | while (true) |
khuang | 1:a0d9eeedb771 | 126 | { |
cotigac | 4:c2ab3a0de448 | 127 | UpdateSensorData(); |
khuang | 0:c80666325948 | 128 | |
khuang | 1:a0d9eeedb771 | 129 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
khuang | 0:c80666325948 | 130 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
khuang | 0:c80666325948 | 131 | |
khuang | 1:a0d9eeedb771 | 132 | /*The following is sending dummy data over BLE. Replace with real data*/ |
khuang | 1:a0d9eeedb771 | 133 | |
khuang | 0:c80666325948 | 134 | /*Send Battery Level for 20% */ |
cotigac | 4:c2ab3a0de448 | 135 | kw40z_device.SendBatteryLevel(battery); |
khuang | 0:c80666325948 | 136 | |
khuang | 0:c80666325948 | 137 | /*Send Ambient Light Level at 50% */ |
cotigac | 4:c2ab3a0de448 | 138 | kw40z_device.SendAmbientLight(light); |
khuang | 0:c80666325948 | 139 | |
khuang | 0:c80666325948 | 140 | /*Send Humidity at 90% */ |
cotigac | 4:c2ab3a0de448 | 141 | kw40z_device.SendHumidity(humidity); |
khuang | 0:c80666325948 | 142 | |
khuang | 0:c80666325948 | 143 | /*Send Temperature at 25 degrees Celsius */ |
cotigac | 4:c2ab3a0de448 | 144 | kw40z_device.SendTemperature(temperature); |
khuang | 0:c80666325948 | 145 | |
khuang | 0:c80666325948 | 146 | /*Send Pressure at 100kPA */ |
cotigac | 4:c2ab3a0de448 | 147 | kw40z_device.SendPressure(pressure); |
khuang | 0:c80666325948 | 148 | |
khuang | 1:a0d9eeedb771 | 149 | /*Send Mag,Accel,Gyro Data. */ |
cotigac | 4:c2ab3a0de448 | 150 | kw40z_device.SendGyro(x,y,z); |
cotigac | 4:c2ab3a0de448 | 151 | kw40z_device.SendAccel(z,x,y); |
cotigac | 4:c2ab3a0de448 | 152 | kw40z_device.SendMag(y,z,x); |
cotigac | 4:c2ab3a0de448 | 153 | |
khuang | 1:a0d9eeedb771 | 154 | Thread::wait(1000); |
khuang | 0:c80666325948 | 155 | } |
khuang | 0:c80666325948 | 156 | } |
khuang | 0:c80666325948 | 157 | |
cotigac | 4:c2ab3a0de448 | 158 | void UpdateSensorData(void) |
cotigac | 4:c2ab3a0de448 | 159 | { |
cotigac | 4:c2ab3a0de448 | 160 | battery -= 5; |
cotigac | 4:c2ab3a0de448 | 161 | if(battery < 5) battery = 100; |
cotigac | 4:c2ab3a0de448 | 162 | |
cotigac | 4:c2ab3a0de448 | 163 | light += 20; |
cotigac | 4:c2ab3a0de448 | 164 | if(light > 100) light = 0; |
cotigac | 4:c2ab3a0de448 | 165 | |
cotigac | 4:c2ab3a0de448 | 166 | humidity += 500; |
cotigac | 4:c2ab3a0de448 | 167 | if(humidity > 8000) humidity = 2000; |
cotigac | 4:c2ab3a0de448 | 168 | |
cotigac | 4:c2ab3a0de448 | 169 | temperature -= 200; |
cotigac | 4:c2ab3a0de448 | 170 | if(temperature < 200) temperature = 4200; |
cotigac | 4:c2ab3a0de448 | 171 | |
cotigac | 4:c2ab3a0de448 | 172 | pressure += 300; |
cotigac | 4:c2ab3a0de448 | 173 | if(pressure > 10300) pressure = 7500; |
cotigac | 4:c2ab3a0de448 | 174 | |
cotigac | 4:c2ab3a0de448 | 175 | x += 1400; |
cotigac | 4:c2ab3a0de448 | 176 | y -= 2300; |
cotigac | 4:c2ab3a0de448 | 177 | z += 1700; |
cotigac | 4:c2ab3a0de448 | 178 | } |
cotigac | 4:c2ab3a0de448 | 179 | |
khuang | 1:a0d9eeedb771 | 180 | void StartHaptic(void) { |
khuang | 0:c80666325948 | 181 | hapticTimer.start(50); |
khuang | 0:c80666325948 | 182 | haptic = 1; |
khuang | 0:c80666325948 | 183 | } |
khuang | 0:c80666325948 | 184 | |
khuang | 0:c80666325948 | 185 | void StopHaptic(void const *n) { |
khuang | 0:c80666325948 | 186 | haptic = 0; |
khuang | 0:c80666325948 | 187 | hapticTimer.stop(); |
khuang | 0:c80666325948 | 188 | } |
khuang | 1:a0d9eeedb771 | 189 |