DesignForAmputee hw 5 project
Dependencies: mbed BLE_API nRF51822
main.cpp
00001 #include "mbed.h" 00002 #include "BLEDevice.h" 00003 #include <string> 00004 #include "UARTService.h" 00005 00006 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; 00007 * it will have an impact on code-size and power consumption. */ 00008 Serial pc(USBTX, USBRX); 00009 00010 #if NEED_CONSOLE_OUTPUT 00011 #define DEBUG(...) { pc.printf(__VA_ARGS__); } 00012 #else 00013 #define DEBUG(...) /* nothing */ 00014 #endif /* #if NEED_CONSOLE_OUTPUT */ 00015 00016 BLEDevice ble; 00017 DigitalOut led1(LED1); 00018 DigitalOut led2(LED2); 00019 00020 DigitalOut powerSwitch(P0_0); 00021 00022 AnalogIn tempPin(P0_1); 00023 00024 UARTService *uartServicePtr; 00025 00026 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00027 { 00028 DEBUG("Disconnected!\n\r"); 00029 DEBUG("Restarting the advertising process\n\r"); 00030 ble.startAdvertising(); 00031 } 00032 00033 void onDataWritten(const GattCharacteristicWriteCBParams *params) 00034 { 00035 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { 00036 float temp = tempPin.read(); // temperature voltage 00037 00038 float tempC = ((temp*3.0)-0.5)*100.0; // conversion to celcius 00039 float tempF = (tempC*9.0/5.0)+32.0; // conversion to fahrenheit 00040 DEBUG("temp[V]:%f\ttemp[C]:%f\ttemp[F]:%f\n",temp,tempC,tempF); 00041 //DEBUG(params->data, "% 00042 uint16_t bytesRead = params->len; 00043 DEBUG("received %u bytes\n\r", bytesRead); 00044 DEBUG("first byte received: %u\n\r", params->data[0]); 00045 uint8_t on = '1'; 00046 uint8_t off = '0'; 00047 if (params->data[0] == on) { 00048 led2 = 1; 00049 powerSwitch = 1; 00050 } else if (params->data[0] == off) { 00051 led2 = 0; 00052 powerSwitch = 0; 00053 } 00054 //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); 00055 char buffer[9]; 00056 sprintf(buffer,"%4.4f",tempF); 00057 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)buffer, sizeof(&buffer)); 00058 00059 } 00060 } 00061 00062 void periodicCallback(void) 00063 { 00064 led1 = !led1; 00065 } 00066 00067 int main(void) 00068 { 00069 led1 = 1; 00070 led2 = 0; 00071 powerSwitch = 0; 00072 Ticker ticker; 00073 ticker.attach(periodicCallback, 1); 00074 00075 DEBUG("Initialising the nRF51822\n\r"); 00076 ble.init(); 00077 ble.onDisconnection(disconnectionCallback); 00078 ble.onDataWritten(onDataWritten); // called when someone writes to me 00079 00080 /* setup advertising */ 00081 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00082 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00083 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00084 (const uint8_t *)"pgao", sizeof("pgao") - 1); 00085 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00086 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00087 00088 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ 00089 ble.startAdvertising(); 00090 00091 UARTService uartService(ble); 00092 uartServicePtr = &uartService; 00093 00094 while (true) { 00095 ble.waitForEvent(); 00096 } 00097 } 00098
Generated on Thu Jul 28 2022 04:35:57 by 1.7.2