51 52 with same code
Dependencies: CM3592
Fork of MtConnect04S_MtSense05 by
source/main.cpp@0:6d6708b58601, 2017-06-06 (annotated)
- Committer:
- johnathanlyu
- Date:
- Tue Jun 06 02:02:38 2017 +0000
- Revision:
- 0:6d6708b58601
- Child:
- 1:6881d24f8efe
MtConnect04S with MtSense05 initial
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:6d6708b58601 | 24 | Serial pc(p5, p4); |
johnathanlyu | 0:6d6708b58601 | 25 | I2C i2c(p3, p2); |
johnathanlyu | 0:6d6708b58601 | 26 | DigitalOut led1(p16, 1); |
johnathanlyu | 0:6d6708b58601 | 27 | CM3592 cm3592(i2c); |
johnathanlyu | 0:6d6708b58601 | 28 | |
johnathanlyu | 0:6d6708b58601 | 29 | const static char DEVICE_NAME[] = "MtSense05 Test"; |
johnathanlyu | 0:6d6708b58601 | 30 | static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE}; |
johnathanlyu | 0:6d6708b58601 | 31 | |
johnathanlyu | 0:6d6708b58601 | 32 | static EnvironmentUVService *environmentUVServicePtr; |
johnathanlyu | 0:6d6708b58601 | 33 | |
johnathanlyu | 0:6d6708b58601 | 34 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
johnathanlyu | 0:6d6708b58601 | 35 | |
johnathanlyu | 0:6d6708b58601 | 36 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
johnathanlyu | 0:6d6708b58601 | 37 | { |
johnathanlyu | 0:6d6708b58601 | 38 | BLE::Instance().gap().startAdvertising(); |
johnathanlyu | 0:6d6708b58601 | 39 | } |
johnathanlyu | 0:6d6708b58601 | 40 | |
johnathanlyu | 0:6d6708b58601 | 41 | void CM3592Init() { |
johnathanlyu | 0:6d6708b58601 | 42 | |
johnathanlyu | 0:6d6708b58601 | 43 | cm3592.init(); |
johnathanlyu | 0:6d6708b58601 | 44 | |
johnathanlyu | 0:6d6708b58601 | 45 | } |
johnathanlyu | 0:6d6708b58601 | 46 | |
johnathanlyu | 0:6d6708b58601 | 47 | void CM3592Read() { |
johnathanlyu | 0:6d6708b58601 | 48 | |
johnathanlyu | 0:6d6708b58601 | 49 | uint16_t uvReal, uvIndex; |
johnathanlyu | 0:6d6708b58601 | 50 | |
johnathanlyu | 0:6d6708b58601 | 51 | cm3592.readData(&uvReal); |
johnathanlyu | 0:6d6708b58601 | 52 | uvIndex = cm3592.getUVIndex(uvReal); |
johnathanlyu | 0:6d6708b58601 | 53 | |
johnathanlyu | 0:6d6708b58601 | 54 | BLE &ble = BLE::Instance(); |
johnathanlyu | 0:6d6708b58601 | 55 | if (ble.gap().getState().connected) { |
johnathanlyu | 0:6d6708b58601 | 56 | environmentUVServicePtr->updateTemperature(uvIndex); |
johnathanlyu | 0:6d6708b58601 | 57 | } |
johnathanlyu | 0:6d6708b58601 | 58 | |
johnathanlyu | 0:6d6708b58601 | 59 | // pc.printf("uv index is : %d\r\n", uvIndex); |
johnathanlyu | 0:6d6708b58601 | 60 | // pc.printf("uv:%10d\r\n", uvReal); |
johnathanlyu | 0:6d6708b58601 | 61 | } |
johnathanlyu | 0:6d6708b58601 | 62 | |
johnathanlyu | 0:6d6708b58601 | 63 | void blinkCallback(void) |
johnathanlyu | 0:6d6708b58601 | 64 | { |
johnathanlyu | 0:6d6708b58601 | 65 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
johnathanlyu | 0:6d6708b58601 | 66 | |
johnathanlyu | 0:6d6708b58601 | 67 | } |
johnathanlyu | 0:6d6708b58601 | 68 | |
johnathanlyu | 0:6d6708b58601 | 69 | /** |
johnathanlyu | 0:6d6708b58601 | 70 | * This function is called when the ble initialization process has failled |
johnathanlyu | 0:6d6708b58601 | 71 | */ |
johnathanlyu | 0:6d6708b58601 | 72 | void onBleInitError(BLE &ble, ble_error_t error) |
johnathanlyu | 0:6d6708b58601 | 73 | { |
johnathanlyu | 0:6d6708b58601 | 74 | /* Initialization error handling should go here */ |
johnathanlyu | 0:6d6708b58601 | 75 | } |
johnathanlyu | 0:6d6708b58601 | 76 | |
johnathanlyu | 0:6d6708b58601 | 77 | /** |
johnathanlyu | 0:6d6708b58601 | 78 | * Callback triggered when the ble initialization process has finished |
johnathanlyu | 0:6d6708b58601 | 79 | */ |
johnathanlyu | 0:6d6708b58601 | 80 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
johnathanlyu | 0:6d6708b58601 | 81 | { |
johnathanlyu | 0:6d6708b58601 | 82 | BLE& ble = params->ble; |
johnathanlyu | 0:6d6708b58601 | 83 | ble_error_t error = params->error; |
johnathanlyu | 0:6d6708b58601 | 84 | |
johnathanlyu | 0:6d6708b58601 | 85 | if (error != BLE_ERROR_NONE) { |
johnathanlyu | 0:6d6708b58601 | 86 | /* In case of error, forward the error handling to onBleInitError */ |
johnathanlyu | 0:6d6708b58601 | 87 | onBleInitError(ble, error); |
johnathanlyu | 0:6d6708b58601 | 88 | return; |
johnathanlyu | 0:6d6708b58601 | 89 | } |
johnathanlyu | 0:6d6708b58601 | 90 | |
johnathanlyu | 0:6d6708b58601 | 91 | /* Ensure that it is the default instance of BLE */ |
johnathanlyu | 0:6d6708b58601 | 92 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
johnathanlyu | 0:6d6708b58601 | 93 | return; |
johnathanlyu | 0:6d6708b58601 | 94 | } |
johnathanlyu | 0:6d6708b58601 | 95 | |
johnathanlyu | 0:6d6708b58601 | 96 | ble.gap().onDisconnection(disconnectionCallback); |
johnathanlyu | 0:6d6708b58601 | 97 | |
johnathanlyu | 0:6d6708b58601 | 98 | /* Setup primary service */ |
johnathanlyu | 0:6d6708b58601 | 99 | environmentUVServicePtr = new EnvironmentUVService(ble); |
johnathanlyu | 0:6d6708b58601 | 100 | |
johnathanlyu | 0:6d6708b58601 | 101 | /* Setup advertising */ |
johnathanlyu | 0:6d6708b58601 | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
johnathanlyu | 0:6d6708b58601 | 103 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); |
johnathanlyu | 0:6d6708b58601 | 104 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
johnathanlyu | 0:6d6708b58601 | 105 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
johnathanlyu | 0:6d6708b58601 | 106 | ble.gap().setAdvertisingInterval(100); /* 100ms */ |
johnathanlyu | 0:6d6708b58601 | 107 | ble.gap().startAdvertising(); |
johnathanlyu | 0:6d6708b58601 | 108 | } |
johnathanlyu | 0:6d6708b58601 | 109 | |
johnathanlyu | 0:6d6708b58601 | 110 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
johnathanlyu | 0:6d6708b58601 | 111 | BLE &ble = BLE::Instance(); |
johnathanlyu | 0:6d6708b58601 | 112 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
johnathanlyu | 0:6d6708b58601 | 113 | } |
johnathanlyu | 0:6d6708b58601 | 114 | |
johnathanlyu | 0:6d6708b58601 | 115 | int main() |
johnathanlyu | 0:6d6708b58601 | 116 | { |
johnathanlyu | 0:6d6708b58601 | 117 | pc.set_flow_control(SerialBase::Disabled); |
johnathanlyu | 0:6d6708b58601 | 118 | pc.baud(115200); |
johnathanlyu | 0:6d6708b58601 | 119 | pc.printf("\r\n"); |
johnathanlyu | 0:6d6708b58601 | 120 | pc.printf("Welcome MTM Node !\r\n"); |
johnathanlyu | 0:6d6708b58601 | 121 | |
johnathanlyu | 0:6d6708b58601 | 122 | eventQueue.call_every(500, blinkCallback); |
johnathanlyu | 0:6d6708b58601 | 123 | |
johnathanlyu | 0:6d6708b58601 | 124 | CM3592Init(); |
johnathanlyu | 0:6d6708b58601 | 125 | eventQueue.call_every(1000, CM3592Read); |
johnathanlyu | 0:6d6708b58601 | 126 | |
johnathanlyu | 0:6d6708b58601 | 127 | BLE &ble = BLE::Instance(); |
johnathanlyu | 0:6d6708b58601 | 128 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
johnathanlyu | 0:6d6708b58601 | 129 | ble.init(bleInitComplete); |
johnathanlyu | 0:6d6708b58601 | 130 | |
johnathanlyu | 0:6d6708b58601 | 131 | eventQueue.dispatch_forever(); |
johnathanlyu | 0:6d6708b58601 | 132 | |
johnathanlyu | 0:6d6708b58601 | 133 | return 0; |
johnathanlyu | 0:6d6708b58601 | 134 | } |