![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Basic BLE comm Control Led using On/Off Command
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LoopbackUART by
Diff: main.cpp
- Revision:
- 13:4e4ec6fe8123
- Parent:
- 12:51e2c29e3019
diff -r 51e2c29e3019 -r 4e4ec6fe8123 main.cpp --- a/main.cpp Sun Nov 09 23:51:54 2014 +0000 +++ b/main.cpp Mon Nov 10 03:54:49 2014 +0000 @@ -32,20 +32,24 @@ #define ON 1960 #define OFF 40 #define PULSE_WIDTH 2000 +#define OUTPUT_LENGTH 35 BLEDevice ble; DigitalOut led1(LED1); DigitalOut led2(LED2); -DigitalOut pwm_out(P0_30); +DigitalOut pwmOut(P0_30); AnalogIn ldr0(P0_1); AnalogIn ldr1(P0_2); AnalogIn ldr2(P0_3); AnalogIn ldr3(P0_4); +int threshold = 0; + +char output[50] = "LDR0 = w x y z"; UARTService *uartServicePtr; -#define TIMER 1 +#define TIMER 0 //Creating timer Instance Ticker ticker; @@ -60,6 +64,27 @@ ble.startAdvertising(); } +void getValuesofLDR() +{ + if (ldr0.read() < 0.7f) + output[7] = '0'; + else + output[7] = '1'; + if (ldr0.read() < 0.7f) + output[9] = '0'; + else + output[9] = '1'; + if (ldr0.read() < 0.7f) + output[11] = '0'; + else + output[11] = '1'; + if (ldr0.read() < 0.7f) + output[13] = '0'; + else + output[13] = '1'; + +} + void onDataWritten(const GattCharacteristicWriteCBParams *params) { char *str = 0; @@ -79,11 +104,32 @@ *(str + count) = '\0'; DEBUG("payload = %s\n\r",str); if (strncmp(str,"On",bytesRead) == 0) + { led2 = 1; - else + pwmOut = 1; + bytesRead = 7; + strncpy((char *)params->data,"Lamp On", bytesRead); + } + else if (strncmp(str,"Off",bytesRead) == 0) + { led2 = 0; - free(str); - strcpy((char *)params->data,"SPOPOPOSP"); + pwmOut = 0; + bytesRead = 7; + strncpy((char *)params->data,"Lamp Off", bytesRead); + } + else if (strncmp(str,"Read",bytesRead) == 0) + { + bytesRead = 13; + getValuesofLDR(); + strncpy((char *)params->data,output,bytesRead); + + } + else + { + bytesRead = 14; + strncpy((char *)params->data,"Invalid Entry", bytesRead); + } + free(str); ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); } } @@ -113,12 +159,17 @@ int main(void) { led1 = 1; - ticker.attach(periodicCallback, 0.000001f); DEBUG("Initialising the nRF51822\n\r"); + //ticker.attach(periodicCallback, 0.00001f); ble.init(); + + DEBUG("Initialising the nRF51822_1\n\r"); ble.onDisconnection(disconnectionCallback); + + DEBUG("Initialising the nRF51822_2\n\r"); ble.onDataWritten(onDataWritten); + DEBUG("Initialising the nRF51822_3\n\r"); /* setup advertising */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); @@ -133,6 +184,7 @@ UARTService uartService(ble); uartServicePtr = &uartService; + DEBUG("Initialising the nRF51822_4\n\r"); while (true) { ble.waitForEvent(); }