blynk & neopixelring & w7500
Fork of WIZwiki-7500_Blynk by
Embed:
(wiki syntax)
Show/hide line numbers
BlynkSimpleRedBearLab_BLE_Nano.h
Go to the documentation of this file.
00001 /** 00002 * @file BlynkSimpleRedBearLab_BLE_Nano.h 00003 * @author Volodymyr Shymanskyy 00004 * @license This project is released under the MIT License (MIT) 00005 * @copyright Copyright (c) 2015 Volodymyr Shymanskyy 00006 * @date May 2016 00007 * @brief 00008 * 00009 */ 00010 00011 #ifndef BlynkSimpleRedBearLab_BLE_Nano_h 00012 #define BlynkSimpleRedBearLab_BLE_Nano_h 00013 00014 #ifndef BLYNK_INFO_CONNECTION 00015 #define BLYNK_INFO_CONNECTION "RBL_BLE_Nano" 00016 #endif 00017 00018 #define BLYNK_SEND_ATOMIC 00019 #define BLYNK_SEND_CHUNK 20 00020 #define BLYNK_SEND_THROTTLE 20 00021 00022 #include <BlynkApiMbed.h > 00023 #include <Blynk/BlynkProtocol.h> 00024 #include <utility/BlynkFifo2.h> 00025 #include <ble/BLE.h> 00026 00027 /* 00028 * The Nordic UART Service 00029 */ 00030 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00031 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00032 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00033 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00034 static const uint8_t uart_dev_name[] = "Blynk"; 00035 00036 #define TXRX_BUF_LEN 20 00037 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00038 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00039 00040 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, 00041 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 00042 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00043 00044 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, 00045 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00046 00047 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00048 00049 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic*)); 00050 00051 BLE ble; 00052 00053 class BlynkTransportRedBearLab_BLE_Nano 00054 { 00055 public: 00056 BlynkTransportRedBearLab_BLE_Nano() 00057 : mConn (false) 00058 {} 00059 00060 // IP redirect not available 00061 void begin(char* h, uint16_t p) {} 00062 00063 void begin() { 00064 instance = this; 00065 00066 ble.gap().onConnection(connectCallback); 00067 ble.gap().onDisconnection(disconnectCallback); 00068 00069 ble.gattServer().addService(uartService); 00070 ble.gattServer().onDataWritten(writeCallback); 00071 ble.gattServer().onDataSent(sentCallback); 00072 00073 // Setup advertising 00074 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00075 uart_base_uuid_rev, sizeof(uart_base_uuid)); 00076 00077 } 00078 00079 bool connect() { 00080 mBuffRX.clear(); 00081 return mConn = true; 00082 } 00083 00084 void disconnect() { 00085 mConn = false; 00086 } 00087 00088 bool connected() { 00089 return mConn; 00090 } 00091 00092 size_t read(void* buf, size_t len) { 00093 uint32_t start = millis(); 00094 while (millis() - start < BLYNK_TIMEOUT_MS) { 00095 if (available() < len) { 00096 ble.waitForEvent(); 00097 } else { 00098 break; 00099 } 00100 } 00101 noInterrupts(); 00102 size_t res = mBuffRX.get((uint8_t*)buf, len); 00103 interrupts(); 00104 return res; 00105 } 00106 00107 size_t write(const void* buf, size_t len) { 00108 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), (uint8_t*)buf, len); 00109 return len; 00110 } 00111 00112 size_t available() { 00113 noInterrupts(); 00114 size_t rxSize = mBuffRX.size(); 00115 interrupts(); 00116 return rxSize; 00117 } 00118 00119 private: 00120 static BlynkTransportRedBearLab_BLE_Nano* instance; 00121 00122 static 00123 void writeCallback(const GattWriteCallbackParams *params) 00124 { 00125 if (!instance) 00126 return; 00127 noInterrupts(); 00128 //BLYNK_DBG_DUMP(">> ", params->data, params->len); 00129 instance->mBuffRX.put(params->data, params->len); 00130 interrupts(); 00131 } 00132 00133 static 00134 void sentCallback(unsigned count) 00135 { 00136 //Serial.print("SENT: "); 00137 //Serial.println(count); 00138 } 00139 00140 static 00141 void connectCallback(const Gap::ConnectionCallbackParams_t *params); 00142 00143 static 00144 void disconnectCallback(const Gap::DisconnectionCallbackParams_t *params); 00145 00146 private: 00147 bool mConn; 00148 00149 BlynkFifo<uint8_t, BLYNK_MAX_READBYTES*2> mBuffRX; 00150 }; 00151 00152 class BlynkRedBearLab_BLE_Nano 00153 : public BlynkProtocol<BlynkTransportRedBearLab_BLE_Nano> 00154 { 00155 typedef BlynkProtocol<BlynkTransportRedBearLab_BLE_Nano> Base; 00156 public: 00157 BlynkRedBearLab_BLE_Nano(BlynkTransportRedBearLab_BLE_Nano& transp) 00158 : Base(transp) 00159 {} 00160 00161 void begin(const char* auth) 00162 { 00163 Base::begin(auth); 00164 state = DISCONNECTED; 00165 conn.begin(); 00166 } 00167 }; 00168 00169 BlynkTransportRedBearLab_BLE_Nano* BlynkTransportRedBearLab_BLE_Nano::instance = NULL; 00170 00171 static BlynkTransportRedBearLab_BLE_Nano _blynkTransport; 00172 BlynkRedBearLab_BLE_Nano Blynk(_blynkTransport); 00173 00174 void BlynkTransportRedBearLab_BLE_Nano::connectCallback(const Gap::ConnectionCallbackParams_t *params) 00175 { 00176 BLYNK_LOG1("Device connected"); 00177 Blynk.startSession(); 00178 } 00179 00180 void BlynkTransportRedBearLab_BLE_Nano::disconnectCallback(const Gap::DisconnectionCallbackParams_t *params) 00181 { 00182 BLYNK_LOG1("Device disconnected"); 00183 //__disable_irq(); 00184 Blynk.disconnect(); 00185 //__enable_irq(); 00186 ble.startAdvertising(); 00187 } 00188 00189 #include <BlynkWidgets.h > 00190 00191 #endif
Generated on Thu Jul 14 2022 00:21:50 by 1.7.2