51 52 with same code

Dependencies:   CM3592

Fork of MtConnect04S_MtSense05 by MtM+

Committer:
johnathanlyu
Date:
Fri Apr 27 09:58:41 2018 +0000
Revision:
1:6881d24f8efe
Parent:
0:6d6708b58601
51 52 with same code

Who changed what in which revision?

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