noboru koshinaka
/
BLE_Peripheral_test
ver1.0
main.cpp@0:7a3c39a4007a, 2018-02-07 (annotated)
- Committer:
- noboruk
- Date:
- Wed Feb 07 19:53:44 2018 +0000
- Revision:
- 0:7a3c39a4007a
Ver1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noboruk | 0:7a3c39a4007a | 1 | /* mbed Microcontroller Library |
noboruk | 0:7a3c39a4007a | 2 | * Copyright (c) 2006-2013 ARM Limited |
noboruk | 0:7a3c39a4007a | 3 | * |
noboruk | 0:7a3c39a4007a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
noboruk | 0:7a3c39a4007a | 5 | * you may not use this file except in compliance with the License. |
noboruk | 0:7a3c39a4007a | 6 | * You may obtain a copy of the License at |
noboruk | 0:7a3c39a4007a | 7 | * |
noboruk | 0:7a3c39a4007a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
noboruk | 0:7a3c39a4007a | 9 | * |
noboruk | 0:7a3c39a4007a | 10 | * Unless required by applicable law or agreed to in writing, software |
noboruk | 0:7a3c39a4007a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
noboruk | 0:7a3c39a4007a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
noboruk | 0:7a3c39a4007a | 13 | * See the License for the specific language governing permissions and |
noboruk | 0:7a3c39a4007a | 14 | * limitations under the License. |
noboruk | 0:7a3c39a4007a | 15 | */ |
noboruk | 0:7a3c39a4007a | 16 | |
noboruk | 0:7a3c39a4007a | 17 | /* |
noboruk | 0:7a3c39a4007a | 18 | * ------- BLE Peripheral/Server UART function -------------------------------- |
noboruk | 0:7a3c39a4007a | 19 | * communicate with BLE_UART_Client program |
noboruk | 0:7a3c39a4007a | 20 | * --- Tested on Switch Science mbed TY51822r3 --- |
noboruk | 0:7a3c39a4007a | 21 | * |
noboruk | 0:7a3c39a4007a | 22 | * http://www.page.sannet.ne.jp/kenjia/index.html |
noboruk | 0:7a3c39a4007a | 23 | * http://mbed.org/users/kenjiArai/ |
noboruk | 0:7a3c39a4007a | 24 | * |
noboruk | 0:7a3c39a4007a | 25 | * Started: March 7th, 2016 |
noboruk | 0:7a3c39a4007a | 26 | * Revised: June 13th, 2016 |
noboruk | 0:7a3c39a4007a | 27 | * Revised: October 22nd, 2017 Run on mbed-OS-5.6.2 |
noboruk | 0:7a3c39a4007a | 28 | * |
noboruk | 0:7a3c39a4007a | 29 | * Original program: |
noboruk | 0:7a3c39a4007a | 30 | * BLE_LoopbackUART |
noboruk | 0:7a3c39a4007a | 31 | * https://developer.mbed.org/teams/Bluetooth-Low-Energy/ |
noboruk | 0:7a3c39a4007a | 32 | * code/BLE_LoopbackUART/ |
noboruk | 0:7a3c39a4007a | 33 | * Tested Controller Device: |
noboruk | 0:7a3c39a4007a | 34 | * BLE_Uart_Client |
noboruk | 0:7a3c39a4007a | 35 | * https://developer.mbed.org/users/kenjiArai/code/BLE_Uart_Client/ |
noboruk | 0:7a3c39a4007a | 36 | */ |
noboruk | 0:7a3c39a4007a | 37 | |
noboruk | 0:7a3c39a4007a | 38 | // 2018/02/07 太陽誘電 EYSGCNZWY 使用の試作機向けに改造 |
noboruk | 0:7a3c39a4007a | 39 | |
noboruk | 0:7a3c39a4007a | 40 | |
noboruk | 0:7a3c39a4007a | 41 | // Include -------------------------------------------------------------------- |
noboruk | 0:7a3c39a4007a | 42 | #include "mbed.h" |
noboruk | 0:7a3c39a4007a | 43 | #include "BLE.h" |
noboruk | 0:7a3c39a4007a | 44 | #include "UARTService.h" |
noboruk | 0:7a3c39a4007a | 45 | #include "RingBuffer.h" |
noboruk | 0:7a3c39a4007a | 46 | |
noboruk | 0:7a3c39a4007a | 47 | // Definition ----------------------------------------------------------------- |
noboruk | 0:7a3c39a4007a | 48 | #define NUM_ONCE 20 |
noboruk | 0:7a3c39a4007a | 49 | #define BFSIZE (NUM_ONCE+4) |
noboruk | 0:7a3c39a4007a | 50 | |
noboruk | 0:7a3c39a4007a | 51 | // If you set 1, you need to connected LED1 and P_0 |
noboruk | 0:7a3c39a4007a | 52 | // Please refer nRF51_WakeUP library |
noboruk | 0:7a3c39a4007a | 53 | #define GOTO_SLEEP_MODE 0 |
noboruk | 0:7a3c39a4007a | 54 | |
noboruk | 0:7a3c39a4007a | 55 | //#define USE_DEBUG_MODE |
noboruk | 0:7a3c39a4007a | 56 | #ifdef USE_DEBUG_MODE |
noboruk | 0:7a3c39a4007a | 57 | #define DEBUG(...) { printf(__VA_ARGS__); } |
noboruk | 0:7a3c39a4007a | 58 | #else |
noboruk | 0:7a3c39a4007a | 59 | #define DEBUG(...) |
noboruk | 0:7a3c39a4007a | 60 | #endif |
noboruk | 0:7a3c39a4007a | 61 | |
noboruk | 0:7a3c39a4007a | 62 | #define LED1_PIN P0_25 |
noboruk | 0:7a3c39a4007a | 63 | #define LED2_PIN P0_23 |
noboruk | 0:7a3c39a4007a | 64 | #define LED3_PIN P0_21 |
noboruk | 0:7a3c39a4007a | 65 | #define EXT_SW_PIN P0_11 |
noboruk | 0:7a3c39a4007a | 66 | #define ROM_CS_PIN P0_12 |
noboruk | 0:7a3c39a4007a | 67 | #define ROM_SCK_PIN P0_13 |
noboruk | 0:7a3c39a4007a | 68 | #define ROM_SDI_PIN P0_15 |
noboruk | 0:7a3c39a4007a | 69 | |
noboruk | 0:7a3c39a4007a | 70 | // Object --------------------------------------------------------------------- |
noboruk | 0:7a3c39a4007a | 71 | DigitalOut alivenessLED(LED1_PIN,1); |
noboruk | 0:7a3c39a4007a | 72 | DigitalOut connectedLED(LED2_PIN,1); |
noboruk | 0:7a3c39a4007a | 73 | DigitalOut out_led(LED3_PIN,1); |
noboruk | 0:7a3c39a4007a | 74 | DigitalIn external_sw(EXT_SW_PIN); |
noboruk | 0:7a3c39a4007a | 75 | // |
noboruk | 0:7a3c39a4007a | 76 | // UARTの端子ははEEPROMの端子を流用 |
noboruk | 0:7a3c39a4007a | 77 | DigitalOut dummy_cs(ROM_CS_PIN,1); //Disable Chip |
noboruk | 0:7a3c39a4007a | 78 | Serial pc(ROM_SDI_PIN,ROM_SCK_PIN, 19200); //SDI=TXD,SCK=RXD |
noboruk | 0:7a3c39a4007a | 79 | |
noboruk | 0:7a3c39a4007a | 80 | // Object |
noboruk | 0:7a3c39a4007a | 81 | BLE ble; |
noboruk | 0:7a3c39a4007a | 82 | UARTService *uartServicePtr; |
noboruk | 0:7a3c39a4007a | 83 | Ticker ticker; |
noboruk | 0:7a3c39a4007a | 84 | Ticker switch_read_ticker; |
noboruk | 0:7a3c39a4007a | 85 | RingBuffer ser_bf(1024); |
noboruk | 0:7a3c39a4007a | 86 | Thread tsk; |
noboruk | 0:7a3c39a4007a | 87 | Mutex bletx_mutex; |
noboruk | 0:7a3c39a4007a | 88 | |
noboruk | 0:7a3c39a4007a | 89 | // ROM / Constant data -------------------------------------------------------- |
noboruk | 0:7a3c39a4007a | 90 | //#warning "You need to confirm your device name." |
noboruk | 0:7a3c39a4007a | 91 | const static char DEVICE_NAME[] = "UART_S"; |
noboruk | 0:7a3c39a4007a | 92 | |
noboruk | 0:7a3c39a4007a | 93 | // RAM ------------------------------------------------------------------------ |
noboruk | 0:7a3c39a4007a | 94 | Gap::Address_t my_mac; |
noboruk | 0:7a3c39a4007a | 95 | uint8_t tx_buf[BFSIZE]; |
noboruk | 0:7a3c39a4007a | 96 | uint8_t tx_len = 0; |
noboruk | 0:7a3c39a4007a | 97 | uint8_t rx_buf[BFSIZE]; |
noboruk | 0:7a3c39a4007a | 98 | volatile bool trigger_transmit = false; |
noboruk | 0:7a3c39a4007a | 99 | volatile bool trigger_receive = false; |
noboruk | 0:7a3c39a4007a | 100 | volatile uint8_t command_continue = 0; |
noboruk | 0:7a3c39a4007a | 101 | uint16_t time_out_cntr = 3600; |
noboruk | 0:7a3c39a4007a | 102 | volatile bool time_out = false; |
noboruk | 0:7a3c39a4007a | 103 | uint32_t sleep_time = 30; // unit:second |
noboruk | 0:7a3c39a4007a | 104 | volatile bool rx_isr_busy = false; |
noboruk | 0:7a3c39a4007a | 105 | // |
noboruk | 0:7a3c39a4007a | 106 | bool sw_now; |
noboruk | 0:7a3c39a4007a | 107 | bool sw_old; |
noboruk | 0:7a3c39a4007a | 108 | uint16_t push_cnt = 0; |
noboruk | 0:7a3c39a4007a | 109 | |
noboruk | 0:7a3c39a4007a | 110 | // Function prototypes -------------------------------------------------------- |
noboruk | 0:7a3c39a4007a | 111 | // BLE |
noboruk | 0:7a3c39a4007a | 112 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *); |
noboruk | 0:7a3c39a4007a | 113 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params); |
noboruk | 0:7a3c39a4007a | 114 | void onDataWritten_action(const GattWriteCallbackParams *); |
noboruk | 0:7a3c39a4007a | 115 | // Tasks |
noboruk | 0:7a3c39a4007a | 116 | void pc_ser_rx(void); |
noboruk | 0:7a3c39a4007a | 117 | void data_from_ble(void); |
noboruk | 0:7a3c39a4007a | 118 | void Update_Values(void); |
noboruk | 0:7a3c39a4007a | 119 | // Application related |
noboruk | 0:7a3c39a4007a | 120 | void command(uint8_t *cmd); |
noboruk | 0:7a3c39a4007a | 121 | int xatoi (char **, int32_t *); |
noboruk | 0:7a3c39a4007a | 122 | void adjust_line(uint8_t *); |
noboruk | 0:7a3c39a4007a | 123 | // Interrupt related |
noboruk | 0:7a3c39a4007a | 124 | void serialRxCallback(void); |
noboruk | 0:7a3c39a4007a | 125 | void periodicCallback(void); |
noboruk | 0:7a3c39a4007a | 126 | void switch_tickerCallback(void); |
noboruk | 0:7a3c39a4007a | 127 | |
noboruk | 0:7a3c39a4007a | 128 | //------------------------------------------------------------------------------ |
noboruk | 0:7a3c39a4007a | 129 | // Control Program |
noboruk | 0:7a3c39a4007a | 130 | //------------------------------------------------------------------------------ |
noboruk | 0:7a3c39a4007a | 131 | int main(void) |
noboruk | 0:7a3c39a4007a | 132 | { |
noboruk | 0:7a3c39a4007a | 133 | connectedLED = 1; |
noboruk | 0:7a3c39a4007a | 134 | out_led = 1; |
noboruk | 0:7a3c39a4007a | 135 | dummy_cs = 1; |
noboruk | 0:7a3c39a4007a | 136 | sw_now = !external_sw; |
noboruk | 0:7a3c39a4007a | 137 | sw_old = sw_now; |
noboruk | 0:7a3c39a4007a | 138 | |
noboruk | 0:7a3c39a4007a | 139 | pc.attach(&serialRxCallback, Serial::RxIrq); |
noboruk | 0:7a3c39a4007a | 140 | ticker.attach(periodicCallback, 1); |
noboruk | 0:7a3c39a4007a | 141 | switch_read_ticker.attach(switch_tickerCallback, 0.1); |
noboruk | 0:7a3c39a4007a | 142 | tsk.start(pc_ser_rx); |
noboruk | 0:7a3c39a4007a | 143 | |
noboruk | 0:7a3c39a4007a | 144 | // clear terminal output |
noboruk | 0:7a3c39a4007a | 145 | for (int k = 0; k < 5; k++) { pc.printf("\r\n");} |
noboruk | 0:7a3c39a4007a | 146 | // opening message |
noboruk | 0:7a3c39a4007a | 147 | pc.printf("UART Communication / Peripheral side\r\n"); |
noboruk | 0:7a3c39a4007a | 148 | pc.printf(" need Central module (run BLE_Central_EYSGCNZWY program)\r\n"); |
noboruk | 0:7a3c39a4007a | 149 | |
noboruk | 0:7a3c39a4007a | 150 | ble.init(); |
noboruk | 0:7a3c39a4007a | 151 | Gap::AddressType_t my_mac_type; |
noboruk | 0:7a3c39a4007a | 152 | ble.gap().getAddress(&my_mac_type, my_mac); |
noboruk | 0:7a3c39a4007a | 153 | DEBUG( |
noboruk | 0:7a3c39a4007a | 154 | " my_MAC %02x:%02x:%02x:%02x:%02x:%02x (%s)\r\n", |
noboruk | 0:7a3c39a4007a | 155 | my_mac[5], my_mac[4], my_mac[3], my_mac[2], my_mac[1], my_mac[0], |
noboruk | 0:7a3c39a4007a | 156 | (my_mac_type == Gap::ADDR_TYPE_PUBLIC) ? "public" : "random" |
noboruk | 0:7a3c39a4007a | 157 | ); |
noboruk | 0:7a3c39a4007a | 158 | pc.printf( |
noboruk | 0:7a3c39a4007a | 159 | " my_MAC 0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x \r\n", |
noboruk | 0:7a3c39a4007a | 160 | my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5] |
noboruk | 0:7a3c39a4007a | 161 | ); |
noboruk | 0:7a3c39a4007a | 162 | |
noboruk | 0:7a3c39a4007a | 163 | ble.onDisconnection(disconnectionCallback); |
noboruk | 0:7a3c39a4007a | 164 | ble.onConnection(connectionCallback); |
noboruk | 0:7a3c39a4007a | 165 | ble.onDataWritten(onDataWritten_action); |
noboruk | 0:7a3c39a4007a | 166 | |
noboruk | 0:7a3c39a4007a | 167 | /* setup advertising */ |
noboruk | 0:7a3c39a4007a | 168 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
noboruk | 0:7a3c39a4007a | 169 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
noboruk | 0:7a3c39a4007a | 170 | ble.accumulateAdvertisingPayload( |
noboruk | 0:7a3c39a4007a | 171 | GapAdvertisingData::COMPLETE_LOCAL_NAME, |
noboruk | 0:7a3c39a4007a | 172 | (const uint8_t *)DEVICE_NAME, |
noboruk | 0:7a3c39a4007a | 173 | sizeof(DEVICE_NAME) |
noboruk | 0:7a3c39a4007a | 174 | ); |
noboruk | 0:7a3c39a4007a | 175 | ble.accumulateAdvertisingPayload( |
noboruk | 0:7a3c39a4007a | 176 | GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
noboruk | 0:7a3c39a4007a | 177 | (const uint8_t *)UARTServiceUUID_reversed, |
noboruk | 0:7a3c39a4007a | 178 | sizeof(UARTServiceUUID_reversed) |
noboruk | 0:7a3c39a4007a | 179 | ); |
noboruk | 0:7a3c39a4007a | 180 | |
noboruk | 0:7a3c39a4007a | 181 | // Advertize Interval |
noboruk | 0:7a3c39a4007a | 182 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
noboruk | 0:7a3c39a4007a | 183 | |
noboruk | 0:7a3c39a4007a | 184 | // Start |
noboruk | 0:7a3c39a4007a | 185 | ble.startAdvertising(); |
noboruk | 0:7a3c39a4007a | 186 | UARTService uartService(ble); |
noboruk | 0:7a3c39a4007a | 187 | uartServicePtr = &uartService; |
noboruk | 0:7a3c39a4007a | 188 | while(true){ |
noboruk | 0:7a3c39a4007a | 189 | if (time_out){ |
noboruk | 0:7a3c39a4007a | 190 | } |
noboruk | 0:7a3c39a4007a | 191 | if (trigger_transmit){ |
noboruk | 0:7a3c39a4007a | 192 | static uint8_t cmd_buf[BFSIZE]; |
noboruk | 0:7a3c39a4007a | 193 | static volatile bool flag_continue = 0; |
noboruk | 0:7a3c39a4007a | 194 | trigger_transmit = false; |
noboruk | 0:7a3c39a4007a | 195 | pc.printf((const char*)rx_buf); |
noboruk | 0:7a3c39a4007a | 196 | if (flag_continue == true){ |
noboruk | 0:7a3c39a4007a | 197 | strcat((char *)cmd_buf, (char *)rx_buf); |
noboruk | 0:7a3c39a4007a | 198 | if (strchr((const char*)cmd_buf,(int)'\r') == 0){ |
noboruk | 0:7a3c39a4007a | 199 | flag_continue = true; |
noboruk | 0:7a3c39a4007a | 200 | } else { |
noboruk | 0:7a3c39a4007a | 201 | command(cmd_buf); |
noboruk | 0:7a3c39a4007a | 202 | for(uint8_t i = 0; i < BFSIZE; i++){ cmd_buf[i] = 0;} |
noboruk | 0:7a3c39a4007a | 203 | flag_continue = false; |
noboruk | 0:7a3c39a4007a | 204 | } |
noboruk | 0:7a3c39a4007a | 205 | } |
noboruk | 0:7a3c39a4007a | 206 | if ((rx_buf[0] == '~')){ |
noboruk | 0:7a3c39a4007a | 207 | strcpy((char *)cmd_buf, (char *)rx_buf); |
noboruk | 0:7a3c39a4007a | 208 | if (strchr((const char*)cmd_buf,(int)'\r') == 0){ |
noboruk | 0:7a3c39a4007a | 209 | flag_continue = true; |
noboruk | 0:7a3c39a4007a | 210 | } else { |
noboruk | 0:7a3c39a4007a | 211 | command(cmd_buf); |
noboruk | 0:7a3c39a4007a | 212 | for(uint8_t i = 0; i < BFSIZE; i++){ cmd_buf[i] = 0;} |
noboruk | 0:7a3c39a4007a | 213 | flag_continue = false; |
noboruk | 0:7a3c39a4007a | 214 | } |
noboruk | 0:7a3c39a4007a | 215 | } |
noboruk | 0:7a3c39a4007a | 216 | } |
noboruk | 0:7a3c39a4007a | 217 | ble.waitForEvent(); |
noboruk | 0:7a3c39a4007a | 218 | } |
noboruk | 0:7a3c39a4007a | 219 | } |
noboruk | 0:7a3c39a4007a | 220 | |
noboruk | 0:7a3c39a4007a | 221 | void command(uint8_t *cmd) |
noboruk | 0:7a3c39a4007a | 222 | { |
noboruk | 0:7a3c39a4007a | 223 | uint8_t *p = cmd; |
noboruk | 0:7a3c39a4007a | 224 | |
noboruk | 0:7a3c39a4007a | 225 | while(*p == ' '){ ++p;} // skip space |
noboruk | 0:7a3c39a4007a | 226 | if (*p++ == '~'){ |
noboruk | 0:7a3c39a4007a | 227 | while(*p < '!'){ ++p;} // skip space |
noboruk | 0:7a3c39a4007a | 228 | uint8_t c = *p; |
noboruk | 0:7a3c39a4007a | 229 | //pc.printf("c=%c\r\n", c); |
noboruk | 0:7a3c39a4007a | 230 | switch (c){ |
noboruk | 0:7a3c39a4007a | 231 | default: |
noboruk | 0:7a3c39a4007a | 232 | //pc.printf("\r\nStep(%u)\r\n", __LINE__); |
noboruk | 0:7a3c39a4007a | 233 | break; |
noboruk | 0:7a3c39a4007a | 234 | } |
noboruk | 0:7a3c39a4007a | 235 | } |
noboruk | 0:7a3c39a4007a | 236 | } |
noboruk | 0:7a3c39a4007a | 237 | |
noboruk | 0:7a3c39a4007a | 238 | void periodicCallback(void) |
noboruk | 0:7a3c39a4007a | 239 | { |
noboruk | 0:7a3c39a4007a | 240 | if (rx_isr_busy == true){ |
noboruk | 0:7a3c39a4007a | 241 | rx_isr_busy = false; |
noboruk | 0:7a3c39a4007a | 242 | } else { |
noboruk | 0:7a3c39a4007a | 243 | tsk.signal_set(0x01); |
noboruk | 0:7a3c39a4007a | 244 | } |
noboruk | 0:7a3c39a4007a | 245 | alivenessLED = !alivenessLED; |
noboruk | 0:7a3c39a4007a | 246 | } |
noboruk | 0:7a3c39a4007a | 247 | |
noboruk | 0:7a3c39a4007a | 248 | void switch_tickerCallback(void) |
noboruk | 0:7a3c39a4007a | 249 | { |
noboruk | 0:7a3c39a4007a | 250 | sw_old = sw_now; |
noboruk | 0:7a3c39a4007a | 251 | sw_now = !external_sw; |
noboruk | 0:7a3c39a4007a | 252 | if( sw_now ) |
noboruk | 0:7a3c39a4007a | 253 | { |
noboruk | 0:7a3c39a4007a | 254 | out_led = 0; |
noboruk | 0:7a3c39a4007a | 255 | } |
noboruk | 0:7a3c39a4007a | 256 | else |
noboruk | 0:7a3c39a4007a | 257 | { |
noboruk | 0:7a3c39a4007a | 258 | out_led = 1; |
noboruk | 0:7a3c39a4007a | 259 | } |
noboruk | 0:7a3c39a4007a | 260 | if( ( sw_now != sw_old ) && ( sw_now ) ) |
noboruk | 0:7a3c39a4007a | 261 | { |
noboruk | 0:7a3c39a4007a | 262 | // push triger |
noboruk | 0:7a3c39a4007a | 263 | push_cnt++; |
noboruk | 0:7a3c39a4007a | 264 | sprintf((char *)tx_buf,"%04X\r\n",push_cnt); |
noboruk | 0:7a3c39a4007a | 265 | tx_len = strlen((const char *)tx_buf); |
noboruk | 0:7a3c39a4007a | 266 | Update_Values(); |
noboruk | 0:7a3c39a4007a | 267 | } |
noboruk | 0:7a3c39a4007a | 268 | } |
noboruk | 0:7a3c39a4007a | 269 | |
noboruk | 0:7a3c39a4007a | 270 | void serialRxCallback() |
noboruk | 0:7a3c39a4007a | 271 | { |
noboruk | 0:7a3c39a4007a | 272 | ser_bf.save(pc.getc()); |
noboruk | 0:7a3c39a4007a | 273 | rx_isr_busy = true; |
noboruk | 0:7a3c39a4007a | 274 | tsk.signal_set(0x01); |
noboruk | 0:7a3c39a4007a | 275 | } |
noboruk | 0:7a3c39a4007a | 276 | |
noboruk | 0:7a3c39a4007a | 277 | void pc_ser_rx() |
noboruk | 0:7a3c39a4007a | 278 | { |
noboruk | 0:7a3c39a4007a | 279 | static uint8_t linebf_irq[BFSIZE]; |
noboruk | 0:7a3c39a4007a | 280 | static volatile uint8_t linebf_irq_len = 0; |
noboruk | 0:7a3c39a4007a | 281 | |
noboruk | 0:7a3c39a4007a | 282 | while(true){ |
noboruk | 0:7a3c39a4007a | 283 | Thread::signal_wait(0x01); |
noboruk | 0:7a3c39a4007a | 284 | if (ser_bf.check() == 0){ |
noboruk | 0:7a3c39a4007a | 285 | if (linebf_irq_len != 0){ |
noboruk | 0:7a3c39a4007a | 286 | linebf_irq[linebf_irq_len] = 0; |
noboruk | 0:7a3c39a4007a | 287 | adjust_line(linebf_irq); |
noboruk | 0:7a3c39a4007a | 288 | linebf_irq_len = 0; |
noboruk | 0:7a3c39a4007a | 289 | bletx_mutex.lock(); |
noboruk | 0:7a3c39a4007a | 290 | ble.updateCharacteristicValue( |
noboruk | 0:7a3c39a4007a | 291 | uartServicePtr->getRXCharacteristicHandle(), |
noboruk | 0:7a3c39a4007a | 292 | linebf_irq, |
noboruk | 0:7a3c39a4007a | 293 | NUM_ONCE |
noboruk | 0:7a3c39a4007a | 294 | ); |
noboruk | 0:7a3c39a4007a | 295 | bletx_mutex.unlock(); |
noboruk | 0:7a3c39a4007a | 296 | } |
noboruk | 0:7a3c39a4007a | 297 | } |
noboruk | 0:7a3c39a4007a | 298 | while(ser_bf.check() != 0){ |
noboruk | 0:7a3c39a4007a | 299 | char c = ser_bf.read(); |
noboruk | 0:7a3c39a4007a | 300 | if (c == '\b'){ |
noboruk | 0:7a3c39a4007a | 301 | linebf_irq_len--; |
noboruk | 0:7a3c39a4007a | 302 | pc.putc(c); |
noboruk | 0:7a3c39a4007a | 303 | pc.putc(' '); |
noboruk | 0:7a3c39a4007a | 304 | pc.putc(c); |
noboruk | 0:7a3c39a4007a | 305 | } else if ((c >= ' ') || (c == '\r') || (c == '\n')){ |
noboruk | 0:7a3c39a4007a | 306 | bool overflow = false; |
noboruk | 0:7a3c39a4007a | 307 | if ((c == '\r') || (c == '\n')) { |
noboruk | 0:7a3c39a4007a | 308 | if (linebf_irq_len == NUM_ONCE - 1){// remain only 1 buffer |
noboruk | 0:7a3c39a4007a | 309 | overflow = true; |
noboruk | 0:7a3c39a4007a | 310 | linebf_irq[linebf_irq_len++] = '\r'; |
noboruk | 0:7a3c39a4007a | 311 | pc.putc('\r'); |
noboruk | 0:7a3c39a4007a | 312 | } else { |
noboruk | 0:7a3c39a4007a | 313 | overflow = false; |
noboruk | 0:7a3c39a4007a | 314 | linebf_irq[linebf_irq_len++] = '\r'; |
noboruk | 0:7a3c39a4007a | 315 | linebf_irq[linebf_irq_len++] = '\n'; |
noboruk | 0:7a3c39a4007a | 316 | pc.printf("\r\n"); |
noboruk | 0:7a3c39a4007a | 317 | } |
noboruk | 0:7a3c39a4007a | 318 | } else { |
noboruk | 0:7a3c39a4007a | 319 | linebf_irq[linebf_irq_len++] = c; |
noboruk | 0:7a3c39a4007a | 320 | pc.putc(c); |
noboruk | 0:7a3c39a4007a | 321 | } |
noboruk | 0:7a3c39a4007a | 322 | if (linebf_irq_len >= NUM_ONCE ){ |
noboruk | 0:7a3c39a4007a | 323 | linebf_irq[linebf_irq_len] = 0; |
noboruk | 0:7a3c39a4007a | 324 | adjust_line(linebf_irq); |
noboruk | 0:7a3c39a4007a | 325 | linebf_irq_len = 0; |
noboruk | 0:7a3c39a4007a | 326 | bletx_mutex.lock(); |
noboruk | 0:7a3c39a4007a | 327 | ble.updateCharacteristicValue( |
noboruk | 0:7a3c39a4007a | 328 | uartServicePtr->getRXCharacteristicHandle(), |
noboruk | 0:7a3c39a4007a | 329 | linebf_irq, |
noboruk | 0:7a3c39a4007a | 330 | NUM_ONCE |
noboruk | 0:7a3c39a4007a | 331 | ); |
noboruk | 0:7a3c39a4007a | 332 | bletx_mutex.unlock(); |
noboruk | 0:7a3c39a4007a | 333 | if (overflow == true){ |
noboruk | 0:7a3c39a4007a | 334 | overflow = false; |
noboruk | 0:7a3c39a4007a | 335 | linebf_irq[linebf_irq_len++] = '\n'; |
noboruk | 0:7a3c39a4007a | 336 | pc.putc('\n'); |
noboruk | 0:7a3c39a4007a | 337 | } |
noboruk | 0:7a3c39a4007a | 338 | } |
noboruk | 0:7a3c39a4007a | 339 | } |
noboruk | 0:7a3c39a4007a | 340 | } |
noboruk | 0:7a3c39a4007a | 341 | } |
noboruk | 0:7a3c39a4007a | 342 | } |
noboruk | 0:7a3c39a4007a | 343 | |
noboruk | 0:7a3c39a4007a | 344 | void adjust_line(uint8_t *bf) |
noboruk | 0:7a3c39a4007a | 345 | { |
noboruk | 0:7a3c39a4007a | 346 | uint8_t i, c; |
noboruk | 0:7a3c39a4007a | 347 | |
noboruk | 0:7a3c39a4007a | 348 | for (i = 0; i <NUM_ONCE; bf++, i++){ |
noboruk | 0:7a3c39a4007a | 349 | c = *bf; |
noboruk | 0:7a3c39a4007a | 350 | if (c == 0){ break;} |
noboruk | 0:7a3c39a4007a | 351 | } |
noboruk | 0:7a3c39a4007a | 352 | for (; i < NUM_ONCE; bf++, i++){ *bf = 0x11;} |
noboruk | 0:7a3c39a4007a | 353 | *(bf + 1) = 0; |
noboruk | 0:7a3c39a4007a | 354 | } |
noboruk | 0:7a3c39a4007a | 355 | |
noboruk | 0:7a3c39a4007a | 356 | void onDataWritten_action(const GattWriteCallbackParams *params) |
noboruk | 0:7a3c39a4007a | 357 | { |
noboruk | 0:7a3c39a4007a | 358 | if ((uartServicePtr != NULL) && |
noboruk | 0:7a3c39a4007a | 359 | (params->handle == uartServicePtr->getTXCharacteristicHandle())) |
noboruk | 0:7a3c39a4007a | 360 | { |
noboruk | 0:7a3c39a4007a | 361 | strcpy((char *)rx_buf, (const char *)params->data); |
noboruk | 0:7a3c39a4007a | 362 | trigger_transmit = true; |
noboruk | 0:7a3c39a4007a | 363 | } |
noboruk | 0:7a3c39a4007a | 364 | } |
noboruk | 0:7a3c39a4007a | 365 | |
noboruk | 0:7a3c39a4007a | 366 | |
noboruk | 0:7a3c39a4007a | 367 | void action_tx_wait_time(uint8_t *cmd) |
noboruk | 0:7a3c39a4007a | 368 | { |
noboruk | 0:7a3c39a4007a | 369 | int32_t dt; |
noboruk | 0:7a3c39a4007a | 370 | char *p; |
noboruk | 0:7a3c39a4007a | 371 | |
noboruk | 0:7a3c39a4007a | 372 | p = (char *)(cmd); |
noboruk | 0:7a3c39a4007a | 373 | p += 2; // point to time value |
noboruk | 0:7a3c39a4007a | 374 | if (xatoi(&p, &dt)){ |
noboruk | 0:7a3c39a4007a | 375 | if (dt <= 5){ dt = 5;} |
noboruk | 0:7a3c39a4007a | 376 | sleep_time = dt; // set next wake-up period |
noboruk | 0:7a3c39a4007a | 377 | } else { |
noboruk | 0:7a3c39a4007a | 378 | DEBUG("data is unknown!\r\n"); |
noboruk | 0:7a3c39a4007a | 379 | sleep_time = 30; |
noboruk | 0:7a3c39a4007a | 380 | } |
noboruk | 0:7a3c39a4007a | 381 | DEBUG("slp_t:%d\r\n", sleep_time); |
noboruk | 0:7a3c39a4007a | 382 | //pc.printf("slp_t:%d\r\n", sleep_time); |
noboruk | 0:7a3c39a4007a | 383 | // 12345678901234567890 |
noboruk | 0:7a3c39a4007a | 384 | sprintf((char *)tx_buf, "W: %d sec\r\n", sleep_time); |
noboruk | 0:7a3c39a4007a | 385 | tx_len = strlen((const char *)tx_buf); |
noboruk | 0:7a3c39a4007a | 386 | Update_Values(); |
noboruk | 0:7a3c39a4007a | 387 | } |
noboruk | 0:7a3c39a4007a | 388 | |
noboruk | 0:7a3c39a4007a | 389 | // Change string -> integer |
noboruk | 0:7a3c39a4007a | 390 | int xatoi (char **str, int32_t *res) |
noboruk | 0:7a3c39a4007a | 391 | { |
noboruk | 0:7a3c39a4007a | 392 | unsigned long val; |
noboruk | 0:7a3c39a4007a | 393 | unsigned char c, radix, s = 0; |
noboruk | 0:7a3c39a4007a | 394 | |
noboruk | 0:7a3c39a4007a | 395 | for (;;){ |
noboruk | 0:7a3c39a4007a | 396 | c = **str; |
noboruk | 0:7a3c39a4007a | 397 | if (c == 0){ return 0;} |
noboruk | 0:7a3c39a4007a | 398 | if (c == '-'){ break;} |
noboruk | 0:7a3c39a4007a | 399 | if (c == '+'){ |
noboruk | 0:7a3c39a4007a | 400 | (*str)++; |
noboruk | 0:7a3c39a4007a | 401 | c = **str; |
noboruk | 0:7a3c39a4007a | 402 | } |
noboruk | 0:7a3c39a4007a | 403 | if (c>='0'&& c<='9'){ |
noboruk | 0:7a3c39a4007a | 404 | break; |
noboruk | 0:7a3c39a4007a | 405 | } else { |
noboruk | 0:7a3c39a4007a | 406 | (*str)++; |
noboruk | 0:7a3c39a4007a | 407 | c = **str; |
noboruk | 0:7a3c39a4007a | 408 | } |
noboruk | 0:7a3c39a4007a | 409 | } |
noboruk | 0:7a3c39a4007a | 410 | if (c == '-') { |
noboruk | 0:7a3c39a4007a | 411 | s = 1; |
noboruk | 0:7a3c39a4007a | 412 | c = *(++(*str)); |
noboruk | 0:7a3c39a4007a | 413 | } |
noboruk | 0:7a3c39a4007a | 414 | if (c == '0') { |
noboruk | 0:7a3c39a4007a | 415 | c = *(++(*str)); |
noboruk | 0:7a3c39a4007a | 416 | if (c <= ' ') { |
noboruk | 0:7a3c39a4007a | 417 | *res = 0; |
noboruk | 0:7a3c39a4007a | 418 | return 1; |
noboruk | 0:7a3c39a4007a | 419 | } |
noboruk | 0:7a3c39a4007a | 420 | if (c == 'x') { |
noboruk | 0:7a3c39a4007a | 421 | radix = 16; |
noboruk | 0:7a3c39a4007a | 422 | c = *(++(*str)); |
noboruk | 0:7a3c39a4007a | 423 | } else { |
noboruk | 0:7a3c39a4007a | 424 | if (c == 'b') { |
noboruk | 0:7a3c39a4007a | 425 | radix = 2; |
noboruk | 0:7a3c39a4007a | 426 | c = *(++(*str)); |
noboruk | 0:7a3c39a4007a | 427 | } else { |
noboruk | 0:7a3c39a4007a | 428 | if ((c >= '0')&&(c <= '9')) { |
noboruk | 0:7a3c39a4007a | 429 | radix = 8; |
noboruk | 0:7a3c39a4007a | 430 | } else { |
noboruk | 0:7a3c39a4007a | 431 | return 0; |
noboruk | 0:7a3c39a4007a | 432 | } |
noboruk | 0:7a3c39a4007a | 433 | } |
noboruk | 0:7a3c39a4007a | 434 | } |
noboruk | 0:7a3c39a4007a | 435 | } else { |
noboruk | 0:7a3c39a4007a | 436 | if ((c < '1')||(c > '9')) { |
noboruk | 0:7a3c39a4007a | 437 | return 0; |
noboruk | 0:7a3c39a4007a | 438 | } |
noboruk | 0:7a3c39a4007a | 439 | radix = 10; |
noboruk | 0:7a3c39a4007a | 440 | } |
noboruk | 0:7a3c39a4007a | 441 | val = 0; |
noboruk | 0:7a3c39a4007a | 442 | while (c > ' ') { |
noboruk | 0:7a3c39a4007a | 443 | if (c >= 'a') c -= 0x20; |
noboruk | 0:7a3c39a4007a | 444 | c -= '0'; |
noboruk | 0:7a3c39a4007a | 445 | if (c >= 17) { |
noboruk | 0:7a3c39a4007a | 446 | c -= 7; |
noboruk | 0:7a3c39a4007a | 447 | if (c <= 9) return 0; |
noboruk | 0:7a3c39a4007a | 448 | } |
noboruk | 0:7a3c39a4007a | 449 | if (c >= radix) return 0; |
noboruk | 0:7a3c39a4007a | 450 | val = val * radix + c; |
noboruk | 0:7a3c39a4007a | 451 | c = *(++(*str)); |
noboruk | 0:7a3c39a4007a | 452 | } |
noboruk | 0:7a3c39a4007a | 453 | if (s) val = -val; |
noboruk | 0:7a3c39a4007a | 454 | *res = val; |
noboruk | 0:7a3c39a4007a | 455 | return 1; |
noboruk | 0:7a3c39a4007a | 456 | } |
noboruk | 0:7a3c39a4007a | 457 | |
noboruk | 0:7a3c39a4007a | 458 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
noboruk | 0:7a3c39a4007a | 459 | { |
noboruk | 0:7a3c39a4007a | 460 | DEBUG("Disconnected!\r\n"); |
noboruk | 0:7a3c39a4007a | 461 | DEBUG("Restarting the advertising process\r\n"); |
noboruk | 0:7a3c39a4007a | 462 | ble.startAdvertising(); |
noboruk | 0:7a3c39a4007a | 463 | connectedLED = 1; |
noboruk | 0:7a3c39a4007a | 464 | } |
noboruk | 0:7a3c39a4007a | 465 | |
noboruk | 0:7a3c39a4007a | 466 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
noboruk | 0:7a3c39a4007a | 467 | { |
noboruk | 0:7a3c39a4007a | 468 | DEBUG("Connected!\r\n"); |
noboruk | 0:7a3c39a4007a | 469 | connectedLED = 0; |
noboruk | 0:7a3c39a4007a | 470 | } |
noboruk | 0:7a3c39a4007a | 471 | |
noboruk | 0:7a3c39a4007a | 472 | void Update_Values(void) |
noboruk | 0:7a3c39a4007a | 473 | { |
noboruk | 0:7a3c39a4007a | 474 | bletx_mutex.lock(); |
noboruk | 0:7a3c39a4007a | 475 | ble.updateCharacteristicValue( |
noboruk | 0:7a3c39a4007a | 476 | uartServicePtr->getRXCharacteristicHandle(), |
noboruk | 0:7a3c39a4007a | 477 | tx_buf, |
noboruk | 0:7a3c39a4007a | 478 | tx_len |
noboruk | 0:7a3c39a4007a | 479 | ); |
noboruk | 0:7a3c39a4007a | 480 | bletx_mutex.unlock(); |
noboruk | 0:7a3c39a4007a | 481 | tx_len = 0; |
noboruk | 0:7a3c39a4007a | 482 | } |