MtM+ / Mbed OS Mt05_MtSense05

Dependencies:   CM3592

Fork of MtConnect04S_MtSense05 by MtM+

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2014 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <events/mbed_events.h>
00018 #include <mbed.h>
00019 #include "ble/BLE.h"
00020 #include "ble/Gap.h"
00021 #include "EnvironmentalUVService.h"
00022 #include "CM3592.h"
00023 
00024 /* UART printf */
00025 #ifdef NRF52
00026 Serial pc(p20, p24);
00027 #else
00028 Serial pc(p5, p4);
00029 #endif
00030 
00031 I2C i2c(p3, p2);
00032 DigitalOut led1(p16, 1);
00033 CM3592 cm3592(i2c);
00034 
00035 const static char     DEVICE_NAME[] = "Mt5MtSense05";
00036 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE};
00037 
00038 static EnvironmentUVService *environmentUVServicePtr;
00039 
00040 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
00041 
00042 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00043 {
00044     BLE::Instance().gap().startAdvertising();
00045 }
00046 
00047 void CM3592Init() {
00048     
00049     cm3592.init();
00050 
00051 }
00052 
00053 void CM3592Read() {
00054     
00055     uint16_t uvReal, uvIndex;
00056     
00057     cm3592.readData(&uvReal);
00058     uvIndex = cm3592.getUVIndex(uvReal);
00059     
00060     BLE &ble = BLE::Instance();
00061     if (ble.gap().getState().connected) {
00062         environmentUVServicePtr->updateTemperature(uvIndex);
00063     }
00064     
00065 //    pc.printf("uv index is : %d\r\n", uvIndex);
00066 //    pc.printf("uv:%10d\r\n", uvReal);
00067 }
00068 
00069 void blinkCallback(void)
00070 {
00071     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00072     
00073 }
00074 
00075 /**
00076  * This function is called when the ble initialization process has failled
00077  */
00078 void onBleInitError(BLE &ble, ble_error_t error)
00079 {
00080     /* Initialization error handling should go here */
00081 }
00082 
00083 /**
00084  * Callback triggered when the ble initialization process has finished
00085  */
00086 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00087 {
00088     BLE&        ble   = params->ble;
00089     ble_error_t error = params->error;
00090 
00091     if (error != BLE_ERROR_NONE) {
00092         /* In case of error, forward the error handling to onBleInitError */
00093         onBleInitError(ble, error);
00094         return;
00095     }
00096 
00097     /* Ensure that it is the default instance of BLE */
00098     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00099         return;
00100     }
00101 
00102     ble.gap().onDisconnection(disconnectionCallback);
00103 
00104     /* Setup primary service */
00105     environmentUVServicePtr = new EnvironmentUVService(ble);
00106 
00107     /* Setup advertising */
00108     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00109     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
00110     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
00111     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00112     ble.gap().setAdvertisingInterval(100); /* 100ms */
00113     ble.gap().startAdvertising();
00114 }
00115 
00116 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00117     BLE &ble = BLE::Instance();
00118     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00119 }
00120 
00121 int main()
00122 {
00123     pc.set_flow_control(SerialBase::Disabled);
00124     pc.baud(115200);
00125     pc.printf("\r\n");
00126     pc.printf("Welcome MTM Node !\r\n");
00127     
00128     eventQueue.call_every(500, blinkCallback);
00129 
00130     CM3592Init();
00131     eventQueue.call_every(1000, CM3592Read);
00132 
00133     BLE &ble = BLE::Instance();
00134     ble.onEventsToProcess(scheduleBleEventsProcessing);
00135     ble.init(bleInitComplete);
00136 
00137     eventQueue.dispatch_forever();
00138 
00139     return 0;
00140 }