nRF51822_OBS + mocro:bit_ADV

Dependencies:   mbed BLE_API Adafruit_GFX nRF51822

Тесты по созданию связи между micro:bit и nRF51822 & BLE400 эта часть под nRF51822

Committer:
mamont090671
Date:
Mon Dec 16 13:25:59 2019 +0000
Revision:
14:985bfbee997b
Parent:
13:ae46f7a91beb
Child:
15:23ce464fa0b5
+2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:332983584a9c 1 /* mbed Microcontroller Library
rgrover1 0:332983584a9c 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 0:332983584a9c 3 *
rgrover1 0:332983584a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:332983584a9c 5 * you may not use this file except in compliance with the License.
rgrover1 0:332983584a9c 6 * You may obtain a copy of the License at
rgrover1 0:332983584a9c 7 *
rgrover1 0:332983584a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:332983584a9c 9 *
rgrover1 0:332983584a9c 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:332983584a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:332983584a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:332983584a9c 13 * See the License for the specific language governing permissions and
rgrover1 0:332983584a9c 14 * limitations under the License.
rgrover1 0:332983584a9c 15 */
rgrover1 0:332983584a9c 16
rgrover1 0:332983584a9c 17 #include "mbed.h"
rgrover1 4:dd8231564124 18 #include "BLE.h"
mamont090671 12:0f6e700ca698 19 #include "Adafruit_SSD1306.h"
mamont090671 14:985bfbee997b 20 //#include "bme280.h"
mamont090671 12:0f6e700ca698 21
mamont090671 12:0f6e700ca698 22 #define SDA P0_0
mamont090671 12:0f6e700ca698 23 #define SCL P0_1
sunsmile2015 7:91324daa3bfa 24
mamont090671 10:b7d532c63124 25 #define APP_SPECIFIC_ID_TEST 0x0059 //0xFEFE
sunsmile2015 7:91324daa3bfa 26
sunsmile2015 7:91324daa3bfa 27 #pragma pack(1)
sunsmile2015 7:91324daa3bfa 28 /* Advertising data */
sunsmile2015 8:649bd171929e 29 struct AdvertisingData_t {
sunsmile2015 8:649bd171929e 30 uint8_t length; /* doesn't include itself */
sunsmile2015 7:91324daa3bfa 31 GapAdvertisingData::DataType dataType;
sunsmile2015 7:91324daa3bfa 32 uint8_t data[1];
sunsmile2015 8:649bd171929e 33 };
sunsmile2015 7:91324daa3bfa 34
sunsmile2015 8:649bd171929e 35 struct ApplicationData_t {
sunsmile2015 8:649bd171929e 36 uint16_t applicationSpecificId; /* An ID used to identify temperature value
sunsmile2015 8:649bd171929e 37 in the manufacture specific AD data field */
mamont090671 14:985bfbee997b 38 uint8_t ExtT_Value; /* User defined application data */
mamont090671 11:f6274e2a0b07 39 // uint8_t btnA_Value;
mamont090671 11:f6274e2a0b07 40 // uint8_t btnB_Value;
sunsmile2015 8:649bd171929e 41 };
sunsmile2015 7:91324daa3bfa 42 #pragma pack()
rgrover1 0:332983584a9c 43
mamont090671 14:985bfbee997b 44 //Функции обработки событий кнопок
mamont090671 14:985bfbee997b 45 void triggerfall_1(); //Button1 falling interrupt function
mamont090671 14:985bfbee997b 46 void triggerrise_1(); //Button1 rising interrupt function
mamont090671 14:985bfbee997b 47 void triggerfall_2(); //Button2 falling interrupt function
mamont090671 14:985bfbee997b 48 void triggerrise_2(); //Button2 rising interrupt function
mamont090671 14:985bfbee997b 49
mamont090671 14:985bfbee997b 50 DigitalIn sw1(BUTTON1);
mamont090671 14:985bfbee997b 51 DigitalIn sw2(BUTTON2);
mamont090671 14:985bfbee997b 52
mamont090671 14:985bfbee997b 53 //Initiate input interrupts
mamont090671 14:985bfbee997b 54 InterruptIn sw1Press(BUTTON1);
mamont090671 14:985bfbee997b 55 InterruptIn sw2Press(BUTTON2);
mamont090671 14:985bfbee997b 56 int flag_button;
mamont090671 14:985bfbee997b 57 //float t;
mamont090671 14:985bfbee997b 58 //float pr;
mamont090671 14:985bfbee997b 59 //float h;
mamont090671 14:985bfbee997b 60
mamont090671 12:0f6e700ca698 61 class I2C2 : public I2C
mamont090671 12:0f6e700ca698 62 {
mamont090671 12:0f6e700ca698 63 public:
mamont090671 12:0f6e700ca698 64 I2C2(PinName sda, PinName scl) : I2C(sda, scl)
mamont090671 12:0f6e700ca698 65 {
mamont090671 12:0f6e700ca698 66 frequency(400000);
mamont090671 12:0f6e700ca698 67 start();
mamont090671 12:0f6e700ca698 68 };
mamont090671 12:0f6e700ca698 69 };
mamont090671 12:0f6e700ca698 70
mamont090671 12:0f6e700ca698 71 I2C2 gI2C(SDA,SCL);
mamont090671 12:0f6e700ca698 72 Adafruit_SSD1306_I2c gOled2(gI2C, NC, 0x78, 64, 128);
mamont090671 12:0f6e700ca698 73
rgrover1 5:103717ce54e5 74 BLE ble;
mamont090671 14:985bfbee997b 75 //BME280 bme280;
rgrover1 0:332983584a9c 76 DigitalOut led1(LED1);
rgrover1 0:332983584a9c 77
mamont090671 9:56bb343c76ae 78 Serial pc(USBTX, USBRX);
mamont090671 14:985bfbee997b 79 /*//bme280
mamont090671 14:985bfbee997b 80 void i2cWrite(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size, uint8_t repeated_start)
mamont090671 14:985bfbee997b 81 {
mamont090671 14:985bfbee997b 82 // mbed uses 8-bit addresses, always confusing.
mamont090671 14:985bfbee997b 83 gI2C.write(i2c_address<<1,(const char *)p_data,data_size,repeated_start);
mamont090671 14:985bfbee997b 84 }
mamont090671 9:56bb343c76ae 85
mamont090671 14:985bfbee997b 86 void i2cRead(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size)
mamont090671 14:985bfbee997b 87 {
mamont090671 14:985bfbee997b 88 // mbed uses 8-bit addresses, always confusing.
mamont090671 14:985bfbee997b 89 gI2C.read(i2c_address<<1,(char *)p_data,data_size);
mamont090671 14:985bfbee997b 90 }
mamont090671 14:985bfbee997b 91 */
rgrover1 0:332983584a9c 92 void periodicCallback(void)
rgrover1 0:332983584a9c 93 {
rgrover1 0:332983584a9c 94 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
rgrover1 0:332983584a9c 95 }
rgrover1 0:332983584a9c 96
sunsmile2015 6:850f44146c9f 97 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
sunsmile2015 6:850f44146c9f 98 {
sunsmile2015 8:649bd171929e 99 AdvertisingData_t *pAdvData = NULL;
sunsmile2015 7:91324daa3bfa 100 uint8_t len = 0;
mamont090671 11:f6274e2a0b07 101
sunsmile2015 7:91324daa3bfa 102 /* Search for the manufacturer data */
sunsmile2015 7:91324daa3bfa 103 while(len < params->advertisingDataLen) {
sunsmile2015 8:649bd171929e 104 pAdvData = (AdvertisingData_t *)&params->advertisingData[len];
sunsmile2015 7:91324daa3bfa 105 if(pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
sunsmile2015 8:649bd171929e 106 ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
sunsmile2015 8:649bd171929e 107 if(pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
mamont090671 14:985bfbee997b 108 // pc.printf("From [%02x %02x %02x %02x %02x %02x], ", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
mamont090671 14:985bfbee997b 109 if(params->peerAddr[0] == 0x38 && flag_button == 1) {
mamont090671 14:985bfbee997b 110 pc.printf("Temp is %.02f\r", (float)pAppData->ExtT_Value);
mamont090671 14:985bfbee997b 111 // gOled2.clearDisplay();
mamont090671 14:985bfbee997b 112 gOled2.drawRoundRect(0, 0, 127, 15, 3, 1);
mamont090671 14:985bfbee997b 113 gOled2.drawRoundRect(1, 1, 125, 13, 2, 1);
mamont090671 14:985bfbee997b 114 gOled2.drawRoundRect(0, 16, 127, 47, 3, 1);
mamont090671 14:985bfbee997b 115 gOled2.drawRoundRect(1, 17, 125, 45, 2, 1);
mamont090671 14:985bfbee997b 116 // gOled2.drawRect(0, 0, 127, 63, 1);
mamont090671 14:985bfbee997b 117 // gOled2.drawRect(1, 1, 125, 61, 1);
mamont090671 14:985bfbee997b 118 gOled2.display();
mamont090671 14:985bfbee997b 119 gOled2.setTextCursor(4, 4);
mamont090671 14:985bfbee997b 120 gOled2.setTextSize(1);
mamont090671 14:985bfbee997b 121 gOled2.printf("Observer Init \r");
mamont090671 14:985bfbee997b 122 gOled2.display();
mamont090671 14:985bfbee997b 123
mamont090671 14:985bfbee997b 124 gOled2.setTextCursor(4, 22);
mamont090671 12:0f6e700ca698 125 gOled2.setTextSize(2);
mamont090671 14:985bfbee997b 126 gOled2.printf("ExtT: %.01f", (float)pAppData->ExtT_Value);
mamont090671 12:0f6e700ca698 127 gOled2.display();
mamont090671 14:985bfbee997b 128 } else if (params->peerAddr[0] != 0x38 && flag_button == 2) {
mamont090671 14:985bfbee997b 129 pc.printf("XZ is %.02f\r", (float)pAppData->ExtT_Value);
mamont090671 14:985bfbee997b 130 gOled2.setTextCursor(4, 22);
mamont090671 13:ae46f7a91beb 131 gOled2.setTextSize(2);
mamont090671 14:985bfbee997b 132 gOled2.printf("IntT: 0x%02x", pAppData->ExtT_Value);
mamont090671 13:ae46f7a91beb 133 gOled2.display();
mamont090671 11:f6274e2a0b07 134 }
mamont090671 11:f6274e2a0b07 135 // pc.printf(" ButtonAB is %02x", pAppData->btnA_Value);
mamont090671 11:f6274e2a0b07 136 // pc.printf("%02x\r", pAppData->btnB_Value);
sunsmile2015 7:91324daa3bfa 137 break;
sunsmile2015 7:91324daa3bfa 138 }
sunsmile2015 6:850f44146c9f 139 }
sunsmile2015 7:91324daa3bfa 140 len += (pAdvData->length + 1);
rgrover1 0:332983584a9c 141 }
rgrover1 0:332983584a9c 142 }
mamont090671 14:985bfbee997b 143 /*
mamont090671 14:985bfbee997b 144 void updateFromBME280()
mamont090671 14:985bfbee997b 145 {
mamont090671 14:985bfbee997b 146 bme280.read();
mamont090671 14:985bfbee997b 147 t = bme280.temperature();
mamont090671 14:985bfbee997b 148 float p = bme280.pressure()/100;
mamont090671 14:985bfbee997b 149 pr = p*0.750062;
mamont090671 14:985bfbee997b 150 // pr = bme280.pressure();
mamont090671 14:985bfbee997b 151 h = bme280.humidity();
mamont090671 14:985bfbee997b 152 }
mamont090671 14:985bfbee997b 153 */
rgrover1 0:332983584a9c 154 int main(void)
rgrover1 0:332983584a9c 155 {
mamont090671 14:985bfbee997b 156 //Set falling and rising edge to apppropriate interrup function
mamont090671 14:985bfbee997b 157 sw1Press.fall(&triggerfall_1);
mamont090671 14:985bfbee997b 158 sw1Press.rise(&triggerrise_1);
mamont090671 14:985bfbee997b 159 sw2Press.fall(&triggerfall_2);
mamont090671 14:985bfbee997b 160 sw2Press.rise(&triggerrise_2);
mamont090671 14:985bfbee997b 161 flag_button = 0;
mamont090671 14:985bfbee997b 162
rgrover1 0:332983584a9c 163 led1 = 1;
rgrover1 0:332983584a9c 164 Ticker ticker;
rgrover1 0:332983584a9c 165 ticker.attach(periodicCallback, 1);
mamont090671 14:985bfbee997b 166 /*
mamont090671 14:985bfbee997b 167 bme280.begin(BME280_I2C_ADDRESS1);
mamont090671 14:985bfbee997b 168 // Configure for test purposes.
mamont090671 14:985bfbee997b 169 bme280.writeConfigRegister(BME280_STANDBY_500_US,BME280_FILTER_OFF,0);
mamont090671 14:985bfbee997b 170 bme280.writeControlRegisters(BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_MODE_NORMAL);
mamont090671 14:985bfbee997b 171 */
rgrover1 0:332983584a9c 172 ble.init();
sunsmile2015 6:850f44146c9f 173 ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
rgrover1 5:103717ce54e5 174 ble.gap().startScan(advertisementCallback);
rgrover1 0:332983584a9c 175
mamont090671 9:56bb343c76ae 176 pc.baud(9600);
mamont090671 9:56bb343c76ae 177 pc.printf("Observer Init \r\n");
mamont090671 9:56bb343c76ae 178
mamont090671 12:0f6e700ca698 179 gOled2.clearDisplay();
mamont090671 12:0f6e700ca698 180 gOled2.display();
mamont090671 12:0f6e700ca698 181
rgrover1 0:332983584a9c 182 while (true) {
mamont090671 14:985bfbee997b 183 /* if (flag_button == 2) {
mamont090671 14:985bfbee997b 184 updateFromBME280();
mamont090671 14:985bfbee997b 185 gOled2.setTextCursor(4, 22);
mamont090671 14:985bfbee997b 186 gOled2.setTextSize(2);
mamont090671 14:985bfbee997b 187 gOled2.printf("IntT: %.01f", t);
mamont090671 14:985bfbee997b 188 gOled2.setTextCursor(4, 42);
mamont090671 14:985bfbee997b 189 gOled2.printf("IntP: %.01f", pr);
mamont090671 14:985bfbee997b 190 gOled2.display();
mamont090671 14:985bfbee997b 191 }
mamont090671 14:985bfbee997b 192 */
rgrover1 0:332983584a9c 193 ble.waitForEvent();
rgrover1 0:332983584a9c 194 }
rgrover1 0:332983584a9c 195 }
mamont090671 14:985bfbee997b 196
mamont090671 14:985bfbee997b 197 //Button1 falling interrupt function
mamont090671 14:985bfbee997b 198 void triggerfall_1()
mamont090671 14:985bfbee997b 199 {
mamont090671 14:985bfbee997b 200 flag_button = flag_button++;
mamont090671 14:985bfbee997b 201 pc.printf("\r\nflag: %d\r\n", flag_button);
mamont090671 14:985bfbee997b 202 if (flag_button == 3) {
mamont090671 14:985bfbee997b 203 flag_button = 0;
mamont090671 14:985bfbee997b 204 gOled2.clearDisplay();
mamont090671 14:985bfbee997b 205 gOled2.display();
mamont090671 14:985bfbee997b 206 }
mamont090671 14:985bfbee997b 207 }
mamont090671 14:985bfbee997b 208 //Button1 rising interrupt function
mamont090671 14:985bfbee997b 209 void triggerrise_1()
mamont090671 14:985bfbee997b 210 {
mamont090671 14:985bfbee997b 211 }
mamont090671 14:985bfbee997b 212
mamont090671 14:985bfbee997b 213 //Button1 falling interrupt function
mamont090671 14:985bfbee997b 214 void triggerfall_2()
mamont090671 14:985bfbee997b 215 {
mamont090671 14:985bfbee997b 216 }
mamont090671 14:985bfbee997b 217 //Button1 rising interrupt function
mamont090671 14:985bfbee997b 218 void triggerrise_2()
mamont090671 14:985bfbee997b 219 {
mamont090671 14:985bfbee997b 220 }