Taeil Kim / Mbed OS color-firmware
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <events/mbed_events.h>
00002 #include <mbed.h>
00003 #include "ble/BLE.h"
00004 #include "ble/Gap.h"
00005 #include "ble/services/BatteryService.h"
00006 #include "LEDService.h"
00007 
00008 DigitalOut led1(LED1, 1); //TODO LED 디바이스
00009 
00010 const static char     DEVICE_NAME[] = "Color Device";
00011 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE}; //TODO 헤더 추가 바람!!!
00012 
00013 static uint8_t batteryLevel = 50;
00014 static BatteryService* batteryServicePtr;
00015 
00016 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
00017 
00018 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
00019     BLE::Instance().gap().startAdvertising();
00020 }
00021 
00022 void updateSensorValue() {
00023     //TODO 실제 센서벨류 업데이트 해야하는 부분
00024     batteryLevel++;
00025     if (batteryLevel > 100) {
00026         batteryLevel = 20;
00027     }
00028     thermometerServicePtr->updateTemperature(currentTemperature);
00029     batteryServicePtr->updateBatteryLevel(batteryLevel);
00030 }
00031 
00032 void periodicCallback(void) {
00033     //TODO 연결대기 메소드
00034     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00035 
00036     if (BLE::Instance().gap().getState().connected) {
00037         eventQueue.call(updateSensorValue);
00038     }
00039 }
00040 
00041 /**
00042  * This function is called when the ble initialization process has failled
00043  */
00044 void onBleInitError(BLE &ble, ble_error_t error) {
00045     //TODO 초기화 에러
00046     /* Initialization error handling should go here */
00047 }
00048 
00049 void printMacAddress()
00050 {
00051     /* Print out device MAC address to the console*/
00052     Gap::AddressType_t addr_type;
00053     Gap::Address_t address;
00054     BLE::Instance().gap().getAddress(&addr_type, address);
00055     printf("DEVICE MAC ADDRESS: ");
00056     for (int i = 5; i >= 1; i--){
00057         printf("%02x:", address[i]);
00058     }
00059     printf("%02x\r\n", address[0]);
00060 }
00061 
00062 /**
00063  * Callback triggered when the ble initialization process has finished
00064  */
00065 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00066 {
00067     BLE&        ble   = params->ble;
00068     ble_error_t error = params->error;
00069 
00070     if (error != BLE_ERROR_NONE) {
00071         /* In case of error, forward the error handling to onBleInitError */
00072         onBleInitError(ble, error);
00073         return;
00074     }
00075 
00076     /* Ensure that it is the default instance of BLE */
00077     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00078         return;
00079     }
00080 
00081     ble.gap().onDisconnection(disconnectionCallback);
00082 
00083     /* Setup primary service */
00084     batteryServicePtr = new BatteryService(ble, batteryLevel);
00085 
00086     /* Setup advertising */
00087     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00088     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
00089     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
00090     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00091     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00092     ble.gap().startAdvertising();
00093 
00094     printMacAddress();
00095 }
00096 
00097 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00098     BLE &ble = BLE::Instance();
00099     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00100 }
00101 
00102 int main()
00103 {
00104     eventQueue.call_every(500, periodicCallback);
00105 
00106     BLE &ble = BLE::Instance();
00107     ble.onEventsToProcess(scheduleBleEventsProcessing);
00108     ble.init(bleInitComplete);
00109 
00110     eventQueue.dispatch_forever();
00111 
00112     return 0;
00113 }