Basic BLE comm Control Led using On/Off Command
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LoopbackUART by
main.cpp@13:4e4ec6fe8123, 2014-11-10 (annotated)
- Committer:
- adarsh5723
- Date:
- Mon Nov 10 03:54:49 2014 +0000
- Revision:
- 13:4e4ec6fe8123
- Parent:
- 12:51e2c29e3019
Complete Demo.; Added functionality to read LDR data, power on and off the lamp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:e910d9bb040f | 1 | /* mbed Microcontroller Library |
yihui | 0:e910d9bb040f | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 0:e910d9bb040f | 3 | * |
yihui | 0:e910d9bb040f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 0:e910d9bb040f | 5 | * you may not use this file except in compliance with the License. |
yihui | 0:e910d9bb040f | 6 | * You may obtain a copy of the License at |
yihui | 0:e910d9bb040f | 7 | * |
yihui | 0:e910d9bb040f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 0:e910d9bb040f | 9 | * |
yihui | 0:e910d9bb040f | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 0:e910d9bb040f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 0:e910d9bb040f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 0:e910d9bb040f | 13 | * See the License for the specific language governing permissions and |
yihui | 0:e910d9bb040f | 14 | * limitations under the License. |
yihui | 0:e910d9bb040f | 15 | */ |
yihui | 0:e910d9bb040f | 16 | |
yihui | 0:e910d9bb040f | 17 | #include "mbed.h" |
Rohit Grover |
2:e060367b9024 | 18 | #include "BLEDevice.h" |
adarsh5723 | 10:d4cd8edc6216 | 19 | #include <string.h> |
yihui | 0:e910d9bb040f | 20 | |
rgrover1 | 6:e0fc9072e853 | 21 | #include "UARTService.h" |
yihui | 0:e910d9bb040f | 22 | |
adarsh5723 | 10:d4cd8edc6216 | 23 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
Rohit Grover |
2:e060367b9024 | 24 | * it will have an impact on code-size and power consumption. */ |
yihui | 0:e910d9bb040f | 25 | |
Rohit Grover |
2:e060367b9024 | 26 | #if NEED_CONSOLE_OUTPUT |
rgrover1 | 6:e0fc9072e853 | 27 | #define DEBUG(...) { printf(__VA_ARGS__); } |
yihui | 0:e910d9bb040f | 28 | #else |
Rohit Grover |
2:e060367b9024 | 29 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
2:e060367b9024 | 30 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
yihui | 0:e910d9bb040f | 31 | |
adarsh5723 | 12:51e2c29e3019 | 32 | #define ON 1960 |
adarsh5723 | 12:51e2c29e3019 | 33 | #define OFF 40 |
adarsh5723 | 12:51e2c29e3019 | 34 | #define PULSE_WIDTH 2000 |
adarsh5723 | 13:4e4ec6fe8123 | 35 | #define OUTPUT_LENGTH 35 |
adarsh5723 | 12:51e2c29e3019 | 36 | |
Rohit Grover |
2:e060367b9024 | 37 | BLEDevice ble; |
Rohit Grover |
2:e060367b9024 | 38 | DigitalOut led1(LED1); |
adarsh5723 | 10:d4cd8edc6216 | 39 | DigitalOut led2(LED2); |
adarsh5723 | 13:4e4ec6fe8123 | 40 | DigitalOut pwmOut(P0_30); |
adarsh5723 | 10:d4cd8edc6216 | 41 | |
adarsh5723 | 12:51e2c29e3019 | 42 | AnalogIn ldr0(P0_1); |
adarsh5723 | 12:51e2c29e3019 | 43 | AnalogIn ldr1(P0_2); |
adarsh5723 | 12:51e2c29e3019 | 44 | AnalogIn ldr2(P0_3); |
adarsh5723 | 12:51e2c29e3019 | 45 | AnalogIn ldr3(P0_4); |
adarsh5723 | 12:51e2c29e3019 | 46 | |
adarsh5723 | 13:4e4ec6fe8123 | 47 | int threshold = 0; |
adarsh5723 | 13:4e4ec6fe8123 | 48 | |
adarsh5723 | 13:4e4ec6fe8123 | 49 | char output[50] = "LDR0 = w x y z"; |
yihui | 0:e910d9bb040f | 50 | |
rgrover1 | 6:e0fc9072e853 | 51 | UARTService *uartServicePtr; |
adarsh5723 | 13:4e4ec6fe8123 | 52 | #define TIMER 0 |
adarsh5723 | 12:51e2c29e3019 | 53 | //Creating timer Instance |
adarsh5723 | 12:51e2c29e3019 | 54 | |
adarsh5723 | 12:51e2c29e3019 | 55 | Ticker ticker; |
adarsh5723 | 12:51e2c29e3019 | 56 | #if TIMER |
adarsh5723 | 12:51e2c29e3019 | 57 | long int timerCount = 0; |
adarsh5723 | 12:51e2c29e3019 | 58 | #endif |
yihui | 0:e910d9bb040f | 59 | |
rgrover1 | 5:4bc41267a03a | 60 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
yihui | 0:e910d9bb040f | 61 | { |
Rohit Grover |
2:e060367b9024 | 62 | DEBUG("Disconnected!\n\r"); |
Rohit Grover |
2:e060367b9024 | 63 | DEBUG("Restarting the advertising process\n\r"); |
Rohit Grover |
2:e060367b9024 | 64 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 65 | } |
yihui | 0:e910d9bb040f | 66 | |
adarsh5723 | 13:4e4ec6fe8123 | 67 | void getValuesofLDR() |
adarsh5723 | 13:4e4ec6fe8123 | 68 | { |
adarsh5723 | 13:4e4ec6fe8123 | 69 | if (ldr0.read() < 0.7f) |
adarsh5723 | 13:4e4ec6fe8123 | 70 | output[7] = '0'; |
adarsh5723 | 13:4e4ec6fe8123 | 71 | else |
adarsh5723 | 13:4e4ec6fe8123 | 72 | output[7] = '1'; |
adarsh5723 | 13:4e4ec6fe8123 | 73 | if (ldr0.read() < 0.7f) |
adarsh5723 | 13:4e4ec6fe8123 | 74 | output[9] = '0'; |
adarsh5723 | 13:4e4ec6fe8123 | 75 | else |
adarsh5723 | 13:4e4ec6fe8123 | 76 | output[9] = '1'; |
adarsh5723 | 13:4e4ec6fe8123 | 77 | if (ldr0.read() < 0.7f) |
adarsh5723 | 13:4e4ec6fe8123 | 78 | output[11] = '0'; |
adarsh5723 | 13:4e4ec6fe8123 | 79 | else |
adarsh5723 | 13:4e4ec6fe8123 | 80 | output[11] = '1'; |
adarsh5723 | 13:4e4ec6fe8123 | 81 | if (ldr0.read() < 0.7f) |
adarsh5723 | 13:4e4ec6fe8123 | 82 | output[13] = '0'; |
adarsh5723 | 13:4e4ec6fe8123 | 83 | else |
adarsh5723 | 13:4e4ec6fe8123 | 84 | output[13] = '1'; |
adarsh5723 | 13:4e4ec6fe8123 | 85 | |
adarsh5723 | 13:4e4ec6fe8123 | 86 | } |
adarsh5723 | 13:4e4ec6fe8123 | 87 | |
rgrover1 | 6:e0fc9072e853 | 88 | void onDataWritten(const GattCharacteristicWriteCBParams *params) |
yihui | 0:e910d9bb040f | 89 | { |
adarsh5723 | 10:d4cd8edc6216 | 90 | char *str = 0; |
adarsh5723 | 10:d4cd8edc6216 | 91 | int count = 0; |
adarsh5723 | 10:d4cd8edc6216 | 92 | |
rgrover1 | 6:e0fc9072e853 | 93 | if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { |
rgrover1 | 5:4bc41267a03a | 94 | uint16_t bytesRead = params->len; |
rgrover1 | 5:4bc41267a03a | 95 | DEBUG("received %u bytes\n\r", bytesRead); |
adarsh5723 | 10:d4cd8edc6216 | 96 | |
adarsh5723 | 10:d4cd8edc6216 | 97 | str = (char *)malloc((sizeof(char) * bytesRead) + 1); |
adarsh5723 | 10:d4cd8edc6216 | 98 | while(count <= bytesRead) |
adarsh5723 | 10:d4cd8edc6216 | 99 | { |
adarsh5723 | 10:d4cd8edc6216 | 100 | *(str + count) = *(params->data + count); |
adarsh5723 | 10:d4cd8edc6216 | 101 | count++; |
adarsh5723 | 10:d4cd8edc6216 | 102 | } |
adarsh5723 | 10:d4cd8edc6216 | 103 | count--; |
adarsh5723 | 10:d4cd8edc6216 | 104 | *(str + count) = '\0'; |
adarsh5723 | 10:d4cd8edc6216 | 105 | DEBUG("payload = %s\n\r",str); |
adarsh5723 | 10:d4cd8edc6216 | 106 | if (strncmp(str,"On",bytesRead) == 0) |
adarsh5723 | 13:4e4ec6fe8123 | 107 | { |
adarsh5723 | 10:d4cd8edc6216 | 108 | led2 = 1; |
adarsh5723 | 13:4e4ec6fe8123 | 109 | pwmOut = 1; |
adarsh5723 | 13:4e4ec6fe8123 | 110 | bytesRead = 7; |
adarsh5723 | 13:4e4ec6fe8123 | 111 | strncpy((char *)params->data,"Lamp On", bytesRead); |
adarsh5723 | 13:4e4ec6fe8123 | 112 | } |
adarsh5723 | 13:4e4ec6fe8123 | 113 | else if (strncmp(str,"Off",bytesRead) == 0) |
adarsh5723 | 13:4e4ec6fe8123 | 114 | { |
adarsh5723 | 10:d4cd8edc6216 | 115 | led2 = 0; |
adarsh5723 | 13:4e4ec6fe8123 | 116 | pwmOut = 0; |
adarsh5723 | 13:4e4ec6fe8123 | 117 | bytesRead = 7; |
adarsh5723 | 13:4e4ec6fe8123 | 118 | strncpy((char *)params->data,"Lamp Off", bytesRead); |
adarsh5723 | 13:4e4ec6fe8123 | 119 | } |
adarsh5723 | 13:4e4ec6fe8123 | 120 | else if (strncmp(str,"Read",bytesRead) == 0) |
adarsh5723 | 13:4e4ec6fe8123 | 121 | { |
adarsh5723 | 13:4e4ec6fe8123 | 122 | bytesRead = 13; |
adarsh5723 | 13:4e4ec6fe8123 | 123 | getValuesofLDR(); |
adarsh5723 | 13:4e4ec6fe8123 | 124 | strncpy((char *)params->data,output,bytesRead); |
adarsh5723 | 13:4e4ec6fe8123 | 125 | |
adarsh5723 | 13:4e4ec6fe8123 | 126 | } |
adarsh5723 | 13:4e4ec6fe8123 | 127 | else |
adarsh5723 | 13:4e4ec6fe8123 | 128 | { |
adarsh5723 | 13:4e4ec6fe8123 | 129 | bytesRead = 14; |
adarsh5723 | 13:4e4ec6fe8123 | 130 | strncpy((char *)params->data,"Invalid Entry", bytesRead); |
adarsh5723 | 13:4e4ec6fe8123 | 131 | } |
adarsh5723 | 13:4e4ec6fe8123 | 132 | free(str); |
rgrover1 | 6:e0fc9072e853 | 133 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
yihui | 0:e910d9bb040f | 134 | } |
Rohit Grover |
2:e060367b9024 | 135 | } |
yihui | 0:e910d9bb040f | 136 | |
Rohit Grover |
2:e060367b9024 | 137 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 138 | { |
adarsh5723 | 12:51e2c29e3019 | 139 | #if TIMER |
adarsh5723 | 12:51e2c29e3019 | 140 | timerCount++; |
adarsh5723 | 12:51e2c29e3019 | 141 | if ((timerCount < threshold)) |
adarsh5723 | 12:51e2c29e3019 | 142 | { |
adarsh5723 | 12:51e2c29e3019 | 143 | led1 = 1; |
adarsh5723 | 12:51e2c29e3019 | 144 | pwm_out = 1; |
adarsh5723 | 12:51e2c29e3019 | 145 | } |
adarsh5723 | 12:51e2c29e3019 | 146 | else |
adarsh5723 | 12:51e2c29e3019 | 147 | { |
adarsh5723 | 12:51e2c29e3019 | 148 | led1 = 0; |
adarsh5723 | 12:51e2c29e3019 | 149 | pwm_out = 0; |
adarsh5723 | 12:51e2c29e3019 | 150 | } |
adarsh5723 | 12:51e2c29e3019 | 151 | if (timerCount == PULSE_WIDTH) |
adarsh5723 | 12:51e2c29e3019 | 152 | timerCount = 0; |
adarsh5723 | 12:51e2c29e3019 | 153 | #else |
rgrover1 | 6:e0fc9072e853 | 154 | led1 = !led1; |
adarsh5723 | 12:51e2c29e3019 | 155 | #endif |
adarsh5723 | 12:51e2c29e3019 | 156 | |
Rohit Grover |
2:e060367b9024 | 157 | } |
yihui | 0:e910d9bb040f | 158 | |
yihui | 0:e910d9bb040f | 159 | int main(void) |
yihui | 0:e910d9bb040f | 160 | { |
Rohit Grover |
2:e060367b9024 | 161 | led1 = 1; |
Rohit Grover |
2:e060367b9024 | 162 | DEBUG("Initialising the nRF51822\n\r"); |
adarsh5723 | 13:4e4ec6fe8123 | 163 | //ticker.attach(periodicCallback, 0.00001f); |
Rohit Grover |
2:e060367b9024 | 164 | ble.init(); |
adarsh5723 | 13:4e4ec6fe8123 | 165 | |
adarsh5723 | 13:4e4ec6fe8123 | 166 | DEBUG("Initialising the nRF51822_1\n\r"); |
Rohit Grover |
2:e060367b9024 | 167 | ble.onDisconnection(disconnectionCallback); |
adarsh5723 | 13:4e4ec6fe8123 | 168 | |
adarsh5723 | 13:4e4ec6fe8123 | 169 | DEBUG("Initialising the nRF51822_2\n\r"); |
Rohit Grover |
2:e060367b9024 | 170 | ble.onDataWritten(onDataWritten); |
yihui | 0:e910d9bb040f | 171 | |
adarsh5723 | 13:4e4ec6fe8123 | 172 | DEBUG("Initialising the nRF51822_3\n\r"); |
Rohit Grover |
2:e060367b9024 | 173 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 174 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 175 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 176 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
adarsh5723 | 10:d4cd8edc6216 | 177 | (const uint8_t *)"AdarshBLE UART", sizeof("AdarshBLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 178 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
rgrover1 | 6:e0fc9072e853 | 179 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
yihui | 0:e910d9bb040f | 180 | |
Rohit Grover |
2:e060367b9024 | 181 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
Rohit Grover |
2:e060367b9024 | 182 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 183 | |
rgrover1 | 6:e0fc9072e853 | 184 | UARTService uartService(ble); |
rgrover1 | 6:e0fc9072e853 | 185 | uartServicePtr = &uartService; |
yihui | 0:e910d9bb040f | 186 | |
adarsh5723 | 13:4e4ec6fe8123 | 187 | DEBUG("Initialising the nRF51822_4\n\r"); |
adarsh5723 | 12:51e2c29e3019 | 188 | while (true) { |
Rohit Grover |
2:e060367b9024 | 189 | ble.waitForEvent(); |
yihui | 0:e910d9bb040f | 190 | } |
yihui | 0:e910d9bb040f | 191 | } |