Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed AdafruitST7735 BLE_API Adafruit_GFX nRF51822
Revision 19:02bb67cb303c, committed 2019-08-11
- Comitter:
- tekasijimo
- Date:
- Sun Aug 11 13:43:37 2019 +0000
- Parent:
- 18:0cbeb28ce4f6
- Commit message:
- derfthe
Changed in this revision
diff -r 0cbeb28ce4f6 -r 02bb67cb303c AdafruitST7735.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AdafruitST7735.lib Sun Aug 11 13:43:37 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/tekasijimo/code/AdafruitST7735/#c964b41674fc
diff -r 0cbeb28ce4f6 -r 02bb67cb303c Adafruit_GFX.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Adafruit_GFX.lib Sun Aug 11 13:43:37 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/tekasijimo/code/Adafruit_GFX/#a102b3e4c43d
diff -r 0cbeb28ce4f6 -r 02bb67cb303c BLE400.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BLE400.cpp Sun Aug 11 13:43:37 2019 +0000 @@ -0,0 +1,262 @@ +/* +Port for WaveShare BLE400 Evothings good example for Nordic nRF51822-DK +As target for mbed-online used <Nordic nRF51822> (Large green board Nordic nRF51822-mKIT, actually deprecated on ~01/03/2019) +Briefly: Handle via Evothings BLE Application 4 LEDs and 2 Buttons(via notify messages). + +This example original: +https://os.mbed.com/users/jensstruemper/code/Evothings-Updated/ +Android application: +http://evothings.com/2.2/doc/examples/nordic-nRF51-ble.html +This application explanation: +How to turn a Nordic Semiconductor nRF51-DK into a discoverable beacon using mbed +https://evothings.com/how-to-turn-a-nordic-semiconductor-nrf51-dk-into-a-discoverable-beacon-using-mbed/ + +Author porting: +Ibragimov Maksim aka maxxir +Russia Togliatty +01/03/2019 +*/ + +/* +PS. +My Win7 CMD script string example to flash BLE400 via STLINK-V2 && OpenOCD +content from flash_ble400.bat: +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; + +Flash example via console: +>flash_ble400.bat BLE_Evothings_NRF51822-BLE400.NRF51822.hex +*/ + +/* + * nRF51-DK BLEDevice service/characteristic (read/write) using mbed.org + */ + +// uncomment if not interested in a console log +#define CONSOLE_LOG + +#include "mbed.h" +#include "ble/BLE.h" + +//------------------------------------------------------------------------- + +#ifdef CONSOLE_LOG +#define INFO(x, ...) printf(x, ##__VA_ARGS__); +#define INFO_NL(x, ...) printf(x "\r\n", ##__VA_ARGS__); +#else +#define INFO(x, ...) +#define INFO_NL(x, ...) +#endif + +// a little routine to print a 128-bit UUID nicely +void INFO_UUID(const char *prefix, UUID uuid) +{ + uint8_t *p = (uint8_t *)uuid.getBaseUUID(); + INFO("%s: ", prefix); + for (int i=0; i<16; i++) + { + INFO("%02x", p[i]); + if ((i == 3) || (i == 5) || (i == 7) || (i == 9)) INFO("-"); + } + INFO_NL(""); +} + +//------------------------------------------------------------------------- + +// name of the device +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 + +// GATT service and characteristic UUIDs +const UUID nRF51_GATT_SERVICE = UUID((uint8_t *)"nRF51-DK "); //Decided not edit it too +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.. +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.. + +#define CHARACTERISTIC_BUTTON 0 +#define CHARACTERISTIC_LED 1 +#define CHARACTERISTIC_COUNT 2 + +// our bluetooth smart objects +BLE ble; +GattService *gatt_service; +GattCharacteristic *gatt_characteristics[CHARACTERISTIC_COUNT]; +uint8_t gatt_char_value[CHARACTERISTIC_COUNT]; + +#ifdef CONSOLE_LOG +Serial pc(USBTX,USBRX); +#endif + +//------------------------------------------------------------------------- +// button handling +//------------------------------------------------------------------------- + +// define our digital in values we will be using for the characteristic +//WaveShare BLE400 digital inputs as Button inputs +DigitalIn button1(P0_16); //KEY1 +DigitalIn button2(P0_17); //KEY2 +//DigitalIn button3(P0_14); //Should not used for WaveShare BLE400 +//DigitalIn button4(P0_15); //Should not used for WaveShare BLE400 + +uint8_t button_new_value = 0; +uint8_t button_old_value = button_new_value; + +void monitorButtons() +{ + // read in the buttons, mapped into nibble (0000 = all off, 1111 = all on) + button_new_value = 0; + button_new_value |= (button1.read() != 1); button_new_value <<= 1; + button_new_value |= (button2.read() != 1); button_new_value <<= 1; + //Should not used for WaveShare BLE400 + /* + button_new_value |= (button3.read() != 1); button_new_value <<= 1; + button_new_value |= (button4.read() != 1); + */ + // set the updated value of the characteristic if data has changed + if (button_new_value != button_old_value) + { + ble.updateCharacteristicValue( + gatt_characteristics[CHARACTERISTIC_BUTTON] -> getValueHandle(), + &button_new_value, sizeof(button_new_value)); + button_old_value = button_new_value; + + INFO_NL(" button state: [0x%02x]", button_new_value); + } +} + +//------------------------------------------------------------------------- +// LED handling +//------------------------------------------------------------------------- +//WaveShare BLE400 digital outputs as LED outputs +DigitalOut led1(P0_18); +DigitalOut led2(P0_19); +DigitalOut led3(P0_20); +DigitalOut led4(P0_21); +DigitalOut led5(P0_22); //Used here to view BLE connect/disconnect + +uint8_t led_value = 0; + +//Adapted for WaveShare BLE400, LED_ON = HIGH (NRF51822-DK vice versa LED_ON = LOW) +void onLedDataWritten(const uint8_t* value, uint8_t length) +{ + // we only care about a single byte + led_value = value[0]; + + // depending on the value coming through; set/unset LED's + if ((led_value & 0x01) != 0) led1.write(1); else led1.write(0); + if ((led_value & 0x02) != 0) led2.write(1); else led2.write(0); + if ((led_value & 0x04) != 0) led3.write(1); else led3.write(0); + if ((led_value & 0x08) != 0) led4.write(1); else led4.write(0); + + INFO_NL(" led state: [0x%02x]", led_value); +} + +//------------------------------------------------------------------------- + +void onConnection(const Gap::ConnectionCallbackParams_t *params) +{ + INFO_NL(">> connected"); + + // set the initial values of the characteristics (for every session) + //led_value = 0; + onLedDataWritten(&led_value, 1); // force LED's to be in off state + //LED5=ON on connection (for BLE400) + led5.write(1); +} + +void onDataWritten(const GattWriteCallbackParams *context) +{ + // was the characteristic being written to nRF51_GATT_CHAR_LED? + if (context -> handle == + gatt_characteristics[CHARACTERISTIC_LED] -> getValueHandle()) + { + onLedDataWritten(context -> data, context -> len); + } +} + +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +{ + INFO_NL(">> disconnected"); + ble.gap().startAdvertising(); // restart advertising + INFO_NL(">> device advertising"); + //LED5=OFF on diconnection (for BLE400) + led5.write(0); +} + + +int main() +{ +#ifdef CONSOLE_LOG + // wait a second before trying to write something to console + wait(1); +#endif + INFO_NL(">> nRF51-BLE400 start"); + + // create our button characteristic (read, notify) + gatt_characteristics[CHARACTERISTIC_BUTTON] = + new GattCharacteristic( + nRF51_GATT_CHAR_BUTTON, + &gatt_char_value[CHARACTERISTIC_BUTTON], 1, 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); + + // create our LED characteristic (read, write) + gatt_characteristics[CHARACTERISTIC_LED] = + new GattCharacteristic( + nRF51_GATT_CHAR_LED, + &gatt_char_value[CHARACTERISTIC_LED], 1, 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); + + // create our service, with both characteristics + gatt_service = + new GattService(nRF51_GATT_SERVICE, + gatt_characteristics, CHARACTERISTIC_COUNT); + + // initialize our ble device + ble.init(); + ble.gap().setDeviceName((uint8_t *)DEVICE_NAME); + INFO_NL(">> initialized device '%s'", DEVICE_NAME); + + // configure our advertising type, payload and interval + ble.gap().accumulateAdvertisingPayload( + GapAdvertisingData::BREDR_NOT_SUPPORTED | + GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.gap().accumulateAdvertisingPayload( + GapAdvertisingData::COMPLETE_LOCAL_NAME, + (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.gap().setAdvertisingInterval(160); // 100ms + INFO_NL(">> configured advertising type, payload and interval"); + + // configure our callbacks + ble.gap().onDisconnection(disconnectionCallback); + ble.gap().onConnection(onConnection); + ble.onDataWritten(onDataWritten); + INFO_NL(">> registered for callbacks"); + + // add our gatt service with two characteristics + ble.addService(*gatt_service); + INFO_NL(">> added GATT service with two characteristics"); + + // show some debugging information about service/characteristics + INFO_UUID(" ", nRF51_GATT_SERVICE); + INFO_UUID(" :", nRF51_GATT_CHAR_BUTTON); + INFO_UUID(" :", nRF51_GATT_CHAR_LED); + + // start advertising + ble.gap().startAdvertising(); + INFO_NL(">> device advertising"); + + // start monitoring the buttons and posting new values + Ticker ticker; + ticker.attach(monitorButtons, 0.1); // every 10th of a second + INFO_NL(">> monitoring button state"); + + // let the device do its thing + INFO_NL(">> waiting for events "); + for (;;) + { + ble.waitForEvent(); + } +} + +//------------------------------------------------------------------------- \ No newline at end of file
diff -r 0cbeb28ce4f6 -r 02bb67cb303c main.cpp --- a/main.cpp Sun Mar 03 15:14:13 2019 +0000 +++ b/main.cpp Sun Aug 11 13:43:37 2019 +0000 @@ -1,262 +1,493 @@ -/* -Port for WaveShare BLE400 Evothings good example for Nordic nRF51822-DK -As target for mbed-online used <Nordic nRF51822> (Large green board Nordic nRF51822-mKIT, actually deprecated on ~01/03/2019) -Briefly: Handle via Evothings BLE Application 4 LEDs and 2 Buttons(via notify messages). - -This example original: -https://os.mbed.com/users/jensstruemper/code/Evothings-Updated/ -Android application: -http://evothings.com/2.2/doc/examples/nordic-nRF51-ble.html -This application explanation: -How to turn a Nordic Semiconductor nRF51-DK into a discoverable beacon using mbed -https://evothings.com/how-to-turn-a-nordic-semiconductor-nrf51-dk-into-a-discoverable-beacon-using-mbed/ - -Author porting: -Ibragimov Maksim aka maxxir -Russia Togliatty -01/03/2019 -*/ - -/* -PS. -My Win7 CMD script string example to flash BLE400 via STLINK-V2 && OpenOCD -content from flash_ble400.bat: -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; - -Flash example via console: ->flash_ble400.bat BLE_Evothings_NRF51822-BLE400.NRF51822.hex -*/ - -/* - * nRF51-DK BLEDevice service/characteristic (read/write) using mbed.org - */ - -// uncomment if not interested in a console log -#define CONSOLE_LOG - #include "mbed.h" #include "ble/BLE.h" +#include "Adafruit_GFX/Adafruit_GFX.h" +#include "AdafruitST7735/Adafruit_ST7735.h" -//------------------------------------------------------------------------- +// Конфигурация BLE +const static uint16_t SERVICE_UUID = 0xA000; +const static char DEVICE_NAME[] = "HPSP BT-Lite"; + +// Настройка выводов +DigitalOut led1(P0_21); +DigitalOut DISP_LED (P0_16); +DigitalIn key1 (P0_8); +DigitalIn key2 (P0_17); +DigitalOut OSC (P0_6); + +#define TFT_MISO P0_4 // NC(NC) //Не подключен так как отсутсвует на дисплее +#define TFT_RST P0_9 // RST(5) +#define TFT_DC P0_10 // D/C(6) // не работает на 10 +#define TFT_MOSI P0_11 // DIN(7) +#define TFT_SCK P0_12 // CLK(8) +#define TFT_SS P0_13 // CS(11) + +// +Adafruit_ST7735 tft(TFT_MOSI, TFT_MISO, TFT_SCK, TFT_SS, TFT_DC, TFT_RST); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST -#ifdef CONSOLE_LOG -#define INFO(x, ...) printf(x, ##__VA_ARGS__); -#define INFO_NL(x, ...) printf(x "\r\n", ##__VA_ARGS__); -#else -#define INFO(x, ...) -#define INFO_NL(x, ...) -#endif +// Назначение цветов +#define BLACK 0x0000 +#define BLUE 0x001F +#define RED 0xF800 +#define GREEN 0x07E0 +#define CYAN 0x07FF +#define MAGENTA 0xF81F +#define YELLOW 0xFFE0 +#define WHITE 0xFFFF +int COLOR = WHITE; + +Timeout KEY_TIME; // Счетчик для таймера работы программы +short FLAG = 0; // Флаг старта программы +unsigned int HOUR = 0; // Часы +unsigned int MIN = 0; // Минуты +unsigned int SEC = 0; // Секунды +unsigned int TIME_PROGRAMM = 5400; // Время работы программы +int START = 0; +// Переменные типа char для дисплея +char HOUR_CHAR[2] = {'0', '0'}; +char MIN_CHAR[2] = {'0', '0'}; +char SEC_CHAR[2] = {'0', '0'}; +int r = 20; + +//static UARTService* uartService; + +//static EventQueue eventQueue( +// /* event count */ 16 * /* event size */ 32 +//); -// a little routine to print a 128-bit UUID nicely -void INFO_UUID(const char *prefix, UUID uuid) +Ticker ticker; + +char freq_storage[900] = {0}; //резервируем блок памяти в EEPROM для хранения частот +char timer_storage[100] = {0}; //резервируем блок памяти в EEPROM для хранения таймеров + +char* freq_storage_pos = freq_storage; +char* timer_storage_pos = timer_storage; + +void generate() { - uint8_t *p = (uint8_t *)uuid.getBaseUUID(); - INFO("%s: ", prefix); - for (int i=0; i<16; i++) - { - INFO("%02x", p[i]); - if ((i == 3) || (i == 5) || (i == 7) || (i == 9)) INFO("-"); - } - INFO_NL(""); + char* freq_storage_pos = freq_storage; + unsigned short freq; + unsigned short duration = 180; // длительность + + memcpy( &freq, freq_storage_pos, sizeof(freq) ); + freq_storage_pos += sizeof(freq); + + while ( freq != 0 ) + { + memcpy( &duration, freq_storage_pos, sizeof(duration) ); + freq_storage_pos += sizeof(duration); + + // распаковываем частоту + float real_freq; + if ( freq <= 1000 ) + { + real_freq = (float)freq / 100; + } + else if ( freq <= 11000 ) + { + real_freq = (float)(freq - 1000) / 10; + } + else + { + real_freq = freq - 10000; + } + + float real_duration = (float)duration * 1000; + + OSC = 1; + wait_ms(real_duration); + OSC = 0; + + /* + digitalWrite(led, HIGH); + tone(ant, real_freq); + customDelay(real_duration); + noTone(ant); + digitalWrite(led, LOW); + */ + + memcpy( &freq, freq_storage_pos, sizeof(freq) ); + freq_storage_pos += sizeof(freq); + } } -//------------------------------------------------------------------------- +int frequencies_scan_mode = 1; + +int parse_bt_data( char *buffer // буфер, в котором уже лежат принятые данные в виде стандартной C строки + ) +{ + if ( buffer == NULL) // проверяем корректность входных данных + { + return 0; + } + + // вспомогательные переменные + char *current = buffer; // указатель на текущий символ + + // сканируем частоты + while ( frequencies_scan_mode != 0 && *current != 0 ) + { + if ( *current == ':' ) + { + break; // выходим, так как частоты закончились + } + + unsigned long freq; // частота в сотых долях герца + unsigned long duration; // длительность в секундах + char separator[2]; // разделитель + + int symbols_read; + int values_read = sscanf( current, "%ld %ld%1[,:]%n", &freq, &duration, separator, &symbols_read); // считываем частоту и длительность + + if ( values_read != 3 ) + { + // закончился буффер + // копируем оставшиеся данные в начало буффера + int rest_len = strlen( current ); // длина остатка в буффере + memmove( buffer, current, rest_len ); + return rest_len; + } + + // кодируем частоту для умещения в память + unsigned short packed_freq; + if ( freq <= 10 * 100 ) // 10Гц + { + packed_freq = freq; + } + else if ( freq <= 1000 * 100 ) // 1кГц + { + packed_freq = 1000 + freq / 10; + } + else + { + packed_freq = 10000 + freq / 100; + } + + // сохраняем данные в EEPROM + memcpy( freq_storage_pos, &packed_freq, sizeof(packed_freq) ); + freq_storage_pos += sizeof(packed_freq); + memcpy( freq_storage_pos, &duration, sizeof(duration) ); + freq_storage_pos += sizeof(duration); + + // моргаем после каждых данных + //led2 = !led2; + + current += symbols_read; //переходим к следующей паре + + if ( *separator == ':' ) + { + // пишем признак конца данных + unsigned short end = 0; + memcpy( freq_storage_pos, &end, sizeof(end) ); + + frequencies_scan_mode = 0; // загрузка частот завершена + break; + } + } + + // сканируем таймеры + while ( *current != 0 ) + { + if ( *current == '.' ) + { + break; // выходим, так как таймеры закончились + } + + unsigned int timer_start; + + int symbols_read; + char separator[2]; //разделитель + int values_read = sscanf( current, "%d%1[,.]%n", &timer_start, separator, &symbols_read); //считываем время до запуска таймера -// name of the device -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 + if ( values_read != 2 ) + { + // закончился буффер + // копируем оставшиеся данные в начало буффера + int rest_len = strlen( current ); // длина остатка в буффере + memmove( buffer, current, rest_len ); + return rest_len; + } + + // сохраняем данные в EEPROM + //eeprom_write_block( timer_start, timer_storage_pos, sizeof(timer_start) ); + //timer_storage_pos += sizeof(timer_start); + + current += symbols_read; //переходим к следующей паре + + if ( *separator == '.' ) + { + // пишем признак конца данных + unsigned short end = 0; + memcpy( timer_storage_pos, &end, sizeof(end) ); + break; // конец данных + } + } + + return 0; +} + +void updateSensorValue() +{ + +} + +void periodicCallback(void) +{ + OSC = !OSC; /* Do blinky on LED1 to indicate system aliveness. */ + TIME_PROGRAMM--; + r = r + 2; + if (r >= 60){ + r = 30; + if(COLOR == WHITE){ + COLOR = BLACK; + } + else (COLOR = WHITE); + }; + if (BLE::Instance().getGapState().connected) { +// eventQueue.call(updateSensorValue); + } +} + +void connectionCallback(const Gap::ConnectionCallbackParams_t *params) +{ + tft.fillScreen(ST7735_BLACK); + FLAG = 1; + r= 30; +} + +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +{ + BLE::Instance().gap().startAdvertising(); + tft.fillScreen(ST7735_BLACK); + FLAG = 1; + r= 30; +} + +#define INPUT_BUFFER_SIZE 50 +char buffer[INPUT_BUFFER_SIZE + 1]; //приемный буффер +static int config_complete = 0; + +void onDataWrittenCallback(const GattWriteCallbackParams *params) { + if ( params != NULL ) + { + //led2 = 0; + unsigned int buffer_pos = 0; //текущая позиция в буффере + + //если есть данные - читаем + while ( buffer_pos < params->len ) + { + //загоняем прочитанное в буфер + char symbol = params->data[buffer_pos++]; + buffer[buffer_pos] = symbol; + if ( symbol == '.') + { + buffer[buffer_pos] = 0; // закрываем строку + parse_bt_data(buffer); // обрабатываем полученный кусок данных + + // конец данных, прекращаем чтение + // данные получены, надо выставить флаг и больше из BT не читать + config_complete = 1; + break; + } + + if ( buffer_pos == INPUT_BUFFER_SIZE ) + { + buffer[INPUT_BUFFER_SIZE] = 0; //символ конца строки для полностью заполненного буффера + buffer_pos = parse_bt_data(buffer); // обрабатываем полученный кусок данных + } + } + //led2 = 1; + } +} // GATT service and characteristic UUIDs -const UUID nRF51_GATT_SERVICE = UUID((uint8_t *)"nRF51-DK "); //Decided not edit it too -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.. -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.. +const uint8_t nRF51ServiceUUID[UUID::LENGTH_OF_LONG_UUID] = { + 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB +}; +const uint8_t nRF51ServiceUUIDreversed[UUID::LENGTH_OF_LONG_UUID] = { + 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x01, 0x11, 0x00, 0x00 +}; +const UUID nRF51_GATT_SERVICE = UUID("00001101-0000-1000-8000-00805F9B34FB"); +//const UUID nRF51_GATT_CHAR_BUTTON = UUID((uint8_t *)"nRF51-DK button "); +const UUID nRF51_GATT_CHAR_LED = UUID("00001102-0000-1000-8000-00805F9B34FB"); -#define CHARACTERISTIC_BUTTON 0 -#define CHARACTERISTIC_LED 1 -#define CHARACTERISTIC_COUNT 2 +//#define CHARACTERISTIC_BUTTON 0 +#define CHARACTERISTIC_LED 0 +#define CHARACTERISTIC_COUNT 1 // our bluetooth smart objects -BLE ble; GattService *gatt_service; GattCharacteristic *gatt_characteristics[CHARACTERISTIC_COUNT]; uint8_t gatt_char_value[CHARACTERISTIC_COUNT]; -#ifdef CONSOLE_LOG -Serial pc(USBTX,USBRX); -#endif - -//------------------------------------------------------------------------- -// button handling -//------------------------------------------------------------------------- +/** + Callback triggered when the ble initialization process has finished +*/ +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) +{ + BLE& ble = params->ble; + ble_error_t error = params->error; -// define our digital in values we will be using for the characteristic -//WaveShare BLE400 digital inputs as Button inputs -DigitalIn button1(P0_16); //KEY1 -DigitalIn button2(P0_17); //KEY2 -//DigitalIn button3(P0_14); //Should not used for WaveShare BLE400 -//DigitalIn button4(P0_15); //Should not used for WaveShare BLE400 + if (error != BLE_ERROR_NONE) { + /* In case of error, forward the error handling to onBleInitError */ + //onBleInitError(ble, error); + return; + } -uint8_t button_new_value = 0; -uint8_t button_old_value = button_new_value; + /* Ensure that it is the default instance of BLE */ + if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { + return; + } + + ble.gap().onDisconnection(disconnectionCallback); + ble.gattServer().onDataWritten(onDataWrittenCallback); + +// bool initialValueForLEDCharacteristic = false; -void monitorButtons() -{ - // read in the buttons, mapped into nibble (0000 = all off, 1111 = all on) - button_new_value = 0; - button_new_value |= (button1.read() != 1); button_new_value <<= 1; - button_new_value |= (button2.read() != 1); button_new_value <<= 1; - //Should not used for WaveShare BLE400 - /* - button_new_value |= (button3.read() != 1); button_new_value <<= 1; - button_new_value |= (button4.read() != 1); - */ - // set the updated value of the characteristic if data has changed - if (button_new_value != button_old_value) - { - ble.updateCharacteristicValue( - gatt_characteristics[CHARACTERISTIC_BUTTON] -> getValueHandle(), - &button_new_value, sizeof(button_new_value)); - button_old_value = button_new_value; + /* Setup primary service. */ + //uartService = new UARTService(ble); + + /* Setup advertising. */ + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *)nRF51ServiceUUIDreversed, sizeof(nRF51ServiceUUIDreversed)); + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.gap().setAdvertisingInterval(100); /* 100ms. */ + + // add our gatt service with two characteristics + ble.addService(*gatt_service); - INFO_NL(" button state: [0x%02x]", button_new_value); + ble.gap().startAdvertising(); +} + +void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { + BLE &ble = BLE::Instance(); +// eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); + led2 = !led2; +} + +void BUTTON () { + if (!key1){ + START = 0; } } -//------------------------------------------------------------------------- -// LED handling -//------------------------------------------------------------------------- -//WaveShare BLE400 digital outputs as LED outputs -DigitalOut led1(P0_18); -DigitalOut led2(P0_19); -DigitalOut led3(P0_20); -DigitalOut led4(P0_21); -DigitalOut led5(P0_22); //Used here to view BLE connect/disconnect - -uint8_t led_value = 0; - -//Adapted for WaveShare BLE400, LED_ON = HIGH (NRF51822-DK vice versa LED_ON = LOW) -void onLedDataWritten(const uint8_t* value, uint8_t length) +// Основной цикл +int main(void) { - // we only care about a single byte - led_value = value[0]; - - // depending on the value coming through; set/unset LED's - if ((led_value & 0x01) != 0) led1.write(1); else led1.write(0); - if ((led_value & 0x02) != 0) led2.write(1); else led2.write(0); - if ((led_value & 0x04) != 0) led3.write(1); else led3.write(0); - if ((led_value & 0x08) != 0) led4.write(1); else led4.write(0); - - INFO_NL(" led state: [0x%02x]", led_value); -} - -//------------------------------------------------------------------------- - -void onConnection(const Gap::ConnectionCallbackParams_t *params) -{ - INFO_NL(">> connected"); + DISP_LED = 0; // Инициализация дисплея + tft.initR(INITR_144GREENTAB); + //tft.invertDisplay(true); // Инвертирование цветов дисплея для корректного отображения цветов + tft.fillScreen(ST7735_BLACK); // Заливка черным цветом + tft.setTextColor(WHITE); // Установка цвета текста + tft.setCursor(40, 48); + tft.setTextSize(2); + tft.printf("HPSP"); + tft.setCursor(22, 64); + tft.printf("BT-Lite"); + wait(1); + tft.setTextSize(1); + tft.setCursor(2, 110); + tft.printf("HPSP BT-Lite V:pre0.2"); + tft.setCursor(2, 120); + tft.printf("NOT FOR SALE"); + wait(2); + tft.fillScreen(ST7735_BLACK); + tft.setTextSize(2); + tft.setCursor(10, 57); + tft.printf("Wait Data"); + // tft.drawRect(100, 0, 20, 30, WHITE); - // set the initial values of the characteristics (for every session) - //led_value = 0; - onLedDataWritten(&led_value, 1); // force LED's to be in off state - //LED5=ON on connection (for BLE400) - led5.write(1); -} + ticker.attach(periodicCallback, 0.8); /* Blink LED every 3 seconds */ -void onDataWritten(const GattWriteCallbackParams *context) -{ - // was the characteristic being written to nRF51_GATT_CHAR_LED? - if (context -> handle == - gatt_characteristics[CHARACTERISTIC_LED] -> getValueHandle()) - { - onLedDataWritten(context -> data, context -> len); - } -} + // create our button characteristic (read, notify) + /*gatt_characteristics[CHARACTERISTIC_BUTTON] = + new GattCharacteristic( + nRF51_GATT_CHAR_BUTTON, + &gatt_char_value[CHARACTERISTIC_BUTTON], 1, 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);*/ -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) -{ - INFO_NL(">> disconnected"); - ble.gap().startAdvertising(); // restart advertising - INFO_NL(">> device advertising"); - //LED5=OFF on diconnection (for BLE400) - led5.write(0); -} + // create our LED characteristic (read, write) + gatt_characteristics[CHARACTERISTIC_LED] = + new GattCharacteristic( + nRF51ServiceUUID, + &gatt_char_value[CHARACTERISTIC_LED], 1, 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY ); + // create our service, with both characteristics + gatt_service = + new GattService(nRF51_GATT_SERVICE, + gatt_characteristics, CHARACTERISTIC_COUNT); -int main() -{ -#ifdef CONSOLE_LOG - // wait a second before trying to write something to console - wait(1); -#endif - INFO_NL(">> nRF51-BLE400 start"); + BLE &ble = BLE::Instance(); +// ble.onEventsToProcess(scheduleBleEventsProcessing); + ble.init(bleInitComplete); - // create our button characteristic (read, notify) - gatt_characteristics[CHARACTERISTIC_BUTTON] = - new GattCharacteristic( - nRF51_GATT_CHAR_BUTTON, - &gatt_char_value[CHARACTERISTIC_BUTTON], 1, 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); + /* SpinWait for initialization to complete. This is necessary because the + BLE object is used in the main loop below. */ + while (ble.hasInitialized() == false) + { + /* spin loop */ + } - // create our LED characteristic (read, write) - gatt_characteristics[CHARACTERISTIC_LED] = - new GattCharacteristic( - nRF51_GATT_CHAR_LED, - &gatt_char_value[CHARACTERISTIC_LED], 1, 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); + // Основной цикл + while (true) + { + if ( !key1 || !key2) { + DISP_LED = !DISP_LED; + KEY_TIME.attach(BUTTON, 5); + wait(0.2); - // create our service, with both characteristics - gatt_service = - new GattService(nRF51_GATT_SERVICE, - gatt_characteristics, CHARACTERISTIC_COUNT); + } - // initialize our ble device - ble.init(); - ble.gap().setDeviceName((uint8_t *)DEVICE_NAME); - INFO_NL(">> initialized device '%s'", DEVICE_NAME); + //BACKLIGTH.attach(&TFT_LED, 30); + + // Проверка флага на старт программы и отоброжение таймера ( TODO вынести в отдельную функцию и обновлять по прерыванию ) + if ( FLAG == 1 ) { - // configure our advertising type, payload and interval - ble.gap().accumulateAdvertisingPayload( - GapAdvertisingData::BREDR_NOT_SUPPORTED | - GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.gap().accumulateAdvertisingPayload( - GapAdvertisingData::COMPLETE_LOCAL_NAME, - (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); - ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.gap().setAdvertisingInterval(160); // 100ms - INFO_NL(">> configured advertising type, payload and interval"); - - // configure our callbacks - ble.gap().onDisconnection(disconnectionCallback); - ble.gap().onConnection(onConnection); - ble.onDataWritten(onDataWritten); - INFO_NL(">> registered for callbacks"); - - // add our gatt service with two characteristics - ble.addService(*gatt_service); - INFO_NL(">> added GATT service with two characteristics"); + // переводим время в другой формат ЧЧ:ММ:СС + HOUR = TIME_PROGRAMM / 60 / 60; + MIN = (TIME_PROGRAMM / 60) % 60; + SEC = TIME_PROGRAMM % 60; + // Тут надо добавить конвертацию типов пока вижу это как разделение на десятки и еденицы и далее по формуле i + '0' записывать их в массив. + // конвертацию то я сделал но как то не так. сыпет муссором на дисплей вида 00:0000:000000 и это в лучшем случае... + // Часы + HOUR_CHAR[0] = ((HOUR / 10) % 10) + '0'; + HOUR_CHAR[1] = (HOUR % 10) + '0'; + // Минуты + MIN_CHAR[0] = ((MIN / 10) % 10) + '0'; + MIN_CHAR[1] = (MIN % 10) + '0'; + // Секунды + SEC_CHAR[0] = ((SEC / 10) % 10) + '0'; + SEC_CHAR[1] = (SEC % 10) + '0'; + // Так как библиотека принимает только char пришлось посимвольно выводить время... + tft.drawChar(52, 48, HOUR_CHAR[0], WHITE, BLACK, 2); + tft.drawChar(64, 48, HOUR_CHAR[1], WHITE, BLACK, 2); + tft.drawChar(52, 64, MIN_CHAR[0], WHITE, BLACK, 2); + tft.drawChar(64, 64, MIN_CHAR[1], WHITE, BLACK, 2); - // show some debugging information about service/characteristics - INFO_UUID(" ", nRF51_GATT_SERVICE); - INFO_UUID(" :", nRF51_GATT_CHAR_BUTTON); - INFO_UUID(" :", nRF51_GATT_CHAR_LED); - - // start advertising - ble.gap().startAdvertising(); - INFO_NL(">> device advertising"); + if (r >= 30) { + r = 20; + if (COLOR == WHITE) { + COLOR = BLACK; + } + else (COLOR = WHITE); + }; - // start monitoring the buttons and posting new values - Ticker ticker; - ticker.attach(monitorButtons, 0.1); // every 10th of a second - INFO_NL(">> monitoring button state"); + tft.drawCircle(63, 62, r, COLOR); + if (TIME_PROGRAMM < 1) { + FLAG = 0; + } + } - // let the device do its thing - INFO_NL(">> waiting for events "); - for (;;) - { - ble.waitForEvent(); - } -} - -//------------------------------------------------------------------------- \ No newline at end of file + else { + ble.waitForEvent(); + + } + } +} \ No newline at end of file