TYBLE16 on os5 sample programs
Dependencies: BME280 TextLCD nRF51_Vdd
Fork of TYBLE16_mbedlized_os5_BASE by
Please refer following notebook.
/users/kenjiArai/notebook/tyble16-module-as-mbed-os-5-board-mbedlization/
Revision 3:c0010c8ad17f, committed 2018-04-15
- Comitter:
- kenjiArai
- Date:
- Sun Apr 15 04:15:18 2018 +0000
- Parent:
- 2:47ad8c48224e
- Commit message:
- Separated Uart_Clinent & Uart_Server due to memory overflow
Changed in this revision
--- a/7_Uart_Client/main.cpp Sat Apr 14 12:57:53 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,482 +0,0 @@ -/* - * ------- BLE Central/Client UART function ----------------------------------- - * communicate with BLE_UART_Server program - * --- Tested on Switch Science mbed TY51822r3 --- - * - * Modified by Kenji Arai - * http://www.page.sannet.ne.jp/kenjia/index.html - * https://os.mbed.com/users/kenjiArai/ - * - * Started: April 8th, 2016 - * Revised: June 13th, 2016 - * Revised: Feburary 10th, 2018 Not set mac addr but use device name - * Revised: Feburary 11th, 2018 use mbed-os5.7.4 with CircularBuffer - * Revised: April 14th, 2018 only for TYBLE16 - * - * Original program (see original.cpp file): - * S130 potential unstability case [closed] by Fabien Comte - * https://devzone.nordicsemi.com/question/49705/ - * s130-potential-unstability-case/ - * GitHub Q&A by Fabien COMTE - * https://github.com/ARMmbed/ble/issues/69 - * Reference program: - * BLE_Central_test by noboru koshinaka - * https://os.mbed.com/users/noboruk/code/BLE_Central_test/ - * Tested Server Device: - * BLE_Uart_Server - * https://os.mbed.com/users/kenjiArai/code/BLE_Uart_Server/ - */ - -//#define EXAMPLE_7_UART_CLIENT -#ifdef EXAMPLE_7_UART_CLIENT - -// Include -------------------------------------------------------------------- -#include "mbed.h" -#include "BLE.h" -#include "DiscoveredCharacteristic.h" -#include "DiscoveredService.h" -#include "UARTService.h" -#include "CircularBuffer.h" - -// Definition ----------------------------------------------------------------- -//#define USE_MAC // if you use mac address, please define it - -#define NUM_ONCE 20 -#define BFSIZE (NUM_ONCE+4) - -//#define USE_DEBUG_MODE -#ifdef USE_DEBUG_MODE -#define DBG(...) { pc.printf(__VA_ARGS__); } -#else -#define DBG(...) -#endif - -#define SOFT_DEVICE_FATHER_HANDLE 3 - -// Object --------------------------------------------------------------------- -BLE& ble_uart = BLE::Instance(); -DigitalOut alivenessLED(LED1, 1); -DigitalOut connectedLED(D0, 0); -Serial pc(USBTX, USBRX, 115200); -//Serial pc(P0_3, P0_1, 115200); // for another board -Ticker ticker; -CircularBuffer<char, 1536> ser_bf; -Thread tsk; - -// ROM / Constant data -------------------------------------------------------- -#ifdef USE_MAC -#warning "You need to modify below value based on your board." -const Gap::Address_t mac_board_0 = {0x50, 0x2b, 0xea, 0x14, 0x95, 0xd2}; -const Gap::Address_t mac_board_1 = {0x59, 0x2c, 0xa8, 0x0e, 0xe2, 0xef}; -const Gap::Address_t mac_board_2 = {0x0f, 0x72, 0xbf, 0x43, 0xbc, 0xd0}; -const Gap::Address_t mac_board_3 = {0x83, 0xc9, 0x1a, 0x90, 0xdf, 0xd6}; -const Gap::Address_t mac_board_4 = {0x43, 0xa4, 0x36, 0x11, 0x5b, 0xeb}; -#else -const char PEER_NAME[] = "UART_PJL"; -#endif - -// RAM ------------------------------------------------------------------------ -Gap::Handle_t connectionHandle = 0xFFFF; -DiscoveredCharacteristic uartTXCharacteristic; -DiscoveredCharacteristic uartRXCharacteristic; -bool foundUartRXCharacteristic = false; -bool connected2server = false; -bool connection_tx = false; -bool connection_rx = false; -UARTService * uartServicePtr = NULL; -Gap::Address_t my_mac; -int my_board_index = -1; -bool received_uart_dat = false; -int8_t uart_buffer[BFSIZE]; -uint8_t uart_bf_len; -volatile bool rx_isr_busy = false; - -// Function prototypes -------------------------------------------------------- -// BLE -void advertisementCallback(const Gap::AdvertisementCallbackParams_t *); -void serviceDiscoveryCallback(const DiscoveredService *); -void characteristicDiscoveryCallback(const DiscoveredCharacteristic *); -void discoveryTerminationCallback(Gap::Handle_t ); -void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *); -void connectionCallback(const Gap::ConnectionCallbackParams_t *); -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *); -// Interrupt related -void periodicCallback(void); -void serialRxCallback(void); -// serial receiving -void pc_ser_rx(void); -void preparation_sending_data(void); -// Pre-check -bool mac_equals(const Gap::Address_t, const Gap::Address_t); -int get_board_index(const Gap::Address_t); -void adjust_line(uint8_t *); - -//------------------------------------------------------------------------------ -// Control Program -//------------------------------------------------------------------------------ -int main(void) -{ - alivenessLED = 0; - pc.attach(&serialRxCallback, Serial::RxIrq); - ticker.attach(periodicCallback, 1); - tsk.start(pc_ser_rx); - // clear terminal output - for (int k = 0; k < 3; k++) { - pc.printf("\r\n"); - } - // opening message - pc.printf("UART Communication / Client(Central) side\r\n"); - pc.printf(" need Server module (run BLE_Uart_Server program)\r\n"); - // Mixed role ************************************************************** - ble_uart.init(); - ble_uart.gap().onConnection(connectionCallback); - ble_uart.gap().onDisconnection(disconnectionCallback); - // Client(Central) role **************************************************** - ble_uart.gattClient().onHVX(onReceivedDataFromDeviceCallback); - ble_uart.gap().setScanParams(500, 450); - ble_uart.gap().startScan(advertisementCallback); - while(true) { - // allow notifications from Server(Peripheral) - if (foundUartRXCharacteristic && - !ble_uart.gattClient().isServiceDiscoveryActive()) { - // need to do the following only once - foundUartRXCharacteristic = false; - uint16_t value = BLE_HVX_NOTIFICATION; - ble_uart.gattClient().write( - GattClient::GATT_OP_WRITE_REQ, - connectionHandle, - uartRXCharacteristic.getValueHandle() + 1, - sizeof(uint16_t), - reinterpret_cast<const uint8_t *>(&value) - ); - } - if (received_uart_dat == true) { - received_uart_dat = false; - for(int i = 0; i < uart_bf_len; i++) { - //pc.printf("%c", uart_buffer[i]); - pc.putc(uart_buffer[i]); - } - } - ble_uart.waitForEvent(); - } -} - -void periodicCallback(void) -{ - // Do blinky on alivenessLED to indicate system aliveness - alivenessLED = !alivenessLED; - if (connected2server) { - connectedLED = 1; - } else { - connectedLED = 0; - } - if (rx_isr_busy == true) { - rx_isr_busy = false; - } else { - tsk.signal_set(0x01); - } -} - -void serialRxCallback() -{ - ser_bf.push(pc.getc()); - rx_isr_busy = true; - tsk.signal_set(0x01); -} - -void pc_ser_rx() -{ - static uint8_t linebf_irq[BFSIZE]; - static volatile uint8_t linebf_irq_len = 0; - - while(true) { - Thread::signal_wait(0x01); - if (ser_bf.empty()) { - if (linebf_irq_len != 0) { - linebf_irq[linebf_irq_len] = 0; - adjust_line(linebf_irq); - linebf_irq_len = 0; - uartTXCharacteristic.write(NUM_ONCE, linebf_irq); - } - } - while(!ser_bf.empty()) { - char c; - ser_bf.pop(c); - if (c == '\b') { - linebf_irq_len--; - pc.putc(c); - pc.putc(' '); - pc.putc(c); - } else if ((c >= ' ') || (c == '\r') || (c == '\n')) { - bool overflow = false; - if ((c == '\r') || (c == '\n')) { - if (linebf_irq_len == NUM_ONCE - 1) { // remain only 1 buffer - overflow = true; - linebf_irq[linebf_irq_len++] = '\r'; - pc.putc('\r'); - } else { - overflow = false; - linebf_irq[linebf_irq_len++] = '\r'; - linebf_irq[linebf_irq_len++] = '\n'; - pc.printf("\r\n"); - } - } else { - linebf_irq[linebf_irq_len++] = c; - pc.putc(c); - } - if (linebf_irq_len >= NUM_ONCE ) { - linebf_irq[linebf_irq_len] = 0; - uartTXCharacteristic.write(linebf_irq_len, linebf_irq); - linebf_irq_len = 0; - if (overflow == true) { - overflow = false; - linebf_irq[linebf_irq_len++] = '\n'; - pc.putc('\n'); - } - } - } - } - } -} - -void adjust_line(uint8_t *bf) -{ - uint8_t i, c; - - for (i = 0; i <NUM_ONCE; bf++, i++) { - c = *bf; - if (c == 0) { - break; - } - } - for (; i < NUM_ONCE; bf++, i++) { - *bf = 0x11; - } - *(bf + 1) = 0; -} - -void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *params) -{ - DBG( - "received HVX callback for handle %u; type %s\r\r\n", - params->handle, - (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication" - ); - if (params->type == BLE_HVX_NOTIFICATION) { - if ((params->handle - == uartRXCharacteristic.getValueHandle()) && (params->len > 0)) { - uart_bf_len = params->len; - strcpy((char *)uart_buffer, (char *)params->data); - received_uart_dat = true; - } - } -} - -#ifdef USE_MAC - -bool mac_equals(const Gap::Address_t mac_1, const Gap::Address_t mac_2) -{ - DBG("Address: "); - for (int i = 0; i < 6; i++) { - DBG("0x%02x ", mac_1[i]); - } - DBG("\r\n"); - for (int i = 0; i < 6; i++) { - if (mac_1[i] != mac_2[i]) { - DBG("0x%02x != 0x%02x at %d\r\n", mac_1[i], mac_2[i], i); - return false; - } else { - DBG("0x%02x == 0x%02x at %d\r\n", mac_1[i], mac_2[i], i); - } - } - return true; -} - -int get_board_index(const Gap::Address_t mac) -{ - if (mac_equals(mac, mac_board_0)) { - return 0; - } - if (mac_equals(mac, mac_board_1)) { - return 1; - } - if (mac_equals(mac, mac_board_2)) { - return 2; - } - if (mac_equals(mac, mac_board_3)) { - return 3; - } - if (mac_equals(mac, mac_board_4)) { - return 4; - } - return -1; -} - -// Client(Central) role ******************************************************** -void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) -{ - // connections - int peer_board_index = get_board_index(params->peerAddr); - if (peer_board_index != -1) { - pc.printf(""); - pc.printf( - "adv peerAddr [%02x %02x %02x %02x %02x %02x]\r\n", - params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], - params->peerAddr[2], params->peerAddr[1], params->peerAddr[0] - ); - pc.printf( - "rssi=%+4d, isScanResponse %u, AdvertisementType %u\r\n", - params->rssi, params->isScanResponse, params->type - ); - ble_uart.gap().connect( - params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); - } -} - -#else - -// Client(Central) role ******************************************************** -void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) -{ - bool name_match = false; - - // parse the advertising payload, looking for data type COMPLETE_LOCAL_NAME - // The advertising payload is a collection of key/value records where - // byte 0: length of the record excluding this byte - // byte 1: The key, it is the type of the data - // byte [2..N] The value. N is equal to byte0 - 1 - - for( uint8_t i = 0; i < params->advertisingDataLen; ++i) { - const uint8_t record_length = params->advertisingData[i]; - if (record_length == 0) { - continue; - } - const uint8_t type = params->advertisingData[i + 1]; - const uint8_t* value = params->advertisingData + i + 2; - const uint8_t value_length = record_length - 1; - - if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) { - if ((value_length == sizeof(PEER_NAME)) - && (memcmp(value, PEER_NAME, value_length) == 0)) { - pc.printf( - "\r\nadv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, ", - params->peerAddr[5], params->peerAddr[4], - params->peerAddr[3], params->peerAddr[2], - params->peerAddr[1], params->peerAddr[0], - params->rssi - ); - pc.printf( - "isScanResponse %u, AdvertisementType %u\r\n", - params->isScanResponse, params->type - ); - name_match = true; - break; - } - } - i += record_length; - } - if( name_match != true ) { - return; - } - - pc.printf("Found device name : %s\r\n",PEER_NAME); - // connections - ble_uart.gap().connect(params->peerAddr, - Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); -} - -#endif - -void serviceDiscoveryCallback(const DiscoveredService *service) -{ - DBG("service found...\r\n"); - if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { - DBG( - "Service UUID-%x attrs[%u %u]\r\n", - service->getUUID().getShortUUID(), - service->getStartHandle(), - service->getEndHandle() - ); - } else { - DBG("Service UUID-"); - const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); - for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { - DBG("%02x", longUUIDBytes[i]); - } - DBG(" attrs[%u %u]\r\n", - service->getStartHandle(), service->getEndHandle()); - } -} - -void characteristicDiscoveryCallback( - const DiscoveredCharacteristic *characteristicP) -{ - DBG( - " C UUID-%x valueAttr[%u] props[%x]\r\n", - characteristicP->getUUID().getShortUUID(), - characteristicP->getValueHandle(), - (uint8_t)characteristicP->getProperties().broadcast() - ); - if (characteristicP->getUUID().getShortUUID() - == UARTServiceTXCharacteristicShortUUID) { - DBG("Sevice TX 0x%04x\r\n", UARTServiceTXCharacteristicShortUUID); - uartTXCharacteristic = *characteristicP; - connection_tx = true; - } else if (characteristicP->getUUID().getShortUUID() - == UARTServiceRXCharacteristicShortUUID) { - DBG("Sevice RX 0x%04x\r\n", UARTServiceRXCharacteristicShortUUID); - uartRXCharacteristic = *characteristicP; - foundUartRXCharacteristic = true; - connection_rx = true; - } -} - -void discoveryTerminationCallback(Gap::Handle_t connectionHandle) -{ - DBG("terminated SD for handle=%u\r\n", connectionHandle); -} - -// Mixed role ****************************************************************** -void connectionCallback(const Gap::ConnectionCallbackParams_t *params) -{ - if (params->role == Gap::CENTRAL) { - pc.printf("connected as Client(Central) (handle = %d)\r\n\r", - params->handle); - connected2server = true; - connectionHandle = params->handle; - ble_uart.gattClient().onServiceDiscoveryTermination( - discoveryTerminationCallback); - ble_uart.gattClient().launchServiceDiscovery( - params->handle, - serviceDiscoveryCallback, - characteristicDiscoveryCallback - ); - } - pc.printf( - "Client(Central/Myself) %02x:%02x:%02x:%02x:%02x:%02x\r\n", - params->ownAddr[5], params->ownAddr[4], params->ownAddr[3], - params->ownAddr[2], params->ownAddr[1], params->ownAddr[0] - ); - pc.printf( - "Connected Server(Peripheral) %02x:%02x:%02x:%02x:%02x:%02x\r\n", - params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], - params->peerAddr[2], params->peerAddr[1], params->peerAddr[0] - ); -} - -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) -{ - DBG("handle = %d ", params->handle); - pc.printf(" -> disconnected\r\n", params->handle); - connected2server = false; -// connection_1st = false; - connection_tx = false; - connection_rx = false; - if (params->handle == SOFT_DEVICE_FATHER_HANDLE) { - ble_uart.startAdvertising(); // restart advertising - } else { - ble_uart.gap().startScan(advertisementCallback);// restart scan - } -} - -#endif
--- a/7_Uart_Client/original.cpp Sat Apr 14 12:57:53 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,614 +0,0 @@ -#if 0 -//------------------------------------------------------------------------------------------------- -// ORIGINAL PROGRAM -// S130 potential unstability case [closed] by Fabien Comte -// https://devzone.nordicsemi.com/question/49705/s130-potential-unstability-case/ -// -#include "mbed.h" -#include "BLE.h" -#include "UARTService.h" -#include "ble/DiscoveredCharacteristic.h" -#include "ble/DiscoveredService.h" -#include "ble/service/UARTService.h" - -#define SOFT_DEVICE_FATHER_HANDLE 3 - -#define BOARDS_COUNT 3 - -const Gap::Address_t mac_board_0 = {0xb8, 0xac, 0x4e, 0x8d, 0x8b, 0xeb}; -const Gap::Address_t mac_board_1 = {0x9c, 0x43, 0x62, 0x30, 0xaf, 0xd2}; -const Gap::Address_t mac_board_2 = {0x5f, 0x1a, 0x9e, 0x6a, 0x63, 0xdd}; - - -// tiny ble board -#define LED_GREEN p21 -#define LED_RED p22 -#define LED_BLUE p23 -#define BUTTON_PIN p17 -#define BATTERY_PIN p1 - -#define MPU6050_SDA p12 -#define MPU6050_SCL p13 - -#define UART_TX p9 -#define UART_RX p11 -#define UART_CTS p8 -#define UART_RTS p10 - -DigitalOut led(LED_RED); -DigitalOut alivenessLED(LED_GREEN); -InterruptIn button(BUTTON_PIN); -AnalogIn battery(BATTERY_PIN); -Serial pc(UART_TX, UART_RX); - -bool mac_equals(const Gap::Address_t mac_1, const Gap::Address_t mac_2) -{ - #if 0 - if (mac_1[0] != mac_2[0]) - { - return false; - } - if (mac_1[1] != mac_2[1]) - { - return false; - } - if (mac_1[2] != mac_2[2]) - { - return false; - } - if (mac_1[3] != mac_2[3]) - { - return false; - } - if (mac_1[4] != mac_2[4]) - { - return false; - } - if (mac_1[5] != mac_2[5]) - { - return false; - } - #else - for (int i = 0; i < 6; i++) - { - if (mac_1[i] != mac_2[i]) - { - //pc.printf("0x%02x != 0x%02x at %d\r\n", mac_1[i], mac_2[i], i); - return false; - } - else - { - //pc.printf("0x%02x == 0x%02x at %d\r\n", mac_1[i], mac_2[i], i); - } - } - #endif - return true; -} - -int get_board_index(const Gap::Address_t mac) -{ - if (mac_equals(mac, mac_board_0)) - { - return 0; - } - if (mac_equals(mac, mac_board_1)) - { - return 1; - } - if (mac_equals(mac, mac_board_2)) - { - return 2; - } - - return -1; -} - -void periodicCallback(void) -{ - alivenessLED = !alivenessLED; /* do blinky on alivenessLED while we're waiting for BLE events */ -} - - -// Mixed role **************************************************** -BLE ble; -Gap::Address_t my_mac; -int my_board_index = -1; - -// Device role **************************************************** -UARTService * uartServicePtr = NULL; -const static char DEVICE_NAME[] = "ChangeMe!!"; // change this -static const uint16_t uuid16_list[] = {UARTServiceShortUUID}; -volatile int central_handle = -1; - - -// Central role **************************************************** -Gap::Handle_t connectionHandle = 0xFFFF; -DiscoveredCharacteristic uartTXCharacteristic; -DiscoveredCharacteristic uartRXCharacteristic; -bool foundUartRXCharacteristic = false; -volatile int device_handle = -1; - - -// Device role **************************************************** -void onReceivedDataFromCentralCallback(const GattWriteCallbackParams *params) -{ - if (uartServicePtr != NULL) - { - if ((params->handle == uartServicePtr->getTXCharacteristicHandle()) && (params->len >= 1)) - { - if (params->data[0] != '0') - { - led = 1; - } - else - { - led = 0; - } - - for(int i = 0; i < params->len; i++) - { - pc.printf("%c", params->data[i]); - } - - pc.printf(" (%d, %d)\r\n", params->handle, params->connHandle); - } - } -} - -// Central role **************************************************** -void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) -{ - // do connections like a triangle - int peer_board_index = get_board_index(params->peerAddr); - - int next_board_index = my_board_index + 1; - if (next_board_index >= BOARDS_COUNT) - { - next_board_index = 0; - } - - //pc.printf("adv %d, %d, %d\r\n", peer_board_index, my_board_index, next_board_index); - - // force order - if ((central_handle != -1) || (peer_board_index == 0)) - { - if (peer_board_index == next_board_index) - { - //pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", - // params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], - // params->rssi, params->isScanResponse, params->type); - - ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); - } - } -} - -void serviceDiscoveryCallback(const DiscoveredService *service) -{ - if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) - { - pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); - } - else - { - //pc.printf("S UUID-"); - const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); - for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) - { - pc.printf("%02x", longUUIDBytes[i]); - } - pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); - } -} - -void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) -{ - //pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); - if (characteristicP->getUUID().getShortUUID() == UARTServiceTXCharacteristicShortUUID) - { - pc.printf("fit TX 0x%04x\r\n", UARTServiceTXCharacteristicShortUUID); - /* !ALERT! Alter this filter to suit your device. */ - uartTXCharacteristic = *characteristicP; - } - else if (characteristicP->getUUID().getShortUUID() == UARTServiceRXCharacteristicShortUUID) - { - pc.printf("fit RX 0x%04x\r\n", UARTServiceRXCharacteristicShortUUID); - /* !ALERT! Alter this filter to suit your device. */ - uartRXCharacteristic = *characteristicP; - foundUartRXCharacteristic = true; - } -} - -void discoveryTerminationCallback(Gap::Handle_t connectionHandle) -{ - pc.printf("terminated SD for handle %u\r\n", connectionHandle); -} - -void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *params) -{ - //pc.printf("received HVX callback for handle %u; type %s\r\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication"); - if (params->type == BLE_HVX_NOTIFICATION) - { - if ((params->handle == uartRXCharacteristic.getValueHandle()) && (params->len > 0)) - { - for (int i = 0; i < params->len; i++) - { - pc.printf("%c", params->data[i]); - } - - pc.printf(" (%d, %d)\r\n", params->handle, params->connHandle); - } - } - else - { - pc.printf("%d\r\n", params->type); - } -} - -// Mixed role **************************************************** -void connectionCallback(const Gap::ConnectionCallbackParams_t *params) -{ - if (params->role == Gap::CENTRAL) - { - if (central_handle == -1) - { - ble.stopAdvertising(); // stop advertising during discovery, incoming connection breaks discovery - } - - device_handle = params->handle; - pc.printf("connected as central (handle = %d)\r\n\r", params->handle); - connectionHandle = params->handle; - ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); - int ret = ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, UARTServiceShortUUID/*, 0xa001*/); - - if (ret != BLE_ERROR_NONE) - { - pc.printf("launchServiceDiscovery failed error = %d\r\n\r", ret); - } - } - else - { - central_handle = params->handle; - pc.printf("connected as device (handle = %d)\r\n\r", params->handle); - - //pc.printf("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n", params->connectionParams->minConnectionInterval, params->connectionParams->maxConnectionInterval, params->connectionParams->slaveLatency, params->connectionParams->connectionSupervisionTimeout); - /* - Gap::ConnectionParams_t connectionParams; - connectionParams.minConnectionInterval = 6; - connectionParams.maxConnectionInterval = 12; - connectionParams.slaveLatency = 40; - connectionParams.connectionSupervisionTimeout = 500; - - int ret = ble.updateConnectionParams(params->handle, &connectionParams); - if (ret != BLE_ERROR_NONE) - { - pc.printf("failed to update connection parameter\r\n"); - } - */ - } - pc.printf("own %02x:%02x:%02x:%02x:%02x:%02x (%s), peer %02x:%02x:%02x:%02x:%02x:%02x (%s)\r\n", params->ownAddr[5], params->ownAddr[4], params->ownAddr[3], params->ownAddr[2], params->ownAddr[1], params->ownAddr[0], (params->ownAddrType == Gap::ADDR_TYPE_PUBLIC) ? "public" : "random", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], (params->peerAddrType == Gap::ADDR_TYPE_PUBLIC) ? "public" : "random"); -} - -void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) -{ - char * ascii_reason = "?"; - switch (reason) - { - case Gap::CONNECTION_TIMEOUT: - ascii_reason = "connection timeout"; - break; - case Gap::REMOTE_USER_TERMINATED_CONNECTION: - ascii_reason = "user terminated connection"; - break; - case Gap::REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES: - ascii_reason = "low resources"; - break; - case Gap::REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF: - ascii_reason = "power off"; - break; - case Gap::LOCAL_HOST_TERMINATED_CONNECTION: - ascii_reason = "host terminated connection"; - break; - case Gap::CONN_INTERVAL_UNACCEPTABLE: - ascii_reason = "interval unacceptable"; - break; - default: - ascii_reason = "unknown"; - break; - } - - pc.printf("disconnected (reason = %s, handle = %d)\r\n", ascii_reason, handle); - - - if (handle == SOFT_DEVICE_FATHER_HANDLE) - { - central_handle = -1; - // restart advertising - ble.startAdvertising(); - } - else - { - device_handle = -1; - // restart scan - ble.gap().startScan(advertisementCallback); - } -} - - - -void serialTxCallback() -{ - -} - -int rx_char = -1; -void serialRxCallback() -{ - if (rx_char != -1) - { - pc.printf("overflow\r\n"); - } - - //computer.putc(computer.getc()); - rx_char = pc.getc(); -} - -/* -void gattServerOnDataSent(unsigned count) -{ - -} -*/ - - -int main(void) -{ - alivenessLED = 0; - - pc.baud(115200); - //pc.attach(&serialTxCallback, Serial::TxIrq); - pc.attach(&serialRxCallback, Serial::RxIrq); - - // clear terminal output - for (int k = 0; k < 32; k++) - { - pc.printf("\r\n"); - } - - pc.printf("Central and device\r\n"); - - Ticker ticker; - ticker.attach(periodicCallback, 1); - - - // Mixed role **************************************************** - ble.init(); - - Gap::AddressType_t my_mac_type; - ble.gap().getAddress(&my_mac_type, my_mac); - my_board_index = get_board_index(my_mac); - pc.printf("me %02x:%02x:%02x:%02x:%02x:%02x (%s)\r\n", my_mac[5], my_mac[4], my_mac[3], my_mac[2], my_mac[1], my_mac[0], (my_mac_type == Gap::ADDR_TYPE_PUBLIC) ? "public" : "random"); - - - // try to speed up but looks like if it was ignored - Gap::ConnectionParams_t fast; - if (ble.getPreferredConnectionParams(&fast) != BLE_ERROR_NONE) - { - pc.printf("getPreferredConnectionParams failed\r\n"); - } - else - { - fast.minConnectionInterval = 16; // 20 ms - fast.maxConnectionInterval = 32; // 40 ms - fast.slaveLatency = 0; - if (ble.gap().setPreferredConnectionParams(&fast) != BLE_ERROR_NONE) - { - pc.printf("setPreferredConnectionParams failed\r\n"); - } - } - ble.gap().onConnection(connectionCallback); - ble.gap().onDisconnection(disconnectionCallback); - - // Device role **************************************************** - ble.gattServer().onDataWritten(onReceivedDataFromCentralCallback); - //ble.gattServer().onDataSent(gattServerOnDataSent); - - UARTService uartService(ble); - uartServicePtr = &uartService; - - // setup advertising - ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet - ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type - ble.setAdvertisingInterval(100); - - // Central role **************************************************** - ble.gattClient().onHVX(onReceivedDataFromDeviceCallback); - ble.gap().setScanParams(500, 450); - - - // start advertising and scan - ble.startAdvertising(); - ble.gap().startScan(advertisementCallback); - - while (true) - { - // allow notifications from device - if (foundUartRXCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) - { - foundUartRXCharacteristic = false; /* need to do the following only once */ - - uint16_t value = BLE_HVX_NOTIFICATION; - int ret = ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ, - connectionHandle, - uartRXCharacteristic.getValueHandle() + 1, /* HACK Alert. We're assuming that CCCD descriptor immediately follows the value attribute. */ - sizeof(uint16_t), /* HACK Alert! size should be made into a BLE_API constant. */ - reinterpret_cast<const uint8_t *>(&value)); - - if (ret == BLE_ERROR_NONE) - { - pc.printf("\r\ndevice notifications enabled\r\n"); - } - else - { - switch (ret) - { - case BLE_STACK_BUSY: - foundUartRXCharacteristic = true; // retry later - break; - case BLE_ERROR_NO_MEM: - foundUartRXCharacteristic = true; // retry later - break; - case BLE_ERROR_INVALID_STATE: - pc.printf("\r\ndevice notifications enable failed\r\n"); - break; - default: - break; - } - } - - if (!foundUartRXCharacteristic) - { - if (central_handle == -1) - { - ble.startAdvertising(); - } - } - } - - // while a new char from computer is available - while (rx_char != -1) - { - uint8_t temp[20]; - int length = 1; - - uint8_t command = rx_char; - rx_char = -1; - - // if special char to test a 20 bytes frame - /* - if (command == '*') - { - pc.printf("20 chars\r\n"); - - int c = 0; - for (c = 0; c < 20; c++) - { - temp[c] = 'a' + c; - } - length = 20; - } - else - { - temp[0] = command; - } - */ - temp[0] = command; - - // to central - //if (command == '1') - { - if (central_handle != -1) - { - // device to central - while (1) - { - if (central_handle == -1) - { - pc.printf("\r\ndisconnected 1 (to central)\r\n"); - break; - } - if (!ble.gap().getState().connected) - { - pc.printf("\r\ndisconnected 2 (to central)\r\n"); - break; - } - int ret = ble.gattServer().write(uartServicePtr->getRXCharacteristicHandle(), temp, length); - - if (ret == BLE_ERROR_NONE) - { - //pc.printf("\r\nok (to central)\r\n"); - break; - } - else if (ret == BLE_STACK_BUSY) - { - //pc.printf("\r\nbusy (to central)\r\n"); - //break; - } - else if (ret == BLE_ERROR_OPERATION_NOT_PERMITTED) - { - pc.printf("\r\nnot permitted (to central)\r\n"); - break; - } - else if (ret == BLE_ERROR_INVALID_STATE) - { - pc.printf("\r\ninvalid state (to central)\r\n"); - break; - } - else - { - pc.printf("\r\ncode %d (to central)\r\n", ret); - break; - } - } - } - else - { - pc.printf("\r\nnot connected with central\r\n"); - } - } - - // to device - //if (command == '2') - { - if (device_handle != -1) - { - // central to device - while (1) - { - if (device_handle == -1) - { - pc.printf("\r\ndisconnected (to device)\r\n"); - break; - } - int ret = uartTXCharacteristic.write(length, temp); - if (ret == BLE_ERROR_NONE) - { - //pc.printf("\r\nok (to device)\r\n"); - break; - } - else if (ret == BLE_STACK_BUSY) - { - //pc.printf("\r\nbusy (to device)\r\n"); - //break; - } - else if (ret == BLE_ERROR_OPERATION_NOT_PERMITTED) - { - pc.printf("\r\nnot permitted (to device)\r\n"); - break; - } - else if (ret == BLE_ERROR_INVALID_STATE) - { - pc.printf("\r\ninvalid state (to device)\r\n"); - break; - } - else - { - pc.printf("\r\ncode %d (to device)\r\n", ret); - break; - } - } - } - else - { - pc.printf("\r\nnot connected with device\r\n"); - } - } - } - - ble.waitForEvent(); // save power - } -} - -#endif
--- a/8_Uart_Server/main.cpp Sat Apr 14 12:57:53 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,585 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 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. - */ - -/* - * ------- BLE Peripheral/Server UART function -------------------------------- - * communicate with BLE_UART_Client program - * --- Tested on Switch Science mbed TY51822r3 --- - * - * http://www.page.sannet.ne.jp/kenjia/index.html - * https://os.mbed.com/users/kenjiArai/ - * - * Started: March 7th, 2016 - * Revised: June 13th, 2016 - * Revised: Feburary 10th, 2018 Not set mac addr but use device name - * Revised: Feburary 11th, 2018 use mbed-os5.7.4 with CircularBuffer - * Revised: April 14th, 2018 modification only for TYBLE16 - * - * Original program: - * BLE_LoopbackUART - * https://developer.mbed.org/teams/Bluetooth-Low-Energy/ - * code/BLE_LoopbackUART/ - * Reference program: - * BLE_Peripheral_test by noboru koshinaka - * https://os.mbed.com/users/noboruk/code/BLE_Peripheral_test/ - * Tested Client Device: - * BLE_Uart_Client - * https://os.mbed.com/users/kenjiArai/code/BLE_Uart_Client/ - */ - -//#define EXAMPLE_8_UART_SERVER -#ifdef EXAMPLE_8_UART_SERVER - -// Include -------------------------------------------------------------------- -#include "mbed.h" -#include "BLE.h" -#include "UARTService.h" -#include "nRF51_Vdd.h" -#include "nRF51_WakeUp.h" -#include "CircularBuffer.h" - -// Definition ----------------------------------------------------------------- -//#define USE_MAC // if you use mac address, please define it - -#define NUM_ONCE 20 -#define BFSIZE (NUM_ONCE+4) - -// Please refer nRF51_WakeUP library -#define GOTO_SLEEP_MODE 0 -#if GOTO_SLEEP_MODE -#warning "Make sure!! -> You need to connected P0_21(LED1) and P0_0" -#endif - -//#define USE_DEBUG_MODE -#ifdef USE_DEBUG_MODE -#define DEBUG(...) { printf(__VA_ARGS__); } -#else -#define DEBUG(...) -#endif - -// Object --------------------------------------------------------------------- -BLE& ble_uart = BLE::Instance(); -DigitalOut connectedLED(LED1); -//InterruptIn wake_up_sw(P0_1); -//nRF51_WakeUp wakeup(P0_21, P0_0); -nRF51_Vdd vdd(3.0f, 2.2f); -Serial pc(USBTX, USBRX, 115200); -//Serial pc(P0_3, P0_1, 115200); // for another board -UARTService *uartServicePtr; -Ticker ticker; -CircularBuffer<char, 1536> ser_bf; -Thread tsk; -Mutex bletx_mutex; - -// ROM / Constant data -------------------------------------------------------- -#warning "You need to confirm your device name." -const static char DEVICE_NAME[] = "UART_PJL"; - -// RAM ------------------------------------------------------------------------ -Gap::Address_t my_mac; -uint8_t tx_buf[BFSIZE]; -uint8_t tx_len = 0; -uint8_t rx_buf[BFSIZE]; -volatile bool trigger_transmit = false; -volatile bool trigger_receive = false; -volatile uint8_t command_continue = 0; -uint16_t time_out_cntr = 3600; -volatile bool time_out = false; -//uint32_t sleep_time = 30; // unit:second -volatile bool rx_isr_busy = false; - -// Function prototypes -------------------------------------------------------- -// BLE -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *); -void onDataWritten_action(const GattWriteCallbackParams *); -// Tasks -void pc_ser_rx(void); -void data_from_ble(void); -void Update_Values(void); -// Application related -void command(uint8_t *cmd); -void action_tx_help(void); -void action_tx_vdd(void); -void action_tx_temperature(void); -//void action_tx_wait_time(uint8_t *); -void action_tx_quit(void); -static int xatoi (char **, int32_t *); -void adjust_line(uint8_t *); -// Interrupt related -//void interrupt_by_sw(void); -void serialRxCallback(void); -void periodicCallback(void); - -//------------------------------------------------------------------------------ -// Control Program -//------------------------------------------------------------------------------ -int main(void) -{ - connectedLED = 0; - pc.attach(&serialRxCallback, Serial::RxIrq); - ticker.attach(periodicCallback, 1); - tsk.start(pc_ser_rx); - // clear terminal output - for (int k = 0; k < 3; k++) { - pc.printf("\r\n"); - } - // opening message - pc.printf("UART Communication / Server(Peripheral) side\r\n"); - pc.printf(" need Client module (run BLE_Uart_Client program)\r\n"); - // Interrupt by switch -// wake_up_sw.fall(&interrupt_by_sw); - ble_uart.init(); - Gap::AddressType_t my_mac_type; - ble_uart.gap().getAddress(&my_mac_type, my_mac); - DEBUG( - " my_MAC %02x:%02x:%02x:%02x:%02x:%02x (%s)\r\n", - my_mac[5], my_mac[4], my_mac[3], my_mac[2], my_mac[1], my_mac[0], - (my_mac_type == Gap::ADDR_TYPE_PUBLIC) ? "public" : "random" - ); - pc.printf( - " My device name : %s\r\n", DEVICE_NAME); - pc.printf( - " My mac data %02x:%02x:%02x:%02x:%02x:%02x\r\n", - my_mac[5], my_mac[4], my_mac[3], my_mac[2], my_mac[1], my_mac[0] - ); -#ifdef USE_MAC - pc.printf( - " mac_board_x = {0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x};\r\n", - my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5] - ); - pc.printf( - " Please write above data(mac_board_x line (x=0,1,2,...))\r\n"); - pc.printf( - " into Client/main.cpp [ROM / Constant data] area\r\n"); -#endif - ble_uart.onDisconnection(disconnectionCallback); - ble_uart.onDataWritten(onDataWritten_action); - /* setup advertising */ - ble_uart.accumulateAdvertisingPayload( - GapAdvertisingData::BREDR_NOT_SUPPORTED); - ble_uart.setAdvertisingType( - GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble_uart.accumulateAdvertisingPayload( - GapAdvertisingData::COMPLETE_LOCAL_NAME, - (const uint8_t *)DEVICE_NAME, - sizeof(DEVICE_NAME) - ); - ble_uart.accumulateAdvertisingPayload( - GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, - (const uint8_t *)UARTServiceUUID_reversed, - sizeof(UARTServiceUUID_reversed) - ); - // Advertize Interval - ble_uart.setAdvertisingInterval(1000); /* 1000ms;in multiples of 0.625ms.*/ - // Start - ble_uart.startAdvertising(); - UARTService uartService(ble_uart); - uartServicePtr = &uartService; - while(true) { - if (time_out) { -#if GOTO_SLEEP_MODE - wakeup.set_and_wait(sleep_time); - while(true) { // never come here but just in case - deepsleep(); - } -#endif - } - if (trigger_transmit) { - static uint8_t cmd_buf[BFSIZE]; - static volatile bool flag_continue = 0; - trigger_transmit = false; - pc.printf((const char*)rx_buf); - if (flag_continue == true) { - strcat((char *)cmd_buf, (char *)rx_buf); - if (strchr((const char*)cmd_buf,(int)'\r') == 0) { - flag_continue = true; - } else { - command(cmd_buf); - for(uint8_t i = 0; i < BFSIZE; i++) { - cmd_buf[i] = 0; - } - flag_continue = false; - } - } - if ((rx_buf[0] == '~')) { - strcpy((char *)cmd_buf, (char *)rx_buf); - if (strchr((const char*)cmd_buf,(int)'\r') == 0) { - flag_continue = true; - } else { - command(cmd_buf); - for(uint8_t i = 0; i < BFSIZE; i++) { - cmd_buf[i] = 0; - } - flag_continue = false; - } - } - } - ble_uart.waitForEvent(); - } -} - -void command(uint8_t *cmd) -{ - uint8_t *p = cmd; - - while(*p == ' ') { - ++p; // skip space - } - if (*p++ == '~') { - while(*p < '!') { - ++p; // skip space - } - uint8_t c = *p; - //pc.printf("c=%c\r\n", c); - switch (c) { - case 'v': - action_tx_vdd(); - break; - case 't': - action_tx_temperature(); - break; - case 'q': - action_tx_quit(); - break; -#if 0 - case 'w': - action_tx_wait_time(cmd); - break; -#endif - case 'h': - case '?': - action_tx_help(); - break; - default: - //pc.printf("\r\nStep(%u)\r\n", __LINE__); - break; - } - } -} - -void periodicCallback(void) -{ -#if GOTO_SLEEP_MODE - if (--time_out_cntr == 0) { - time_out = true; - } -#endif - if (rx_isr_busy == true) { - rx_isr_busy = false; - } else { - tsk.signal_set(0x01); - } -} - -void serialRxCallback() -{ - ser_bf.push(pc.getc()); - rx_isr_busy = true; - tsk.signal_set(0x01); -} - -void pc_ser_rx() -{ - static uint8_t linebf_irq[BFSIZE]; - static volatile uint8_t linebf_irq_len = 0; - - while(true) { - Thread::signal_wait(0x01); - if (ser_bf.empty()) { - if (linebf_irq_len != 0) { - linebf_irq[linebf_irq_len] = 0; - adjust_line(linebf_irq); - linebf_irq_len = 0; - bletx_mutex.lock(); - ble_uart.updateCharacteristicValue( - uartServicePtr->getRXCharacteristicHandle(), - linebf_irq, - NUM_ONCE - ); - bletx_mutex.unlock(); - } - } - while(!ser_bf.empty()) { - char c; - ser_bf.pop(c); - if (c == '\b') { - linebf_irq_len--; - pc.putc(c); - pc.putc(' '); - pc.putc(c); - } else if ((c >= ' ') || (c == '\r') || (c == '\n')) { - bool overflow = false; - if ((c == '\r') || (c == '\n')) { - if (linebf_irq_len == NUM_ONCE - 1) { // remain only 1 buffer - overflow = true; - linebf_irq[linebf_irq_len++] = '\r'; - pc.putc('\r'); - } else { - overflow = false; - linebf_irq[linebf_irq_len++] = '\r'; - linebf_irq[linebf_irq_len++] = '\n'; - pc.printf("\r\n"); - } - } else { - linebf_irq[linebf_irq_len++] = c; - pc.putc(c); - } - if (linebf_irq_len >= NUM_ONCE ) { - linebf_irq[linebf_irq_len] = 0; - adjust_line(linebf_irq); - linebf_irq_len = 0; - bletx_mutex.lock(); - ble_uart.updateCharacteristicValue( - uartServicePtr->getRXCharacteristicHandle(), - linebf_irq, - NUM_ONCE - ); - bletx_mutex.unlock(); - if (overflow == true) { - overflow = false; - linebf_irq[linebf_irq_len++] = '\n'; - pc.putc('\n'); - } - } - } - } - } -} - -void adjust_line(uint8_t *bf) -{ - uint8_t i, c; - - for (i = 0; i <NUM_ONCE; bf++, i++) { - c = *bf; - if (c == 0) { - break; - } - } - for (; i < NUM_ONCE; bf++, i++) { - *bf = 0x11; - } - *(bf + 1) = 0; -} - -void onDataWritten_action(const GattWriteCallbackParams *params) -{ - if ((uartServicePtr != NULL) && - (params->handle == uartServicePtr->getTXCharacteristicHandle())) { - strcpy((char *)rx_buf, (const char *)params->data); - trigger_transmit = true; - } -} - -void action_tx_help() -{ - // 12345678901234567890 - sprintf((char *)tx_buf," ~?:help\r\n"); - tx_len = strlen((const char *)tx_buf); - Update_Values(); - Thread::wait(200); - // 12345678901234567890 - sprintf((char *)tx_buf," ~v:vdd\r\n"); - tx_len = strlen((const char *)tx_buf); - Update_Values(); - Thread::wait(200); - // 12345678901234567890 - sprintf((char *)tx_buf," ~t:temperature\r\n"); - tx_len = strlen((const char *)tx_buf); - Update_Values(); - Thread::wait(200); -#if 0 - // 12345678901234567890 - sprintf((char *)tx_buf," ~w:wait, w 120\r\n"); - tx_len = strlen((const char *)tx_buf); - Update_Values(); - Thread::wait(200); -#endif - // 12345678901234567890 - sprintf((char *)tx_buf," ~q:quit/sleep\r\n"); - tx_len = strlen((const char *)tx_buf); - Update_Values(); - Thread::wait(200); -} - -void action_tx_vdd() -{ - sprintf((char *)tx_buf,"Vdd: %3.2f V\r\n", vdd.read_real_value()); - tx_len = strlen((const char *)tx_buf); - Update_Values(); -} - -void action_tx_temperature() -{ - int32_t p_temp; - float temperature; - - // Update a temperature (inside nRF51822 chip) - sd_temp_get(&p_temp); - // -16.0f is offset vale for chip die temp - // to ambient temp (depend on your board) - temperature = float(p_temp) / 4; // Original = float(p_temp)/4.0f - 16.0f; - sprintf((char *)tx_buf,"T: %+4.1f dC\r\n", temperature); - tx_len = strlen((const char *)tx_buf); - Update_Values(); -} - -#if 0 -void action_tx_wait_time(uint8_t *cmd) -{ - int32_t dt; - char *p; - - p = (char *)(cmd); - p += 2; // point to time value - if (xatoi(&p, &dt)) { - if (dt <= 5) { - dt = 5; - } - sleep_time = dt; // set next wake-up period - } else { - DEBUG("data is unknown!\r\n"); - sleep_time = 30; - } - DEBUG("slp_t:%d\r\n", sleep_time); - //pc.printf("slp_t:%d\r\n", sleep_time); - // 12345678901234567890 - sprintf((char *)tx_buf, "W: %d sec\r\n", sleep_time); - tx_len = strlen((const char *)tx_buf); - Update_Values(); -} -#endif - -void action_tx_quit() -{ -#if GOTO_SLEEP_MODE - ticker.detach(); - // 12345678901234567890 - sprintf((char *)tx_buf,"Terminated the BLE"); - tx_len = strlen((const char *)tx_buf); - Update_Values(); - Thread::wait(1000); - wakeup.set_and_wait(sleep_time); - while(true) { // never come here but just in case - deepsleep(); - } -#else - SCB->AIRCR = 0x05fa0004; // System RESET!! -#endif -} - -// Change string -> integer -static int xatoi (char **str, int32_t *res) -{ - unsigned long val; - unsigned char c, radix, s = 0; - - for (;;) { - c = **str; - if (c == 0) { - return 0; - } - if (c == '-') { - break; - } - if (c == '+') { - (*str)++; - c = **str; - } - if (c>='0'&& c<='9') { - break; - } else { - (*str)++; - c = **str; - } - } - if (c == '-') { - s = 1; - c = *(++(*str)); - } - if (c == '0') { - c = *(++(*str)); - if (c <= ' ') { - *res = 0; - return 1; - } - if (c == 'x') { - radix = 16; - c = *(++(*str)); - } else { - if (c == 'b') { - radix = 2; - c = *(++(*str)); - } else { - if ((c >= '0')&&(c <= '9')) { - radix = 8; - } else { - return 0; - } - } - } - } else { - if ((c < '1')||(c > '9')) { - return 0; - } - radix = 10; - } - val = 0; - while (c > ' ') { - if (c >= 'a') c -= 0x20; - c -= '0'; - if (c >= 17) { - c -= 7; - if (c <= 9) return 0; - } - if (c >= radix) return 0; - val = val * radix + c; - c = *(++(*str)); - } - if (s) val = -val; - *res = val; - return 1; -} - -#if 0 -void interrupt_by_sw() // Go to sleep -{ - NVIC_SystemReset(); - // Not come here (Just in case) - sleep(); -} -#endif - -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) -{ - DEBUG("Disconnected!\r\n"); - DEBUG("Restarting the advertising process\r\n"); - ble_uart.startAdvertising(); -} - -void Update_Values(void) -{ - bletx_mutex.lock(); - ble_uart.updateCharacteristicValue( - uartServicePtr->getRXCharacteristicHandle(), - tx_buf, - tx_len - ); - bletx_mutex.unlock(); - tx_len = 0; -} - -#endif
--- a/select_example.cpp Sat Apr 14 12:57:53 2018 +0000 +++ b/select_example.cpp Sun Apr 15 04:15:18 2018 +0000 @@ -5,7 +5,7 @@ * http://www.page.sannet.ne.jp/kenjia/index.html * https://os.mbed.com/users/kenjiArai/ * Created: April 7th, 2018 - * Revised: April 14th, 2018 + * Revised: April 15th, 2018 */ /* @@ -30,11 +30,12 @@ 7 & 8. 7_Uart_Client & 8_Uart_Server Uart Client(Central) and Server(Peripheral) -> PLEASE SET VCOM BAUDRATE IS 115200 + https://os.mbed.com/users/kenjiArai/ + code/TYBLE16_mbedlized_os5_several_examples_2/ 9. 9_Monitor Check nRF51 CPU functions */ -#define EXAMPLE_NUMBER 6 -// select 0 to 9 +#define EXAMPLE_NUMBER 0 // select 0 to 6 & 9 //----------------- You don't need any modification ---------------------------- #if EXAMPLE_NUMBER == 0 @@ -73,17 +74,9 @@ #include "6_Thermo/main.cpp" #endif #elif EXAMPLE_NUMBER == 7 -#define EXAMPLE_7_UART_CLIENT -#ifdef EXAMPLE_7_UART_CLIENT -#include "7_Uart_Client/main.cpp" -#warning "Please set VCOM baudrate -> 115200 "" -#endif +#error "Please use TYBLE16_mbedlized_os5_several_examples_2nd" #elif EXAMPLE_NUMBER == 8 -#define EXAMPLE_8_UART_SERVER -#ifdef EXAMPLE_8_UART_SERVER -#include "8_Uart_Server/main.cpp" -#warning "Please set VCOM baudrate -> 115200 "" -#endif +#error "Please use TYBLE16_mbedlized_os5_several_examples_2nd" #elif EXAMPLE_NUMBER == 9 #define EXAMPLE_9_MONITOR #ifdef EXAMPLE_9_MONITOR