Petr Vasilev / Mbed 2 deprecated BLE_Evothings_NRF51822-BLE400

Dependencies:   mbed AdafruitST7735 BLE_API Adafruit_GFX nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BLE400.cpp Source File

BLE400.cpp

00001 /*
00002 Port for WaveShare BLE400 Evothings good example for Nordic nRF51822-DK
00003 As target for mbed-online used <Nordic nRF51822> (Large green board Nordic nRF51822-mKIT, actually deprecated on ~01/03/2019)
00004 Briefly: Handle via Evothings BLE Application 4 LEDs and 2 Buttons(via notify messages).
00005 
00006 This example original:
00007 https://os.mbed.com/users/jensstruemper/code/Evothings-Updated/
00008 Android application:
00009 http://evothings.com/2.2/doc/examples/nordic-nRF51-ble.html
00010 This application explanation:
00011 How to turn a Nordic Semiconductor nRF51-DK into a discoverable beacon using mbed
00012 https://evothings.com/how-to-turn-a-nordic-semiconductor-nrf51-dk-into-a-discoverable-beacon-using-mbed/
00013 
00014 Author porting:
00015 Ibragimov Maksim aka maxxir
00016 Russia Togliatty
00017 01/03/2019
00018 */
00019 
00020 /*
00021 PS.
00022 My Win7 CMD script string example to flash BLE400 via STLINK-V2 && OpenOCD
00023 content from flash_ble400.bat:
00024 E:\stm32_eclipse_neon\OpenOCD\bin\openocd.exe -d2 -f interface/stlink-v2.cfg ; -f target/nrf51_stlink.tcl -c "program %1 verify reset exit"; shutdown;
00025 
00026 Flash example via console:
00027 >flash_ble400.bat BLE_Evothings_NRF51822-BLE400.NRF51822.hex
00028 */
00029 
00030 /*
00031  * nRF51-DK BLEDevice service/characteristic (read/write) using mbed.org
00032  */
00033 
00034 // uncomment if not interested in a console log
00035 #define CONSOLE_LOG 
00036 
00037 #include "mbed.h"
00038 #include "ble/BLE.h"
00039 
00040 //-------------------------------------------------------------------------
00041 
00042 #ifdef CONSOLE_LOG
00043 #define INFO(x, ...)    printf(x,        ##__VA_ARGS__);
00044 #define INFO_NL(x, ...) printf(x "\r\n", ##__VA_ARGS__);
00045 #else
00046 #define INFO(x, ...)    
00047 #define INFO_NL(x, ...) 
00048 #endif
00049 
00050 // a little routine to print a 128-bit UUID nicely
00051 void INFO_UUID(const char *prefix, UUID uuid)
00052 {
00053    uint8_t *p = (uint8_t *)uuid.getBaseUUID();
00054    INFO("%s: ", prefix);
00055    for (int i=0; i<16; i++)
00056    {
00057      INFO("%02x", p[i]);
00058      if ((i == 3) || (i == 5) || (i == 7) || (i == 9)) INFO("-");
00059    }
00060    INFO_NL("");
00061 }
00062 
00063 //-------------------------------------------------------------------------
00064 
00065 // name of the device 
00066 const static char DEVICE_NAME[] = "nRF51-BLE400"; //Edit here your name device, and also fix on Evothings APP <index.html> look ~ #136-#137 NRF51_ble.connect('nRF51-BLE400', // BLE name
00067 
00068 // GATT service and characteristic UUIDs
00069 const UUID nRF51_GATT_SERVICE     = UUID((uint8_t *)"nRF51-DK        "); //Decided not edit it too
00070 const UUID nRF51_GATT_CHAR_BUTTON = UUID((uint8_t *)"nRF51-DK button "); //Better not edit this, or need fix on Evothings APP <nordic-nRF51-ble.js> look ~ #108 device.setNotification..
00071 const UUID nRF51_GATT_CHAR_LED    = UUID((uint8_t *)"nRF51-DK led    "); //Better not edit this, or need fix on Evothings APP <nordic-nRF51-ble.js> look ~ #93 device.writeDataArray..
00072 
00073 #define CHARACTERISTIC_BUTTON 0
00074 #define CHARACTERISTIC_LED    1
00075 #define CHARACTERISTIC_COUNT  2
00076 
00077 // our bluetooth smart objects
00078 BLE                ble;
00079 GattService        *gatt_service;
00080 GattCharacteristic *gatt_characteristics[CHARACTERISTIC_COUNT];
00081 uint8_t             gatt_char_value[CHARACTERISTIC_COUNT];
00082 
00083 #ifdef CONSOLE_LOG
00084 Serial pc(USBTX,USBRX);
00085 #endif
00086 
00087 //-------------------------------------------------------------------------
00088 // button handling
00089 //-------------------------------------------------------------------------
00090 
00091 // define our digital in values we will be using for the characteristic
00092 //WaveShare BLE400 digital inputs as Button inputs
00093 DigitalIn button1(P0_16); //KEY1
00094 DigitalIn button2(P0_17); //KEY2
00095 //DigitalIn button3(P0_14); //Should not used for WaveShare BLE400
00096 //DigitalIn button4(P0_15); //Should not used for WaveShare BLE400
00097 
00098 uint8_t button_new_value = 0;
00099 uint8_t button_old_value = button_new_value;
00100 
00101 void monitorButtons() 
00102 {
00103     // read in the buttons, mapped into nibble (0000 = all off, 1111 = all on)
00104     button_new_value = 0;
00105     button_new_value |= (button1.read() != 1); button_new_value <<= 1;
00106     button_new_value |= (button2.read() != 1); button_new_value <<= 1;
00107     //Should not used for WaveShare BLE400
00108     /*
00109     button_new_value |= (button3.read() != 1); button_new_value <<= 1;
00110     button_new_value |= (button4.read() != 1); 
00111     */  
00112     // set the updated value of the characteristic if data has changed
00113     if (button_new_value != button_old_value)
00114     {
00115         ble.updateCharacteristicValue(
00116               gatt_characteristics[CHARACTERISTIC_BUTTON] -> getValueHandle(),
00117               &button_new_value, sizeof(button_new_value));
00118         button_old_value = button_new_value;
00119 
00120         INFO_NL("  button state: [0x%02x]", button_new_value);
00121     }
00122 }
00123 
00124 //-------------------------------------------------------------------------
00125 // LED handling
00126 //-------------------------------------------------------------------------
00127 //WaveShare BLE400 digital outputs as LED outputs
00128 DigitalOut led1(P0_18);
00129 DigitalOut led2(P0_19);
00130 DigitalOut led3(P0_20);
00131 DigitalOut led4(P0_21);
00132 DigitalOut led5(P0_22); //Used here to view BLE connect/disconnect
00133 
00134 uint8_t led_value = 0;
00135 
00136 //Adapted for WaveShare BLE400, LED_ON = HIGH (NRF51822-DK vice versa LED_ON = LOW)
00137 void onLedDataWritten(const uint8_t* value, uint8_t length) 
00138 {
00139     // we only care about a single byte
00140     led_value = value[0];
00141 
00142     // depending on the value coming through; set/unset LED's
00143     if ((led_value & 0x01) != 0) led1.write(1); else led1.write(0);
00144     if ((led_value & 0x02) != 0) led2.write(1); else led2.write(0);
00145     if ((led_value & 0x04) != 0) led3.write(1); else led3.write(0);
00146     if ((led_value & 0x08) != 0) led4.write(1); else led4.write(0);
00147 
00148     INFO_NL("     led state: [0x%02x]", led_value);
00149 }
00150 
00151 //-------------------------------------------------------------------------
00152 
00153 void onConnection(const Gap::ConnectionCallbackParams_t *params)
00154 {
00155   INFO_NL(">> connected");
00156 
00157   // set the initial values of the characteristics (for every session)
00158   //led_value = 0;
00159   onLedDataWritten(&led_value, 1); // force LED's to be in off state
00160   //LED5=ON on connection (for BLE400)
00161   led5.write(1);
00162 }
00163 
00164 void onDataWritten(const GattWriteCallbackParams *context) 
00165 {
00166    // was the characteristic being written to nRF51_GATT_CHAR_LED? 
00167   if (context -> handle == 
00168        gatt_characteristics[CHARACTERISTIC_LED] -> getValueHandle())
00169    {
00170      onLedDataWritten(context -> data, context -> len);
00171    } 
00172 }
00173 
00174 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00175 {
00176     INFO_NL(">> disconnected");
00177     ble.gap().startAdvertising(); // restart advertising
00178     INFO_NL(">> device advertising");
00179     //LED5=OFF on diconnection (for BLE400)
00180     led5.write(0);
00181 }
00182 
00183 
00184 int main() 
00185 {
00186 #ifdef CONSOLE_LOG
00187     // wait a second before trying to write something to console 
00188     wait(1);
00189 #endif
00190     INFO_NL(">> nRF51-BLE400 start");
00191 
00192     // create our button characteristic (read, notify)
00193     gatt_characteristics[CHARACTERISTIC_BUTTON] = 
00194       new GattCharacteristic(
00195             nRF51_GATT_CHAR_BUTTON, 
00196             &gatt_char_value[CHARACTERISTIC_BUTTON], 1, 1,
00197             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | 
00198             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00199 
00200     // create our LED characteristic (read, write)
00201     gatt_characteristics[CHARACTERISTIC_LED] = 
00202       new GattCharacteristic(
00203             nRF51_GATT_CHAR_LED, 
00204             &gatt_char_value[CHARACTERISTIC_LED], 1, 1,
00205             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | 
00206             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
00207             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00208 
00209     // create our service, with both characteristics
00210     gatt_service = 
00211       new GattService(nRF51_GATT_SERVICE, 
00212                       gatt_characteristics, CHARACTERISTIC_COUNT);
00213 
00214     // initialize our ble device
00215     ble.init();
00216     ble.gap().setDeviceName((uint8_t *)DEVICE_NAME);
00217     INFO_NL(">> initialized device '%s'", DEVICE_NAME);
00218 
00219     // configure our advertising type, payload and interval
00220     ble.gap().accumulateAdvertisingPayload(
00221           GapAdvertisingData::BREDR_NOT_SUPPORTED | 
00222           GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00223     ble.gap().accumulateAdvertisingPayload(
00224           GapAdvertisingData::COMPLETE_LOCAL_NAME, 
00225           (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00226     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00227     ble.gap().setAdvertisingInterval(160); // 100ms
00228     INFO_NL(">> configured advertising type, payload and interval");
00229 
00230     // configure our callbacks
00231     ble.gap().onDisconnection(disconnectionCallback);
00232     ble.gap().onConnection(onConnection);
00233     ble.onDataWritten(onDataWritten);
00234     INFO_NL(">> registered for callbacks");
00235 
00236     // add our gatt service with two characteristics
00237     ble.addService(*gatt_service);
00238     INFO_NL(">> added GATT service with two characteristics");
00239 
00240     // show some debugging information about service/characteristics
00241     INFO_UUID(" ", nRF51_GATT_SERVICE);
00242     INFO_UUID("  :", nRF51_GATT_CHAR_BUTTON);
00243     INFO_UUID("  :", nRF51_GATT_CHAR_LED);
00244 
00245     // start advertising
00246     ble.gap().startAdvertising();
00247     INFO_NL(">> device advertising");
00248 
00249     // start monitoring the buttons and posting new values
00250     Ticker ticker;
00251     ticker.attach(monitorButtons, 0.1);  // every 10th of a second
00252     INFO_NL(">> monitoring button state");
00253 
00254     // let the device do its thing
00255     INFO_NL(">> waiting for events ");
00256     for (;;)
00257     {
00258       ble.waitForEvent();
00259     }
00260 }
00261 
00262 //-------------------------------------------------------------------------