nRF51822_OBS + mocro:bit_ADV

Dependencies:   mbed BLE_API Adafruit_GFX nRF51822

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

main.cpp

Committer:
mamont090671
Date:
2019-12-15
Revision:
13:ae46f7a91beb
Parent:
12:0f6e700ca698
Child:
14:985bfbee997b

File content as of revision 13:ae46f7a91beb:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "BLE.h"
//#include "TMP_nrf51/TMP_nrf51.h"
#include "Adafruit_SSD1306.h"

#define SDA P0_0
#define SCL P0_1

#define APP_SPECIFIC_ID_TEST 0x0059 //0xFEFE

#pragma pack(1)
/* Advertising data  */
struct AdvertisingData_t {
    uint8_t length; /* doesn't include itself */
    GapAdvertisingData::DataType dataType;
    uint8_t data[1];
};

struct ApplicationData_t {
    uint16_t applicationSpecificId;             /* An ID used to identify temperature value
                                                   in the manufacture specific AD data field */
    uint8_t tmpSensorValue; /* User defined application data */
//    uint8_t btnA_Value;
//    uint8_t btnB_Value;
};
#pragma pack()

class I2C2 : public I2C
{
public:
    I2C2(PinName sda, PinName scl) : I2C(sda, scl)
    {
        frequency(400000);
        start();
    };
};

I2C2 gI2C(SDA,SCL);
Adafruit_SSD1306_I2c gOled2(gI2C, NC, 0x78, 64, 128);

BLE        ble;
DigitalOut led1(LED1);

Serial pc(USBTX, USBRX);

void periodicCallback(void)
{
    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
}

void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
{
    AdvertisingData_t *pAdvData = NULL;
    uint8_t len = 0;

    /* Search for the manufacturer data */
    while(len < params->advertisingDataLen) {
        pAdvData = (AdvertisingData_t *)&params->advertisingData[len];
        if(pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
            ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
            if(pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
                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]);
                if(params->peerAddr[0] == 0x38) {
                    pc.printf("Temp is %.02f\r", (float)pAppData->tmpSensorValue);
                    gOled2.setTextCursor(4, 16);
                    gOled2.setTextSize(2);
                    gOled2.printf("Temp: %.01f", (float)pAppData->tmpSensorValue);
                    gOled2.display();
                } else {
                    pc.printf("XZ is %.02f\r", (float)pAppData->tmpSensorValue);
                    gOled2.setTextCursor(4, 32);
                    gOled2.setTextSize(2);
                    gOled2.printf("XZ: 0x%02xh", pAppData->tmpSensorValue);
                    gOled2.display();
                }
//                pc.printf(" ButtonAB is %02x", pAppData->btnA_Value);
//                pc.printf("%02x\r", pAppData->btnB_Value);
                break;
            }
        }
        len += (pAdvData->length + 1);
    }
}

int main(void)
{
    led1 = 1;
    Ticker ticker;
    ticker.attach(periodicCallback, 1);

    ble.init();
    ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
    ble.gap().startScan(advertisementCallback);

    pc.baud(9600);
    pc.printf("Observer Init \r\n");

    gOled2.clearDisplay();
    gOled2.drawRect(0, 0, 127, 63, 1);
    gOled2.drawRect(1, 1, 125, 61, 1);
    gOled2.display();
    gOled2.setTextCursor(4, 4);
    gOled2.setTextSize(1);
    gOled2.printf("Observer Init \r");
    gOled2.display();

    while (true) {
        ble.waitForEvent();
    }
}