Code for the nRF51822. The BLE advertises, connects to a master, sends information about the status of the LDRs and receives inputs from the Master
Dependencies: BLE_API mbed nRF51822
Fork of BLE_UART1 by
main.cpp@11:4f9451eaca3d, 2014-11-09 (annotated)
- Committer:
- adarsh5723
- Date:
- Sun Nov 09 21:06:47 2014 +0000
- Revision:
- 11:4f9451eaca3d
- Parent:
- 10:d4cd8edc6216
- Child:
- 12:51e2c29e3019
test
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 "Servo.h" |
adarsh5723 | 10:d4cd8edc6216 | 20 | #include <string.h> |
yihui | 0:e910d9bb040f | 21 | |
rgrover1 | 6:e0fc9072e853 | 22 | #include "UARTService.h" |
yihui | 0:e910d9bb040f | 23 | |
adarsh5723 | 10:d4cd8edc6216 | 24 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
Rohit Grover |
2:e060367b9024 | 25 | * it will have an impact on code-size and power consumption. */ |
yihui | 0:e910d9bb040f | 26 | |
Rohit Grover |
2:e060367b9024 | 27 | #if NEED_CONSOLE_OUTPUT |
rgrover1 | 6:e0fc9072e853 | 28 | #define DEBUG(...) { printf(__VA_ARGS__); } |
yihui | 0:e910d9bb040f | 29 | #else |
Rohit Grover |
2:e060367b9024 | 30 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
2:e060367b9024 | 31 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
yihui | 0:e910d9bb040f | 32 | |
Rohit Grover |
2:e060367b9024 | 33 | BLEDevice ble; |
Rohit Grover |
2:e060367b9024 | 34 | DigitalOut led1(LED1); |
adarsh5723 | 10:d4cd8edc6216 | 35 | DigitalOut led2(LED2); |
adarsh5723 | 10:d4cd8edc6216 | 36 | DigitalOut pwm_out(P0_30); |
adarsh5723 | 10:d4cd8edc6216 | 37 | |
yihui | 0:e910d9bb040f | 38 | |
rgrover1 | 6:e0fc9072e853 | 39 | UARTService *uartServicePtr; |
yihui | 0:e910d9bb040f | 40 | |
rgrover1 | 5:4bc41267a03a | 41 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
yihui | 0:e910d9bb040f | 42 | { |
Rohit Grover |
2:e060367b9024 | 43 | DEBUG("Disconnected!\n\r"); |
Rohit Grover |
2:e060367b9024 | 44 | DEBUG("Restarting the advertising process\n\r"); |
Rohit Grover |
2:e060367b9024 | 45 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 46 | } |
yihui | 0:e910d9bb040f | 47 | |
rgrover1 | 6:e0fc9072e853 | 48 | void onDataWritten(const GattCharacteristicWriteCBParams *params) |
yihui | 0:e910d9bb040f | 49 | { |
adarsh5723 | 10:d4cd8edc6216 | 50 | char *str = 0; |
adarsh5723 | 10:d4cd8edc6216 | 51 | int count = 0; |
adarsh5723 | 10:d4cd8edc6216 | 52 | |
rgrover1 | 6:e0fc9072e853 | 53 | if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { |
rgrover1 | 5:4bc41267a03a | 54 | uint16_t bytesRead = params->len; |
rgrover1 | 5:4bc41267a03a | 55 | DEBUG("received %u bytes\n\r", bytesRead); |
adarsh5723 | 10:d4cd8edc6216 | 56 | |
adarsh5723 | 10:d4cd8edc6216 | 57 | str = (char *)malloc((sizeof(char) * bytesRead) + 1); |
adarsh5723 | 10:d4cd8edc6216 | 58 | while(count <= bytesRead) |
adarsh5723 | 10:d4cd8edc6216 | 59 | { |
adarsh5723 | 10:d4cd8edc6216 | 60 | *(str + count) = *(params->data + count); |
adarsh5723 | 10:d4cd8edc6216 | 61 | count++; |
adarsh5723 | 10:d4cd8edc6216 | 62 | } |
adarsh5723 | 10:d4cd8edc6216 | 63 | count--; |
adarsh5723 | 10:d4cd8edc6216 | 64 | *(str + count) = '\0'; |
adarsh5723 | 10:d4cd8edc6216 | 65 | DEBUG("payload = %s\n\r",str); |
adarsh5723 | 10:d4cd8edc6216 | 66 | if (strncmp(str,"On",bytesRead) == 0) |
adarsh5723 | 10:d4cd8edc6216 | 67 | led2 = 1; |
adarsh5723 | 10:d4cd8edc6216 | 68 | else |
adarsh5723 | 10:d4cd8edc6216 | 69 | led2 = 0; |
adarsh5723 | 10:d4cd8edc6216 | 70 | free(str); |
adarsh5723 | 10:d4cd8edc6216 | 71 | strcpy((char *)params->data,"SPOPOPOSP"); |
rgrover1 | 6:e0fc9072e853 | 72 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
yihui | 0:e910d9bb040f | 73 | } |
Rohit Grover |
2:e060367b9024 | 74 | } |
yihui | 0:e910d9bb040f | 75 | |
Rohit Grover |
2:e060367b9024 | 76 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 77 | { |
rgrover1 | 6:e0fc9072e853 | 78 | led1 = !led1; |
Rohit Grover |
2:e060367b9024 | 79 | } |
yihui | 0:e910d9bb040f | 80 | |
yihui | 0:e910d9bb040f | 81 | int main(void) |
yihui | 0:e910d9bb040f | 82 | { |
Rohit Grover |
2:e060367b9024 | 83 | led1 = 1; |
Rohit Grover |
2:e060367b9024 | 84 | Ticker ticker; |
Rohit Grover |
2:e060367b9024 | 85 | ticker.attach(periodicCallback, 1); |
yihui | 0:e910d9bb040f | 86 | |
Rohit Grover |
2:e060367b9024 | 87 | DEBUG("Initialising the nRF51822\n\r"); |
Rohit Grover |
2:e060367b9024 | 88 | ble.init(); |
Rohit Grover |
2:e060367b9024 | 89 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
2:e060367b9024 | 90 | ble.onDataWritten(onDataWritten); |
yihui | 0:e910d9bb040f | 91 | |
Rohit Grover |
2:e060367b9024 | 92 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 93 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 94 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 95 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
adarsh5723 | 10:d4cd8edc6216 | 96 | (const uint8_t *)"AdarshBLE UART", sizeof("AdarshBLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 97 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
rgrover1 | 6:e0fc9072e853 | 98 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
yihui | 0:e910d9bb040f | 99 | |
Rohit Grover |
2:e060367b9024 | 100 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
Rohit Grover |
2:e060367b9024 | 101 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 102 | |
rgrover1 | 6:e0fc9072e853 | 103 | UARTService uartService(ble); |
rgrover1 | 6:e0fc9072e853 | 104 | uartServicePtr = &uartService; |
yihui | 0:e910d9bb040f | 105 | |
adarsh5723 | 11:4f9451eaca3d | 106 | while (true) { |
Rohit Grover |
2:e060367b9024 | 107 | ble.waitForEvent(); |
yihui | 0:e910d9bb040f | 108 | } |
yihui | 0:e910d9bb040f | 109 | } |