Basic BLE comm Control Led using On/Off Command
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LoopbackUART by
main.cpp@12:51e2c29e3019, 2014-11-09 (annotated)
- Committer:
- adarsh5723
- Date:
- Sun Nov 09 23:51:54 2014 +0000
- Revision:
- 12:51e2c29e3019
- Parent:
- 11:4f9451eaca3d
- Child:
- 13:4e4ec6fe8123
Support for Servo Added
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 | 12:51e2c29e3019 | 35 | |
Rohit Grover |
2:e060367b9024 | 36 | BLEDevice ble; |
Rohit Grover |
2:e060367b9024 | 37 | DigitalOut led1(LED1); |
adarsh5723 | 10:d4cd8edc6216 | 38 | DigitalOut led2(LED2); |
adarsh5723 | 10:d4cd8edc6216 | 39 | DigitalOut pwm_out(P0_30); |
adarsh5723 | 10:d4cd8edc6216 | 40 | |
adarsh5723 | 12:51e2c29e3019 | 41 | AnalogIn ldr0(P0_1); |
adarsh5723 | 12:51e2c29e3019 | 42 | AnalogIn ldr1(P0_2); |
adarsh5723 | 12:51e2c29e3019 | 43 | AnalogIn ldr2(P0_3); |
adarsh5723 | 12:51e2c29e3019 | 44 | AnalogIn ldr3(P0_4); |
adarsh5723 | 12:51e2c29e3019 | 45 | |
yihui | 0:e910d9bb040f | 46 | |
rgrover1 | 6:e0fc9072e853 | 47 | UARTService *uartServicePtr; |
adarsh5723 | 12:51e2c29e3019 | 48 | #define TIMER 1 |
adarsh5723 | 12:51e2c29e3019 | 49 | //Creating timer Instance |
adarsh5723 | 12:51e2c29e3019 | 50 | |
adarsh5723 | 12:51e2c29e3019 | 51 | Ticker ticker; |
adarsh5723 | 12:51e2c29e3019 | 52 | #if TIMER |
adarsh5723 | 12:51e2c29e3019 | 53 | long int timerCount = 0; |
adarsh5723 | 12:51e2c29e3019 | 54 | #endif |
yihui | 0:e910d9bb040f | 55 | |
rgrover1 | 5:4bc41267a03a | 56 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
yihui | 0:e910d9bb040f | 57 | { |
Rohit Grover |
2:e060367b9024 | 58 | DEBUG("Disconnected!\n\r"); |
Rohit Grover |
2:e060367b9024 | 59 | DEBUG("Restarting the advertising process\n\r"); |
Rohit Grover |
2:e060367b9024 | 60 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 61 | } |
yihui | 0:e910d9bb040f | 62 | |
rgrover1 | 6:e0fc9072e853 | 63 | void onDataWritten(const GattCharacteristicWriteCBParams *params) |
yihui | 0:e910d9bb040f | 64 | { |
adarsh5723 | 10:d4cd8edc6216 | 65 | char *str = 0; |
adarsh5723 | 10:d4cd8edc6216 | 66 | int count = 0; |
adarsh5723 | 10:d4cd8edc6216 | 67 | |
rgrover1 | 6:e0fc9072e853 | 68 | if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { |
rgrover1 | 5:4bc41267a03a | 69 | uint16_t bytesRead = params->len; |
rgrover1 | 5:4bc41267a03a | 70 | DEBUG("received %u bytes\n\r", bytesRead); |
adarsh5723 | 10:d4cd8edc6216 | 71 | |
adarsh5723 | 10:d4cd8edc6216 | 72 | str = (char *)malloc((sizeof(char) * bytesRead) + 1); |
adarsh5723 | 10:d4cd8edc6216 | 73 | while(count <= bytesRead) |
adarsh5723 | 10:d4cd8edc6216 | 74 | { |
adarsh5723 | 10:d4cd8edc6216 | 75 | *(str + count) = *(params->data + count); |
adarsh5723 | 10:d4cd8edc6216 | 76 | count++; |
adarsh5723 | 10:d4cd8edc6216 | 77 | } |
adarsh5723 | 10:d4cd8edc6216 | 78 | count--; |
adarsh5723 | 10:d4cd8edc6216 | 79 | *(str + count) = '\0'; |
adarsh5723 | 10:d4cd8edc6216 | 80 | DEBUG("payload = %s\n\r",str); |
adarsh5723 | 10:d4cd8edc6216 | 81 | if (strncmp(str,"On",bytesRead) == 0) |
adarsh5723 | 10:d4cd8edc6216 | 82 | led2 = 1; |
adarsh5723 | 10:d4cd8edc6216 | 83 | else |
adarsh5723 | 10:d4cd8edc6216 | 84 | led2 = 0; |
adarsh5723 | 10:d4cd8edc6216 | 85 | free(str); |
adarsh5723 | 10:d4cd8edc6216 | 86 | strcpy((char *)params->data,"SPOPOPOSP"); |
rgrover1 | 6:e0fc9072e853 | 87 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
yihui | 0:e910d9bb040f | 88 | } |
Rohit Grover |
2:e060367b9024 | 89 | } |
yihui | 0:e910d9bb040f | 90 | |
Rohit Grover |
2:e060367b9024 | 91 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 92 | { |
adarsh5723 | 12:51e2c29e3019 | 93 | #if TIMER |
adarsh5723 | 12:51e2c29e3019 | 94 | timerCount++; |
adarsh5723 | 12:51e2c29e3019 | 95 | if ((timerCount < threshold)) |
adarsh5723 | 12:51e2c29e3019 | 96 | { |
adarsh5723 | 12:51e2c29e3019 | 97 | led1 = 1; |
adarsh5723 | 12:51e2c29e3019 | 98 | pwm_out = 1; |
adarsh5723 | 12:51e2c29e3019 | 99 | } |
adarsh5723 | 12:51e2c29e3019 | 100 | else |
adarsh5723 | 12:51e2c29e3019 | 101 | { |
adarsh5723 | 12:51e2c29e3019 | 102 | led1 = 0; |
adarsh5723 | 12:51e2c29e3019 | 103 | pwm_out = 0; |
adarsh5723 | 12:51e2c29e3019 | 104 | } |
adarsh5723 | 12:51e2c29e3019 | 105 | if (timerCount == PULSE_WIDTH) |
adarsh5723 | 12:51e2c29e3019 | 106 | timerCount = 0; |
adarsh5723 | 12:51e2c29e3019 | 107 | #else |
rgrover1 | 6:e0fc9072e853 | 108 | led1 = !led1; |
adarsh5723 | 12:51e2c29e3019 | 109 | #endif |
adarsh5723 | 12:51e2c29e3019 | 110 | |
Rohit Grover |
2:e060367b9024 | 111 | } |
yihui | 0:e910d9bb040f | 112 | |
yihui | 0:e910d9bb040f | 113 | int main(void) |
yihui | 0:e910d9bb040f | 114 | { |
Rohit Grover |
2:e060367b9024 | 115 | led1 = 1; |
adarsh5723 | 12:51e2c29e3019 | 116 | ticker.attach(periodicCallback, 0.000001f); |
Rohit Grover |
2:e060367b9024 | 117 | DEBUG("Initialising the nRF51822\n\r"); |
Rohit Grover |
2:e060367b9024 | 118 | ble.init(); |
Rohit Grover |
2:e060367b9024 | 119 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
2:e060367b9024 | 120 | ble.onDataWritten(onDataWritten); |
yihui | 0:e910d9bb040f | 121 | |
Rohit Grover |
2:e060367b9024 | 122 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 123 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 124 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 125 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
adarsh5723 | 10:d4cd8edc6216 | 126 | (const uint8_t *)"AdarshBLE UART", sizeof("AdarshBLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 127 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
rgrover1 | 6:e0fc9072e853 | 128 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
yihui | 0:e910d9bb040f | 129 | |
Rohit Grover |
2:e060367b9024 | 130 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
Rohit Grover |
2:e060367b9024 | 131 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 132 | |
rgrover1 | 6:e0fc9072e853 | 133 | UARTService uartService(ble); |
rgrover1 | 6:e0fc9072e853 | 134 | uartServicePtr = &uartService; |
yihui | 0:e910d9bb040f | 135 | |
adarsh5723 | 12:51e2c29e3019 | 136 | while (true) { |
Rohit Grover |
2:e060367b9024 | 137 | ble.waitForEvent(); |
yihui | 0:e910d9bb040f | 138 | } |
yihui | 0:e910d9bb040f | 139 | } |