
/* mbed Microcontroller Library
 * Copyright (c) 2006-2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "stdio.h"
//#include "ble/BLE.h"
//#include "TMP_nrf51/TMP_nrf51.h"

#define APP_SPECIFIC_ID_TEST 0xFFFF

#define VIBRATE P0_7
#include "SFE_MicroOLED.h"
//accelerometer
#include "KX22.h"
#define KX_SCL  P0_16  
#define KX_SDA  P0_14

#define UART_TX     p18
#define UART_RX     p17
//got these numbers from roger! tx in tx
//vibrate motor
DigitalOut vib(VIBRATE);


#pragma pack(1)
struct ApplicationData_t {
    uint16_t applicationSpecificId;             /* An ID used to identify temperature value
                                                   in the manufacture specific AD data field */
    //TMP_nrf51::tmpSensorValue_t tmpSensorValue; /* User defined application data */
};
#pragma pack()

//BLE ble;
//TMP_nrf51 tempSensor;
//DigitalOut alivenessLED(LED1, 1);
//static bool triggerTempValueUpdate = false;
//const static char     DEVICE_NAME[]        = "Sandyr";

/*********OLED******************/
//from roger :: works!!!
SPI my_spi(P0_29, NC, P0_30);
MicroOLED oled(my_spi, P0_1, P0_0, P0_2);

//from some guy
//SPI my_spi(P0_2, P0_3, P0_1);
//MicroOLED oled(my_spi, P0_30, P0_0, P0_29);

/************KX ACC*************/
TwoWire wire = TwoWire(NRF_TWI1);
KX22 acc(KX_SCL, KX_SDA, &wire);

/***********Serial*****/
extern Serial pc(UART_TX, UART_RX);



/*void periodicCallback(void)
{
    //Any periodic requests that we need to handle 
    
    /* Do blinky on LED1 while we're waiting for BLE events */
   // alivenessLED = !alivenessLED;
    triggerTempValueUpdate = true;
}
*/

/*void accumulateApplicationData(ApplicationData_t &appData)
{
    appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
    /* Read a new temperature value */
    //appData.tmpSensorValue = 20; %tempSensor.get();
}

void temperatureValueAdvertising(void)
{
    ApplicationData_t appData;
    
    accumulateApplicationData(appData);
    //printf("Temp is %f\r\n", (float)appData.tmpSensorValue);
    
    /* Setup advertising payload */
    //    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, 
    //                   (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED |
     //    GapAdvertisingData::LE_GENERAL_DISCOVERABLE); /* Set flag */
    //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); /* Set appearance */
    //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
     //(uint8_t *)&appData, sizeof(ApplicationData_t)); /* Set data */
    
    /* Setup advertising parameters */
    //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
    //ble.gap().setAdvertisingInterval(1000);
    
    /* Start advertising */
   
    //ble.gap().startAdvertising();
}

void updateSensorValueInAdvPayload(void)
{
    ApplicationData_t appData;
    
    accumulateApplicationData(appData);
    
    /* Stop advertising first */
   // ble.gap().stopAdvertising();
    /* Only update temperature value field */
   // ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
     //(uint8_t *)&appData, sizeof(ApplicationData_t));
    /* Start advertising again */
    //ble.gap().startAdvertising();
}
*/
int main(void)
{
   
    pc.baud(115200);
    pc.printf("Serial setup AGAIN!! \n");
   
   /*do nothing 
    //2 quick vibrations
    vib =1;
    wait(0.25);
    vib=0;
    wait(1);
     vib=1;
     wait(0.25);
     vib=0;
    */
     // OLED part of code
    pc.printf("OLED setting up \n");
    
    /*
    oled.init(0, 8000000);
    oled.clear(PAGE);
    oled.clear(ALL);
    oled.puts("Dance Tracker v1.1");
    oled.display();
    wait(2);   
       pc.printf("OLED set complete\n");
      */
       //
    
    Ticker ticker; // to interrupt the controllers' process every period
    /* Enable trigger every 2 seconds */
    ticker.attach(periodicCallback, 2);
    
   // ble.init();
    pc.printf("BLE set up \n");
    /* Start temperature advertising */
    temperatureValueAdvertising();
    
    
    while (true) {
        //if (triggerTempValueUpdate) {
            /* Update temperature value */
            //updateSensorValueInAdvPayload();
            //triggerTempValueUpdate = false;
            
            //here also display the current accelerometer values 
        /*
              oled.init(0, 8000000);
             oled.clear(PAGE);
             oled.clear(ALL);
             oled.printf( "Acc values:%d,%d,%d\n",
                              (int32_t) acc.getAccel(0,&wire),
                              (int32_t) acc.getAccel(1,&wire),
                              (int32_t) acc.getAccel(2,&wire));
                              
             oled.display();
          */   
            pc.printf("going to print vals now\n");

             pc.printf("Acc values:%d,%d,%d\n",
                              (int32_t) acc.getAccel(0,&wire),
                              (int32_t) acc.getAccel(1,&wire),
                              (int32_t) acc.getAccel(2,&wire));
                   pc.printf("completed the acc read and oled jobs\n");
                   wait(5);
        }
       // pc.printf("completed the acc read and oled jobs\n");
        //ble.waitForEvent();
        //pc.printf("wait for ble event\n");
    //}
    
}
