We are making a bluetooth application for a vehicle.

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

main.cpp

Committer:
Technicus
Date:
2017-07-17
Revision:
28:a56710056f4d
Parent:
27:49f379daad68
Child:
29:e7d4922a4620

File content as of revision 28:a56710056f4d:

/* 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 "ble/BLE.h"
#include "bike_service.h"
#include "ble/services/BatteryService.h"

int alarm_activated = 0;
int alarm_on = 0;
int alarm_counter = 0;

DigitalOut led_0(PC_6);
DigitalOut led_1(PC_7);
DigitalOut buzzer(PC_8);
DigitalOut rumblr(PC_9);;
DigitalOut led1(LED1, 1);

const static char     DEVICE_NAME[]        = "HeckBike!";
static const uint16_t uuid16_list[]        = {GattService::UUID_BATTERY_SERVICE,
                                              0x8EC5};//made up UUID for the Heck bike

#define on  0xFFFF
#define off 0
 
#define buttonMask 0x003F
 
PortIn sixButtons(PortC, buttonMask);
 
unsigned char byteOut = 0; 
unsigned char prevByteOut = 0;
unsigned char byteIn = 0x08;

    uint8_t inputs = 0x00;
    uint8_t outputs = 0x00;
    uint8_t old_outputs = 0x00;
    uint32_t timestamp = 0x00;
    
static volatile bool  triggerSensorPolling = false;

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    (void)params;
    BLE::Instance().gap().startAdvertising(); // restart advertising
}

void periodicCallback(void)
{
    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
     * heavy-weight sensor polling from the main thread. */
    triggerSensorPolling = true;
}

void onBleInitError(BLE &ble, ble_error_t error)
{
    (void)ble;
    (void)error;
   /* Initialization error handling should go here */
}

void onDataWrittenCallback(const GattWriteCallbackParams *params) {
        //if (params->handle == BikeService.outputs.getValueAttribute().getHandle()) {
            //BOB Should update the outputs.
            outputs = *(params->data);
        //}
}


void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
    BLE&        ble   = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        onBleInitError(ble, error);
        return;
    }

    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
        return;
    }

    ble.gap().onDisconnection(disconnectionCallback);

    ble.gattServer().onDataWritten(onDataWrittenCallback);
    /* Setup primary service. */
    BikeService bikeService(ble);
    /* Setup battery service. */
    BatteryService btService(ble,100);
    /* Setup advertising. */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
    ble.gap().startAdvertising();

    // infinite loop
    while (true) {
        // check for trigger from periodicCallback()
        if (triggerSensorPolling && ble.getGapState().connected) {
            if (outputs & 0x01){
                led_0 = 1;
                }
            if ((outputs & 0x01) == 0){
                led_0 = 0;
                }
            if (outputs & 0x02){
                led_1 = 1;
                }
            if ((outputs & 0x02) == 0){
                led_1 = 0;
                }
            if (outputs & 0x04){
                buzzer = 1;
                }
            if ((outputs & 0x04) == 0){
                buzzer = 0;
                }
            if (outputs & 0x08){
                rumblr = 1;
                }
            if ((outputs & 0x08) == 0){
                rumblr = 0;
                }
            if (outputs & 0x10){
                alarm_activated = 1;
            }
            if ((outputs & 0x10) == 0){
                alarm_activated = 0;
            }
            if (outputs & 0x20){
                alarm_on = 1;
            }
            if ((outputs & 0x20) == 0){
                alarm_on = 0;
            }
            
            triggerSensorPolling = false;
             
            byteOut = sixButtons & buttonMask;
            if ((byteOut & 0x01) && alarm_activated==1) {
                alarm_on = 1;
                }
            if (byteOut != prevByteOut){
                // update the ble with the inputs
                bikeService.updateInputs(byteOut);
                bikeService.updateTimestamp();
            }
            prevByteOut = byteOut;
        } else {
            ble.waitForEvent(); // low power wait for event
        }
        
        if (alarm_activated && alarm_on) {                                        //Flag set to sound the alarm?
            
            if (alarm_counter++ < 500) {                            //Beep it on and off
                buzzer = 1;
                led_0 = 1;
                led_1 = 1;
                rumblr = 1;
            }
            else {
                if (alarm_counter > 1000){alarm_counter = 0;}
                buzzer = 0;
                led_0 = 0;
                led_1 = 0;
                rumblr = 0;
            }
        }     
    }
}

int main(void)
{
    Ticker ticker;
    ticker.attach(periodicCallback, 1); // blink LED every second

    BLE::Instance().init(bleInitComplete);
}