Seat sensor first upload wokring 07092016

Dependencies:   BLE_API mbed nRF51822

Committer:
samuelmoss
Date:
Wed Sep 07 15:08:48 2016 +0000
Revision:
0:f23a00038055
First upload working 07092016

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samuelmoss 0:f23a00038055 1 /* mbed Microcontroller Library
samuelmoss 0:f23a00038055 2 * Copyright (c) 2006-2013 ARM Limited
samuelmoss 0:f23a00038055 3 *
samuelmoss 0:f23a00038055 4 * Licensed under the Apache License, Version 2.0 (the "License");
samuelmoss 0:f23a00038055 5 * you may not use this file except in compliance with the License.
samuelmoss 0:f23a00038055 6 * You may obtain a copy of the License at
samuelmoss 0:f23a00038055 7 *
samuelmoss 0:f23a00038055 8 * http://www.apache.org/licenses/LICENSE-2.0
samuelmoss 0:f23a00038055 9 *
samuelmoss 0:f23a00038055 10 * Unless required by applicable law or agreed to in writing, software
samuelmoss 0:f23a00038055 11 * distributed under the License is distributed on an "AS IS" BASIS,
samuelmoss 0:f23a00038055 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
samuelmoss 0:f23a00038055 13 * See the License for the specific language governing permissions and
samuelmoss 0:f23a00038055 14 * limitations under the License.
samuelmoss 0:f23a00038055 15 */
samuelmoss 0:f23a00038055 16
samuelmoss 0:f23a00038055 17 #include "mbed.h"
samuelmoss 0:f23a00038055 18 #include "ble/BLE.h"
samuelmoss 0:f23a00038055 19 #include "ButtonService.h"
samuelmoss 0:f23a00038055 20
samuelmoss 0:f23a00038055 21 Serial pc(USBTX, USBRX);
samuelmoss 0:f23a00038055 22 DigitalOut led1(LED1); //sets led1 to digital out for connection status indicator
samuelmoss 0:f23a00038055 23 DigitalOut led2(LED2); //sets led1 to digital out for seat status indicator
samuelmoss 0:f23a00038055 24 InterruptIn button(P0_7); //sets Pin 7 to an interrupt enabled input for chair status updating
samuelmoss 0:f23a00038055 25
samuelmoss 0:f23a00038055 26 const static char DEVICE_NAME[] = "Seat Sensor"; //specifies name of the device to be found
samuelmoss 0:f23a00038055 27 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
samuelmoss 0:f23a00038055 28
samuelmoss 0:f23a00038055 29 Ticker ticker; //defines a ticker for periodic callback for connection indicator
samuelmoss 0:f23a00038055 30 Timer debounceTimer; //defines a timer for the debouncing loop
samuelmoss 0:f23a00038055 31 int pin = P0_7;
samuelmoss 0:f23a00038055 32 BLE ble;
samuelmoss 0:f23a00038055 33
samuelmoss 0:f23a00038055 34 enum { //an enumerator defining the three possible output states. Chair empty(RELEASED) =0. Chair OCCUPIED(PRESSED) = 1. Chair default(IDLE) = 2.
samuelmoss 0:f23a00038055 35 RELEASED = 0,
samuelmoss 0:f23a00038055 36 PRESSED,
samuelmoss 0:f23a00038055 37 IDLE
samuelmoss 0:f23a00038055 38 };
samuelmoss 0:f23a00038055 39
samuelmoss 0:f23a00038055 40 static uint8_t buttonState = IDLE; //initialised buttonState(Chair state) to the default(IDLE)
samuelmoss 0:f23a00038055 41 static ButtonService *buttonServicePtr;
samuelmoss 0:f23a00038055 42
samuelmoss 0:f23a00038055 43 /*function is called from the ISR when the chair becomes occupied, resetting the debounce timer and changing button state*/
samuelmoss 0:f23a00038055 44 void buttonPressedCallback(void)
samuelmoss 0:f23a00038055 45 {
samuelmoss 0:f23a00038055 46
samuelmoss 0:f23a00038055 47 debounceTimer.reset();
samuelmoss 0:f23a00038055 48 buttonState = PRESSED;
samuelmoss 0:f23a00038055 49
samuelmoss 0:f23a00038055 50 }
samuelmoss 0:f23a00038055 51 /*function is called from the ISR when the chair becomes empty, resetting the debounce timer and changing button state*/
samuelmoss 0:f23a00038055 52 void buttonReleasedCallback(void)
samuelmoss 0:f23a00038055 53 {
samuelmoss 0:f23a00038055 54
samuelmoss 0:f23a00038055 55 debounceTimer.reset();
samuelmoss 0:f23a00038055 56 buttonState = RELEASED;
samuelmoss 0:f23a00038055 57
samuelmoss 0:f23a00038055 58 }
samuelmoss 0:f23a00038055 59
samuelmoss 0:f23a00038055 60
samuelmoss 0:f23a00038055 61 /* upon disconnecting advertising process is repeated */
samuelmoss 0:f23a00038055 62 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
samuelmoss 0:f23a00038055 63 {
samuelmoss 0:f23a00038055 64
samuelmoss 0:f23a00038055 65 BLE::Instance().gap().startAdvertising();
samuelmoss 0:f23a00038055 66
samuelmoss 0:f23a00038055 67 }
samuelmoss 0:f23a00038055 68
samuelmoss 0:f23a00038055 69 /* on connection state is outputted updating firefly */
samuelmoss 0:f23a00038055 70 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
samuelmoss 0:f23a00038055 71 {
samuelmoss 0:f23a00038055 72
samuelmoss 0:f23a00038055 73 // buttonServicePtr->updateButtonState(button.read());
samuelmoss 0:f23a00038055 74
samuelmoss 0:f23a00038055 75
samuelmoss 0:f23a00038055 76 }
samuelmoss 0:f23a00038055 77
samuelmoss 0:f23a00038055 78 /* periodic callback for indicating state of the board */
samuelmoss 0:f23a00038055 79 void periodicCallback(void)
samuelmoss 0:f23a00038055 80 {
samuelmoss 0:f23a00038055 81 if(ble.getGapState().connected == 0) {
samuelmoss 0:f23a00038055 82 led1=!led1;
samuelmoss 0:f23a00038055 83 } else {
samuelmoss 0:f23a00038055 84 led1= 1;
samuelmoss 0:f23a00038055 85 }
samuelmoss 0:f23a00038055 86
samuelmoss 0:f23a00038055 87 }
samuelmoss 0:f23a00038055 88
samuelmoss 0:f23a00038055 89 /**
samuelmoss 0:f23a00038055 90 * This function is called when the ble initialization process has failled
samuelmoss 0:f23a00038055 91 */
samuelmoss 0:f23a00038055 92 void onBleInitError(BLE &ble, ble_error_t error)
samuelmoss 0:f23a00038055 93 {
samuelmoss 0:f23a00038055 94 /* Initialization error handling should go here */
samuelmoss 0:f23a00038055 95 }
samuelmoss 0:f23a00038055 96
samuelmoss 0:f23a00038055 97 /**
samuelmoss 0:f23a00038055 98 * Callback triggered when the ble initialization process has finished
samuelmoss 0:f23a00038055 99 */
samuelmoss 0:f23a00038055 100 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
samuelmoss 0:f23a00038055 101 {
samuelmoss 0:f23a00038055 102 BLE& ble = params->ble;
samuelmoss 0:f23a00038055 103 ble_error_t error = params->error;
samuelmoss 0:f23a00038055 104
samuelmoss 0:f23a00038055 105 if (error != BLE_ERROR_NONE) {
samuelmoss 0:f23a00038055 106 /* In case of error, forward the error handling to onBleInitError */
samuelmoss 0:f23a00038055 107 onBleInitError(ble, error);
samuelmoss 0:f23a00038055 108 return;
samuelmoss 0:f23a00038055 109 }
samuelmoss 0:f23a00038055 110
samuelmoss 0:f23a00038055 111
samuelmoss 0:f23a00038055 112 /* Ensure that it is the default instance of BLE */
samuelmoss 0:f23a00038055 113 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
samuelmoss 0:f23a00038055 114 return;
samuelmoss 0:f23a00038055 115 }
samuelmoss 0:f23a00038055 116
samuelmoss 0:f23a00038055 117 ble.gap().onDisconnection(disconnectionCallback);
samuelmoss 0:f23a00038055 118 ble.gap().onConnection(connectionCallback);
samuelmoss 0:f23a00038055 119
samuelmoss 0:f23a00038055 120 /* Setup primary service */
samuelmoss 0:f23a00038055 121 buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */);
samuelmoss 0:f23a00038055 122
samuelmoss 0:f23a00038055 123 /* setup advertising */
samuelmoss 0:f23a00038055 124 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
samuelmoss 0:f23a00038055 125 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
samuelmoss 0:f23a00038055 126 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
samuelmoss 0:f23a00038055 127 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
samuelmoss 0:f23a00038055 128 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
samuelmoss 0:f23a00038055 129 ble.gap().startAdvertising();
samuelmoss 0:f23a00038055 130
samuelmoss 0:f23a00038055 131 }
samuelmoss 0:f23a00038055 132
samuelmoss 0:f23a00038055 133 int main(void)
samuelmoss 0:f23a00038055 134 {
samuelmoss 0:f23a00038055 135
samuelmoss 0:f23a00038055 136 debounceTimer.start(); //initialises the debounce timer
samuelmoss 0:f23a00038055 137 ticker.attach(periodicCallback, 1); // periodic ticker indicating the board it setup and running
samuelmoss 0:f23a00038055 138 button.fall(buttonPressedCallback); //interrupt call for chair occupied
samuelmoss 0:f23a00038055 139 button.rise(buttonReleasedCallback);//interrupt call for chair empty
samuelmoss 0:f23a00038055 140
samuelmoss 0:f23a00038055 141 BLE &ble = BLE::Instance();
samuelmoss 0:f23a00038055 142 ble.init(bleInitComplete);
samuelmoss 0:f23a00038055 143
samuelmoss 0:f23a00038055 144
samuelmoss 0:f23a00038055 145 while (ble.hasInitialized() == false)
samuelmoss 0:f23a00038055 146 {
samuelmoss 0:f23a00038055 147 /* spin loop */
samuelmoss 0:f23a00038055 148 }
samuelmoss 0:f23a00038055 149
samuelmoss 0:f23a00038055 150 /* updates state to the firefly upon mbed board booting */
samuelmoss 0:f23a00038055 151 if (button.read() == 1) {
samuelmoss 0:f23a00038055 152 buttonServicePtr->updateButtonState(1);
samuelmoss 0:f23a00038055 153 led2 = !button.read();
samuelmoss 0:f23a00038055 154 } else {
samuelmoss 0:f23a00038055 155 buttonServicePtr->updateButtonState(0);
samuelmoss 0:f23a00038055 156 led2 = !button.read();
samuelmoss 0:f23a00038055 157 }
samuelmoss 0:f23a00038055 158
samuelmoss 0:f23a00038055 159 /*main while loop*/
samuelmoss 0:f23a00038055 160 while (true) {
samuelmoss 0:f23a00038055 161
samuelmoss 0:f23a00038055 162 /*this is the debouncing loop, debounce only occurs upon state change from occupied
samuelmoss 0:f23a00038055 163 to empty so as to detect and ignore temporary shifting in the occupants position causing the system to believe the state has changed */
samuelmoss 0:f23a00038055 164
samuelmoss 0:f23a00038055 165 while(buttonState != IDLE)
samuelmoss 0:f23a00038055 166 {
samuelmoss 0:f23a00038055 167 if((buttonState == PRESSED)&&(debounceTimer.read_ms() > 200))
samuelmoss 0:f23a00038055 168 {
samuelmoss 0:f23a00038055 169
samuelmoss 0:f23a00038055 170 led2=buttonState;
samuelmoss 0:f23a00038055 171 buttonServicePtr->updateButtonState(buttonState);
samuelmoss 0:f23a00038055 172 buttonState = IDLE;
samuelmoss 0:f23a00038055 173
samuelmoss 0:f23a00038055 174 }
samuelmoss 0:f23a00038055 175
samuelmoss 0:f23a00038055 176 else if ((buttonState == RELEASED) && (debounceTimer.read_ms()>2000)) //debounce timer set to 2 seconds, can be adjusted for more realtime or reliable updates
samuelmoss 0:f23a00038055 177 {
samuelmoss 0:f23a00038055 178
samuelmoss 0:f23a00038055 179 led2=buttonState;
samuelmoss 0:f23a00038055 180 buttonServicePtr->updateButtonState(buttonState);
samuelmoss 0:f23a00038055 181 buttonState = IDLE;
samuelmoss 0:f23a00038055 182
samuelmoss 0:f23a00038055 183 }
samuelmoss 0:f23a00038055 184 }
samuelmoss 0:f23a00038055 185
samuelmoss 0:f23a00038055 186 ble.waitForEvent();
samuelmoss 0:f23a00038055 187
samuelmoss 0:f23a00038055 188 }
samuelmoss 0:f23a00038055 189 }
samuelmoss 0:f23a00038055 190
samuelmoss 0:f23a00038055 191