nrf52 nrf51 with same code

Dependencies:   CCS811

Fork of MtConnect04S_MtSense02 by MtM+

Committer:
johnathanlyu
Date:
Fri Apr 27 09:52:19 2018 +0000
Revision:
2:f3768a226561
Parent:
0:4d76c0bc0f78
add 52 support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnathanlyu 0:4d76c0bc0f78 1 /* mbed Microcontroller Library
johnathanlyu 0:4d76c0bc0f78 2 * Copyright (c) 2006-2014 ARM Limited
johnathanlyu 0:4d76c0bc0f78 3 *
johnathanlyu 0:4d76c0bc0f78 4 * Licensed under the Apache License, Version 2.0 (the "License");
johnathanlyu 0:4d76c0bc0f78 5 * you may not use this file except in compliance with the License.
johnathanlyu 0:4d76c0bc0f78 6 * You may obtain a copy of the License at
johnathanlyu 0:4d76c0bc0f78 7 *
johnathanlyu 0:4d76c0bc0f78 8 * http://www.apache.org/licenses/LICENSE-2.0
johnathanlyu 0:4d76c0bc0f78 9 *
johnathanlyu 0:4d76c0bc0f78 10 * Unless required by applicable law or agreed to in writing, software
johnathanlyu 0:4d76c0bc0f78 11 * distributed under the License is distributed on an "AS IS" BASIS,
johnathanlyu 0:4d76c0bc0f78 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnathanlyu 0:4d76c0bc0f78 13 * See the License for the specific language governing permissions and
johnathanlyu 0:4d76c0bc0f78 14 * limitations under the License.
johnathanlyu 0:4d76c0bc0f78 15 */
johnathanlyu 0:4d76c0bc0f78 16
johnathanlyu 0:4d76c0bc0f78 17 #include <events/mbed_events.h>
johnathanlyu 0:4d76c0bc0f78 18 #include <mbed.h>
johnathanlyu 0:4d76c0bc0f78 19 #include "ble/BLE.h"
johnathanlyu 0:4d76c0bc0f78 20 #include "ble/Gap.h"
johnathanlyu 0:4d76c0bc0f78 21 #include "CCS811.h"
johnathanlyu 0:4d76c0bc0f78 22 #include "AirQualityService.h"
johnathanlyu 0:4d76c0bc0f78 23
johnathanlyu 0:4d76c0bc0f78 24 //initial function pin
johnathanlyu 2:f3768a226561 25 #ifdef NRF52
johnathanlyu 2:f3768a226561 26 Serial pc(p20, p24);
johnathanlyu 2:f3768a226561 27 #else
johnathanlyu 0:4d76c0bc0f78 28 Serial pc(p5, p4);
johnathanlyu 2:f3768a226561 29 #endif
johnathanlyu 2:f3768a226561 30
johnathanlyu 0:4d76c0bc0f78 31 I2C i2c(p3, p2);
johnathanlyu 0:4d76c0bc0f78 32 DigitalOut ledR(p16, 1);
johnathanlyu 2:f3768a226561 33 CCS811 ccs811(i2c, pc);
johnathanlyu 0:4d76c0bc0f78 34
johnathanlyu 0:4d76c0bc0f78 35 static AirQualityService *AQSPtr;
johnathanlyu 0:4d76c0bc0f78 36 static uint16_t eco2, tvoc;
johnathanlyu 0:4d76c0bc0f78 37
johnathanlyu 2:f3768a226561 38 const static char DEVICE_NAME[] = "Mt5MtSense02";
johnathanlyu 0:4d76c0bc0f78 39 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE};
johnathanlyu 0:4d76c0bc0f78 40
johnathanlyu 0:4d76c0bc0f78 41
johnathanlyu 0:4d76c0bc0f78 42 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
johnathanlyu 0:4d76c0bc0f78 43
johnathanlyu 0:4d76c0bc0f78 44 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
johnathanlyu 0:4d76c0bc0f78 45 {
johnathanlyu 0:4d76c0bc0f78 46 BLE::Instance().gap().startAdvertising();
johnathanlyu 0:4d76c0bc0f78 47 }
johnathanlyu 0:4d76c0bc0f78 48
johnathanlyu 0:4d76c0bc0f78 49 void updateSensorValue() {
johnathanlyu 0:4d76c0bc0f78 50
johnathanlyu 0:4d76c0bc0f78 51 ledR = 0;
johnathanlyu 0:4d76c0bc0f78 52
johnathanlyu 0:4d76c0bc0f78 53 uint16_t revertECO2 = (eco2>>8) | (eco2<<8);
johnathanlyu 0:4d76c0bc0f78 54 uint16_t revertTVOC = (tvoc>>8) | (tvoc<<8);
johnathanlyu 0:4d76c0bc0f78 55
johnathanlyu 0:4d76c0bc0f78 56 AQSPtr->updateCO2(revertECO2);
johnathanlyu 0:4d76c0bc0f78 57 AQSPtr->updateTVOC(revertTVOC);
johnathanlyu 0:4d76c0bc0f78 58
johnathanlyu 0:4d76c0bc0f78 59 }
johnathanlyu 0:4d76c0bc0f78 60
johnathanlyu 0:4d76c0bc0f78 61 void blinkCallback(void)
johnathanlyu 0:4d76c0bc0f78 62 {
johnathanlyu 0:4d76c0bc0f78 63 ledR = !ledR; /* Do blinky on LED1 while we're waiting for BLE events */
johnathanlyu 0:4d76c0bc0f78 64 }
johnathanlyu 0:4d76c0bc0f78 65
johnathanlyu 0:4d76c0bc0f78 66 void CCS811Callback(void) {
johnathanlyu 0:4d76c0bc0f78 67
johnathanlyu 0:4d76c0bc0f78 68 ccs811.readData(&eco2, &tvoc);
johnathanlyu 0:4d76c0bc0f78 69
johnathanlyu 0:4d76c0bc0f78 70 BLE &ble = BLE::Instance();
johnathanlyu 0:4d76c0bc0f78 71 if (ble.gap().getState().connected) {
johnathanlyu 0:4d76c0bc0f78 72 eventQueue.call(updateSensorValue);
johnathanlyu 0:4d76c0bc0f78 73 }
johnathanlyu 0:4d76c0bc0f78 74
johnathanlyu 0:4d76c0bc0f78 75 pc.printf("eco2:%8d, tvoc:%8d\r\n", eco2, tvoc);
johnathanlyu 0:4d76c0bc0f78 76
johnathanlyu 0:4d76c0bc0f78 77 }
johnathanlyu 0:4d76c0bc0f78 78
johnathanlyu 0:4d76c0bc0f78 79 /**
johnathanlyu 0:4d76c0bc0f78 80 * This function is called when the ble initialization process has failled
johnathanlyu 0:4d76c0bc0f78 81 */
johnathanlyu 0:4d76c0bc0f78 82 void onBleInitError(BLE &ble, ble_error_t error)
johnathanlyu 0:4d76c0bc0f78 83 {
johnathanlyu 0:4d76c0bc0f78 84 /* Initialization error handling should go here */
johnathanlyu 0:4d76c0bc0f78 85 }
johnathanlyu 0:4d76c0bc0f78 86
johnathanlyu 0:4d76c0bc0f78 87 /**
johnathanlyu 0:4d76c0bc0f78 88 * Callback triggered when the ble initialization process has finished
johnathanlyu 0:4d76c0bc0f78 89 */
johnathanlyu 0:4d76c0bc0f78 90 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
johnathanlyu 0:4d76c0bc0f78 91 {
johnathanlyu 0:4d76c0bc0f78 92 BLE& ble = params->ble;
johnathanlyu 0:4d76c0bc0f78 93 ble_error_t error = params->error;
johnathanlyu 0:4d76c0bc0f78 94
johnathanlyu 0:4d76c0bc0f78 95
johnathanlyu 0:4d76c0bc0f78 96 uint8_t co2CID[16] = {
johnathanlyu 0:4d76c0bc0f78 97 0x00, 'M', 'T', 'M',
johnathanlyu 0:4d76c0bc0f78 98 'C' , 'O', '2', 0xFF,
johnathanlyu 0:4d76c0bc0f78 99 0xFF, 0xFF, 0xFF, 0xFF,
johnathanlyu 0:4d76c0bc0f78 100 0xFF, 0xFF, 0xFF, 0xFF
johnathanlyu 0:4d76c0bc0f78 101 };
johnathanlyu 0:4d76c0bc0f78 102
johnathanlyu 0:4d76c0bc0f78 103 uint8_t tvocCID[16] = {
johnathanlyu 0:4d76c0bc0f78 104 0x00, 'M', 'T', 'M' ,
johnathanlyu 0:4d76c0bc0f78 105 'T' , 'V', 'O', 'C' ,
johnathanlyu 0:4d76c0bc0f78 106 0xFF, 0xFF, 0xFF, 0xFF,
johnathanlyu 0:4d76c0bc0f78 107 0xFF, 0xFF, 0xFF, 0xFF
johnathanlyu 0:4d76c0bc0f78 108 };
johnathanlyu 0:4d76c0bc0f78 109
johnathanlyu 0:4d76c0bc0f78 110 UUID CO2CID (co2CID);
johnathanlyu 0:4d76c0bc0f78 111 UUID TVOCCID(tvocCID);
johnathanlyu 0:4d76c0bc0f78 112
johnathanlyu 0:4d76c0bc0f78 113 AQSPtr = new AirQualityService(ble, CO2CID, TVOCCID);
johnathanlyu 0:4d76c0bc0f78 114
johnathanlyu 0:4d76c0bc0f78 115
johnathanlyu 0:4d76c0bc0f78 116 if (error != BLE_ERROR_NONE) {
johnathanlyu 0:4d76c0bc0f78 117 /* In case of error, forward the error handling to onBleInitError */
johnathanlyu 0:4d76c0bc0f78 118 onBleInitError(ble, error);
johnathanlyu 0:4d76c0bc0f78 119 return;
johnathanlyu 0:4d76c0bc0f78 120 }
johnathanlyu 0:4d76c0bc0f78 121
johnathanlyu 0:4d76c0bc0f78 122 /* Ensure that it is the default instance of BLE */
johnathanlyu 0:4d76c0bc0f78 123 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
johnathanlyu 0:4d76c0bc0f78 124 return;
johnathanlyu 0:4d76c0bc0f78 125 }
johnathanlyu 0:4d76c0bc0f78 126
johnathanlyu 0:4d76c0bc0f78 127 ble.gap().onDisconnection(disconnectionCallback);
johnathanlyu 0:4d76c0bc0f78 128
johnathanlyu 0:4d76c0bc0f78 129 /* Setup advertising */
johnathanlyu 0:4d76c0bc0f78 130 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
johnathanlyu 0:4d76c0bc0f78 131 // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
johnathanlyu 0:4d76c0bc0f78 132 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
johnathanlyu 0:4d76c0bc0f78 133 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
johnathanlyu 0:4d76c0bc0f78 134 ble.gap().setAdvertisingInterval(100); /* 1000ms */
johnathanlyu 0:4d76c0bc0f78 135 ble.gap().startAdvertising();
johnathanlyu 0:4d76c0bc0f78 136 }
johnathanlyu 0:4d76c0bc0f78 137
johnathanlyu 0:4d76c0bc0f78 138 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
johnathanlyu 0:4d76c0bc0f78 139 BLE &ble = BLE::Instance();
johnathanlyu 0:4d76c0bc0f78 140 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
johnathanlyu 0:4d76c0bc0f78 141 }
johnathanlyu 0:4d76c0bc0f78 142
johnathanlyu 0:4d76c0bc0f78 143 int main()
johnathanlyu 0:4d76c0bc0f78 144 {
johnathanlyu 0:4d76c0bc0f78 145 pc.set_flow_control(SerialBase::Disabled);
johnathanlyu 0:4d76c0bc0f78 146 pc.baud(115200);
johnathanlyu 0:4d76c0bc0f78 147 pc.printf("\r\n");
johnathanlyu 0:4d76c0bc0f78 148 pc.printf("Welcome MTM Node !\r\n");
johnathanlyu 0:4d76c0bc0f78 149
johnathanlyu 0:4d76c0bc0f78 150 eventQueue.call_every(500, blinkCallback);
johnathanlyu 2:f3768a226561 151
johnathanlyu 0:4d76c0bc0f78 152 ccs811.init();
johnathanlyu 0:4d76c0bc0f78 153 // ccs811.softRest();
johnathanlyu 2:f3768a226561 154 eventQueue.call_every(2000, CCS811Callback);
johnathanlyu 0:4d76c0bc0f78 155
johnathanlyu 0:4d76c0bc0f78 156 BLE &ble = BLE::Instance();
johnathanlyu 0:4d76c0bc0f78 157 ble.onEventsToProcess(scheduleBleEventsProcessing);
johnathanlyu 0:4d76c0bc0f78 158 ble.init(bleInitComplete);
johnathanlyu 0:4d76c0bc0f78 159
johnathanlyu 0:4d76c0bc0f78 160 eventQueue.dispatch_forever();
johnathanlyu 0:4d76c0bc0f78 161
johnathanlyu 0:4d76c0bc0f78 162 return 0;
johnathanlyu 0:4d76c0bc0f78 163 }