Nrithya Theetharappan / Mbed OS Hexi_BLE_Time_copy

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_BLE_Time by Adam S

main.cpp

Committer:
nrithya
Date:
2018-06-11
Revision:
8:25985d2711e0
Parent:
7:88af507a85bd

File content as of revision 8:25985d2711e0:

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

#define LED_ON      0
#define LED_OFF     1

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

Serial pc(USBTX, USBRX);

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;
Thread broadcastThread;

 /* Text Buffer */ 
char text[20]; 
char time_text[20];
char date_text[25];

uint8_t battery = 100;
uint8_t light = 0;
uint16_t humidity = 4500;
uint16_t temperature = 2000;
uint16_t pressure = 9000;
uint16_t x = 0;
uint16_t y = 5000;
uint16_t z = 10000;

/****************************Call Back Functions*******************************/


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

void ButtonLeft(void)
{
    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);
}

// Key modification: use the alert functionality enabled by the host-ble interface
// to define our own command.
void AlertReceived(uint8_t *data, uint8_t length)
{
    StartHaptic();
    data[19] = 0;
    char buf[20];
    char buffer[30];
    char *smallbuff;
    pc.printf("%s\n\r", data);
    for(int i=0;i<20;i++)
    {
        printf("%c",data[i]);
        }
   
    if (data) {
         //time_t seconds = (uint8_t)data;
        std::time_t timeinfo;
        //time(&rawtime); 
        
        //timeinfo = data;
        sprintf(buf,"%s",data);
        //printf("%s\n",buf);
        int timedata= atoi(buf);
        //printf("%d\n",timedata);
        timeinfo=(time_t)timedata;
        //printf("%ld\n",long(timeinfo));
        //printf("Time as a string = %s", ctime(&timeinfo));
        sprintf(buffer,"%s",ctime(&timeinfo));
        sprintf(text,"%s","   ");
        //char str[]="sun jun 10 12:34:33 2018";
        //printf("%s\n", str);
        oled.TextBox((uint8_t *)text,1,40,55,15); 
        oled.FillScreen(COLOR_BLACK);
    
        char *token[10];
        token[0] = strtok(buffer, " ");
    
    // Keep printing tokens while one of the
    // delimiters present in str[].
        int i=0;
    while (token[i] != NULL)
    {
        printf("%s\n", token[i]);
        i++;
        token[i] = strtok(NULL, " ");
    }
    char str1[60];
    char str2[10];
    //char str[80];
    /*strcpy (str1,token[0]);
    strcat (str1,token[1]);
    strcat (str1,token[2]);
    strcat (str1,token[4]);*/
    sprintf(str1,"%s %s %s %s",token[0],token[1],token[2],token[4]);
    strcpy(str2,token[3]);
    printf("%s\n %s\n",str1,str2);
    oled.Label((uint8_t *)str1,1,40);
    oled.Label((uint8_t *)str2,1,60);
    /*for(int i=0;i<10;i++){
        //oled.Label((uint8_t *)str[i],1,40+i);
        sprintf(str1,"%s",str[i]);
        printf("%s\n",str1);
        }
        printf("%s\n",str1);*/
        //printf("%s\n", str);
        
        
        
        printf("%s",buffer);
        for(int i=0;i<10;i++)
        {
            sprintf(smallbuff,"%s",buffer[i]);
            }
        printf("%s\n",smallbuff);
        strcpy((char *) text,"sun jun 10 88");
        oled.Label((uint8_t *)text,17,65);
        blueLed = LED_OFF;
        greenLed = LED_ON;
        wait_ms(1000);
        greenLed = LED_OFF;
        blueLed = LED_ON;
        }
    
    /*
    // data (our command) must 20 bytes long.
    // CMD for turning on: 'ledonledonledonledon'
    if (data[4] == 'n') {
        greenLed = LED_ON;
        redLed = LED_ON;
        blueLed = LED_ON;
        pc.printf("on\n\r", data);
    
    // CMD for turning off: 'ledoffledoffledoffled'
    } else if (data[4] == 'f') {
        greenLed = LED_OFF;
        redLed = LED_OFF;
        blueLed = LED_OFF;
        pc.printf("off\n\r", data);
    }
    */
    
}
/***********************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);
    kw40z_device.attach_alert(&AlertReceived);
    
       
    pc.printf("hello\n\r");
    
    /* 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_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);

    // txThread.start(txTask); /*Start transmitting Sensor Tag Data */
    
    int counter = 0;
    int status = 0;
    
    while (true) 
    {
        counter += 1; 
        status = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
        blueLed = status;
        //pc.printf("[%i] blueLed = %i\r\n",counter, status);
        
        /*time_t rawtime;
        struct tm * timeinfo;
        time(&rawtime); 
        timeinfo = localtime (&rawtime);
        const tm *t = localtime(&rawtime); // Convert the unix time to actual time
        
        int h = (t->tm_hour); // The hours
        int year = (t->tm_year);
          
        // Format the time
        sprintf(time_text,"%d:%d:%d",h, t->tm_min, t->tm_sec);  
        sprintf(date_text,"%d-%d-%d",year, (t->tm_mon), (t->tm_mday));
        
        char buf[80];
        
        // Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
        // ts = *localtime(&rawtime);
        // pc.printf ("Current local time and date: %s", asctime(timeinfo));
        // strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
        // pc.printf("%s\r\n", buf);
       
        /*
        pc.printf(time_text);
        pc.printf("\r\n");
        pc.printf(date_text);
        pc.printf("\r\n");
        */
        
        // Display the time on screen
        // oled_status_t SSD1351::TextBox(const uint8_t* text, int8_t xCrd, int8_t yCrd,uint8_t width,uint8_t height)*/
        //oled.TextBox((uint8_t *)time_text,2,2, 91, 15); 
        //oled.TextBox((uint8_t *)date_text,2,16, 91, 15); 
        redLed = !redLed;
        
        Thread::wait(500);
        

        
    }
}

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


/* txTask() transmits the sensor data */
void txTask(void){
   
   while (true) 
   {
        UpdateSensorData();
        
        /*Notify Hexiwear App that it is running Sensor Tag mode*/
        kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
                
        /*The following is sending dummy data over BLE. Replace with real data*/
    
        /*Send Battery Level for 20% */ 
        kw40z_device.SendBatteryLevel(battery);
               
        /*Send Ambient Light Level at 50% */ 
        kw40z_device.SendAmbientLight(light);
        
        /*Send Humidity at 90% */
        kw40z_device.SendHumidity(humidity);
        
        /*Send Temperature at 25 degrees Celsius */
        kw40z_device.SendTemperature(temperature);

        /*Send Pressure at 100kPA */ 
        kw40z_device.SendPressure(pressure);
        
        /*Send Mag,Accel,Gyro Data. */
        kw40z_device.SendGyro(x,y,z);
        kw40z_device.SendAccel(z,x,y);
        kw40z_device.SendMag(y,z,x);

        Thread::wait(1000);                 
    }
}


void UpdateSensorData(void)
{    
    battery -= 5;
    if(battery < 5) battery = 100;
    
    light += 20;
    if(light > 100) light = 0;
    
    humidity += 500;
    if(humidity > 8000) humidity = 2000;
    
    temperature -= 200;
    if(temperature < 200) temperature = 4200;
    
    pressure += 300;
    if(pressure > 10300) pressure = 7500;
    
    x += 1400;
    y -= 2300;
    z += 1700;
}

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

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