Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "ble/BLE.h" 00019 #include "ButtonService.h" 00020 00021 Serial pc(USBTX, USBRX); 00022 DigitalOut led1(LED1); //sets led1 to digital out for connection status indicator 00023 DigitalOut led2(LED2); //sets led1 to digital out for seat status indicator 00024 InterruptIn button(P0_7); //sets Pin 7 to an interrupt enabled input for chair status updating 00025 00026 const static char DEVICE_NAME[] = "Seat Sensor"; //specifies name of the device to be found 00027 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; 00028 00029 Ticker ticker; //defines a ticker for periodic callback for connection indicator 00030 Timer debounceTimer; //defines a timer for the debouncing loop 00031 int pin = P0_7; 00032 BLE ble; 00033 00034 enum { //an enumerator defining the three possible output states. Chair empty(RELEASED) =0. Chair OCCUPIED(PRESSED) = 1. Chair default(IDLE) = 2. 00035 RELEASED = 0, 00036 PRESSED, 00037 IDLE 00038 }; 00039 00040 static uint8_t buttonState = IDLE; //initialised buttonState(Chair state) to the default(IDLE) 00041 static ButtonService *buttonServicePtr; 00042 00043 /*function is called from the ISR when the chair becomes occupied, resetting the debounce timer and changing button state*/ 00044 void buttonPressedCallback(void) 00045 { 00046 00047 debounceTimer.reset(); 00048 buttonState = PRESSED; 00049 00050 } 00051 /*function is called from the ISR when the chair becomes empty, resetting the debounce timer and changing button state*/ 00052 void buttonReleasedCallback(void) 00053 { 00054 00055 debounceTimer.reset(); 00056 buttonState = RELEASED; 00057 00058 } 00059 00060 00061 /* upon disconnecting advertising process is repeated */ 00062 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00063 { 00064 00065 BLE::Instance().gap().startAdvertising(); 00066 00067 } 00068 00069 /* on connection state is outputted updating firefly */ 00070 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00071 { 00072 00073 // buttonServicePtr->updateButtonState(button.read()); 00074 00075 00076 } 00077 00078 /* periodic callback for indicating state of the board */ 00079 void periodicCallback(void) 00080 { 00081 if(ble.getGapState().connected == 0) { 00082 led1=!led1; 00083 } else { 00084 led1= 1; 00085 } 00086 00087 } 00088 00089 /** 00090 * This function is called when the ble initialization process has failled 00091 */ 00092 void onBleInitError(BLE &ble, ble_error_t error) 00093 { 00094 /* Initialization error handling should go here */ 00095 } 00096 00097 /** 00098 * Callback triggered when the ble initialization process has finished 00099 */ 00100 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00101 { 00102 BLE& ble = params->ble; 00103 ble_error_t error = params->error; 00104 00105 if (error != BLE_ERROR_NONE) { 00106 /* In case of error, forward the error handling to onBleInitError */ 00107 onBleInitError(ble, error); 00108 return; 00109 } 00110 00111 00112 /* Ensure that it is the default instance of BLE */ 00113 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00114 return; 00115 } 00116 00117 ble.gap().onDisconnection(disconnectionCallback); 00118 ble.gap().onConnection(connectionCallback); 00119 00120 /* Setup primary service */ 00121 buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */); 00122 00123 /* setup advertising */ 00124 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00125 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00126 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00127 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00128 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ 00129 ble.gap().startAdvertising(); 00130 00131 } 00132 00133 int main(void) 00134 { 00135 00136 debounceTimer.start(); //initialises the debounce timer 00137 ticker.attach(periodicCallback, 1); // periodic ticker indicating the board it setup and running 00138 button.fall(buttonPressedCallback); //interrupt call for chair occupied 00139 button.rise(buttonReleasedCallback);//interrupt call for chair empty 00140 00141 BLE &ble = BLE::Instance(); 00142 ble.init(bleInitComplete); 00143 00144 00145 while (ble.hasInitialized() == false) 00146 { 00147 /* spin loop */ 00148 } 00149 00150 /* updates state to the firefly upon mbed board booting */ 00151 if (button.read() == 1) { 00152 buttonServicePtr->updateButtonState(1); 00153 led2 = !button.read(); 00154 } else { 00155 buttonServicePtr->updateButtonState(0); 00156 led2 = !button.read(); 00157 } 00158 00159 /*main while loop*/ 00160 while (true) { 00161 00162 /*this is the debouncing loop, debounce only occurs upon state change from occupied 00163 to empty so as to detect and ignore temporary shifting in the occupants position causing the system to believe the state has changed */ 00164 00165 while(buttonState != IDLE) 00166 { 00167 if((buttonState == PRESSED)&&(debounceTimer.read_ms() > 200)) 00168 { 00169 00170 led2=buttonState; 00171 buttonServicePtr->updateButtonState(buttonState); 00172 buttonState = IDLE; 00173 00174 } 00175 00176 else if ((buttonState == RELEASED) && (debounceTimer.read_ms()>2000)) //debounce timer set to 2 seconds, can be adjusted for more realtime or reliable updates 00177 { 00178 00179 led2=buttonState; 00180 buttonServicePtr->updateButtonState(buttonState); 00181 buttonState = IDLE; 00182 00183 } 00184 } 00185 00186 ble.waitForEvent(); 00187 00188 } 00189 } 00190 00191
Generated on Tue Aug 9 2022 03:07:37 by
1.7.2