mamont mamont / Mbed 2 deprecated BLE_TemperatureObserver

Dependencies:   mbed BLE_API Adafruit_GFX nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 
00017 #include "mbed.h"
00018 #include "BLE.h"
00019 #include "Adafruit_SSD1306.h"
00020 //Задаем пины для i2c шины
00021 #define SDA P0_0
00022 #define SCL P0_1
00023 
00024 #define APP_SPECIFIC_ID_TEST 0x0059 //0xFEFE
00025 
00026 #pragma pack(1)
00027 /* Advertising data  */
00028 struct AdvertisingData_t {
00029     uint8_t length; /* doesn't include itself */
00030     GapAdvertisingData::DataType dataType;
00031     uint8_t data[1];
00032 };
00033 
00034 struct ApplicationData_t {
00035     uint16_t applicationSpecificId;             /* An ID used to identify temperature value
00036                                                    in the manufacture specific AD data field */
00037     uint8_t ExtT_Value; /* User defined application data */
00038 //    uint8_t btnA_Value;
00039 //    uint8_t btnB_Value;
00040 };
00041 #pragma pack()
00042 
00043 //Функции обработки событий кнопок
00044 void triggerfall_1();   //Button1 falling interrupt function
00045 void triggerrise_1();   //Button1 rising interrupt function
00046 void triggerfall_2();   //Button2 falling interrupt function
00047 void triggerrise_2();   //Button2 rising interrupt function
00048 //Задаем кнопки
00049 DigitalIn  sw1(BUTTON1);
00050 DigitalIn  sw2(BUTTON2);
00051 
00052 //Initiate input interrupts
00053 InterruptIn sw1Press(BUTTON1);
00054 InterruptIn sw2Press(BUTTON2);
00055 int flag_button;
00056 
00057 class I2C2 : public I2C
00058 {
00059 public:
00060     I2C2(PinName sda, PinName scl) : I2C(sda, scl)
00061     {
00062         frequency(400000);
00063         start();
00064     };
00065 };
00066 
00067 I2C2 gI2C(SDA,SCL);
00068 Adafruit_SSD1306_I2c gOled2(gI2C, NC, 0x78, 64, 128);
00069 //Используем BLE
00070 BLE        ble;
00071 //Задаем светодиод
00072 DigitalOut led1(LED1);
00073 //Задаем интерфейс rs232
00074 Serial pc(USBTX, USBRX);
00075 //Функция вызываемая по таймеру
00076 void periodicCallback(void)
00077 {
00078     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00079 }
00080 //Функция вызывается при обнаружении ADV пакета
00081 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
00082 {
00083     AdvertisingData_t *pAdvData = NULL;
00084     uint8_t len = 0;
00085 
00086     /* Search for the manufacturer data */
00087     while(len < params->advertisingDataLen) {
00088         pAdvData = (AdvertisingData_t *)&params->advertisingData[len];
00089         if(pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
00090             ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
00091             if(pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
00092 //                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]);
00093 //Если MAC заканчивается на 0х38 то ....
00094                 if(params->peerAddr[0] == 0x38 && flag_button == 1) {
00095                     pc.printf("Temp is %.02f\r", (float)pAppData->ExtT_Value);
00096 //                    gOled2.clearDisplay();
00097 //Выводим двойную рамку для веха экрана
00098                     gOled2.drawRoundRect(0, 0, 127, 15, 3, 1);
00099                     gOled2.drawRoundRect(1, 1, 125, 13, 2, 1);
00100 //Выводим двойную рамку для низа экрана
00101                     gOled2.drawRoundRect(0, 16, 127, 47, 3, 1);
00102                     gOled2.drawRoundRect(1, 17, 125, 45, 2, 1);
00103 //    gOled2.drawRect(0, 0, 127, 63, 1);
00104 //    gOled2.drawRect(1, 1, 125, 61, 1);
00105 //Показываем на экране
00106                     gOled2.display();
00107 //Устанавливаем курсор
00108                     gOled2.setTextCursor(4, 4);
00109 //Задаем размер шрифта
00110                     gOled2.setTextSize(1);
00111                     gOled2.printf("Observer Init \r");
00112 //Показываем на экране
00113                     gOled2.display();
00114 
00115                     gOled2.setTextCursor(4, 22);
00116                     gOled2.setTextSize(2);
00117 //Выводим показания внешнего датчика температуры на экран
00118                     gOled2.printf("ExtT: %.01f", (float)pAppData->ExtT_Value);
00119                     gOled2.display();
00120 //Иначе, если MAC другой, то ...
00121                 } else if (params->peerAddr[0] != 0x38 && flag_button == 2) {
00122 //Выводим показания другого датчика на экран
00123                     pc.printf("XZ is %.02f\r", (float)pAppData->ExtT_Value);
00124                     gOled2.setTextCursor(4, 22);
00125                     gOled2.setTextSize(2);
00126                     gOled2.printf("IntT: 0x%02x", pAppData->ExtT_Value);
00127                     gOled2.display();
00128                 }
00129 //                pc.printf(" ButtonAB is %02x", pAppData->btnA_Value);
00130 //                pc.printf("%02x\r", pAppData->btnB_Value);
00131                 break;
00132             }
00133         }
00134         len += (pAdvData->length + 1);
00135     }
00136 }
00137 
00138 int main(void)
00139 {
00140 //Set falling and rising edge to apppropriate interrup function
00141     sw1Press.fall(&triggerfall_1);
00142     sw1Press.rise(&triggerrise_1);
00143     sw2Press.fall(&triggerfall_2);
00144     sw2Press.rise(&triggerrise_2);
00145     flag_button = 0;
00146 
00147     led1 = 1;
00148 //Объявляем таймер
00149     Ticker ticker;
00150 //Назначаем таймеру функцию и интервал
00151     ticker.attach(periodicCallback, 1);
00152 
00153 //инициализируем BLE
00154     ble.init();
00155 //Задаем параметры сканирования
00156     ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
00157 //Запускаем сканирование
00158     ble.gap().startScan(advertisementCallback);
00159 
00160 //Задаем параметры порта rs232
00161     pc.baud(9600);
00162     pc.printf("Observer Init \r\n");
00163 
00164 //Очищаем экран
00165     gOled2.clearDisplay();
00166     gOled2.display();
00167 
00168     while (true) {
00169         ble.waitForEvent();
00170     }
00171 }
00172 
00173 //Button1 falling interrupt function
00174 void triggerfall_1()
00175 {
00176     flag_button = flag_button++;
00177     pc.printf("\r\nflag: %d\r\n", flag_button);
00178     if (flag_button == 3) {
00179         flag_button = 0;
00180         gOled2.clearDisplay();
00181         gOled2.display();
00182     }
00183 }
00184 //Button1 rising interrupt function
00185 void triggerrise_1()
00186 {
00187 }
00188 
00189 //Button1 falling interrupt function
00190 void triggerfall_2()
00191 {
00192 }
00193 //Button1 rising interrupt function
00194 void triggerrise_2()
00195 {
00196 }