to cai

Dependencies:   BLE_API nRF51822

Fork of BLE_NODE_TEST by Yihui Xiong

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
11:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

UserRevisionLine numberNew 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"
yihui 9:05f0b5a3a70a 19 #include "DFUService.h"
rgrover1 6:e0fc9072e853 20 #include "UARTService.h"
yihui 9:05f0b5a3a70a 21 #include "nrf_delay.h"
yihui 9:05f0b5a3a70a 22 #include "battery.h"
yihui 0:e910d9bb040f 23
yihui 9:05f0b5a3a70a 24 #define DEBUG 0
yihui 9:05f0b5a3a70a 25
yihui 0:e910d9bb040f 26
yihui 9:05f0b5a3a70a 27 #define LOG(...)
yihui 9:05f0b5a3a70a 28 #define BUTTON_DOWN 0
yihui 9:05f0b5a3a70a 29 #define LED_ON 0
yihui 9:05f0b5a3a70a 30 #define LED_OFF 1
yihui 9:05f0b5a3a70a 31
yihui 9:05f0b5a3a70a 32 DigitalOut blue(p18);
yihui 9:05f0b5a3a70a 33 DigitalOut green(p17);
yihui 9:05f0b5a3a70a 34 InterruptIn button(p30);
yihui 9:05f0b5a3a70a 35 Battery battery(p5);
yihui 11:22480ac31879 36 AnalogIn vcc(p6);
yihui 0:e910d9bb040f 37
Rohit Grover 2:e060367b9024 38 BLEDevice ble;
yihui 9:05f0b5a3a70a 39 UARTService *uartServicePtr;
yihui 9:05f0b5a3a70a 40 Ticker ticker;
yihui 9:05f0b5a3a70a 41
yihui 9:05f0b5a3a70a 42 BusOut outputGrove(p3, p4);
yihui 9:05f0b5a3a70a 43 BusIn inputGrove(p1, p2);
yihui 11:22480ac31879 44 BusIn charge(p29, p28);
yihui 9:05f0b5a3a70a 45
yihui 9:05f0b5a3a70a 46 volatile bool button_event = false;
yihui 0:e910d9bb040f 47
yihui 9:05f0b5a3a70a 48 static const uint8_t SIZEOF_TX_RX_BUFFER = 32;
yihui 9:05f0b5a3a70a 49 uint8_t rxPayload[SIZEOF_TX_RX_BUFFER] = {0,};
yihui 9:05f0b5a3a70a 50
yihui 9:05f0b5a3a70a 51 extern "C" void power_on();
yihui 9:05f0b5a3a70a 52 extern "C" void power_off();
yihui 9:05f0b5a3a70a 53
yihui 9:05f0b5a3a70a 54
yihui 9:05f0b5a3a70a 55 int button_detect();
yihui 0:e910d9bb040f 56
rgrover1 5:4bc41267a03a 57 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
yihui 0:e910d9bb040f 58 {
yihui 9:05f0b5a3a70a 59 LOG("Disconnected!\n");
yihui 9:05f0b5a3a70a 60 LOG("Restarting the advertising process\n");
Rohit Grover 2:e060367b9024 61 ble.startAdvertising();
Rohit Grover 2:e060367b9024 62 }
yihui 0:e910d9bb040f 63
rgrover1 6:e0fc9072e853 64 void onDataWritten(const GattCharacteristicWriteCBParams *params)
yihui 0:e910d9bb040f 65 {
rgrover1 6:e0fc9072e853 66 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
rgrover1 5:4bc41267a03a 67 uint16_t bytesRead = params->len;
yihui 9:05f0b5a3a70a 68 LOG("received %u bytes\n\r", bytesRead);
yihui 9:05f0b5a3a70a 69 if (bytesRead < sizeof(rxPayload)) {
yihui 9:05f0b5a3a70a 70 memcpy(rxPayload, params->data, bytesRead);
yihui 9:05f0b5a3a70a 71 rxPayload[bytesRead] = '\0';
yihui 9:05f0b5a3a70a 72 }
yihui 9:05f0b5a3a70a 73
yihui 9:05f0b5a3a70a 74 LOG("%s\n", (char *)rxPayload);
yihui 9:05f0b5a3a70a 75
yihui 0:e910d9bb040f 76 }
Rohit Grover 2:e060367b9024 77 }
yihui 0:e910d9bb040f 78
yihui 9:05f0b5a3a70a 79 void tick(void)
Rohit Grover 2:e060367b9024 80 {
yihui 9:05f0b5a3a70a 81 green = !green;
yihui 9:05f0b5a3a70a 82
yihui 9:05f0b5a3a70a 83 static uint8_t output = 0x01;
yihui 9:05f0b5a3a70a 84 uint8_t input;
yihui 9:05f0b5a3a70a 85
yihui 9:05f0b5a3a70a 86 outputGrove = output;
yihui 9:05f0b5a3a70a 87
yihui 9:05f0b5a3a70a 88 uartServicePtr->printf("battery:%3.2fV\n", battery.read());
yihui 9:05f0b5a3a70a 89
yihui 11:22480ac31879 90 uartServicePtr->printf("vcc:%3.2fV\n", vcc.read() * 1.2 * 12.2 / 2.2);
yihui 11:22480ac31879 91
yihui 9:05f0b5a3a70a 92
yihui 9:05f0b5a3a70a 93 input = inputGrove;
yihui 9:05f0b5a3a70a 94 uartServicePtr->printf("o->i:%d->%d\n", output, input);
yihui 9:05f0b5a3a70a 95
yihui 9:05f0b5a3a70a 96 output = 3 - output;
yihui 9:05f0b5a3a70a 97
yihui 9:05f0b5a3a70a 98 uint8_t chargeStatus = charge;
yihui 9:05f0b5a3a70a 99 uartServicePtr->printf("charge:%d\n", chargeStatus);
yihui 9:05f0b5a3a70a 100 }
yihui 9:05f0b5a3a70a 101
yihui 9:05f0b5a3a70a 102 void button_down(void)
yihui 9:05f0b5a3a70a 103 {
yihui 9:05f0b5a3a70a 104 button_event = true;
Rohit Grover 2:e060367b9024 105 }
yihui 0:e910d9bb040f 106
yihui 0:e910d9bb040f 107 int main(void)
yihui 0:e910d9bb040f 108 {
yihui 9:05f0b5a3a70a 109 power_on();
yihui 9:05f0b5a3a70a 110 blue = LED_ON;
yihui 9:05f0b5a3a70a 111 green = LED_ON;
yihui 9:05f0b5a3a70a 112
yihui 9:05f0b5a3a70a 113 #if BUTTON_DOWN
yihui 9:05f0b5a3a70a 114 button.mode(PullDown);
yihui 9:05f0b5a3a70a 115 button.rise(button_down);
yihui 9:05f0b5a3a70a 116 #else
yihui 9:05f0b5a3a70a 117 button.mode(PullUp);
yihui 9:05f0b5a3a70a 118 button.fall(button_down);
yihui 9:05f0b5a3a70a 119 #endif
yihui 0:e910d9bb040f 120
yihui 9:05f0b5a3a70a 121 LOG("Initialising the nRF51822\n");
Rohit Grover 2:e060367b9024 122 ble.init();
Rohit Grover 2:e060367b9024 123 ble.onDisconnection(disconnectionCallback);
Rohit Grover 2:e060367b9024 124 ble.onDataWritten(onDataWritten);
yihui 0:e910d9bb040f 125
Rohit Grover 2:e060367b9024 126 /* setup advertising */
Rohit Grover 2:e060367b9024 127 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 2:e060367b9024 128 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 2:e060367b9024 129 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
yihui 9:05f0b5a3a70a 130 (const uint8_t *)"NODE TEST", sizeof("NODE TEST"));
Rohit Grover 2:e060367b9024 131 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 6:e0fc9072e853 132 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
yihui 0:e910d9bb040f 133
yihui 9:05f0b5a3a70a 134 //DFUService dfu(ble);
yihui 0:e910d9bb040f 135
rgrover1 6:e0fc9072e853 136 UARTService uartService(ble);
yihui 9:05f0b5a3a70a 137 uartService.retargetStdout();
rgrover1 6:e0fc9072e853 138 uartServicePtr = &uartService;
yihui 9:05f0b5a3a70a 139
yihui 9:05f0b5a3a70a 140 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
yihui 9:05f0b5a3a70a 141 ble.startAdvertising();
yihui 9:05f0b5a3a70a 142
yihui 9:05f0b5a3a70a 143 blue = LED_OFF;
yihui 9:05f0b5a3a70a 144
yihui 9:05f0b5a3a70a 145 ticker.attach(tick, 3);
yihui 0:e910d9bb040f 146
Rohit Grover 2:e060367b9024 147 while (true) {
yihui 9:05f0b5a3a70a 148 if (button_event) {
yihui 9:05f0b5a3a70a 149 int click;
yihui 9:05f0b5a3a70a 150
yihui 9:05f0b5a3a70a 151 blue = LED_ON;
yihui 9:05f0b5a3a70a 152 click = button_detect();
yihui 9:05f0b5a3a70a 153 blue = LED_OFF;
yihui 9:05f0b5a3a70a 154 LOG("click type: %d\n\r", click);
yihui 9:05f0b5a3a70a 155
yihui 9:05f0b5a3a70a 156 button_event = false;
yihui 9:05f0b5a3a70a 157
yihui 9:05f0b5a3a70a 158 if (1 == click) {
yihui 9:05f0b5a3a70a 159 blue = !blue;
yihui 9:05f0b5a3a70a 160 } else if (2 == click) {
yihui 9:05f0b5a3a70a 161 //green = LED_ON;
yihui 9:05f0b5a3a70a 162 } else if (-1 == click) {
yihui 9:05f0b5a3a70a 163 ticker.detach();
yihui 9:05f0b5a3a70a 164 green = LED_OFF;
yihui 9:05f0b5a3a70a 165 blue = LED_OFF;
yihui 9:05f0b5a3a70a 166 while (BUTTON_DOWN == button.read()) {
yihui 9:05f0b5a3a70a 167
yihui 9:05f0b5a3a70a 168 }
yihui 9:05f0b5a3a70a 169 nrf_delay_us(3000);
yihui 9:05f0b5a3a70a 170
yihui 9:05f0b5a3a70a 171 power_off();
yihui 9:05f0b5a3a70a 172 } else {
yihui 9:05f0b5a3a70a 173 continue;
yihui 9:05f0b5a3a70a 174 }
yihui 9:05f0b5a3a70a 175
yihui 9:05f0b5a3a70a 176 } else {
yihui 9:05f0b5a3a70a 177 ble.waitForEvent();
yihui 9:05f0b5a3a70a 178 }
yihui 0:e910d9bb040f 179 }
yihui 0:e910d9bb040f 180 }
yihui 9:05f0b5a3a70a 181
yihui 9:05f0b5a3a70a 182 int button_detect(void)
yihui 9:05f0b5a3a70a 183 {
yihui 9:05f0b5a3a70a 184 int t = 0;
yihui 9:05f0b5a3a70a 185
yihui 9:05f0b5a3a70a 186 while (1) {
yihui 9:05f0b5a3a70a 187 if (button.read() != BUTTON_DOWN) {
yihui 9:05f0b5a3a70a 188 if (t < 30) {
yihui 9:05f0b5a3a70a 189 return 0; // for anti shake
yihui 9:05f0b5a3a70a 190 } else {
yihui 9:05f0b5a3a70a 191 break;
yihui 9:05f0b5a3a70a 192 }
yihui 9:05f0b5a3a70a 193 }
yihui 9:05f0b5a3a70a 194
yihui 9:05f0b5a3a70a 195 if (t > 30000) { // More than 3 seconds
yihui 9:05f0b5a3a70a 196 return -1; // long click
yihui 9:05f0b5a3a70a 197 }
yihui 9:05f0b5a3a70a 198
yihui 9:05f0b5a3a70a 199 t++;
yihui 9:05f0b5a3a70a 200 nrf_delay_us(100);
yihui 9:05f0b5a3a70a 201 }
yihui 9:05f0b5a3a70a 202
yihui 9:05f0b5a3a70a 203 if (t > 4000) { // More than 0.4 seconds
yihui 9:05f0b5a3a70a 204 return 1; // single click
yihui 9:05f0b5a3a70a 205 }
yihui 9:05f0b5a3a70a 206
yihui 9:05f0b5a3a70a 207 while (true) {
yihui 9:05f0b5a3a70a 208 if (button.read() == BUTTON_DOWN) {
yihui 9:05f0b5a3a70a 209 nrf_delay_us(1000);
yihui 9:05f0b5a3a70a 210 if (button.read() == BUTTON_DOWN) {
yihui 9:05f0b5a3a70a 211 return 2; // double click
yihui 9:05f0b5a3a70a 212 }
yihui 9:05f0b5a3a70a 213
yihui 9:05f0b5a3a70a 214 t += 10;
yihui 9:05f0b5a3a70a 215 }
yihui 9:05f0b5a3a70a 216
yihui 9:05f0b5a3a70a 217 if (t > 4000) {
yihui 9:05f0b5a3a70a 218 return 1; // The interval of double click should less than 0.4 seconds, so it's single click
yihui 9:05f0b5a3a70a 219 }
yihui 9:05f0b5a3a70a 220
yihui 9:05f0b5a3a70a 221 t++;
yihui 9:05f0b5a3a70a 222 nrf_delay_us(100);
yihui 9:05f0b5a3a70a 223 }
yihui 9:05f0b5a3a70a 224 }