Kenji Arai / TYBLE16_mbedlized_Thermometer

Dependencies:   HDC1000 TYBLE16_BASE TextLCD

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-2013 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     Modified by Kenji Arai, Feburary 25th, 2018
00018  */
00019 
00020 #include "mbed.h"
00021 #include "TYBLE16_BASE.h"
00022 #include "ble/BLE.h"
00023 #include "ble/services/HealthThermometerService.h"
00024 #include "TextLCD.h"
00025 #include "HDC1000.h"
00026 
00027 #define USE_LCD     0
00028 
00029 DigitalOut      led1(P0_5);
00030 Serial          pc(P0_1, P0_3);
00031 I2C             i2c(P0_4, P0_6);
00032 HDC1000         hmtp(i2c);
00033 #if USE_LCD
00034 TextLCD_I2C_N   lcd(&i2c, 0x7c, TextLCD::LCD8x2);  // LCD(Akizuki  AQM0802A)
00035 #endif
00036 
00037 static HealthThermometerService *thermometerServicePtr;
00038 
00039 const static char     DEVICE_NAME[]        = "TYBLE16";
00040 static const uint16_t uuid16_list[]        
00041                             = {GattService::UUID_HEALTH_THERMOMETER_SERVICE};
00042 static volatile bool  triggerSensorPolling = false;
00043 static float          currentTemperature   = 21.0;
00044 
00045 /* Restart Advertising on disconnection*/
00046 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00047 {
00048     BLE::Instance().gap().startAdvertising();
00049 }
00050 
00051 void periodicCallback(void)
00052 {
00053     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00054     triggerSensorPolling = true;
00055 }
00056 
00057 /**
00058  * This function is called when the ble initialization process has failed
00059  */
00060 void onBleInitError(BLE &ble, ble_error_t error)
00061 {
00062     /* Avoid compiler warnings */
00063     (void) ble;
00064     (void) error;
00065     /* Initialization error handling should go here */
00066 }
00067 
00068 /**
00069  * Callback triggered when the ble initialization process has finished
00070  */
00071 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00072 {
00073     BLE&        ble   = params->ble;
00074     ble_error_t error = params->error;
00075 
00076     if (error != BLE_ERROR_NONE) {
00077         /* In case of error, forward the error handling to onBleInitError */
00078         onBleInitError(ble, error);
00079         return;
00080     }
00081     /* Ensure that it is the default instance of BLE */
00082     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00083         return;
00084     }
00085 
00086     ble.gap().onDisconnection(disconnectionCallback);
00087 
00088     /* Setup primary service. */
00089     thermometerServicePtr = new HealthThermometerService(
00090             ble,
00091             currentTemperature,
00092             HealthThermometerService::LOCATION_EAR
00093     );
00094     /* setup advertising */
00095     ble.gap().accumulateAdvertisingPayload(
00096         GapAdvertisingData::BREDR_NOT_SUPPORTED |
00097         GapAdvertisingData::LE_GENERAL_DISCOVERABLE
00098     );
00099     ble.gap().accumulateAdvertisingPayload(
00100         GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
00101         (uint8_t *)uuid16_list,
00102         sizeof(uuid16_list)
00103     );
00104     ble.gap().accumulateAdvertisingPayload(
00105         GapAdvertisingData::THERMOMETER_EAR
00106     );
00107     ble.gap().accumulateAdvertisingPayload(
00108         GapAdvertisingData::COMPLETE_LOCAL_NAME,
00109         (uint8_t *)DEVICE_NAME,
00110         sizeof(DEVICE_NAME)
00111     );
00112     ble.gap().setAdvertisingType(
00113         GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED
00114     );
00115     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00116     ble.gap().startAdvertising();    
00117 }
00118 
00119 int main(void)
00120 {
00121     led1 = 1;
00122     Ticker ticker;
00123     ticker.attach(periodicCallback, 1);
00124 #if USE_LCD
00125     lcd.locate(0, 0);
00126     //        12345678
00127     lcd.puts("12345678");
00128     lcd.locate(0, 1);
00129     //        12345678
00130     lcd.puts(" JH1PJL ");
00131     lcd.setCursor(TextLCD_Base::CurOff_BlkOff);
00132     lcd.setContrast(0x19);
00133     wait(2.0f);
00134 #endif
00135     BLE &ble = BLE::Instance();
00136     ble.init(bleInitComplete);
00137     while (ble.hasInitialized()  == false) { /* spin loop */ }
00138     // Check TYBLE-16 configuration
00139     cpu_sys();
00140     if (compile_condition() == false) {
00141         pc.printf("This is wrong configuration!!\r\n");
00142         while(true) {
00143             led1 = !led1;
00144             wait(0.2);
00145         }
00146     }
00147     //
00148     while (true) {
00149         if (triggerSensorPolling && ble.gap().getState().connected) {
00150             triggerSensorPolling = false;
00151             currentTemperature = hmtp.temperature();
00152             thermometerServicePtr->updateTemperature(currentTemperature);
00153             pc.printf("Temperature= %+5.2f [degC]\r\n", currentTemperature);
00154 #if USE_LCD
00155             lcd.locate(0, 0);
00156             //          123456 78
00157             lcd.printf("Temp: %cC", 0xdf);
00158             lcd.locate(0, 1);
00159             lcd.printf(" %+5.2f ", currentTemperature);
00160 #endif
00161         } else {
00162             ble.waitForEvent();
00163         }
00164         hmtp.get();    // Triger conversion
00165     }
00166 }