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@14:a8c2ad841fc6, 2014-11-10 (annotated)
- Committer:
- adarsh5723
- Date:
- Mon Nov 10 11:29:20 2014 +0000
- Revision:
- 14:a8c2ad841fc6
- Parent:
- 13:4e4ec6fe8123
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
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 | 14:a8c2ad841fc6 | 49 | //Output string to indicate the status of the LDRs |
adarsh5723 | 13:4e4ec6fe8123 | 50 | char output[50] = "LDR0 = w x y z"; |
yihui | 0:e910d9bb040f | 51 | |
rgrover1 | 6:e0fc9072e853 | 52 | UARTService *uartServicePtr; |
adarsh5723 | 13:4e4ec6fe8123 | 53 | #define TIMER 0 |
adarsh5723 | 12:51e2c29e3019 | 54 | //Creating timer Instance |
adarsh5723 | 12:51e2c29e3019 | 55 | |
adarsh5723 | 12:51e2c29e3019 | 56 | Ticker ticker; |
adarsh5723 | 12:51e2c29e3019 | 57 | #if TIMER |
adarsh5723 | 12:51e2c29e3019 | 58 | long int timerCount = 0; |
adarsh5723 | 12:51e2c29e3019 | 59 | #endif |
yihui | 0:e910d9bb040f | 60 | |
rgrover1 | 5:4bc41267a03a | 61 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
yihui | 0:e910d9bb040f | 62 | { |
Rohit Grover |
2:e060367b9024 | 63 | DEBUG("Disconnected!\n\r"); |
Rohit Grover |
2:e060367b9024 | 64 | DEBUG("Restarting the advertising process\n\r"); |
Rohit Grover |
2:e060367b9024 | 65 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 66 | } |
yihui | 0:e910d9bb040f | 67 | |
adarsh5723 | 14:a8c2ad841fc6 | 68 | //Thresholding the values of the LDRs |
adarsh5723 | 13:4e4ec6fe8123 | 69 | void getValuesofLDR() |
adarsh5723 | 13:4e4ec6fe8123 | 70 | { |
adarsh5723 | 14:a8c2ad841fc6 | 71 | if (ldr0.read() < 0.52f) |
adarsh5723 | 13:4e4ec6fe8123 | 72 | output[7] = '0'; |
adarsh5723 | 13:4e4ec6fe8123 | 73 | else |
adarsh5723 | 13:4e4ec6fe8123 | 74 | output[7] = '1'; |
adarsh5723 | 14:a8c2ad841fc6 | 75 | if (ldr0.read() < 0.52f) |
adarsh5723 | 13:4e4ec6fe8123 | 76 | output[9] = '0'; |
adarsh5723 | 13:4e4ec6fe8123 | 77 | else |
adarsh5723 | 13:4e4ec6fe8123 | 78 | output[9] = '1'; |
adarsh5723 | 14:a8c2ad841fc6 | 79 | if (ldr0.read() < 0.52f) |
adarsh5723 | 13:4e4ec6fe8123 | 80 | output[11] = '0'; |
adarsh5723 | 13:4e4ec6fe8123 | 81 | else |
adarsh5723 | 13:4e4ec6fe8123 | 82 | output[11] = '1'; |
adarsh5723 | 14:a8c2ad841fc6 | 83 | if (ldr0.read() < 0.52f) |
adarsh5723 | 13:4e4ec6fe8123 | 84 | output[13] = '0'; |
adarsh5723 | 13:4e4ec6fe8123 | 85 | else |
adarsh5723 | 13:4e4ec6fe8123 | 86 | output[13] = '1'; |
adarsh5723 | 13:4e4ec6fe8123 | 87 | |
adarsh5723 | 13:4e4ec6fe8123 | 88 | } |
adarsh5723 | 13:4e4ec6fe8123 | 89 | |
adarsh5723 | 14:a8c2ad841fc6 | 90 | //Preparation of output string |
rgrover1 | 6:e0fc9072e853 | 91 | void onDataWritten(const GattCharacteristicWriteCBParams *params) |
yihui | 0:e910d9bb040f | 92 | { |
adarsh5723 | 10:d4cd8edc6216 | 93 | char *str = 0; |
adarsh5723 | 10:d4cd8edc6216 | 94 | int count = 0; |
adarsh5723 | 10:d4cd8edc6216 | 95 | |
rgrover1 | 6:e0fc9072e853 | 96 | if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { |
rgrover1 | 5:4bc41267a03a | 97 | uint16_t bytesRead = params->len; |
rgrover1 | 5:4bc41267a03a | 98 | DEBUG("received %u bytes\n\r", bytesRead); |
adarsh5723 | 10:d4cd8edc6216 | 99 | |
adarsh5723 | 14:a8c2ad841fc6 | 100 | //Obtaining user command |
adarsh5723 | 10:d4cd8edc6216 | 101 | str = (char *)malloc((sizeof(char) * bytesRead) + 1); |
adarsh5723 | 10:d4cd8edc6216 | 102 | while(count <= bytesRead) |
adarsh5723 | 10:d4cd8edc6216 | 103 | { |
adarsh5723 | 10:d4cd8edc6216 | 104 | *(str + count) = *(params->data + count); |
adarsh5723 | 10:d4cd8edc6216 | 105 | count++; |
adarsh5723 | 10:d4cd8edc6216 | 106 | } |
adarsh5723 | 10:d4cd8edc6216 | 107 | count--; |
adarsh5723 | 10:d4cd8edc6216 | 108 | *(str + count) = '\0'; |
adarsh5723 | 10:d4cd8edc6216 | 109 | DEBUG("payload = %s\n\r",str); |
adarsh5723 | 14:a8c2ad841fc6 | 110 | |
adarsh5723 | 14:a8c2ad841fc6 | 111 | //Comparing user input against keywords |
adarsh5723 | 10:d4cd8edc6216 | 112 | if (strncmp(str,"On",bytesRead) == 0) |
adarsh5723 | 13:4e4ec6fe8123 | 113 | { |
adarsh5723 | 14:a8c2ad841fc6 | 114 | led2 = 0; |
adarsh5723 | 14:a8c2ad841fc6 | 115 | pwmOut = 0; |
adarsh5723 | 13:4e4ec6fe8123 | 116 | bytesRead = 7; |
adarsh5723 | 13:4e4ec6fe8123 | 117 | strncpy((char *)params->data,"Lamp On", bytesRead); |
adarsh5723 | 13:4e4ec6fe8123 | 118 | } |
adarsh5723 | 13:4e4ec6fe8123 | 119 | else if (strncmp(str,"Off",bytesRead) == 0) |
adarsh5723 | 13:4e4ec6fe8123 | 120 | { |
adarsh5723 | 14:a8c2ad841fc6 | 121 | led2 = 1; |
adarsh5723 | 14:a8c2ad841fc6 | 122 | pwmOut = 1; |
adarsh5723 | 14:a8c2ad841fc6 | 123 | bytesRead = 8; |
adarsh5723 | 13:4e4ec6fe8123 | 124 | strncpy((char *)params->data,"Lamp Off", bytesRead); |
adarsh5723 | 13:4e4ec6fe8123 | 125 | } |
adarsh5723 | 13:4e4ec6fe8123 | 126 | else if (strncmp(str,"Read",bytesRead) == 0) |
adarsh5723 | 13:4e4ec6fe8123 | 127 | { |
adarsh5723 | 13:4e4ec6fe8123 | 128 | bytesRead = 13; |
adarsh5723 | 13:4e4ec6fe8123 | 129 | getValuesofLDR(); |
adarsh5723 | 13:4e4ec6fe8123 | 130 | strncpy((char *)params->data,output,bytesRead); |
adarsh5723 | 13:4e4ec6fe8123 | 131 | |
adarsh5723 | 13:4e4ec6fe8123 | 132 | } |
adarsh5723 | 14:a8c2ad841fc6 | 133 | else//None of the commands match. Invalid entry |
adarsh5723 | 13:4e4ec6fe8123 | 134 | { |
adarsh5723 | 13:4e4ec6fe8123 | 135 | bytesRead = 14; |
adarsh5723 | 13:4e4ec6fe8123 | 136 | strncpy((char *)params->data,"Invalid Entry", bytesRead); |
adarsh5723 | 13:4e4ec6fe8123 | 137 | } |
adarsh5723 | 13:4e4ec6fe8123 | 138 | free(str); |
rgrover1 | 6:e0fc9072e853 | 139 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
yihui | 0:e910d9bb040f | 140 | } |
Rohit Grover |
2:e060367b9024 | 141 | } |
yihui | 0:e910d9bb040f | 142 | |
Rohit Grover |
2:e060367b9024 | 143 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 144 | { |
adarsh5723 | 12:51e2c29e3019 | 145 | #if TIMER |
adarsh5723 | 12:51e2c29e3019 | 146 | timerCount++; |
adarsh5723 | 12:51e2c29e3019 | 147 | if ((timerCount < threshold)) |
adarsh5723 | 12:51e2c29e3019 | 148 | { |
adarsh5723 | 12:51e2c29e3019 | 149 | led1 = 1; |
adarsh5723 | 12:51e2c29e3019 | 150 | pwm_out = 1; |
adarsh5723 | 12:51e2c29e3019 | 151 | } |
adarsh5723 | 12:51e2c29e3019 | 152 | else |
adarsh5723 | 12:51e2c29e3019 | 153 | { |
adarsh5723 | 12:51e2c29e3019 | 154 | led1 = 0; |
adarsh5723 | 12:51e2c29e3019 | 155 | pwm_out = 0; |
adarsh5723 | 12:51e2c29e3019 | 156 | } |
adarsh5723 | 12:51e2c29e3019 | 157 | if (timerCount == PULSE_WIDTH) |
adarsh5723 | 12:51e2c29e3019 | 158 | timerCount = 0; |
adarsh5723 | 12:51e2c29e3019 | 159 | #else |
rgrover1 | 6:e0fc9072e853 | 160 | led1 = !led1; |
adarsh5723 | 12:51e2c29e3019 | 161 | #endif |
adarsh5723 | 12:51e2c29e3019 | 162 | |
Rohit Grover |
2:e060367b9024 | 163 | } |
yihui | 0:e910d9bb040f | 164 | |
yihui | 0:e910d9bb040f | 165 | int main(void) |
yihui | 0:e910d9bb040f | 166 | { |
Rohit Grover |
2:e060367b9024 | 167 | led1 = 1; |
Rohit Grover |
2:e060367b9024 | 168 | DEBUG("Initialising the nRF51822\n\r"); |
adarsh5723 | 13:4e4ec6fe8123 | 169 | //ticker.attach(periodicCallback, 0.00001f); |
Rohit Grover |
2:e060367b9024 | 170 | ble.init(); |
adarsh5723 | 13:4e4ec6fe8123 | 171 | |
Rohit Grover |
2:e060367b9024 | 172 | ble.onDisconnection(disconnectionCallback); |
adarsh5723 | 14:a8c2ad841fc6 | 173 | |
adarsh5723 | 14:a8c2ad841fc6 | 174 | ble.onDataWritten(onDataWritten); |
adarsh5723 | 13:4e4ec6fe8123 | 175 | |
Rohit Grover |
2:e060367b9024 | 176 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 177 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 178 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 179 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
adarsh5723 | 10:d4cd8edc6216 | 180 | (const uint8_t *)"AdarshBLE UART", sizeof("AdarshBLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 181 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
rgrover1 | 6:e0fc9072e853 | 182 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
yihui | 0:e910d9bb040f | 183 | |
Rohit Grover |
2:e060367b9024 | 184 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
Rohit Grover |
2:e060367b9024 | 185 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 186 | |
rgrover1 | 6:e0fc9072e853 | 187 | UARTService uartService(ble); |
rgrover1 | 6:e0fc9072e853 | 188 | uartServicePtr = &uartService; |
adarsh5723 | 14:a8c2ad841fc6 | 189 | |
adarsh5723 | 12:51e2c29e3019 | 190 | while (true) { |
Rohit Grover |
2:e060367b9024 | 191 | ble.waitForEvent(); |
yihui | 0:e910d9bb040f | 192 | } |
yihui | 0:e910d9bb040f | 193 | } |