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: mbedtls mbed BLE_API nRF51822 AccelSensor
main.cpp
00001 #include "mbed.h" 00002 #include "ble/BLE.h" 00003 #include "RELAYService.h" 00004 #include "ALARMService.h" 00005 #include "BatteryService.h" 00006 #include "InternalValuesService.h" 00007 #include "ImobStateService.h" 00008 #include "AccelSensorService.h" 00009 00010 #define TIME_CICLE 80.0 //ms 00011 #define ANALOGIN 3 00012 #define DISCONNECTION_TIME (1000.0/TIME_CICLE)*10.0 // seg 00013 #define AUTHENTICATION_TIME (1000.0/TIME_CICLE)*25.0 // seg 00014 00015 00016 /* LED aliveness indicator system */ 00017 DigitalOut alivenessLED(P0_18, 0); // P0_25 00018 00019 /* battery charge level, Pin P0_1 */ 00020 AnalogIn batteryCharge(P0_1); 00021 /* lipo charger Status, Pin P0_2 */ 00022 AnalogIn lcStat(P0_2); 00023 /* Chack contact, Pin P0_6 */ 00024 AnalogIn contact(P0_6); 00025 00026 00027 /* Device name setting to identifying your device */ 00028 const static char DEVICE_NAME[] = "I-Mob"; 00029 static const uint16_t uuid16_list[] = {ImobStateService::IMOB_STATE_SERVICE_UUID, RELAYService::RELAY_SERVICE_UUID, ALARMService::ALARM_SERVICE_UUID, InternalValuesService::INTERNAL_VALUES_SERVICE_UUID, AccelSensorService::ACCEL_SENSOR_SERVICE_UUID, GattService::UUID_BATTERY_SERVICE}; 00030 00031 00032 ImobStateService * imobStateServicePtr; 00033 00034 InternalValuesService * internalValuesServicePtr; 00035 RELAYService * relayServicePtr; 00036 ALARMService * alarmServicePtr; 00037 AccelSensorService * accelSensorServicePtr; 00038 BatteryService * batteryServicePtr; 00039 00040 float lipochargerState = 0; 00041 float contactState = -1; 00042 uint8_t batteryLevel = 0; 00043 00044 uint8_t selectedAnalogIn = 0; 00045 00046 uint32_t forceDisconnectionCounter = 0; 00047 uint32_t forceActivationCounter = 0; 00048 00049 /* Calibration variables */ 00050 bool batteryLevelCalibration = false; 00051 float batteryLevelConstant = 100.0f; 00052 float contactStateThreshold = 0.21f; 00053 /* ----- */ 00054 00055 Ticker ticker; 00056 00057 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00058 { 00059 /* Re-enable advertisements after a connection teardown */ 00060 BLE::Instance().gap().startAdvertising(); 00061 } 00062 00063 void periodicCallback(void) 00064 { 00065 /* Do blinky on LED1 to indicate system aliveness. */ 00066 alivenessLED = !alivenessLED; 00067 } 00068 00069 /** 00070 * This function is called when the ble initialization process has failed 00071 */ 00072 void onBleInitError(BLE &ble, ble_error_t error) 00073 { 00074 /* Initialization error handling should go here */ 00075 } 00076 00077 /** 00078 * Callback triggered when the ble initialization process has finished 00079 */ 00080 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00081 { 00082 BLE& ble = params->ble; 00083 ble_error_t error = params->error; 00084 00085 if (error != BLE_ERROR_NONE) 00086 { 00087 /* In case of error, forward the error handling to onBleInitError */ 00088 onBleInitError(ble, error); 00089 return; 00090 } 00091 00092 /* Ensure that it is the default instance of BLE */ 00093 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) 00094 { 00095 return; 00096 } 00097 00098 ble.gap().onDisconnection(disconnectionCallback); 00099 00100 imobStateServicePtr = new ImobStateService(ble); 00101 00102 internalValuesServicePtr = new InternalValuesService(ble, imobStateServicePtr); 00103 relayServicePtr = new RELAYService(ble, imobStateServicePtr); 00104 alarmServicePtr = new ALARMService(ble); 00105 accelSensorServicePtr = new AccelSensorService(ble); 00106 batteryServicePtr = new BatteryService(ble, batteryLevel); 00107 00108 /* setup advertising */ 00109 00110 /* BREDR_NOT_SUPPORTED means classic bluetooth not supported; 00111 * LE_GENERAL_DISCOVERABLE means that this peripheral can be discovered by any BLE scanner--i.e. any phone. 00112 */ 00113 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00114 /* Adding the RELAY service UUID to the advertising payload*/ 00115 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00116 /* This is where we're collecting the device name into the advertisement payload. */ 00117 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00118 /* We'd like for this BLE peripheral to be connectable. */ 00119 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00120 /* set the interval at which advertisements are sent out, 1000ms. */ 00121 ble.gap().setAdvertisingInterval(TIME_CICLE); 00122 /* we're finally good to go with advertisements. */ 00123 ble.gap().startAdvertising(); 00124 00125 } 00126 00127 int main(void) 00128 { 00129 /* Setting up a callback to go at an interval of 1s. */ 00130 ticker.attach(periodicCallback, TIME_CICLE/1000.0); 00131 00132 /* initialize the BLE stack and controller. */ 00133 BLE &ble = BLE::Instance(); 00134 ble.init(bleInitComplete); 00135 ble.setTxPower(4);//change power tx 00136 00137 /* Accelerometer functions 00138 accelerometer.init(); 00139 accelerometer.standby(); 00140 accelerometer.active(); 00141 int accel[3]; 00142 accelerometer.readData(accel); 00143 accelerometer.standby(); 00144 */ 00145 00146 /* SpinWait for initialization to complete. This is necessary because the 00147 * BLE object is used in the main loop below. */ 00148 while (ble.hasInitialized() == false) { /* spin loop */ } 00149 00150 while (true) 00151 { 00152 /* this will return upon any system event (such as an interrupt or a ticker wakeup) */ 00153 ble.waitForEvent(); 00154 bool accel = accelSensorServicePtr->updateAccelDetection(); 00155 00156 /* Update battery charge level */ 00157 if (selectedAnalogIn == 0) 00158 { 00159 batteryLevel = (uint8_t)(batteryCharge.read()*batteryLevelConstant); 00160 batteryServicePtr->updateBatteryLevel(batteryLevel); 00161 } 00162 /* Update lipo charger state */ 00163 if (selectedAnalogIn == 1) 00164 { 00165 lipochargerState = lcStat.read(); 00166 uint8_t aux_lipochargerState; 00167 00168 uint8_t pass_lipochargerState = internalValuesServicePtr->getLipoChargerState(); 00169 00170 if (lipochargerState > 0.4) 00171 { 00172 if (lipochargerState > 0.9) 00173 { 00174 aux_lipochargerState = 1; 00175 if (pass_lipochargerState == 0) internalValuesServicePtr->updateChargeProgramCyclesCharacteristic(); 00176 } 00177 else 00178 { 00179 aux_lipochargerState = 2; 00180 if (activated && accel) alarmServicePtr->updateAlarmState(1); 00181 } 00182 } 00183 else 00184 { 00185 aux_lipochargerState = 0; 00186 if (pass_lipochargerState == 1) internalValuesServicePtr->updateDischargeProgramCyclesCharacteristic(); 00187 } 00188 00189 if(!batteryLevelCalibration && aux_lipochargerState == 1) 00190 { 00191 batteryLevelConstant *= 100.0f/batteryLevel; 00192 batteryLevelCalibration = true; 00193 } 00194 00195 internalValuesServicePtr->updateLipoChargerState(aux_lipochargerState); 00196 } 00197 00198 if (internalValuesServicePtr->getLipoChargerState() == 0) internalValuesServicePtr->incrementChargeProgramCycles(); 00199 else if (internalValuesServicePtr->getLipoChargerState() == 1) internalValuesServicePtr->incrementDischargeProgramCycles(); 00200 00201 /* Update contact state */ 00202 if (selectedAnalogIn == 2) 00203 { 00204 contactState = contact.read(); 00205 uint8_t aux_contactState; 00206 00207 if (contactState > contactStateThreshold) 00208 { 00209 aux_contactState = 1; 00210 00211 if (!authenticated && activated) 00212 relayServicePtr->activate(); 00213 } 00214 else 00215 aux_contactState = 0; 00216 00217 internalValuesServicePtr->updateContactState(aux_contactState); 00218 } 00219 00220 selectedAnalogIn++; 00221 00222 if (selectedAnalogIn == ANALOGIN) selectedAnalogIn = 0; 00223 00224 if (!authenticated && userIsConnected) 00225 { 00226 if (forceDisconnectionCounter > DISCONNECTION_TIME) 00227 { 00228 forceDisconnectionCounter = 0; 00229 Gap::DisconnectionReason_t res=Gap::LOCAL_HOST_TERMINATED_CONNECTION; 00230 ble.disconnect(res); 00231 } 00232 else 00233 forceDisconnectionCounter++; 00234 } 00235 else 00236 forceDisconnectionCounter = 0; 00237 00238 if ( !initial_activation && !userIsConnected) 00239 if (forceActivationCounter > AUTHENTICATION_TIME) 00240 { 00241 forceActivationCounter = 0; 00242 imobStateServicePtr->updateActivationValue(1); 00243 initial_activation = true; 00244 } 00245 else 00246 forceActivationCounter++; 00247 00248 00249 } 00250 }
Generated on Mon Dec 12 2022 13:13:31 by
1.7.2