MtM+ / Mbed OS Mt05_MtSense06

Dependencies:   MtSense06

Fork of MtConnect04S_MtSense06 by MtM+

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (c) 2016 MtM Technology Corporation, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 #include <events/mbed_events.h>
00019 #include <mbed.h>
00020 #include "ble/BLE.h"
00021 #include "ble/Gap.h"
00022 #include "ble/services/BatteryService.h"
00023 #include "ble/services/DeviceInformationService.h"
00024 #include "PulseOximeterService.h"
00025 #include "M1.h"
00026 
00027 /* 
00028  * set 0 disable M1 data send to uart
00029  *     1 enable M1 data send to urat
00030  */
00031 #define M1_UART_RAW_DATA_ENABLE     0
00032 
00033 DigitalOut ledRed(p16, 1);
00034 #ifdef NRF52
00035 Serial pc(p20, p24);
00036 #else
00037 Serial pc(p5, p4);
00038 #endif
00039 I2C i2c(p3, p2);
00040 M1 m1(i2c);
00041 
00042 const static char     DEVICE_NAME[] = "Mt5MtSense06";
00043 static const uint16_t uuid16_list[] = { 0x1822, /* UUID_PULSE_OXIMETER_SERVICE */
00044                                         GattService::UUID_BATTERY_SERVICE,
00045                                         GattService::UUID_DEVICE_INFORMATION_SERVICE};
00046 
00047 static PulseOximeterService* plxServicePtr;
00048 static BatteryService* batteryServicePtr;
00049 
00050 static EventQueue eventQueue(
00051     /* event count */ 16 * /* event size */ 32
00052 );
00053 
00054 
00055 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00056 {
00057     BLE::Instance().gap().startAdvertising();
00058 }
00059 
00060 void blinkCallback(void)
00061 {
00062     ledRed = !ledRed;
00063 }
00064 
00065 void plxCallback(void)
00066 {
00067     M1::Plx plx;
00068     
00069     BLE &ble = BLE::Instance();
00070     if (ble.gap().getState().connected && m1.IsSkinIn()) {
00071         m1.GetPlx(&plx);
00072         plxServicePtr->updatePlxMeas((float)plx.spo2, (float)plx.pulseRate);
00073     }
00074 }
00075 
00076 void rawCallback(void)
00077 {
00078     M1::Raw raw;
00079     static uint8_t cnt = 0;
00080     static uint8_t raw_x3[6*3];
00081 
00082     BLE &ble = BLE::Instance();
00083     if (ble.gap().getState().connected && m1.IsSkinIn()) {
00084         m1.GetRaw(&raw);
00085         
00086         raw_x3[6*cnt+0] = (uint8_t)(raw.red   >> 8);
00087         raw_x3[6*cnt+1] = (uint8_t)(raw.red   >> 0);
00088         raw_x3[6*cnt+2] = (uint8_t)(raw.ir    >> 8);
00089         raw_x3[6*cnt+3] = (uint8_t)(raw.ir    >> 0);
00090         raw_x3[6*cnt+4] = (uint8_t)(raw.green >> 8);
00091         raw_x3[6*cnt+5] = (uint8_t)(raw.green >> 0);
00092         
00093         if (++cnt >= 3) {
00094             plxServicePtr->updateRaw(raw_x3);
00095             cnt = 0;
00096         }
00097     } else {
00098         cnt = 0;
00099     }
00100 }
00101 
00102 void rawCallbackToUART() {
00103     M1::Raw raw;
00104     if(m1.IsSkinIn()) {
00105             m1.GetRaw(&raw);  
00106             pc.printf("%d,%d,%d\r\n",raw.red, raw.ir, raw.green);
00107         }   
00108 }
00109 
00110 void StatusCallback(void)
00111 {
00112     M1::Status sta;
00113     
00114     BLE &ble = BLE::Instance();
00115     if (ble.gap().getState().connected) {
00116         m1.GetStatus(&sta);
00117         plxServicePtr->updateStatusFlagAndSignalQuality(sta.flags, sta.signalQuality);
00118     }  
00119 }
00120 
00121 void batteryCallback(void )
00122 {
00123     uint8_t batteryLevel = 99;
00124 
00125     BLE &ble = BLE::Instance();
00126     if (ble.gap().getState().connected) {
00127         batteryServicePtr->updateBatteryLevel(batteryLevel);
00128     }
00129 }
00130 
00131 /**
00132  * This function is called when the ble initialization process has failled
00133  */
00134 void onBleInitError(BLE &ble, ble_error_t error)
00135 {
00136     /* Initialization error handling should go here */
00137 }
00138 
00139 /**
00140  * Callback triggered when the ble initialization process has finished
00141  */
00142 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00143 {
00144     BLE&        ble   = params->ble;
00145     ble_error_t error = params->error;
00146 
00147     if (error != BLE_ERROR_NONE) {
00148         /* In case of error, forward the error handling to onBleInitError */
00149         onBleInitError(ble, error);
00150         return;
00151     }
00152 
00153     /* Ensure that it is the default instance of BLE */
00154     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00155         return;
00156     }
00157 
00158     ble.gap().onDisconnection(disconnectionCallback);
00159 
00160     /* Setup primary service */
00161     plxServicePtr     = new PulseOximeterService(ble, 0, 0);
00162     batteryServicePtr = new BatteryService(ble, 100);
00163     DeviceInformationService deviceInfo(ble, "MtM");
00164 
00165     /* Setup advertising */
00166     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00167     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
00168     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
00169     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::PULSE_OXIMETER_GENERIC);
00170     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00171     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00172     ble.gap().startAdvertising();
00173 }
00174 
00175 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00176     BLE &ble = BLE::Instance();
00177     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00178 }
00179 
00180 int main()
00181 {
00182     /* Disable the hardware flow control of Serial, then show the mbed version */
00183     pc.set_flow_control(SerialBase::Disabled);
00184     pc.baud(115200);
00185     pc.printf("\r\n");
00186     pc.printf("mbed version(%d.%d.%d)\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00187     pc.printf("\r\n");
00188 
00189     /* Config device */
00190     m1.ConfigDevice();
00191 
00192 #if M1_UART_RAW_DATA_ENABLE
00193     /* 35ms getter raw data to uart */
00194     eventQueue.call_every(35,   rawCallbackToUART);
00195     eventQueue.call_every(500,  blinkCallback);
00196 #else
00197     /* Update each data every N ms */
00198     eventQueue.call_every(1000, plxCallback);
00199     eventQueue.call_every(35,   rawCallback);
00200     eventQueue.call_every(1000, StatusCallback);
00201     eventQueue.call_every(500,  batteryCallback);
00202     eventQueue.call_every(500,  blinkCallback);
00203 #endif
00204 
00205     BLE &ble = BLE::Instance();
00206     ble.onEventsToProcess(scheduleBleEventsProcessing);
00207     ble.init(bleInitComplete);
00208    
00209 
00210     eventQueue.dispatch_forever();
00211 
00212     return 0;
00213 }