Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API Peripheral_1 mbed nRF51822
Fork of Peripheral_1 by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include <cstdlib> 00017 #include <ctime> 00018 #include <math.h> 00019 #include "mbed.h" 00020 #include "ble/BLE.h" 00021 #include "ble/DiscoveredCharacteristic.h" 00022 #include "ble/DiscoveredService.h" 00023 00024 BLE ble; 00025 DigitalOut led(P0_19,1); 00026 DigitalOut myled(P0_8); 00027 DigitalOut neo(P0_6,1); 00028 DigitalOut pin4(P0_4); 00029 DigitalOut pin5(P0_5); 00030 DigitalOut pin15(P0_15); 00031 DigitalOut pin29(P0_29); 00032 00033 static const unsigned NUM_ROBOTS = 4; 00034 00035 uint16_t customServiceUUID = 0xA000; 00036 uint16_t readwriteCharUUID = 0xA001; 00037 00038 uint8_t info[NUM_ROBOTS]= {0,0,0,1}; 00039 00040 ReadWriteArrayGattCharacteristic<uint8_t, NUM_ROBOTS> readwriteChar(readwriteCharUUID, info); 00041 00042 GattCharacteristic *characteristics[] = {&readwriteChar}; 00043 00044 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00045 00046 DiscoveredCharacteristic characteristics_discovered; 00047 00048 uint8_t rgb=1; 00049 uint8_t info_t[NUM_ROBOTS]= {0,}; 00050 00051 void RGB_Show(uint8_t r, uint8_t g, uint8_t b) 00052 { 00053 uint8_t rgb[3] = {g, r, b}; 00054 uint8_t *p = rgb; 00055 uint8_t *end = p + 3; 00056 while (p < end) { 00057 uint8_t pix = *p++; 00058 for (uint8_t mask = 0x80; mask; mask >>= 1) { 00059 if (pix & mask) { 00060 // T1H 760ns 00061 NRF_GPIO->OUTSET = (1UL << 8); 00062 NRF_GPIO->OUTSET = (1UL << 8); 00063 NRF_GPIO->OUTSET = (1UL << 8); 00064 // T1L 660ns 00065 NRF_GPIO->OUTCLR = (1UL << 8); 00066 } else { 00067 // T0H 380ns 00068 NRF_GPIO->OUTSET = (1UL << 8); 00069 // T0L 840ns 00070 NRF_GPIO->OUTCLR = (1UL << 8); 00071 NRF_GPIO->OUTCLR = (1UL << 8); 00072 NRF_GPIO->OUTCLR = (1UL << 8); 00073 NRF_GPIO->OUTCLR = (1UL << 8); 00074 } 00075 } 00076 } 00077 NRF_GPIO->OUTCLR = (1UL << 8); 00078 wait_us(100); 00079 } 00080 00081 void periodicCallback(void) 00082 { 00083 led=!led; 00084 } 00085 00086 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) 00087 { 00088 00089 } 00090 00091 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00092 { 00093 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x]\r\n", 00094 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]); 00095 ble.gap().stopAdvertising(); 00096 } 00097 00098 void onDataWrittenCallback(const GattWriteCallbackParams *params) 00099 { 00100 ble.gap().disconnect(params->connHandle,Gap::REMOTE_USER_TERMINATED_CONNECTION); 00101 } 00102 00103 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00104 { 00105 printf("Current info : "); 00106 for(int i=0; i<NUM_ROBOTS; i++) { 00107 printf("%u \t",info[i]); 00108 } 00109 printf("\r\n"); 00110 00111 wait(10); 00112 00113 if(rgb==7) { 00114 rgb = 1; 00115 }else { 00116 rgb++; 00117 } 00118 00119 info[0] = rgb%2; 00120 info[1] = (rgb>>1)%2; 00121 info[2] = (rgb>>2)%2; 00122 info[3] = 1; 00123 00124 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00125 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)info, sizeof(info)); 00126 ble.gap().setAdvertisingInterval(59); 00127 ble.gap().startAdvertising(); 00128 } 00129 00130 int main(void) 00131 { 00132 Ticker ticker; 00133 ticker.attach(periodicCallback, 1); 00134 00135 info[0] = rgb%2; 00136 info[1] = (rgb>>1)%2; 00137 info[2] = (rgb>>2)%2; 00138 00139 printf("Source node \r\n"); 00140 printf("Current info : "); 00141 for(int i=0; i<NUM_ROBOTS; i++) { 00142 printf("%u \t",info[i]); 00143 } 00144 printf("\r\n"); 00145 00146 ble.init(); 00147 ble.gap().onConnection(connectionCallback); 00148 ble.gap().onDisconnection(disconnectionCallback); 00149 ble.gattServer().onDataWritten(onDataWrittenCallback); 00150 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00151 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::RABOT_REDBEAR_BLE_NANO, (uint8_t *)info, sizeof(info)); 00152 ble.gap().setAdvertisingInterval(59); 00153 ble.gap().startAdvertising(); 00154 00155 ble.addService(customService); 00156 00157 while (true) { 00158 ble.waitForEvent(); 00159 RGB_Show(info[0],info[1],info[2]); 00160 } 00161 }
Generated on Fri Jul 15 2022 00:02:04 by
1.7.2