aa

Dependencies:   BLE_API Peripheral_1 mbed nRF51822

Fork of Peripheral_1 by -deleted-

Committer:
ssenwkdw
Date:
Mon Jan 18 01:42:57 2016 +0000
Revision:
4:fb9e24ab03d8
Parent:
3:3f4aa38b0fb7
ADD demo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DDUdui 0:482d3fcc94f4 1 /* mbed Microcontroller Library
DDUdui 0:482d3fcc94f4 2 * Copyright (c) 2006-2015 ARM Limited
DDUdui 0:482d3fcc94f4 3 *
DDUdui 0:482d3fcc94f4 4 * Licensed under the Apache License, Version 2.0 (the "License");
DDUdui 0:482d3fcc94f4 5 * you may not use this file except in compliance with the License.
DDUdui 0:482d3fcc94f4 6 * You may obtain a copy of the License at
DDUdui 0:482d3fcc94f4 7 *
DDUdui 0:482d3fcc94f4 8 * http://www.apache.org/licenses/LICENSE-2.0
DDUdui 0:482d3fcc94f4 9 *
DDUdui 0:482d3fcc94f4 10 * Unless required by applicable law or agreed to in writing, software
DDUdui 0:482d3fcc94f4 11 * distributed under the License is distributed on an "AS IS" BASIS,
DDUdui 0:482d3fcc94f4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
DDUdui 0:482d3fcc94f4 13 * See the License for the specific language governing permissions and
DDUdui 0:482d3fcc94f4 14 * limitations under the License.
DDUdui 0:482d3fcc94f4 15 */
ssenwkdw 4:fb9e24ab03d8 16 #include <cstdlib>
ssenwkdw 4:fb9e24ab03d8 17 #include <ctime>
ssenwkdw 4:fb9e24ab03d8 18 #include <math.h>
DDUdui 0:482d3fcc94f4 19 #include "mbed.h"
DDUdui 0:482d3fcc94f4 20 #include "ble/BLE.h"
DDUdui 0:482d3fcc94f4 21 #include "ble/DiscoveredCharacteristic.h"
DDUdui 0:482d3fcc94f4 22 #include "ble/DiscoveredService.h"
ssenwkdw 1:c227f3064399 23
DDUdui 0:482d3fcc94f4 24 BLE ble;
ssenwkdw 1:c227f3064399 25 DigitalOut led(P0_19,1);
ssenwkdw 1:c227f3064399 26 DigitalOut myled(P0_8);
ssenwkdw 1:c227f3064399 27 DigitalOut neo(P0_6,1);
ssenwkdw 1:c227f3064399 28 DigitalOut pin4(P0_4);
ssenwkdw 1:c227f3064399 29 DigitalOut pin5(P0_5);
ssenwkdw 1:c227f3064399 30 DigitalOut pin15(P0_15);
ssenwkdw 1:c227f3064399 31 DigitalOut pin29(P0_29);
ssenwkdw 1:c227f3064399 32
ssenwkdw 4:fb9e24ab03d8 33 static const unsigned NUM_ROBOTS = 4;
ssenwkdw 1:c227f3064399 34
ssenwkdw 1:c227f3064399 35 uint16_t customServiceUUID = 0xA000;
ssenwkdw 1:c227f3064399 36 uint16_t readwriteCharUUID = 0xA001;
ssenwkdw 1:c227f3064399 37
ssenwkdw 4:fb9e24ab03d8 38 uint8_t info[NUM_ROBOTS]= {0,0,0,1};
ssenwkdw 1:c227f3064399 39
ssenwkdw 4:fb9e24ab03d8 40 ReadWriteArrayGattCharacteristic<uint8_t, NUM_ROBOTS> readwriteChar(readwriteCharUUID, info);
ssenwkdw 1:c227f3064399 41
ssenwkdw 1:c227f3064399 42 GattCharacteristic *characteristics[] = {&readwriteChar};
ssenwkdw 1:c227f3064399 43
ssenwkdw 1:c227f3064399 44 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
ssenwkdw 1:c227f3064399 45
ssenwkdw 1:c227f3064399 46 DiscoveredCharacteristic characteristics_discovered;
DDUdui 0:482d3fcc94f4 47
ssenwkdw 4:fb9e24ab03d8 48 uint8_t rgb=1;
ssenwkdw 1:c227f3064399 49 uint8_t info_t[NUM_ROBOTS]= {0,};
ssenwkdw 1:c227f3064399 50
ssenwkdw 1:c227f3064399 51 void RGB_Show(uint8_t r, uint8_t g, uint8_t b)
ssenwkdw 1:c227f3064399 52 {
ssenwkdw 1:c227f3064399 53 uint8_t rgb[3] = {g, r, b};
ssenwkdw 1:c227f3064399 54 uint8_t *p = rgb;
ssenwkdw 1:c227f3064399 55 uint8_t *end = p + 3;
ssenwkdw 1:c227f3064399 56 while (p < end) {
ssenwkdw 1:c227f3064399 57 uint8_t pix = *p++;
ssenwkdw 1:c227f3064399 58 for (uint8_t mask = 0x80; mask; mask >>= 1) {
ssenwkdw 1:c227f3064399 59 if (pix & mask) {
ssenwkdw 1:c227f3064399 60 // T1H 760ns
ssenwkdw 1:c227f3064399 61 NRF_GPIO->OUTSET = (1UL << 8);
ssenwkdw 1:c227f3064399 62 NRF_GPIO->OUTSET = (1UL << 8);
ssenwkdw 1:c227f3064399 63 NRF_GPIO->OUTSET = (1UL << 8);
ssenwkdw 1:c227f3064399 64 // T1L 660ns
ssenwkdw 1:c227f3064399 65 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 66 } else {
ssenwkdw 1:c227f3064399 67 // T0H 380ns
ssenwkdw 1:c227f3064399 68 NRF_GPIO->OUTSET = (1UL << 8);
ssenwkdw 1:c227f3064399 69 // T0L 840ns
ssenwkdw 1:c227f3064399 70 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 71 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 72 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 73 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 74 }
ssenwkdw 1:c227f3064399 75 }
ssenwkdw 1:c227f3064399 76 }
ssenwkdw 1:c227f3064399 77 NRF_GPIO->OUTCLR = (1UL << 8);
ssenwkdw 1:c227f3064399 78 wait_us(100);
ssenwkdw 1:c227f3064399 79 }
DDUdui 0:482d3fcc94f4 80
DDUdui 0:482d3fcc94f4 81 void periodicCallback(void)
DDUdui 0:482d3fcc94f4 82 {
ssenwkdw 1:c227f3064399 83 led=!led;
DDUdui 0:482d3fcc94f4 84 }
DDUdui 0:482d3fcc94f4 85
ssenwkdw 1:c227f3064399 86 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
ssenwkdw 1:c227f3064399 87 {
ssenwkdw 4:fb9e24ab03d8 88
DDUdui 0:482d3fcc94f4 89 }
ssenwkdw 1:c227f3064399 90
ssenwkdw 1:c227f3064399 91 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
ssenwkdw 1:c227f3064399 92 {
ssenwkdw 4:fb9e24ab03d8 93 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x]\r\n",
ssenwkdw 4:fb9e24ab03d8 94 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
ssenwkdw 1:c227f3064399 95 ble.gap().stopAdvertising();
DDUdui 0:482d3fcc94f4 96 }
ssenwkdw 1:c227f3064399 97
ssenwkdw 1:c227f3064399 98 void onDataWrittenCallback(const GattWriteCallbackParams *params)
ssenwkdw 1:c227f3064399 99 {
ssenwkdw 1:c227f3064399 100 ble.gap().disconnect(params->connHandle,Gap::REMOTE_USER_TERMINATED_CONNECTION);
ssenwkdw 1:c227f3064399 101 }
ssenwkdw 1:c227f3064399 102
DDUdui 0:482d3fcc94f4 103 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
DDUdui 0:482d3fcc94f4 104 {
ssenwkdw 4:fb9e24ab03d8 105 printf("Current info : ");
ssenwkdw 1:c227f3064399 106 for(int i=0; i<NUM_ROBOTS; i++) {
ssenwkdw 1:c227f3064399 107 printf("%u \t",info[i]);
ssenwkdw 1:c227f3064399 108 }
ssenwkdw 1:c227f3064399 109 printf("\r\n");
ssenwkdw 1:c227f3064399 110
ssenwkdw 4:fb9e24ab03d8 111 wait(10);
ssenwkdw 4:fb9e24ab03d8 112
ssenwkdw 4:fb9e24ab03d8 113 if(rgb==7) {
ssenwkdw 4:fb9e24ab03d8 114 rgb = 1;
ssenwkdw 4:fb9e24ab03d8 115 }else {
ssenwkdw 4:fb9e24ab03d8 116 rgb++;
ssenwkdw 4:fb9e24ab03d8 117 }
ssenwkdw 4:fb9e24ab03d8 118
ssenwkdw 4:fb9e24ab03d8 119 info[0] = rgb%2;
ssenwkdw 4:fb9e24ab03d8 120 info[1] = (rgb>>1)%2;
ssenwkdw 4:fb9e24ab03d8 121 info[2] = (rgb>>2)%2;
ssenwkdw 4:fb9e24ab03d8 122 info[3] = 1;
ssenwkdw 4:fb9e24ab03d8 123
ssenwkdw 4:fb9e24ab03d8 124 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ssenwkdw 4:fb9e24ab03d8 125 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)info, sizeof(info));
ssenwkdw 4:fb9e24ab03d8 126 ble.gap().setAdvertisingInterval(59);
ssenwkdw 4:fb9e24ab03d8 127 ble.gap().startAdvertising();
ssenwkdw 4:fb9e24ab03d8 128 }
ssenwkdw 1:c227f3064399 129
ssenwkdw 4:fb9e24ab03d8 130 int main(void)
ssenwkdw 4:fb9e24ab03d8 131 {
ssenwkdw 4:fb9e24ab03d8 132 Ticker ticker;
ssenwkdw 4:fb9e24ab03d8 133 ticker.attach(periodicCallback, 1);
ssenwkdw 4:fb9e24ab03d8 134
ssenwkdw 4:fb9e24ab03d8 135 info[0] = rgb%2;
ssenwkdw 4:fb9e24ab03d8 136 info[1] = (rgb>>1)%2;
ssenwkdw 4:fb9e24ab03d8 137 info[2] = (rgb>>2)%2;
ssenwkdw 4:fb9e24ab03d8 138
ssenwkdw 4:fb9e24ab03d8 139 printf("Source node \r\n");
ssenwkdw 4:fb9e24ab03d8 140 printf("Current info : ");
ssenwkdw 4:fb9e24ab03d8 141 for(int i=0; i<NUM_ROBOTS; i++) {
ssenwkdw 4:fb9e24ab03d8 142 printf("%u \t",info[i]);
ssenwkdw 4:fb9e24ab03d8 143 }
ssenwkdw 4:fb9e24ab03d8 144 printf("\r\n");
ssenwkdw 4:fb9e24ab03d8 145
ssenwkdw 4:fb9e24ab03d8 146 ble.init();
ssenwkdw 3:3f4aa38b0fb7 147 ble.gap().onConnection(connectionCallback);
ssenwkdw 3:3f4aa38b0fb7 148 ble.gap().onDisconnection(disconnectionCallback);
ssenwkdw 1:c227f3064399 149 ble.gattServer().onDataWritten(onDataWrittenCallback);
DDUdui 0:482d3fcc94f4 150 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ssenwkdw 4:fb9e24ab03d8 151 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)info, sizeof(info));
ssenwkdw 4:fb9e24ab03d8 152 ble.gap().setAdvertisingInterval(59);
ssenwkdw 4:fb9e24ab03d8 153 ble.gap().startAdvertising();
ssenwkdw 4:fb9e24ab03d8 154
ssenwkdw 1:c227f3064399 155 ble.addService(customService);
ssenwkdw 4:fb9e24ab03d8 156
DDUdui 0:482d3fcc94f4 157 while (true) {
DDUdui 0:482d3fcc94f4 158 ble.waitForEvent();
ssenwkdw 4:fb9e24ab03d8 159 RGB_Show(info[0],info[1],info[2]);
DDUdui 0:482d3fcc94f4 160 }
ssenwkdw 4:fb9e24ab03d8 161 }