Taiyuk
Created October 10, 2016 © Apache-2.0

WhereMy...

Inspired by my idea "Where's my baggage", this project will show you how to create your track anything device using Hexiwear on your phone.

BeginnerFull instructions provided1 hour94
WhereMy...

Things used in this project

Hardware components

Hexiwear
NXP Hexiwear
×1
Android device
Android device
×1

Hand tools and fabrication machines

mbed
Arm mbed

Story

Read more

Schematics

WhereMy

Code

main.cpp

C/C++
Hexiwear code based on Hexi_BLE_Example which toggle Bluetooth on movement (accelerator).
#include "mbed.h"
#include "Hexi_KW40Z.h"
#include "Hexi_OLED_SSD1351.h"
#include "OLED_types.h"
#include "OpenSans_Font.h"
#include "string.h"
#include "FXOS8700.h"

#define LED_ON      0
#define LED_OFF     1

FXOS8700 accel(PTC11, PTC10);
FXOS8700 mag(PTC11, PTC10);

void StartHaptic(void);
void StopHaptic(void const *n);
void txTask(void);

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) */

/*Create a Thread to handle sending BLE Sensor Data */ 
Thread txThread;

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

float accel_data[3];
int old[3];
int new1[3];

uint8_t enable1 = 0;
uint8_t auto1 = 0;
DigitalOut led(LED1,0);

/****************************Call Back Functions*******************************/
void ButtonRight(void) //auto
{
    oled_text_properties_t textProperties = {0};
    oled.GetTextProperties(&textProperties);
    StartHaptic();
    auto1 = !auto1;
    led = !led;
    wait(1.0f);
    led = !led;
    if (auto1){
        textProperties.fontColor   = COLOR_WHITE;
        oled.SetTextProperties(&textProperties);
        strcpy((char *) text,"Auto");
        oled.Label((uint8_t *)text,60,80);
        txThread.start(txTask);
    }else{
        textProperties.fontColor   = COLOR_GRAY;
        oled.SetTextProperties(&textProperties);
        strcpy((char *) text,"Auto");
        oled.Label((uint8_t *)text,60,80);
        txThread.terminate();
        kw40z_device.ToggleAdvertisementMode();
    }
}

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

void PassKey(void)
{
    StartHaptic();
    strcpy((char *) text,"PAIR CODE");
    oled.TextBox((uint8_t *)text,0,25,95,18);
  
    /* 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);
}

/***********************End of Call Back Functions*****************************/

/********************************Main******************************************/

int main()
{    
    /* Register callbacks to application functions */
    kw40z_device.attach_buttonLeft(&ButtonLeft);
    kw40z_device.attach_buttonRight(&ButtonRight);
    kw40z_device.attach_passkey(&PassKey);

    /* Turn on the backlight of the OLED Display */
    oled.DimScreenON();
    
    /* Fills the screen with solid black */         
    oled.FillScreen(COLOR_BLACK);

    /* Get OLED Class Default Text Properties */
    oled_text_properties_t textProperties = {0};
    oled.GetTextProperties(&textProperties);    
        
    /* Change font color to Blue */ 
    textProperties.fontColor   = COLOR_WHITE;
    oled.SetTextProperties(&textProperties);
    
    /* Display Bluetooth Label at x=17,y=65 */ 
    strcpy((char *) text,"WhereMY...");
    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);
    
    strcpy((char *) text,"Enable |");
    oled.Label((uint8_t *)text,5,80);
    
    textProperties.fontColor   = COLOR_GRAY;
    oled.SetTextProperties(&textProperties);
    strcpy((char *) text,"Auto");
    oled.Label((uint8_t *)text,60,80);
    
    led = !led;
    
    accel.accel_config();
    
    accel.acquire_accel_data_g(accel_data);
    old[0] = (int) abs(accel_data[0]*10);
    old[1] = (int) abs(accel_data[1]*10);
    old[2] = (int) abs(accel_data[2]*10);
  
    while (true) 
    {
        blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
        Thread::wait(50);
    }
}

/******************************End of Main*************************************/


/* txTask() transmits the sensor data */
void txTask(void){
   
   while (auto1) 
   {
        accel.acquire_accel_data_g(accel_data);
        
        
    oled_text_properties_t textProperties = {0};
    oled.GetTextProperties(&textProperties);
    textProperties.fontColor   = COLOR_WHITE;
    oled.SetTextProperties(&textProperties);

    new1[0] = (int) abs(accel_data[0]*10);
    new1[1] = (int) abs(accel_data[1]*10);
    new1[2] = (int) abs(accel_data[2]*10);

    if ( old[0]!=new1[0] || old[1]!=new1[1] || old[2]!=new1[2] ){
        kw40z_device.ToggleAdvertisementMode();
        blueLed!=blueLed;
        Thread::wait(10000);

        old[0]=new1[0];
        old[1]=new1[1];
        old[2]=new1[2];
        blueLed!=blueLed;

        kw40z_device.ToggleAdvertisementMode();
    }
    Thread::wait(1000);                 
  }
}

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

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

Credits

Taiyuk

Taiyuk

3 projects • 7 followers

Comments