51 52 with same code
Dependencies: MtSense06
Fork of MtConnect04S_MtSense06 by
source/main.cpp@3:cab2817f6c40, 2018-04-27 (annotated)
- Committer:
- johnathanlyu
- Date:
- Fri Apr 27 09:59:38 2018 +0000
- Revision:
- 3:cab2817f6c40
update library flow
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnathanlyu | 3:cab2817f6c40 | 1 | /* Copyright (c) 2016 MtM Technology Corporation, MIT License |
johnathanlyu | 3:cab2817f6c40 | 2 | * |
johnathanlyu | 3:cab2817f6c40 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
johnathanlyu | 3:cab2817f6c40 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
johnathanlyu | 3:cab2817f6c40 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
johnathanlyu | 3:cab2817f6c40 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
johnathanlyu | 3:cab2817f6c40 | 7 | * furnished to do so, subject to the following conditions: |
johnathanlyu | 3:cab2817f6c40 | 8 | * |
johnathanlyu | 3:cab2817f6c40 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
johnathanlyu | 3:cab2817f6c40 | 10 | * substantial portions of the Software. |
johnathanlyu | 3:cab2817f6c40 | 11 | * |
johnathanlyu | 3:cab2817f6c40 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
johnathanlyu | 3:cab2817f6c40 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
johnathanlyu | 3:cab2817f6c40 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
johnathanlyu | 3:cab2817f6c40 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
johnathanlyu | 3:cab2817f6c40 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
johnathanlyu | 3:cab2817f6c40 | 17 | */ |
johnathanlyu | 3:cab2817f6c40 | 18 | #include <events/mbed_events.h> |
johnathanlyu | 3:cab2817f6c40 | 19 | #include <mbed.h> |
johnathanlyu | 3:cab2817f6c40 | 20 | #include "ble/BLE.h" |
johnathanlyu | 3:cab2817f6c40 | 21 | #include "ble/Gap.h" |
johnathanlyu | 3:cab2817f6c40 | 22 | #include "ble/services/BatteryService.h" |
johnathanlyu | 3:cab2817f6c40 | 23 | #include "ble/services/DeviceInformationService.h" |
johnathanlyu | 3:cab2817f6c40 | 24 | #include "PulseOximeterService.h" |
johnathanlyu | 3:cab2817f6c40 | 25 | #include "M1.h" |
johnathanlyu | 3:cab2817f6c40 | 26 | |
johnathanlyu | 3:cab2817f6c40 | 27 | /* |
johnathanlyu | 3:cab2817f6c40 | 28 | * set 0 disable M1 data send to uart |
johnathanlyu | 3:cab2817f6c40 | 29 | * 1 enable M1 data send to urat |
johnathanlyu | 3:cab2817f6c40 | 30 | */ |
johnathanlyu | 3:cab2817f6c40 | 31 | #define M1_UART_RAW_DATA_ENABLE 0 |
johnathanlyu | 3:cab2817f6c40 | 32 | |
johnathanlyu | 3:cab2817f6c40 | 33 | DigitalOut ledRed(p16, 1); |
johnathanlyu | 3:cab2817f6c40 | 34 | #ifdef NRF52 |
johnathanlyu | 3:cab2817f6c40 | 35 | Serial pc(p20, p24); |
johnathanlyu | 3:cab2817f6c40 | 36 | #else |
johnathanlyu | 3:cab2817f6c40 | 37 | Serial pc(p5, p4); |
johnathanlyu | 3:cab2817f6c40 | 38 | #endif |
johnathanlyu | 3:cab2817f6c40 | 39 | I2C i2c(p3, p2); |
johnathanlyu | 3:cab2817f6c40 | 40 | M1 m1(i2c); |
johnathanlyu | 3:cab2817f6c40 | 41 | |
johnathanlyu | 3:cab2817f6c40 | 42 | const static char DEVICE_NAME[] = "Mt5MtSense06"; |
johnathanlyu | 3:cab2817f6c40 | 43 | static const uint16_t uuid16_list[] = { 0x1822, /* UUID_PULSE_OXIMETER_SERVICE */ |
johnathanlyu | 3:cab2817f6c40 | 44 | GattService::UUID_BATTERY_SERVICE, |
johnathanlyu | 3:cab2817f6c40 | 45 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
johnathanlyu | 3:cab2817f6c40 | 46 | |
johnathanlyu | 3:cab2817f6c40 | 47 | static PulseOximeterService* plxServicePtr; |
johnathanlyu | 3:cab2817f6c40 | 48 | static BatteryService* batteryServicePtr; |
johnathanlyu | 3:cab2817f6c40 | 49 | |
johnathanlyu | 3:cab2817f6c40 | 50 | static EventQueue eventQueue( |
johnathanlyu | 3:cab2817f6c40 | 51 | /* event count */ 16 * /* event size */ 32 |
johnathanlyu | 3:cab2817f6c40 | 52 | ); |
johnathanlyu | 3:cab2817f6c40 | 53 | |
johnathanlyu | 3:cab2817f6c40 | 54 | |
johnathanlyu | 3:cab2817f6c40 | 55 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
johnathanlyu | 3:cab2817f6c40 | 56 | { |
johnathanlyu | 3:cab2817f6c40 | 57 | BLE::Instance().gap().startAdvertising(); |
johnathanlyu | 3:cab2817f6c40 | 58 | } |
johnathanlyu | 3:cab2817f6c40 | 59 | |
johnathanlyu | 3:cab2817f6c40 | 60 | void blinkCallback(void) |
johnathanlyu | 3:cab2817f6c40 | 61 | { |
johnathanlyu | 3:cab2817f6c40 | 62 | ledRed = !ledRed; |
johnathanlyu | 3:cab2817f6c40 | 63 | } |
johnathanlyu | 3:cab2817f6c40 | 64 | |
johnathanlyu | 3:cab2817f6c40 | 65 | void plxCallback(void) |
johnathanlyu | 3:cab2817f6c40 | 66 | { |
johnathanlyu | 3:cab2817f6c40 | 67 | M1::Plx plx; |
johnathanlyu | 3:cab2817f6c40 | 68 | |
johnathanlyu | 3:cab2817f6c40 | 69 | BLE &ble = BLE::Instance(); |
johnathanlyu | 3:cab2817f6c40 | 70 | if (ble.gap().getState().connected && m1.IsSkinIn()) { |
johnathanlyu | 3:cab2817f6c40 | 71 | m1.GetPlx(&plx); |
johnathanlyu | 3:cab2817f6c40 | 72 | plxServicePtr->updatePlxMeas((float)plx.spo2, (float)plx.pulseRate); |
johnathanlyu | 3:cab2817f6c40 | 73 | } |
johnathanlyu | 3:cab2817f6c40 | 74 | } |
johnathanlyu | 3:cab2817f6c40 | 75 | |
johnathanlyu | 3:cab2817f6c40 | 76 | void rawCallback(void) |
johnathanlyu | 3:cab2817f6c40 | 77 | { |
johnathanlyu | 3:cab2817f6c40 | 78 | M1::Raw raw; |
johnathanlyu | 3:cab2817f6c40 | 79 | static uint8_t cnt = 0; |
johnathanlyu | 3:cab2817f6c40 | 80 | static uint8_t raw_x3[6*3]; |
johnathanlyu | 3:cab2817f6c40 | 81 | |
johnathanlyu | 3:cab2817f6c40 | 82 | BLE &ble = BLE::Instance(); |
johnathanlyu | 3:cab2817f6c40 | 83 | if (ble.gap().getState().connected && m1.IsSkinIn()) { |
johnathanlyu | 3:cab2817f6c40 | 84 | m1.GetRaw(&raw); |
johnathanlyu | 3:cab2817f6c40 | 85 | |
johnathanlyu | 3:cab2817f6c40 | 86 | raw_x3[6*cnt+0] = (uint8_t)(raw.red >> 8); |
johnathanlyu | 3:cab2817f6c40 | 87 | raw_x3[6*cnt+1] = (uint8_t)(raw.red >> 0); |
johnathanlyu | 3:cab2817f6c40 | 88 | raw_x3[6*cnt+2] = (uint8_t)(raw.ir >> 8); |
johnathanlyu | 3:cab2817f6c40 | 89 | raw_x3[6*cnt+3] = (uint8_t)(raw.ir >> 0); |
johnathanlyu | 3:cab2817f6c40 | 90 | raw_x3[6*cnt+4] = (uint8_t)(raw.green >> 8); |
johnathanlyu | 3:cab2817f6c40 | 91 | raw_x3[6*cnt+5] = (uint8_t)(raw.green >> 0); |
johnathanlyu | 3:cab2817f6c40 | 92 | |
johnathanlyu | 3:cab2817f6c40 | 93 | if (++cnt >= 3) { |
johnathanlyu | 3:cab2817f6c40 | 94 | plxServicePtr->updateRaw(raw_x3); |
johnathanlyu | 3:cab2817f6c40 | 95 | cnt = 0; |
johnathanlyu | 3:cab2817f6c40 | 96 | } |
johnathanlyu | 3:cab2817f6c40 | 97 | } else { |
johnathanlyu | 3:cab2817f6c40 | 98 | cnt = 0; |
johnathanlyu | 3:cab2817f6c40 | 99 | } |
johnathanlyu | 3:cab2817f6c40 | 100 | } |
johnathanlyu | 3:cab2817f6c40 | 101 | |
johnathanlyu | 3:cab2817f6c40 | 102 | void rawCallbackToUART() { |
johnathanlyu | 3:cab2817f6c40 | 103 | M1::Raw raw; |
johnathanlyu | 3:cab2817f6c40 | 104 | if(m1.IsSkinIn()) { |
johnathanlyu | 3:cab2817f6c40 | 105 | m1.GetRaw(&raw); |
johnathanlyu | 3:cab2817f6c40 | 106 | pc.printf("%d,%d,%d\r\n",raw.red, raw.ir, raw.green); |
johnathanlyu | 3:cab2817f6c40 | 107 | } |
johnathanlyu | 3:cab2817f6c40 | 108 | } |
johnathanlyu | 3:cab2817f6c40 | 109 | |
johnathanlyu | 3:cab2817f6c40 | 110 | void StatusCallback(void) |
johnathanlyu | 3:cab2817f6c40 | 111 | { |
johnathanlyu | 3:cab2817f6c40 | 112 | M1::Status sta; |
johnathanlyu | 3:cab2817f6c40 | 113 | |
johnathanlyu | 3:cab2817f6c40 | 114 | BLE &ble = BLE::Instance(); |
johnathanlyu | 3:cab2817f6c40 | 115 | if (ble.gap().getState().connected) { |
johnathanlyu | 3:cab2817f6c40 | 116 | m1.GetStatus(&sta); |
johnathanlyu | 3:cab2817f6c40 | 117 | plxServicePtr->updateStatusFlagAndSignalQuality(sta.flags, sta.signalQuality); |
johnathanlyu | 3:cab2817f6c40 | 118 | } |
johnathanlyu | 3:cab2817f6c40 | 119 | } |
johnathanlyu | 3:cab2817f6c40 | 120 | |
johnathanlyu | 3:cab2817f6c40 | 121 | void batteryCallback(void ) |
johnathanlyu | 3:cab2817f6c40 | 122 | { |
johnathanlyu | 3:cab2817f6c40 | 123 | uint8_t batteryLevel = 99; |
johnathanlyu | 3:cab2817f6c40 | 124 | |
johnathanlyu | 3:cab2817f6c40 | 125 | BLE &ble = BLE::Instance(); |
johnathanlyu | 3:cab2817f6c40 | 126 | if (ble.gap().getState().connected) { |
johnathanlyu | 3:cab2817f6c40 | 127 | batteryServicePtr->updateBatteryLevel(batteryLevel); |
johnathanlyu | 3:cab2817f6c40 | 128 | } |
johnathanlyu | 3:cab2817f6c40 | 129 | } |
johnathanlyu | 3:cab2817f6c40 | 130 | |
johnathanlyu | 3:cab2817f6c40 | 131 | /** |
johnathanlyu | 3:cab2817f6c40 | 132 | * This function is called when the ble initialization process has failled |
johnathanlyu | 3:cab2817f6c40 | 133 | */ |
johnathanlyu | 3:cab2817f6c40 | 134 | void onBleInitError(BLE &ble, ble_error_t error) |
johnathanlyu | 3:cab2817f6c40 | 135 | { |
johnathanlyu | 3:cab2817f6c40 | 136 | /* Initialization error handling should go here */ |
johnathanlyu | 3:cab2817f6c40 | 137 | } |
johnathanlyu | 3:cab2817f6c40 | 138 | |
johnathanlyu | 3:cab2817f6c40 | 139 | /** |
johnathanlyu | 3:cab2817f6c40 | 140 | * Callback triggered when the ble initialization process has finished |
johnathanlyu | 3:cab2817f6c40 | 141 | */ |
johnathanlyu | 3:cab2817f6c40 | 142 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
johnathanlyu | 3:cab2817f6c40 | 143 | { |
johnathanlyu | 3:cab2817f6c40 | 144 | BLE& ble = params->ble; |
johnathanlyu | 3:cab2817f6c40 | 145 | ble_error_t error = params->error; |
johnathanlyu | 3:cab2817f6c40 | 146 | |
johnathanlyu | 3:cab2817f6c40 | 147 | if (error != BLE_ERROR_NONE) { |
johnathanlyu | 3:cab2817f6c40 | 148 | /* In case of error, forward the error handling to onBleInitError */ |
johnathanlyu | 3:cab2817f6c40 | 149 | onBleInitError(ble, error); |
johnathanlyu | 3:cab2817f6c40 | 150 | return; |
johnathanlyu | 3:cab2817f6c40 | 151 | } |
johnathanlyu | 3:cab2817f6c40 | 152 | |
johnathanlyu | 3:cab2817f6c40 | 153 | /* Ensure that it is the default instance of BLE */ |
johnathanlyu | 3:cab2817f6c40 | 154 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
johnathanlyu | 3:cab2817f6c40 | 155 | return; |
johnathanlyu | 3:cab2817f6c40 | 156 | } |
johnathanlyu | 3:cab2817f6c40 | 157 | |
johnathanlyu | 3:cab2817f6c40 | 158 | ble.gap().onDisconnection(disconnectionCallback); |
johnathanlyu | 3:cab2817f6c40 | 159 | |
johnathanlyu | 3:cab2817f6c40 | 160 | /* Setup primary service */ |
johnathanlyu | 3:cab2817f6c40 | 161 | plxServicePtr = new PulseOximeterService(ble, 0, 0); |
johnathanlyu | 3:cab2817f6c40 | 162 | batteryServicePtr = new BatteryService(ble, 100); |
johnathanlyu | 3:cab2817f6c40 | 163 | DeviceInformationService deviceInfo(ble, "MtM"); |
johnathanlyu | 3:cab2817f6c40 | 164 | |
johnathanlyu | 3:cab2817f6c40 | 165 | /* Setup advertising */ |
johnathanlyu | 3:cab2817f6c40 | 166 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
johnathanlyu | 3:cab2817f6c40 | 167 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); |
johnathanlyu | 3:cab2817f6c40 | 168 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
johnathanlyu | 3:cab2817f6c40 | 169 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::PULSE_OXIMETER_GENERIC); |
johnathanlyu | 3:cab2817f6c40 | 170 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
johnathanlyu | 3:cab2817f6c40 | 171 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
johnathanlyu | 3:cab2817f6c40 | 172 | ble.gap().startAdvertising(); |
johnathanlyu | 3:cab2817f6c40 | 173 | } |
johnathanlyu | 3:cab2817f6c40 | 174 | |
johnathanlyu | 3:cab2817f6c40 | 175 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
johnathanlyu | 3:cab2817f6c40 | 176 | BLE &ble = BLE::Instance(); |
johnathanlyu | 3:cab2817f6c40 | 177 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
johnathanlyu | 3:cab2817f6c40 | 178 | } |
johnathanlyu | 3:cab2817f6c40 | 179 | |
johnathanlyu | 3:cab2817f6c40 | 180 | int main() |
johnathanlyu | 3:cab2817f6c40 | 181 | { |
johnathanlyu | 3:cab2817f6c40 | 182 | /* Disable the hardware flow control of Serial, then show the mbed version */ |
johnathanlyu | 3:cab2817f6c40 | 183 | pc.set_flow_control(SerialBase::Disabled); |
johnathanlyu | 3:cab2817f6c40 | 184 | pc.baud(115200); |
johnathanlyu | 3:cab2817f6c40 | 185 | pc.printf("\r\n"); |
johnathanlyu | 3:cab2817f6c40 | 186 | pc.printf("mbed version(%d.%d.%d)\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
johnathanlyu | 3:cab2817f6c40 | 187 | pc.printf("\r\n"); |
johnathanlyu | 3:cab2817f6c40 | 188 | |
johnathanlyu | 3:cab2817f6c40 | 189 | /* Config device */ |
johnathanlyu | 3:cab2817f6c40 | 190 | m1.ConfigDevice(); |
johnathanlyu | 3:cab2817f6c40 | 191 | |
johnathanlyu | 3:cab2817f6c40 | 192 | #if M1_UART_RAW_DATA_ENABLE |
johnathanlyu | 3:cab2817f6c40 | 193 | /* 35ms getter raw data to uart */ |
johnathanlyu | 3:cab2817f6c40 | 194 | eventQueue.call_every(35, rawCallbackToUART); |
johnathanlyu | 3:cab2817f6c40 | 195 | eventQueue.call_every(500, blinkCallback); |
johnathanlyu | 3:cab2817f6c40 | 196 | #else |
johnathanlyu | 3:cab2817f6c40 | 197 | /* Update each data every N ms */ |
johnathanlyu | 3:cab2817f6c40 | 198 | eventQueue.call_every(1000, plxCallback); |
johnathanlyu | 3:cab2817f6c40 | 199 | eventQueue.call_every(35, rawCallback); |
johnathanlyu | 3:cab2817f6c40 | 200 | eventQueue.call_every(1000, StatusCallback); |
johnathanlyu | 3:cab2817f6c40 | 201 | eventQueue.call_every(500, batteryCallback); |
johnathanlyu | 3:cab2817f6c40 | 202 | eventQueue.call_every(500, blinkCallback); |
johnathanlyu | 3:cab2817f6c40 | 203 | #endif |
johnathanlyu | 3:cab2817f6c40 | 204 | |
johnathanlyu | 3:cab2817f6c40 | 205 | BLE &ble = BLE::Instance(); |
johnathanlyu | 3:cab2817f6c40 | 206 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
johnathanlyu | 3:cab2817f6c40 | 207 | ble.init(bleInitComplete); |
johnathanlyu | 3:cab2817f6c40 | 208 | |
johnathanlyu | 3:cab2817f6c40 | 209 | |
johnathanlyu | 3:cab2817f6c40 | 210 | eventQueue.dispatch_forever(); |
johnathanlyu | 3:cab2817f6c40 | 211 | |
johnathanlyu | 3:cab2817f6c40 | 212 | return 0; |
johnathanlyu | 3:cab2817f6c40 | 213 | } |