ECE119 BLE Project Exploration

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_BLE_Example by Hexiwear

main.cpp

Committer:
khuang
Date:
2016-09-20
Revision:
0:c80666325948
Child:
1:a0d9eeedb771

File content as of revision 0:c80666325948:

/* Use this in conjunction with the iOS or Android Hexiwear App. 
   This example currently shows the Sensor Tag functionality. 
   Program the Hexiwear with the binary, then tap to turn on
   Bluetooth.(Blue LED indicates BLE is on) Use Hexiwear app to 
   connect. Pairing code will appear on the OLED screen. */ 

#include "mbed.h"
#include "Hexi_KW40Z.h"
#include "Hexi_OLED_SSD1351.h"
#include "OLED_types.h"
#include "OpenSans_Font.h"
#include "string.h"

#define LED_ON      0
#define LED_OFF     1
   
void StartHaptic(void);
void StopHaptic(void const *n);

DigitalOut redLed(LED1,1);
DigitalOut greenLed(LED2,1);
DigitalOut blueLed(LED3,1);
DigitalOut haptic(PTB9);

/* Define timer for haptic feedback */
RtosTimer hapticTimer(StopHaptic, osTimerOnce);

/* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 
KW40Z kw40z_device(PTE24, PTE25);

/* Instantiate the SSD1351 OLED Driver */ 
SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */

char text[20];  /* Text Buffer */ 

void ButtonRight(void)
{
    StartHaptic();
    kw40z_device.ToggleAdvertisementMode();
}

void ButtonLeft(void)
{
    StartHaptic();
    kw40z_device.ToggleAdvertisementMode();
}

void PassKey(void)
{
    StartHaptic();
    
    /* Display Text at (x=20,y=2) */
    strcpy((char *) text,"PAIR CODE");
    oled.Label((uint8_t *)text,20,25);
  
    /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
    sprintf(text,"%d", kw40z_device.GetPassKey());
    oled.TextBox((uint8_t *)text,0,40,95,18);
}
   
int main()
{    
    /* Get OLED Class Default Text Properties */
    oled_text_properties_t textProperties = {0};
    oled.GetTextProperties(&textProperties);    

    /* Turn on the backlight of the OLED Display */
    oled.DimScreenON();
    
    /* Fills the screen with solid black */         
    oled.FillScreen(COLOR_BLACK);
        
    /* Register callbacks to application functions */
    kw40z_device.attach_buttonLeft(&ButtonLeft);
    kw40z_device.attach_buttonRight(&ButtonRight);
    kw40z_device.attach_passkey(&PassKey);
    
    /* Change font color to Blue */ 
    textProperties.fontColor   = COLOR_BLUE;
    oled.SetTextProperties(&textProperties);
    
    /* Display Bluetooth Label at x=17,y=65 */ 
    strcpy((char *) text,"BLUETOOTH");
    oled.Label((uint8_t *)text,17,65);
    
    /* Change font color to white */ 
    textProperties.fontColor   = COLOR_WHITE;
    textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
    oled.SetTextProperties(&textProperties);
    
     /* Display Label at x=22,y=80 */ 
    strcpy((char *) text,"Tap Below");
    oled.Label((uint8_t *)text,22,80);
         
    
    while (true) {
        
        blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
        
        /*Notify device that it is running Sensor Tag mode for the Hexiwear App*/
        kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
                
        /*The following is sending dummy data over Bluetooth LE. Replace with real data*/
          
        /*Send Battery Level for 20% */ 
        kw40z_device.SendBatteryLevel(20);
               
        /*Send Ambient Light Level at 50% */ 
        kw40z_device.SendAmbientLight(50);
        
        /*Send Humidity at 90% */
        kw40z_device.SendHumidity(9000);
        
        /*Send Temperature at 25 degrees Celsius */
        kw40z_device.SendTemperature(2500);

        /*Send Pressure at 100kPA */ 
        kw40z_device.SendPressure(10000);
        
        /*Send Mag,Accel,Gyro Data. Y-axis has offset in Hexiwear App*/
        kw40z_device.SendGyro(0,0,0);
        kw40z_device.SendAccel(0,0,0);        
        kw40z_device.SendMag(0,0,0);
        
        Thread::wait(1000);
      
    }
}

void StartHaptic(void)
{
    hapticTimer.start(50);
    haptic = 1;
}

void StopHaptic(void const *n) {
    haptic = 0;
    hapticTimer.stop();
}