Hexiwear Code for Game Controls

Dependencies:   FXAS21002 FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_Final_Project by Destin Raymundo

Committer:
khuang
Date:
Tue Sep 20 01:13:43 2016 +0000
Revision:
0:c80666325948
Child:
1:a0d9eeedb771
Hexiwear BLE Sensor Tag Example.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khuang 0:c80666325948 1 /* Use this in conjunction with the iOS or Android Hexiwear App.
khuang 0:c80666325948 2 This example currently shows the Sensor Tag functionality.
khuang 0:c80666325948 3 Program the Hexiwear with the binary, then tap to turn on
khuang 0:c80666325948 4 Bluetooth.(Blue LED indicates BLE is on) Use Hexiwear app to
khuang 0:c80666325948 5 connect. Pairing code will appear on the OLED screen. */
khuang 0:c80666325948 6
khuang 0:c80666325948 7 #include "mbed.h"
khuang 0:c80666325948 8 #include "Hexi_KW40Z.h"
khuang 0:c80666325948 9 #include "Hexi_OLED_SSD1351.h"
khuang 0:c80666325948 10 #include "OLED_types.h"
khuang 0:c80666325948 11 #include "OpenSans_Font.h"
khuang 0:c80666325948 12 #include "string.h"
khuang 0:c80666325948 13
khuang 0:c80666325948 14 #define LED_ON 0
khuang 0:c80666325948 15 #define LED_OFF 1
khuang 0:c80666325948 16
khuang 0:c80666325948 17 void StartHaptic(void);
khuang 0:c80666325948 18 void StopHaptic(void const *n);
khuang 0:c80666325948 19
khuang 0:c80666325948 20 DigitalOut redLed(LED1,1);
khuang 0:c80666325948 21 DigitalOut greenLed(LED2,1);
khuang 0:c80666325948 22 DigitalOut blueLed(LED3,1);
khuang 0:c80666325948 23 DigitalOut haptic(PTB9);
khuang 0:c80666325948 24
khuang 0:c80666325948 25 /* Define timer for haptic feedback */
khuang 0:c80666325948 26 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
khuang 0:c80666325948 27
khuang 0:c80666325948 28 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */
khuang 0:c80666325948 29 KW40Z kw40z_device(PTE24, PTE25);
khuang 0:c80666325948 30
khuang 0:c80666325948 31 /* Instantiate the SSD1351 OLED Driver */
khuang 0:c80666325948 32 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
khuang 0:c80666325948 33
khuang 0:c80666325948 34 char text[20]; /* Text Buffer */
khuang 0:c80666325948 35
khuang 0:c80666325948 36 void ButtonRight(void)
khuang 0:c80666325948 37 {
khuang 0:c80666325948 38 StartHaptic();
khuang 0:c80666325948 39 kw40z_device.ToggleAdvertisementMode();
khuang 0:c80666325948 40 }
khuang 0:c80666325948 41
khuang 0:c80666325948 42 void ButtonLeft(void)
khuang 0:c80666325948 43 {
khuang 0:c80666325948 44 StartHaptic();
khuang 0:c80666325948 45 kw40z_device.ToggleAdvertisementMode();
khuang 0:c80666325948 46 }
khuang 0:c80666325948 47
khuang 0:c80666325948 48 void PassKey(void)
khuang 0:c80666325948 49 {
khuang 0:c80666325948 50 StartHaptic();
khuang 0:c80666325948 51
khuang 0:c80666325948 52 /* Display Text at (x=20,y=2) */
khuang 0:c80666325948 53 strcpy((char *) text,"PAIR CODE");
khuang 0:c80666325948 54 oled.Label((uint8_t *)text,20,25);
khuang 0:c80666325948 55
khuang 0:c80666325948 56 /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
khuang 0:c80666325948 57 sprintf(text,"%d", kw40z_device.GetPassKey());
khuang 0:c80666325948 58 oled.TextBox((uint8_t *)text,0,40,95,18);
khuang 0:c80666325948 59 }
khuang 0:c80666325948 60
khuang 0:c80666325948 61 int main()
khuang 0:c80666325948 62 {
khuang 0:c80666325948 63 /* Get OLED Class Default Text Properties */
khuang 0:c80666325948 64 oled_text_properties_t textProperties = {0};
khuang 0:c80666325948 65 oled.GetTextProperties(&textProperties);
khuang 0:c80666325948 66
khuang 0:c80666325948 67 /* Turn on the backlight of the OLED Display */
khuang 0:c80666325948 68 oled.DimScreenON();
khuang 0:c80666325948 69
khuang 0:c80666325948 70 /* Fills the screen with solid black */
khuang 0:c80666325948 71 oled.FillScreen(COLOR_BLACK);
khuang 0:c80666325948 72
khuang 0:c80666325948 73 /* Register callbacks to application functions */
khuang 0:c80666325948 74 kw40z_device.attach_buttonLeft(&ButtonLeft);
khuang 0:c80666325948 75 kw40z_device.attach_buttonRight(&ButtonRight);
khuang 0:c80666325948 76 kw40z_device.attach_passkey(&PassKey);
khuang 0:c80666325948 77
khuang 0:c80666325948 78 /* Change font color to Blue */
khuang 0:c80666325948 79 textProperties.fontColor = COLOR_BLUE;
khuang 0:c80666325948 80 oled.SetTextProperties(&textProperties);
khuang 0:c80666325948 81
khuang 0:c80666325948 82 /* Display Bluetooth Label at x=17,y=65 */
khuang 0:c80666325948 83 strcpy((char *) text,"BLUETOOTH");
khuang 0:c80666325948 84 oled.Label((uint8_t *)text,17,65);
khuang 0:c80666325948 85
khuang 0:c80666325948 86 /* Change font color to white */
khuang 0:c80666325948 87 textProperties.fontColor = COLOR_WHITE;
khuang 0:c80666325948 88 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
khuang 0:c80666325948 89 oled.SetTextProperties(&textProperties);
khuang 0:c80666325948 90
khuang 0:c80666325948 91 /* Display Label at x=22,y=80 */
khuang 0:c80666325948 92 strcpy((char *) text,"Tap Below");
khuang 0:c80666325948 93 oled.Label((uint8_t *)text,22,80);
khuang 0:c80666325948 94
khuang 0:c80666325948 95
khuang 0:c80666325948 96 while (true) {
khuang 0:c80666325948 97
khuang 0:c80666325948 98 blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
khuang 0:c80666325948 99
khuang 0:c80666325948 100 /*Notify device that it is running Sensor Tag mode for the Hexiwear App*/
khuang 0:c80666325948 101 kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
khuang 0:c80666325948 102
khuang 0:c80666325948 103 /*The following is sending dummy data over Bluetooth LE. Replace with real data*/
khuang 0:c80666325948 104
khuang 0:c80666325948 105 /*Send Battery Level for 20% */
khuang 0:c80666325948 106 kw40z_device.SendBatteryLevel(20);
khuang 0:c80666325948 107
khuang 0:c80666325948 108 /*Send Ambient Light Level at 50% */
khuang 0:c80666325948 109 kw40z_device.SendAmbientLight(50);
khuang 0:c80666325948 110
khuang 0:c80666325948 111 /*Send Humidity at 90% */
khuang 0:c80666325948 112 kw40z_device.SendHumidity(9000);
khuang 0:c80666325948 113
khuang 0:c80666325948 114 /*Send Temperature at 25 degrees Celsius */
khuang 0:c80666325948 115 kw40z_device.SendTemperature(2500);
khuang 0:c80666325948 116
khuang 0:c80666325948 117 /*Send Pressure at 100kPA */
khuang 0:c80666325948 118 kw40z_device.SendPressure(10000);
khuang 0:c80666325948 119
khuang 0:c80666325948 120 /*Send Mag,Accel,Gyro Data. Y-axis has offset in Hexiwear App*/
khuang 0:c80666325948 121 kw40z_device.SendGyro(0,0,0);
khuang 0:c80666325948 122 kw40z_device.SendAccel(0,0,0);
khuang 0:c80666325948 123 kw40z_device.SendMag(0,0,0);
khuang 0:c80666325948 124
khuang 0:c80666325948 125 Thread::wait(1000);
khuang 0:c80666325948 126
khuang 0:c80666325948 127 }
khuang 0:c80666325948 128 }
khuang 0:c80666325948 129
khuang 0:c80666325948 130 void StartHaptic(void)
khuang 0:c80666325948 131 {
khuang 0:c80666325948 132 hapticTimer.start(50);
khuang 0:c80666325948 133 haptic = 1;
khuang 0:c80666325948 134 }
khuang 0:c80666325948 135
khuang 0:c80666325948 136 void StopHaptic(void const *n) {
khuang 0:c80666325948 137 haptic = 0;
khuang 0:c80666325948 138 hapticTimer.stop();
khuang 0:c80666325948 139 }