![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
test
Dependencies: BufferedSerial WatchdogTimer
main.cpp@2:a88b0b4c4f37, 2018-06-05 (annotated)
- Committer:
- harada_jpms
- Date:
- Tue Jun 05 03:00:08 2018 +0000
- Revision:
- 2:a88b0b4c4f37
- Parent:
- 1:d2ebf1c3ae5a
- Child:
- 3:455575531b33
20180605
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harada_jpms | 0:ad0d1afb4462 | 1 | // Include -------------------------------------------------------------------- |
harada_jpms | 0:ad0d1afb4462 | 2 | #include "mbed.h" |
harada_jpms | 0:ad0d1afb4462 | 3 | #include "BLE.h" |
harada_jpms | 0:ad0d1afb4462 | 4 | #include "DiscoveredCharacteristic.h" |
harada_jpms | 0:ad0d1afb4462 | 5 | #include "DiscoveredService.h" |
harada_jpms | 0:ad0d1afb4462 | 6 | #include "UARTService.h" |
harada_jpms | 0:ad0d1afb4462 | 7 | |
harada_jpms | 0:ad0d1afb4462 | 8 | // Definition ----------------------------------------------------------------- |
harada_jpms | 0:ad0d1afb4462 | 9 | #define NUM_ONCE 20 |
harada_jpms | 0:ad0d1afb4462 | 10 | #define BFSIZE (NUM_ONCE+4) |
harada_jpms | 0:ad0d1afb4462 | 11 | |
harada_jpms | 0:ad0d1afb4462 | 12 | #define SOFT_DEVICE_FATHER_HANDLE 3 |
harada_jpms | 0:ad0d1afb4462 | 13 | |
harada_jpms | 0:ad0d1afb4462 | 14 | // Object --------------------------------------------------------------------- |
harada_jpms | 0:ad0d1afb4462 | 15 | BLE& ble_uart = BLE::Instance(); |
harada_jpms | 0:ad0d1afb4462 | 16 | Serial pc(P0_1, P0_3, 115200); // for another board |
harada_jpms | 0:ad0d1afb4462 | 17 | Ticker main_timer; // メインタイマー |
harada_jpms | 0:ad0d1afb4462 | 18 | |
harada_jpms | 0:ad0d1afb4462 | 19 | // ROM / Constant data -------------------------------------------------------- |
harada_jpms | 0:ad0d1afb4462 | 20 | const Gap::Address_t mac_board_0 = {0xFF, 0x42, 0xD5, 0x9C, 0x9C, 0xE1}; |
harada_jpms | 0:ad0d1afb4462 | 21 | |
harada_jpms | 0:ad0d1afb4462 | 22 | // RAM ------------------------------------------------------------------------ |
harada_jpms | 0:ad0d1afb4462 | 23 | Gap::Handle_t connectionHandle = 0xFFFF; |
harada_jpms | 0:ad0d1afb4462 | 24 | DiscoveredCharacteristic uartTXCharacteristic; |
harada_jpms | 0:ad0d1afb4462 | 25 | DiscoveredCharacteristic uartRXCharacteristic; |
harada_jpms | 0:ad0d1afb4462 | 26 | bool foundUartRXCharacteristic = false; |
harada_jpms | 0:ad0d1afb4462 | 27 | bool connected2server = false; |
harada_jpms | 0:ad0d1afb4462 | 28 | bool connection_tx = false; |
harada_jpms | 0:ad0d1afb4462 | 29 | bool connection_rx = false; |
harada_jpms | 0:ad0d1afb4462 | 30 | UARTService * uartServicePtr = NULL; |
harada_jpms | 0:ad0d1afb4462 | 31 | Gap::Address_t my_mac; |
harada_jpms | 0:ad0d1afb4462 | 32 | int my_board_index = -1; |
harada_jpms | 0:ad0d1afb4462 | 33 | bool received_uart_dat = false; |
harada_jpms | 0:ad0d1afb4462 | 34 | int8_t uart_buffer[BFSIZE]; |
harada_jpms | 0:ad0d1afb4462 | 35 | uint8_t uart_bf_len; |
harada_jpms | 0:ad0d1afb4462 | 36 | volatile bool rx_isr_busy = false; |
harada_jpms | 0:ad0d1afb4462 | 37 | |
harada_jpms | 1:d2ebf1c3ae5a | 38 | unsigned long mac_1; |
harada_jpms | 1:d2ebf1c3ae5a | 39 | unsigned long mac_2; |
harada_jpms | 1:d2ebf1c3ae5a | 40 | unsigned long mac_3; |
harada_jpms | 1:d2ebf1c3ae5a | 41 | |
harada_jpms | 1:d2ebf1c3ae5a | 42 | #define SIO_BUF_SIZE 256 /* SIO リングバッファーサイズ (1K byte) */ |
harada_jpms | 1:d2ebf1c3ae5a | 43 | |
harada_jpms | 1:d2ebf1c3ae5a | 44 | typedef struct { |
harada_jpms | 1:d2ebf1c3ae5a | 45 | unsigned short data_cnt; /* リング・バッファ データカウント */ |
harada_jpms | 1:d2ebf1c3ae5a | 46 | unsigned short rp; /* リング・バッファ Read ポインタ */ |
harada_jpms | 1:d2ebf1c3ae5a | 47 | unsigned short wp; /* リング・バッファ Write ポインタ */ |
harada_jpms | 1:d2ebf1c3ae5a | 48 | unsigned char buf[SIO_BUF_SIZE]; /* リング・バッファ */ |
harada_jpms | 1:d2ebf1c3ae5a | 49 | } ring_cnt_t; /* リング・バッファ 制御用構造体 */ |
harada_jpms | 1:d2ebf1c3ae5a | 50 | |
harada_jpms | 1:d2ebf1c3ae5a | 51 | ring_cnt_t pc_uart_buf; // 通信用リングバッファ |
harada_jpms | 1:d2ebf1c3ae5a | 52 | ring_cnt_t pc_cmd_buf; // 通信用コマンドバッファ |
harada_jpms | 1:d2ebf1c3ae5a | 53 | |
harada_jpms | 1:d2ebf1c3ae5a | 54 | ring_cnt_t ble_uart_buf; // 通信用リングバッファ |
harada_jpms | 1:d2ebf1c3ae5a | 55 | ring_cnt_t ble_cmd_buf; // 通信用コマンドバッファ |
harada_jpms | 1:d2ebf1c3ae5a | 56 | |
harada_jpms | 1:d2ebf1c3ae5a | 57 | int timer_cnt; |
harada_jpms | 1:d2ebf1c3ae5a | 58 | int timer_cnt_d; |
harada_jpms | 1:d2ebf1c3ae5a | 59 | |
harada_jpms | 0:ad0d1afb4462 | 60 | // Function prototypes -------------------------------------------------------- |
harada_jpms | 0:ad0d1afb4462 | 61 | // BLE |
harada_jpms | 0:ad0d1afb4462 | 62 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *); |
harada_jpms | 0:ad0d1afb4462 | 63 | void serviceDiscoveryCallback(const DiscoveredService *); |
harada_jpms | 0:ad0d1afb4462 | 64 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *); |
harada_jpms | 0:ad0d1afb4462 | 65 | void discoveryTerminationCallback(Gap::Handle_t ); |
harada_jpms | 0:ad0d1afb4462 | 66 | void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *); |
harada_jpms | 0:ad0d1afb4462 | 67 | void connectionCallback(const Gap::ConnectionCallbackParams_t *); |
harada_jpms | 0:ad0d1afb4462 | 68 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *); |
harada_jpms | 0:ad0d1afb4462 | 69 | // Pre-check |
harada_jpms | 0:ad0d1afb4462 | 70 | bool mac_equals(const Gap::Address_t, const Gap::Address_t); |
harada_jpms | 0:ad0d1afb4462 | 71 | int get_board_index(const Gap::Address_t); |
harada_jpms | 0:ad0d1afb4462 | 72 | |
harada_jpms | 1:d2ebf1c3ae5a | 73 | void uart_init(); |
harada_jpms | 1:d2ebf1c3ae5a | 74 | void main_timer_proc(); |
harada_jpms | 1:d2ebf1c3ae5a | 75 | unsigned int atow(char *str,unsigned short leng); |
harada_jpms | 1:d2ebf1c3ae5a | 76 | unsigned long atoh(char *str,unsigned short leng); |
harada_jpms | 1:d2ebf1c3ae5a | 77 | void ble_rs232c_cmd(); |
harada_jpms | 1:d2ebf1c3ae5a | 78 | void pc_rs232c_cmd(); |
harada_jpms | 1:d2ebf1c3ae5a | 79 | void ble_write(char* dat); |
harada_jpms | 1:d2ebf1c3ae5a | 80 | |
harada_jpms | 0:ad0d1afb4462 | 81 | //------------------------------------------------------------------------------ |
harada_jpms | 0:ad0d1afb4462 | 82 | // Control Program |
harada_jpms | 0:ad0d1afb4462 | 83 | //------------------------------------------------------------------------------ |
harada_jpms | 0:ad0d1afb4462 | 84 | int main(void) |
harada_jpms | 0:ad0d1afb4462 | 85 | { |
harada_jpms | 1:d2ebf1c3ae5a | 86 | uart_init(); |
harada_jpms | 1:d2ebf1c3ae5a | 87 | |
harada_jpms | 1:d2ebf1c3ae5a | 88 | // メインタイマー設定(1ms) |
harada_jpms | 1:d2ebf1c3ae5a | 89 | main_timer.attach(&main_timer_proc, 0.001); |
harada_jpms | 1:d2ebf1c3ae5a | 90 | |
harada_jpms | 0:ad0d1afb4462 | 91 | // opening message |
harada_jpms | 0:ad0d1afb4462 | 92 | pc.printf("UART Communication / Client(Central) side\r\n"); |
harada_jpms | 0:ad0d1afb4462 | 93 | pc.printf(" need Server module (run BLE_Uart_Server program)\r\n"); |
harada_jpms | 0:ad0d1afb4462 | 94 | // Mixed role ************************************************************** |
harada_jpms | 0:ad0d1afb4462 | 95 | ble_uart.init(); |
harada_jpms | 0:ad0d1afb4462 | 96 | ble_uart.gap().onConnection(connectionCallback); |
harada_jpms | 0:ad0d1afb4462 | 97 | ble_uart.gap().onDisconnection(disconnectionCallback); |
harada_jpms | 0:ad0d1afb4462 | 98 | // Client(Central) role **************************************************** |
harada_jpms | 0:ad0d1afb4462 | 99 | ble_uart.gattClient().onHVX(onReceivedDataFromDeviceCallback); |
harada_jpms | 0:ad0d1afb4462 | 100 | ble_uart.gap().setScanParams(500, 450); |
harada_jpms | 0:ad0d1afb4462 | 101 | ble_uart.gap().startScan(advertisementCallback); |
harada_jpms | 1:d2ebf1c3ae5a | 102 | |
harada_jpms | 1:d2ebf1c3ae5a | 103 | |
harada_jpms | 0:ad0d1afb4462 | 104 | while(true) { |
harada_jpms | 0:ad0d1afb4462 | 105 | // allow notifications from Server(Peripheral) |
harada_jpms | 0:ad0d1afb4462 | 106 | if (foundUartRXCharacteristic && |
harada_jpms | 0:ad0d1afb4462 | 107 | !ble_uart.gattClient().isServiceDiscoveryActive()) { |
harada_jpms | 0:ad0d1afb4462 | 108 | // need to do the following only once |
harada_jpms | 0:ad0d1afb4462 | 109 | foundUartRXCharacteristic = false; |
harada_jpms | 0:ad0d1afb4462 | 110 | uint16_t value = BLE_HVX_NOTIFICATION; |
harada_jpms | 0:ad0d1afb4462 | 111 | ble_uart.gattClient().write( |
harada_jpms | 0:ad0d1afb4462 | 112 | GattClient::GATT_OP_WRITE_REQ, |
harada_jpms | 0:ad0d1afb4462 | 113 | connectionHandle, |
harada_jpms | 0:ad0d1afb4462 | 114 | uartRXCharacteristic.getValueHandle() + 1, |
harada_jpms | 0:ad0d1afb4462 | 115 | sizeof(uint16_t), |
harada_jpms | 0:ad0d1afb4462 | 116 | reinterpret_cast<const uint8_t *>(&value) |
harada_jpms | 0:ad0d1afb4462 | 117 | ); |
harada_jpms | 0:ad0d1afb4462 | 118 | } |
harada_jpms | 1:d2ebf1c3ae5a | 119 | |
harada_jpms | 1:d2ebf1c3ae5a | 120 | // タイマーカウント更新時(1ms以上経過している) |
harada_jpms | 1:d2ebf1c3ae5a | 121 | if( timer_cnt != timer_cnt_d ) |
harada_jpms | 1:d2ebf1c3ae5a | 122 | { |
harada_jpms | 1:d2ebf1c3ae5a | 123 | timer_cnt_d = timer_cnt; |
harada_jpms | 1:d2ebf1c3ae5a | 124 | |
harada_jpms | 1:d2ebf1c3ae5a | 125 | ble_rs232c_cmd(); |
harada_jpms | 1:d2ebf1c3ae5a | 126 | pc_rs232c_cmd(); |
harada_jpms | 0:ad0d1afb4462 | 127 | } |
harada_jpms | 0:ad0d1afb4462 | 128 | |
harada_jpms | 0:ad0d1afb4462 | 129 | ble_uart.waitForEvent(); |
harada_jpms | 0:ad0d1afb4462 | 130 | } |
harada_jpms | 0:ad0d1afb4462 | 131 | } |
harada_jpms | 0:ad0d1afb4462 | 132 | |
harada_jpms | 0:ad0d1afb4462 | 133 | void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *params) |
harada_jpms | 0:ad0d1afb4462 | 134 | { |
harada_jpms | 0:ad0d1afb4462 | 135 | if (params->type == BLE_HVX_NOTIFICATION) { |
harada_jpms | 0:ad0d1afb4462 | 136 | if ((params->handle |
harada_jpms | 0:ad0d1afb4462 | 137 | == uartRXCharacteristic.getValueHandle()) && (params->len > 0)) { |
harada_jpms | 0:ad0d1afb4462 | 138 | uart_bf_len = params->len; |
harada_jpms | 0:ad0d1afb4462 | 139 | strcpy((char *)uart_buffer, (char *)params->data); |
harada_jpms | 0:ad0d1afb4462 | 140 | received_uart_dat = true; |
harada_jpms | 2:a88b0b4c4f37 | 141 | |
harada_jpms | 2:a88b0b4c4f37 | 142 | for( int i=0; i<uart_bf_len; i++ ) |
harada_jpms | 2:a88b0b4c4f37 | 143 | { |
harada_jpms | 2:a88b0b4c4f37 | 144 | //pc.putc(uart_buffer[i]); |
harada_jpms | 2:a88b0b4c4f37 | 145 | ble_uart_buf.buf[ ble_uart_buf.wp ] = uart_buffer[i]; |
harada_jpms | 2:a88b0b4c4f37 | 146 | ble_uart_buf.data_cnt++; |
harada_jpms | 2:a88b0b4c4f37 | 147 | |
harada_jpms | 2:a88b0b4c4f37 | 148 | if( ble_uart_buf.wp == SIO_BUF_SIZE-1 ) |
harada_jpms | 2:a88b0b4c4f37 | 149 | { |
harada_jpms | 2:a88b0b4c4f37 | 150 | ble_uart_buf.wp = 0; |
harada_jpms | 2:a88b0b4c4f37 | 151 | } |
harada_jpms | 2:a88b0b4c4f37 | 152 | else |
harada_jpms | 2:a88b0b4c4f37 | 153 | { |
harada_jpms | 2:a88b0b4c4f37 | 154 | ble_uart_buf.wp++; |
harada_jpms | 2:a88b0b4c4f37 | 155 | } |
harada_jpms | 2:a88b0b4c4f37 | 156 | } |
harada_jpms | 2:a88b0b4c4f37 | 157 | |
harada_jpms | 2:a88b0b4c4f37 | 158 | uart_bf_len = 0; |
harada_jpms | 0:ad0d1afb4462 | 159 | } |
harada_jpms | 0:ad0d1afb4462 | 160 | } |
harada_jpms | 0:ad0d1afb4462 | 161 | } |
harada_jpms | 0:ad0d1afb4462 | 162 | |
harada_jpms | 0:ad0d1afb4462 | 163 | bool mac_equals(const Gap::Address_t mac_1, const Gap::Address_t mac_2) |
harada_jpms | 0:ad0d1afb4462 | 164 | { |
harada_jpms | 0:ad0d1afb4462 | 165 | for (int i = 0; i < 6; i++) { |
harada_jpms | 0:ad0d1afb4462 | 166 | |
harada_jpms | 0:ad0d1afb4462 | 167 | } |
harada_jpms | 0:ad0d1afb4462 | 168 | |
harada_jpms | 0:ad0d1afb4462 | 169 | for (int i = 0; i < 6; i++) { |
harada_jpms | 0:ad0d1afb4462 | 170 | if (mac_1[i] != mac_2[i]) { |
harada_jpms | 0:ad0d1afb4462 | 171 | return false; |
harada_jpms | 0:ad0d1afb4462 | 172 | } else { |
harada_jpms | 0:ad0d1afb4462 | 173 | } |
harada_jpms | 0:ad0d1afb4462 | 174 | } |
harada_jpms | 0:ad0d1afb4462 | 175 | return true; |
harada_jpms | 0:ad0d1afb4462 | 176 | } |
harada_jpms | 0:ad0d1afb4462 | 177 | |
harada_jpms | 0:ad0d1afb4462 | 178 | int get_board_index(const Gap::Address_t mac) |
harada_jpms | 0:ad0d1afb4462 | 179 | { |
harada_jpms | 0:ad0d1afb4462 | 180 | if (mac_equals(mac, mac_board_0)) { |
harada_jpms | 0:ad0d1afb4462 | 181 | return 0; |
harada_jpms | 0:ad0d1afb4462 | 182 | } |
harada_jpms | 0:ad0d1afb4462 | 183 | #if 0 |
harada_jpms | 0:ad0d1afb4462 | 184 | if (mac_equals(mac, mac_board_1)) { |
harada_jpms | 0:ad0d1afb4462 | 185 | return 1; |
harada_jpms | 0:ad0d1afb4462 | 186 | } |
harada_jpms | 0:ad0d1afb4462 | 187 | if (mac_equals(mac, mac_board_2)) { |
harada_jpms | 0:ad0d1afb4462 | 188 | return 2; |
harada_jpms | 0:ad0d1afb4462 | 189 | } |
harada_jpms | 0:ad0d1afb4462 | 190 | if (mac_equals(mac, mac_board_3)) { |
harada_jpms | 0:ad0d1afb4462 | 191 | return 3; |
harada_jpms | 0:ad0d1afb4462 | 192 | } |
harada_jpms | 0:ad0d1afb4462 | 193 | if (mac_equals(mac, mac_board_4)) { |
harada_jpms | 0:ad0d1afb4462 | 194 | return 4; |
harada_jpms | 0:ad0d1afb4462 | 195 | } |
harada_jpms | 0:ad0d1afb4462 | 196 | #endif |
harada_jpms | 0:ad0d1afb4462 | 197 | return -1; |
harada_jpms | 0:ad0d1afb4462 | 198 | } |
harada_jpms | 0:ad0d1afb4462 | 199 | |
harada_jpms | 0:ad0d1afb4462 | 200 | // Client(Central) role ******************************************************** |
harada_jpms | 0:ad0d1afb4462 | 201 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) |
harada_jpms | 0:ad0d1afb4462 | 202 | { |
harada_jpms | 0:ad0d1afb4462 | 203 | // connections |
harada_jpms | 0:ad0d1afb4462 | 204 | int peer_board_index = get_board_index(params->peerAddr); |
harada_jpms | 0:ad0d1afb4462 | 205 | if (peer_board_index != -1) { |
harada_jpms | 0:ad0d1afb4462 | 206 | pc.printf(""); |
harada_jpms | 0:ad0d1afb4462 | 207 | pc.printf( |
harada_jpms | 0:ad0d1afb4462 | 208 | "adv peerAddr [%02x %02x %02x %02x %02x %02x]\r\n", |
harada_jpms | 0:ad0d1afb4462 | 209 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], |
harada_jpms | 0:ad0d1afb4462 | 210 | params->peerAddr[2], params->peerAddr[1], params->peerAddr[0] |
harada_jpms | 0:ad0d1afb4462 | 211 | ); |
harada_jpms | 0:ad0d1afb4462 | 212 | pc.printf( |
harada_jpms | 0:ad0d1afb4462 | 213 | "rssi=%+4d, isScanResponse %u, AdvertisementType %u\r\n", |
harada_jpms | 0:ad0d1afb4462 | 214 | params->rssi, params->isScanResponse, params->type |
harada_jpms | 0:ad0d1afb4462 | 215 | ); |
harada_jpms | 0:ad0d1afb4462 | 216 | ble_uart.gap().connect( |
harada_jpms | 0:ad0d1afb4462 | 217 | params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
harada_jpms | 0:ad0d1afb4462 | 218 | } |
harada_jpms | 0:ad0d1afb4462 | 219 | } |
harada_jpms | 0:ad0d1afb4462 | 220 | |
harada_jpms | 0:ad0d1afb4462 | 221 | void serviceDiscoveryCallback(const DiscoveredService *service) |
harada_jpms | 0:ad0d1afb4462 | 222 | { |
harada_jpms | 0:ad0d1afb4462 | 223 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
harada_jpms | 0:ad0d1afb4462 | 224 | |
harada_jpms | 0:ad0d1afb4462 | 225 | } else { |
harada_jpms | 0:ad0d1afb4462 | 226 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
harada_jpms | 0:ad0d1afb4462 | 227 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
harada_jpms | 0:ad0d1afb4462 | 228 | |
harada_jpms | 0:ad0d1afb4462 | 229 | } |
harada_jpms | 0:ad0d1afb4462 | 230 | } |
harada_jpms | 0:ad0d1afb4462 | 231 | } |
harada_jpms | 0:ad0d1afb4462 | 232 | |
harada_jpms | 0:ad0d1afb4462 | 233 | void characteristicDiscoveryCallback( |
harada_jpms | 0:ad0d1afb4462 | 234 | const DiscoveredCharacteristic *characteristicP) |
harada_jpms | 0:ad0d1afb4462 | 235 | { |
harada_jpms | 0:ad0d1afb4462 | 236 | |
harada_jpms | 0:ad0d1afb4462 | 237 | if (characteristicP->getUUID().getShortUUID() |
harada_jpms | 0:ad0d1afb4462 | 238 | == UARTServiceTXCharacteristicShortUUID) { |
harada_jpms | 0:ad0d1afb4462 | 239 | uartTXCharacteristic = *characteristicP; |
harada_jpms | 0:ad0d1afb4462 | 240 | connection_tx = true; |
harada_jpms | 0:ad0d1afb4462 | 241 | } else if (characteristicP->getUUID().getShortUUID() |
harada_jpms | 0:ad0d1afb4462 | 242 | == UARTServiceRXCharacteristicShortUUID) { |
harada_jpms | 0:ad0d1afb4462 | 243 | uartRXCharacteristic = *characteristicP; |
harada_jpms | 0:ad0d1afb4462 | 244 | foundUartRXCharacteristic = true; |
harada_jpms | 0:ad0d1afb4462 | 245 | connection_rx = true; |
harada_jpms | 0:ad0d1afb4462 | 246 | } |
harada_jpms | 0:ad0d1afb4462 | 247 | } |
harada_jpms | 0:ad0d1afb4462 | 248 | |
harada_jpms | 0:ad0d1afb4462 | 249 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) |
harada_jpms | 0:ad0d1afb4462 | 250 | { |
harada_jpms | 0:ad0d1afb4462 | 251 | } |
harada_jpms | 0:ad0d1afb4462 | 252 | |
harada_jpms | 0:ad0d1afb4462 | 253 | // Mixed role ****************************************************************** |
harada_jpms | 0:ad0d1afb4462 | 254 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
harada_jpms | 0:ad0d1afb4462 | 255 | { |
harada_jpms | 0:ad0d1afb4462 | 256 | if (params->role == Gap::CENTRAL) { |
harada_jpms | 0:ad0d1afb4462 | 257 | pc.printf("connected as Client(Central) (handle = %d)\r\n\r", |
harada_jpms | 0:ad0d1afb4462 | 258 | params->handle); |
harada_jpms | 0:ad0d1afb4462 | 259 | connected2server = true; |
harada_jpms | 0:ad0d1afb4462 | 260 | connectionHandle = params->handle; |
harada_jpms | 0:ad0d1afb4462 | 261 | ble_uart.gattClient().onServiceDiscoveryTermination( |
harada_jpms | 0:ad0d1afb4462 | 262 | discoveryTerminationCallback); |
harada_jpms | 0:ad0d1afb4462 | 263 | ble_uart.gattClient().launchServiceDiscovery( |
harada_jpms | 0:ad0d1afb4462 | 264 | params->handle, |
harada_jpms | 0:ad0d1afb4462 | 265 | serviceDiscoveryCallback, |
harada_jpms | 0:ad0d1afb4462 | 266 | characteristicDiscoveryCallback |
harada_jpms | 0:ad0d1afb4462 | 267 | ); |
harada_jpms | 0:ad0d1afb4462 | 268 | } |
harada_jpms | 0:ad0d1afb4462 | 269 | pc.printf( |
harada_jpms | 0:ad0d1afb4462 | 270 | "Client(Central/Myself) %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
harada_jpms | 0:ad0d1afb4462 | 271 | params->ownAddr[5], params->ownAddr[4], params->ownAddr[3], |
harada_jpms | 0:ad0d1afb4462 | 272 | params->ownAddr[2], params->ownAddr[1], params->ownAddr[0] |
harada_jpms | 0:ad0d1afb4462 | 273 | ); |
harada_jpms | 0:ad0d1afb4462 | 274 | pc.printf( |
harada_jpms | 0:ad0d1afb4462 | 275 | "Connected Server(Peripheral) %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
harada_jpms | 0:ad0d1afb4462 | 276 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], |
harada_jpms | 0:ad0d1afb4462 | 277 | params->peerAddr[2], params->peerAddr[1], params->peerAddr[0] |
harada_jpms | 0:ad0d1afb4462 | 278 | ); |
harada_jpms | 0:ad0d1afb4462 | 279 | } |
harada_jpms | 0:ad0d1afb4462 | 280 | |
harada_jpms | 0:ad0d1afb4462 | 281 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
harada_jpms | 0:ad0d1afb4462 | 282 | { |
harada_jpms | 0:ad0d1afb4462 | 283 | pc.printf(" -> disconnected\r\n", params->handle); |
harada_jpms | 0:ad0d1afb4462 | 284 | connected2server = false; |
harada_jpms | 0:ad0d1afb4462 | 285 | // connection_1st = false; |
harada_jpms | 0:ad0d1afb4462 | 286 | connection_tx = false; |
harada_jpms | 0:ad0d1afb4462 | 287 | connection_rx = false; |
harada_jpms | 0:ad0d1afb4462 | 288 | if (params->handle == SOFT_DEVICE_FATHER_HANDLE) { |
harada_jpms | 0:ad0d1afb4462 | 289 | ble_uart.startAdvertising(); // restart advertising |
harada_jpms | 0:ad0d1afb4462 | 290 | } else { |
harada_jpms | 0:ad0d1afb4462 | 291 | ble_uart.gap().startScan(advertisementCallback);// restart scan |
harada_jpms | 0:ad0d1afb4462 | 292 | } |
harada_jpms | 0:ad0d1afb4462 | 293 | } |
harada_jpms | 1:d2ebf1c3ae5a | 294 | |
harada_jpms | 1:d2ebf1c3ae5a | 295 | /****************************************************************************/ |
harada_jpms | 1:d2ebf1c3ae5a | 296 | /* 関数名 : uart_init */ |
harada_jpms | 1:d2ebf1c3ae5a | 297 | /* 概要 : 通信初期化 */ |
harada_jpms | 1:d2ebf1c3ae5a | 298 | /* 作成者 : JPMS H.Harada */ |
harada_jpms | 1:d2ebf1c3ae5a | 299 | /* 作成日 : 2017.08.30 */ |
harada_jpms | 1:d2ebf1c3ae5a | 300 | /****************************************************************************/ |
harada_jpms | 1:d2ebf1c3ae5a | 301 | void uart_init() |
harada_jpms | 1:d2ebf1c3ae5a | 302 | { |
harada_jpms | 1:d2ebf1c3ae5a | 303 | int k; |
harada_jpms | 1:d2ebf1c3ae5a | 304 | |
harada_jpms | 1:d2ebf1c3ae5a | 305 | // 初期化時のごみを出力 |
harada_jpms | 1:d2ebf1c3ae5a | 306 | while( pc.readable() ) |
harada_jpms | 1:d2ebf1c3ae5a | 307 | { |
harada_jpms | 1:d2ebf1c3ae5a | 308 | pc.getc(); |
harada_jpms | 1:d2ebf1c3ae5a | 309 | } |
harada_jpms | 1:d2ebf1c3ae5a | 310 | |
harada_jpms | 1:d2ebf1c3ae5a | 311 | // UART バッファ初期化 /////////////////////////////////////////////// |
harada_jpms | 1:d2ebf1c3ae5a | 312 | pc_uart_buf.data_cnt = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 313 | pc_uart_buf.rp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 314 | pc_uart_buf.wp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 315 | |
harada_jpms | 1:d2ebf1c3ae5a | 316 | ble_uart_buf.data_cnt = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 317 | ble_uart_buf.rp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 318 | ble_uart_buf.wp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 319 | |
harada_jpms | 1:d2ebf1c3ae5a | 320 | for(k=0;k<SIO_BUF_SIZE;k++) { |
harada_jpms | 1:d2ebf1c3ae5a | 321 | pc_uart_buf.buf[k] = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 322 | ble_uart_buf.buf[k] = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 323 | } |
harada_jpms | 1:d2ebf1c3ae5a | 324 | |
harada_jpms | 1:d2ebf1c3ae5a | 325 | // コマンド バッファ初期化 /////////////////////////////////////////////// |
harada_jpms | 1:d2ebf1c3ae5a | 326 | pc_cmd_buf.data_cnt = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 327 | pc_cmd_buf.rp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 328 | pc_cmd_buf.wp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 329 | |
harada_jpms | 1:d2ebf1c3ae5a | 330 | ble_cmd_buf.data_cnt = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 331 | ble_cmd_buf.rp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 332 | ble_cmd_buf.wp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 333 | |
harada_jpms | 1:d2ebf1c3ae5a | 334 | for(k=0;k<SIO_BUF_SIZE;k++) { |
harada_jpms | 1:d2ebf1c3ae5a | 335 | pc_cmd_buf.buf[k] = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 336 | ble_cmd_buf.buf[k] = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 337 | } |
harada_jpms | 1:d2ebf1c3ae5a | 338 | |
harada_jpms | 1:d2ebf1c3ae5a | 339 | ///////////////////////////////////////////////////////////////// |
harada_jpms | 1:d2ebf1c3ae5a | 340 | } |
harada_jpms | 1:d2ebf1c3ae5a | 341 | |
harada_jpms | 1:d2ebf1c3ae5a | 342 | /****************************************************************************/ |
harada_jpms | 1:d2ebf1c3ae5a | 343 | /* 関数名 : main_timer_proc */ |
harada_jpms | 2:a88b0b4c4f37 | 344 | /* 概要 : メインタイマ処理 */ |
harada_jpms | 1:d2ebf1c3ae5a | 345 | /* 作成者 : JPMS H.Harada */ |
harada_jpms | 1:d2ebf1c3ae5a | 346 | /* 作成日 : 2018.02.22 */ |
harada_jpms | 1:d2ebf1c3ae5a | 347 | /****************************************************************************/ |
harada_jpms | 1:d2ebf1c3ae5a | 348 | void main_timer_proc() |
harada_jpms | 1:d2ebf1c3ae5a | 349 | { |
harada_jpms | 1:d2ebf1c3ae5a | 350 | timer_cnt++; // タイマカウントを加算 |
harada_jpms | 1:d2ebf1c3ae5a | 351 | } |
harada_jpms | 1:d2ebf1c3ae5a | 352 | |
harada_jpms | 1:d2ebf1c3ae5a | 353 | /****************************************************************************/ |
harada_jpms | 2:a88b0b4c4f37 | 354 | /* 関数名 : atow */ |
harada_jpms | 2:a88b0b4c4f37 | 355 | /* 概要 : アスキー変換処理 */ |
harada_jpms | 2:a88b0b4c4f37 | 356 | /* 作成者 : JPMS H.Harada */ |
harada_jpms | 2:a88b0b4c4f37 | 357 | /* 作成日 : 2017.08.30 */ |
harada_jpms | 1:d2ebf1c3ae5a | 358 | /****************************************************************************/ |
harada_jpms | 1:d2ebf1c3ae5a | 359 | unsigned int atow(char *str,unsigned short leng) |
harada_jpms | 1:d2ebf1c3ae5a | 360 | { |
harada_jpms | 1:d2ebf1c3ae5a | 361 | register unsigned short i; |
harada_jpms | 1:d2ebf1c3ae5a | 362 | unsigned int ret_data = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 363 | |
harada_jpms | 1:d2ebf1c3ae5a | 364 | /*---------------- データー長エラー -----------------*/ |
harada_jpms | 1:d2ebf1c3ae5a | 365 | // if(leng >= 11) |
harada_jpms | 1:d2ebf1c3ae5a | 366 | // { |
harada_jpms | 1:d2ebf1c3ae5a | 367 | // ret_data = ATOW_ERROR; /* データエラー */ |
harada_jpms | 1:d2ebf1c3ae5a | 368 | // } |
harada_jpms | 1:d2ebf1c3ae5a | 369 | /*---------------- データー長 OK ------------------*/ |
harada_jpms | 1:d2ebf1c3ae5a | 370 | // else |
harada_jpms | 1:d2ebf1c3ae5a | 371 | // { |
harada_jpms | 1:d2ebf1c3ae5a | 372 | i = leng; |
harada_jpms | 1:d2ebf1c3ae5a | 373 | |
harada_jpms | 1:d2ebf1c3ae5a | 374 | while(i) |
harada_jpms | 1:d2ebf1c3ae5a | 375 | { |
harada_jpms | 1:d2ebf1c3ae5a | 376 | ret_data *= 10; /* 10倍 */ |
harada_jpms | 1:d2ebf1c3ae5a | 377 | |
harada_jpms | 1:d2ebf1c3ae5a | 378 | /*------ データ '0' ~ '9' 範囲内 -----*/ |
harada_jpms | 1:d2ebf1c3ae5a | 379 | if( (*str >= '0') && (*str <= '9') ) |
harada_jpms | 1:d2ebf1c3ae5a | 380 | { |
harada_jpms | 1:d2ebf1c3ae5a | 381 | ret_data += (*str) - '0'; |
harada_jpms | 1:d2ebf1c3ae5a | 382 | str++; |
harada_jpms | 1:d2ebf1c3ae5a | 383 | } |
harada_jpms | 1:d2ebf1c3ae5a | 384 | /*------ データ '0' ~ '9' 範囲外 -----*/ |
harada_jpms | 1:d2ebf1c3ae5a | 385 | else |
harada_jpms | 1:d2ebf1c3ae5a | 386 | { |
harada_jpms | 1:d2ebf1c3ae5a | 387 | // ret_data = ATOW_ERROR; /* データエラー */ |
harada_jpms | 1:d2ebf1c3ae5a | 388 | break; |
harada_jpms | 1:d2ebf1c3ae5a | 389 | } |
harada_jpms | 1:d2ebf1c3ae5a | 390 | i--; |
harada_jpms | 1:d2ebf1c3ae5a | 391 | } |
harada_jpms | 1:d2ebf1c3ae5a | 392 | /*-------------- データオバーフロー ----------*/ |
harada_jpms | 1:d2ebf1c3ae5a | 393 | // if(ret_data & 0x8000) |
harada_jpms | 1:d2ebf1c3ae5a | 394 | // { |
harada_jpms | 1:d2ebf1c3ae5a | 395 | // ret_data = ATOW_ERROR; /* データエラー */ |
harada_jpms | 1:d2ebf1c3ae5a | 396 | // } |
harada_jpms | 1:d2ebf1c3ae5a | 397 | // } |
harada_jpms | 1:d2ebf1c3ae5a | 398 | return(ret_data); |
harada_jpms | 1:d2ebf1c3ae5a | 399 | } |
harada_jpms | 1:d2ebf1c3ae5a | 400 | |
harada_jpms | 1:d2ebf1c3ae5a | 401 | /****************************************************************************/ |
harada_jpms | 2:a88b0b4c4f37 | 402 | /* 関数名 : atoh */ |
harada_jpms | 2:a88b0b4c4f37 | 403 | /* 概要 : アスキー変換処理 */ |
harada_jpms | 2:a88b0b4c4f37 | 404 | /* 作成者 : JPMS H.Harada */ |
harada_jpms | 2:a88b0b4c4f37 | 405 | /* 作成日 : 2017.08.30 */ |
harada_jpms | 1:d2ebf1c3ae5a | 406 | /****************************************************************************/ |
harada_jpms | 1:d2ebf1c3ae5a | 407 | unsigned long atoh(char *str,unsigned short leng) |
harada_jpms | 1:d2ebf1c3ae5a | 408 | { |
harada_jpms | 1:d2ebf1c3ae5a | 409 | register unsigned short i; |
harada_jpms | 1:d2ebf1c3ae5a | 410 | unsigned long ret_data = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 411 | |
harada_jpms | 1:d2ebf1c3ae5a | 412 | /*---------------- データー長エラー -----------------*/ |
harada_jpms | 1:d2ebf1c3ae5a | 413 | // if(leng >= 11) |
harada_jpms | 1:d2ebf1c3ae5a | 414 | // { |
harada_jpms | 1:d2ebf1c3ae5a | 415 | // ret_data = ATOW_ERROR; /* データエラー */ |
harada_jpms | 1:d2ebf1c3ae5a | 416 | // } |
harada_jpms | 1:d2ebf1c3ae5a | 417 | /*---------------- データー長 OK ------------------*/ |
harada_jpms | 1:d2ebf1c3ae5a | 418 | // else |
harada_jpms | 1:d2ebf1c3ae5a | 419 | // { |
harada_jpms | 1:d2ebf1c3ae5a | 420 | i = leng; |
harada_jpms | 1:d2ebf1c3ae5a | 421 | |
harada_jpms | 1:d2ebf1c3ae5a | 422 | while(i) |
harada_jpms | 1:d2ebf1c3ae5a | 423 | { |
harada_jpms | 1:d2ebf1c3ae5a | 424 | ret_data *= 16; /* 16倍 */ |
harada_jpms | 1:d2ebf1c3ae5a | 425 | |
harada_jpms | 1:d2ebf1c3ae5a | 426 | /*------ データ '0' ~ '9' 範囲内 -----*/ |
harada_jpms | 1:d2ebf1c3ae5a | 427 | if( (*str >= '0') && (*str <= '9') ) |
harada_jpms | 1:d2ebf1c3ae5a | 428 | { |
harada_jpms | 1:d2ebf1c3ae5a | 429 | ret_data += (*str) - '0'; |
harada_jpms | 1:d2ebf1c3ae5a | 430 | str++; |
harada_jpms | 1:d2ebf1c3ae5a | 431 | } |
harada_jpms | 1:d2ebf1c3ae5a | 432 | /*------ データ 'A' ~ 'F' 範囲内 -----*/ |
harada_jpms | 1:d2ebf1c3ae5a | 433 | else if( (*str >= 'A') && (*str <= 'F') ) |
harada_jpms | 1:d2ebf1c3ae5a | 434 | { |
harada_jpms | 1:d2ebf1c3ae5a | 435 | ret_data += (*str) - 'A' + 10; |
harada_jpms | 1:d2ebf1c3ae5a | 436 | str++; |
harada_jpms | 1:d2ebf1c3ae5a | 437 | } |
harada_jpms | 1:d2ebf1c3ae5a | 438 | /*------ データ '0' ~ '9' 範囲外 -----*/ |
harada_jpms | 1:d2ebf1c3ae5a | 439 | else |
harada_jpms | 1:d2ebf1c3ae5a | 440 | { |
harada_jpms | 1:d2ebf1c3ae5a | 441 | // ret_data = ATOW_ERROR; /* データエラー */ |
harada_jpms | 1:d2ebf1c3ae5a | 442 | break; |
harada_jpms | 1:d2ebf1c3ae5a | 443 | } |
harada_jpms | 1:d2ebf1c3ae5a | 444 | i--; |
harada_jpms | 1:d2ebf1c3ae5a | 445 | } |
harada_jpms | 1:d2ebf1c3ae5a | 446 | /*-------------- データオバーフロー ----------*/ |
harada_jpms | 1:d2ebf1c3ae5a | 447 | // if(ret_data & 0x8000) |
harada_jpms | 1:d2ebf1c3ae5a | 448 | // { |
harada_jpms | 1:d2ebf1c3ae5a | 449 | // ret_data = ATOW_ERROR; /* データエラー */ |
harada_jpms | 1:d2ebf1c3ae5a | 450 | // } |
harada_jpms | 1:d2ebf1c3ae5a | 451 | // } |
harada_jpms | 1:d2ebf1c3ae5a | 452 | return(ret_data); |
harada_jpms | 1:d2ebf1c3ae5a | 453 | } |
harada_jpms | 1:d2ebf1c3ae5a | 454 | |
harada_jpms | 1:d2ebf1c3ae5a | 455 | char ble_str[32][10]; |
harada_jpms | 1:d2ebf1c3ae5a | 456 | char ble_str2[10][8] = { "P ", "U ", " ", "RESP ", " ", " ", " ", " ", " ", " " }; |
harada_jpms | 1:d2ebf1c3ae5a | 457 | |
harada_jpms | 1:d2ebf1c3ae5a | 458 | /****************************************************************************/ |
harada_jpms | 2:a88b0b4c4f37 | 459 | /* 関数名 : rs232c_cmd */ |
harada_jpms | 2:a88b0b4c4f37 | 460 | /* 概要 : 通信コマンド処理 */ |
harada_jpms | 2:a88b0b4c4f37 | 461 | /* 作成者 : JPMS H.Harada */ |
harada_jpms | 2:a88b0b4c4f37 | 462 | /* 作成日 : 2017.08.30 */ |
harada_jpms | 1:d2ebf1c3ae5a | 463 | /****************************************************************************/ |
harada_jpms | 1:d2ebf1c3ae5a | 464 | void ble_rs232c_cmd() |
harada_jpms | 1:d2ebf1c3ae5a | 465 | { |
harada_jpms | 1:d2ebf1c3ae5a | 466 | unsigned int c, i, j; |
harada_jpms | 1:d2ebf1c3ae5a | 467 | int rdat; |
harada_jpms | 2:a88b0b4c4f37 | 468 | |
harada_jpms | 1:d2ebf1c3ae5a | 469 | if( ble_uart_buf.data_cnt > 0 ) |
harada_jpms | 1:d2ebf1c3ae5a | 470 | { |
harada_jpms | 1:d2ebf1c3ae5a | 471 | unsigned int len = ble_uart_buf.data_cnt; |
harada_jpms | 1:d2ebf1c3ae5a | 472 | for( c=0; c<len; c++ ) |
harada_jpms | 1:d2ebf1c3ae5a | 473 | { |
harada_jpms | 1:d2ebf1c3ae5a | 474 | rdat = ble_uart_buf.buf[ble_uart_buf.rp]; |
harada_jpms | 1:d2ebf1c3ae5a | 475 | ble_uart_buf.data_cnt--; |
harada_jpms | 1:d2ebf1c3ae5a | 476 | |
harada_jpms | 1:d2ebf1c3ae5a | 477 | if( ble_uart_buf.rp == SIO_BUF_SIZE-1 ) |
harada_jpms | 1:d2ebf1c3ae5a | 478 | { |
harada_jpms | 1:d2ebf1c3ae5a | 479 | ble_uart_buf.rp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 480 | } |
harada_jpms | 1:d2ebf1c3ae5a | 481 | else |
harada_jpms | 1:d2ebf1c3ae5a | 482 | { |
harada_jpms | 1:d2ebf1c3ae5a | 483 | ble_uart_buf.rp++; |
harada_jpms | 1:d2ebf1c3ae5a | 484 | } |
harada_jpms | 1:d2ebf1c3ae5a | 485 | |
harada_jpms | 1:d2ebf1c3ae5a | 486 | if( rdat != '\n' ) |
harada_jpms | 1:d2ebf1c3ae5a | 487 | { |
harada_jpms | 1:d2ebf1c3ae5a | 488 | ble_cmd_buf.buf[ble_cmd_buf.data_cnt++] = rdat; |
harada_jpms | 1:d2ebf1c3ae5a | 489 | } |
harada_jpms | 1:d2ebf1c3ae5a | 490 | |
harada_jpms | 1:d2ebf1c3ae5a | 491 | if (rdat == '\r') |
harada_jpms | 1:d2ebf1c3ae5a | 492 | { |
harada_jpms | 1:d2ebf1c3ae5a | 493 | int k = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 494 | int l = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 495 | for (i=0;i<ble_cmd_buf.data_cnt;i++) |
harada_jpms | 1:d2ebf1c3ae5a | 496 | { |
harada_jpms | 1:d2ebf1c3ae5a | 497 | if( ble_cmd_buf.buf[i] == ' ' || |
harada_jpms | 1:d2ebf1c3ae5a | 498 | ble_cmd_buf.buf[i] == '\r' || |
harada_jpms | 1:d2ebf1c3ae5a | 499 | ble_cmd_buf.buf[i] == '\t' ) |
harada_jpms | 1:d2ebf1c3ae5a | 500 | { |
harada_jpms | 1:d2ebf1c3ae5a | 501 | ble_str[k++][l] = '\0'; |
harada_jpms | 1:d2ebf1c3ae5a | 502 | l = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 503 | } |
harada_jpms | 1:d2ebf1c3ae5a | 504 | else |
harada_jpms | 1:d2ebf1c3ae5a | 505 | { |
harada_jpms | 1:d2ebf1c3ae5a | 506 | ble_str[k][l++] = ble_cmd_buf.buf[i]; |
harada_jpms | 1:d2ebf1c3ae5a | 507 | } |
harada_jpms | 1:d2ebf1c3ae5a | 508 | } |
harada_jpms | 1:d2ebf1c3ae5a | 509 | |
harada_jpms | 1:d2ebf1c3ae5a | 510 | if( ble_cmd_buf.data_cnt == 1 ) |
harada_jpms | 1:d2ebf1c3ae5a | 511 | { |
harada_jpms | 1:d2ebf1c3ae5a | 512 | k = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 513 | } |
harada_jpms | 1:d2ebf1c3ae5a | 514 | |
harada_jpms | 1:d2ebf1c3ae5a | 515 | ble_cmd_buf.data_cnt = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 516 | ble_cmd_buf.buf[0] = '\0'; |
harada_jpms | 1:d2ebf1c3ae5a | 517 | |
harada_jpms | 1:d2ebf1c3ae5a | 518 | if (k > 0) |
harada_jpms | 1:d2ebf1c3ae5a | 519 | { |
harada_jpms | 1:d2ebf1c3ae5a | 520 | int ret; |
harada_jpms | 1:d2ebf1c3ae5a | 521 | int cmd_no = 0xFF; |
harada_jpms | 1:d2ebf1c3ae5a | 522 | |
harada_jpms | 1:d2ebf1c3ae5a | 523 | for( j=0; j<10; j++ ) |
harada_jpms | 1:d2ebf1c3ae5a | 524 | { |
harada_jpms | 1:d2ebf1c3ae5a | 525 | ret = 1; |
harada_jpms | 1:d2ebf1c3ae5a | 526 | |
harada_jpms | 1:d2ebf1c3ae5a | 527 | if( strlen(ble_str[0]) > 0 ) |
harada_jpms | 1:d2ebf1c3ae5a | 528 | { |
harada_jpms | 1:d2ebf1c3ae5a | 529 | for( i=0; i<strlen(ble_str[0]); i++ ) |
harada_jpms | 1:d2ebf1c3ae5a | 530 | { |
harada_jpms | 1:d2ebf1c3ae5a | 531 | if( ble_str[0][i] != ble_str2[j][i] ) |
harada_jpms | 1:d2ebf1c3ae5a | 532 | { |
harada_jpms | 1:d2ebf1c3ae5a | 533 | ret = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 534 | } |
harada_jpms | 1:d2ebf1c3ae5a | 535 | } |
harada_jpms | 1:d2ebf1c3ae5a | 536 | |
harada_jpms | 1:d2ebf1c3ae5a | 537 | if( ret ) |
harada_jpms | 1:d2ebf1c3ae5a | 538 | { |
harada_jpms | 1:d2ebf1c3ae5a | 539 | cmd_no = j; |
harada_jpms | 1:d2ebf1c3ae5a | 540 | break; |
harada_jpms | 1:d2ebf1c3ae5a | 541 | } |
harada_jpms | 1:d2ebf1c3ae5a | 542 | } |
harada_jpms | 1:d2ebf1c3ae5a | 543 | } |
harada_jpms | 1:d2ebf1c3ae5a | 544 | |
harada_jpms | 1:d2ebf1c3ae5a | 545 | // コマンド P |
harada_jpms | 1:d2ebf1c3ae5a | 546 | if( cmd_no == 0 ) |
harada_jpms | 1:d2ebf1c3ae5a | 547 | { |
harada_jpms | 1:d2ebf1c3ae5a | 548 | int data_cnt = atow(ble_str[1], strlen(ble_str[1])); |
harada_jpms | 1:d2ebf1c3ae5a | 549 | int left = atow(ble_str[2], strlen(ble_str[2])); |
harada_jpms | 1:d2ebf1c3ae5a | 550 | int right = atow(ble_str[3], strlen(ble_str[3])); |
harada_jpms | 1:d2ebf1c3ae5a | 551 | |
harada_jpms | 1:d2ebf1c3ae5a | 552 | pc.printf( "POINT %4d %d %d\r", data_cnt, left, right ); |
harada_jpms | 1:d2ebf1c3ae5a | 553 | } |
harada_jpms | 1:d2ebf1c3ae5a | 554 | // コマンド U |
harada_jpms | 1:d2ebf1c3ae5a | 555 | else if( cmd_no == 1 ) |
harada_jpms | 1:d2ebf1c3ae5a | 556 | { |
harada_jpms | 1:d2ebf1c3ae5a | 557 | unsigned int high = atoh(ble_str[1], strlen(ble_str[1])); |
harada_jpms | 1:d2ebf1c3ae5a | 558 | unsigned int low = atoh(ble_str[2], strlen(ble_str[2])); |
harada_jpms | 1:d2ebf1c3ae5a | 559 | |
harada_jpms | 1:d2ebf1c3ae5a | 560 | pc.printf( "UID %08X %08X\r\n", high, low ); |
harada_jpms | 1:d2ebf1c3ae5a | 561 | } |
harada_jpms | 1:d2ebf1c3ae5a | 562 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 563 | else if( cmd_no == 2 ) |
harada_jpms | 1:d2ebf1c3ae5a | 564 | { |
harada_jpms | 1:d2ebf1c3ae5a | 565 | |
harada_jpms | 1:d2ebf1c3ae5a | 566 | } |
harada_jpms | 1:d2ebf1c3ae5a | 567 | // コマンド RESP |
harada_jpms | 1:d2ebf1c3ae5a | 568 | else if( cmd_no == 3 ) |
harada_jpms | 1:d2ebf1c3ae5a | 569 | { |
harada_jpms | 1:d2ebf1c3ae5a | 570 | pc.printf( "RESP\r" ); |
harada_jpms | 1:d2ebf1c3ae5a | 571 | } |
harada_jpms | 1:d2ebf1c3ae5a | 572 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 573 | else if( cmd_no == 4 ) |
harada_jpms | 1:d2ebf1c3ae5a | 574 | { |
harada_jpms | 1:d2ebf1c3ae5a | 575 | |
harada_jpms | 1:d2ebf1c3ae5a | 576 | } |
harada_jpms | 1:d2ebf1c3ae5a | 577 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 578 | else if( cmd_no == 5 ) |
harada_jpms | 1:d2ebf1c3ae5a | 579 | { |
harada_jpms | 1:d2ebf1c3ae5a | 580 | |
harada_jpms | 1:d2ebf1c3ae5a | 581 | } |
harada_jpms | 1:d2ebf1c3ae5a | 582 | |
harada_jpms | 1:d2ebf1c3ae5a | 583 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 584 | else if( cmd_no == 6 ) |
harada_jpms | 1:d2ebf1c3ae5a | 585 | { |
harada_jpms | 1:d2ebf1c3ae5a | 586 | |
harada_jpms | 1:d2ebf1c3ae5a | 587 | } |
harada_jpms | 1:d2ebf1c3ae5a | 588 | |
harada_jpms | 1:d2ebf1c3ae5a | 589 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 590 | else if( cmd_no == 7 ) |
harada_jpms | 1:d2ebf1c3ae5a | 591 | { |
harada_jpms | 1:d2ebf1c3ae5a | 592 | |
harada_jpms | 1:d2ebf1c3ae5a | 593 | } |
harada_jpms | 1:d2ebf1c3ae5a | 594 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 595 | else if( cmd_no == 8 ) |
harada_jpms | 1:d2ebf1c3ae5a | 596 | { |
harada_jpms | 1:d2ebf1c3ae5a | 597 | |
harada_jpms | 1:d2ebf1c3ae5a | 598 | } |
harada_jpms | 1:d2ebf1c3ae5a | 599 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 600 | else if( cmd_no == 9 ) |
harada_jpms | 1:d2ebf1c3ae5a | 601 | { |
harada_jpms | 1:d2ebf1c3ae5a | 602 | |
harada_jpms | 1:d2ebf1c3ae5a | 603 | } |
harada_jpms | 1:d2ebf1c3ae5a | 604 | else |
harada_jpms | 1:d2ebf1c3ae5a | 605 | { |
harada_jpms | 2:a88b0b4c4f37 | 606 | //pc.printf("CMD_ERR\r\n"); |
harada_jpms | 1:d2ebf1c3ae5a | 607 | } |
harada_jpms | 1:d2ebf1c3ae5a | 608 | } |
harada_jpms | 1:d2ebf1c3ae5a | 609 | } |
harada_jpms | 1:d2ebf1c3ae5a | 610 | } |
harada_jpms | 1:d2ebf1c3ae5a | 611 | } |
harada_jpms | 1:d2ebf1c3ae5a | 612 | } |
harada_jpms | 1:d2ebf1c3ae5a | 613 | |
harada_jpms | 1:d2ebf1c3ae5a | 614 | #define CMD_NUM 24 |
harada_jpms | 1:d2ebf1c3ae5a | 615 | |
harada_jpms | 1:d2ebf1c3ae5a | 616 | char pc_str[16][10]; |
harada_jpms | 1:d2ebf1c3ae5a | 617 | char pc_str2[CMD_NUM][8] = { "START ", // 0 |
harada_jpms | 1:d2ebf1c3ae5a | 618 | "STOP ", // 1 |
harada_jpms | 1:d2ebf1c3ae5a | 619 | " ", // 2 |
harada_jpms | 1:d2ebf1c3ae5a | 620 | "MSET ", // 3 |
harada_jpms | 1:d2ebf1c3ae5a | 621 | " ", // 4 |
harada_jpms | 1:d2ebf1c3ae5a | 622 | " ", // 5 |
harada_jpms | 1:d2ebf1c3ae5a | 623 | " ", // 6 |
harada_jpms | 1:d2ebf1c3ae5a | 624 | "ISETL ", // 7 |
harada_jpms | 1:d2ebf1c3ae5a | 625 | "ISETR ", // 8 |
harada_jpms | 1:d2ebf1c3ae5a | 626 | " ", // 9 |
harada_jpms | 1:d2ebf1c3ae5a | 627 | " ", // 10 |
harada_jpms | 1:d2ebf1c3ae5a | 628 | "CONNECT", // 11 |
harada_jpms | 1:d2ebf1c3ae5a | 629 | "DISCO ", // 12 |
harada_jpms | 1:d2ebf1c3ae5a | 630 | "INTVL ", // 13 |
harada_jpms | 1:d2ebf1c3ae5a | 631 | "NTC ", // 14 |
harada_jpms | 1:d2ebf1c3ae5a | 632 | "OTC ", // 15 |
harada_jpms | 1:d2ebf1c3ae5a | 633 | " ", // 16 |
harada_jpms | 1:d2ebf1c3ae5a | 634 | " ", // 17 |
harada_jpms | 1:d2ebf1c3ae5a | 635 | "IDGET ", // 18 |
harada_jpms | 1:d2ebf1c3ae5a | 636 | " ", // 19 |
harada_jpms | 1:d2ebf1c3ae5a | 637 | " ", // 20 |
harada_jpms | 1:d2ebf1c3ae5a | 638 | " ", // 21 |
harada_jpms | 1:d2ebf1c3ae5a | 639 | " ", // 22 |
harada_jpms | 1:d2ebf1c3ae5a | 640 | " "}; // 23 |
harada_jpms | 1:d2ebf1c3ae5a | 641 | |
harada_jpms | 1:d2ebf1c3ae5a | 642 | /****************************************************************************/ |
harada_jpms | 2:a88b0b4c4f37 | 643 | /* 関数名 : rs232c_cmd */ |
harada_jpms | 2:a88b0b4c4f37 | 644 | /* 概要 : 通信コマンド処理 */ |
harada_jpms | 2:a88b0b4c4f37 | 645 | /* 作成者 : JPMS H.Harada */ |
harada_jpms | 2:a88b0b4c4f37 | 646 | /* 作成日 : 2017.08.30 */ |
harada_jpms | 1:d2ebf1c3ae5a | 647 | /****************************************************************************/ |
harada_jpms | 1:d2ebf1c3ae5a | 648 | void pc_rs232c_cmd() |
harada_jpms | 1:d2ebf1c3ae5a | 649 | { |
harada_jpms | 1:d2ebf1c3ae5a | 650 | unsigned int c, i, j; |
harada_jpms | 1:d2ebf1c3ae5a | 651 | int rdat; |
harada_jpms | 1:d2ebf1c3ae5a | 652 | |
harada_jpms | 1:d2ebf1c3ae5a | 653 | while( pc.readable() ) |
harada_jpms | 1:d2ebf1c3ae5a | 654 | { |
harada_jpms | 1:d2ebf1c3ae5a | 655 | pc_uart_buf.buf[pc_uart_buf.wp] = pc.getc(); |
harada_jpms | 1:d2ebf1c3ae5a | 656 | pc_uart_buf.data_cnt++; |
harada_jpms | 1:d2ebf1c3ae5a | 657 | |
harada_jpms | 1:d2ebf1c3ae5a | 658 | if( pc_uart_buf.wp == SIO_BUF_SIZE-1 ) |
harada_jpms | 1:d2ebf1c3ae5a | 659 | { |
harada_jpms | 1:d2ebf1c3ae5a | 660 | pc_uart_buf.wp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 661 | } |
harada_jpms | 1:d2ebf1c3ae5a | 662 | else |
harada_jpms | 1:d2ebf1c3ae5a | 663 | { |
harada_jpms | 1:d2ebf1c3ae5a | 664 | pc_uart_buf.wp++; |
harada_jpms | 1:d2ebf1c3ae5a | 665 | } |
harada_jpms | 1:d2ebf1c3ae5a | 666 | } |
harada_jpms | 1:d2ebf1c3ae5a | 667 | |
harada_jpms | 1:d2ebf1c3ae5a | 668 | if( pc_uart_buf.data_cnt > 0 ) |
harada_jpms | 1:d2ebf1c3ae5a | 669 | { |
harada_jpms | 1:d2ebf1c3ae5a | 670 | unsigned int len = pc_uart_buf.data_cnt; |
harada_jpms | 1:d2ebf1c3ae5a | 671 | for( c=0; c<len; c++ ) |
harada_jpms | 1:d2ebf1c3ae5a | 672 | { |
harada_jpms | 1:d2ebf1c3ae5a | 673 | rdat = pc_uart_buf.buf[pc_uart_buf.rp]; |
harada_jpms | 1:d2ebf1c3ae5a | 674 | pc_uart_buf.data_cnt--; |
harada_jpms | 1:d2ebf1c3ae5a | 675 | |
harada_jpms | 1:d2ebf1c3ae5a | 676 | if( pc_uart_buf.rp == SIO_BUF_SIZE-1 ) |
harada_jpms | 1:d2ebf1c3ae5a | 677 | { |
harada_jpms | 1:d2ebf1c3ae5a | 678 | pc_uart_buf.rp = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 679 | } |
harada_jpms | 1:d2ebf1c3ae5a | 680 | else |
harada_jpms | 1:d2ebf1c3ae5a | 681 | { |
harada_jpms | 1:d2ebf1c3ae5a | 682 | pc_uart_buf.rp++; |
harada_jpms | 1:d2ebf1c3ae5a | 683 | } |
harada_jpms | 1:d2ebf1c3ae5a | 684 | |
harada_jpms | 1:d2ebf1c3ae5a | 685 | if( rdat != '\n' ) |
harada_jpms | 1:d2ebf1c3ae5a | 686 | { |
harada_jpms | 1:d2ebf1c3ae5a | 687 | pc_cmd_buf.buf[pc_cmd_buf.data_cnt++] = rdat; |
harada_jpms | 1:d2ebf1c3ae5a | 688 | } |
harada_jpms | 1:d2ebf1c3ae5a | 689 | |
harada_jpms | 1:d2ebf1c3ae5a | 690 | if( rdat == '\r' ) |
harada_jpms | 1:d2ebf1c3ae5a | 691 | { |
harada_jpms | 1:d2ebf1c3ae5a | 692 | int k = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 693 | int l = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 694 | for (i=0;i<pc_cmd_buf.data_cnt;i++) |
harada_jpms | 1:d2ebf1c3ae5a | 695 | { |
harada_jpms | 1:d2ebf1c3ae5a | 696 | if( pc_cmd_buf.buf[i] == ' ' || |
harada_jpms | 1:d2ebf1c3ae5a | 697 | pc_cmd_buf.buf[i] == '\r' || |
harada_jpms | 1:d2ebf1c3ae5a | 698 | pc_cmd_buf.buf[i] == '\t' ) |
harada_jpms | 1:d2ebf1c3ae5a | 699 | { |
harada_jpms | 1:d2ebf1c3ae5a | 700 | pc_str[k++][l] = '\0'; |
harada_jpms | 1:d2ebf1c3ae5a | 701 | l = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 702 | } |
harada_jpms | 1:d2ebf1c3ae5a | 703 | else |
harada_jpms | 1:d2ebf1c3ae5a | 704 | { |
harada_jpms | 1:d2ebf1c3ae5a | 705 | pc_str[k][l++] = pc_cmd_buf.buf[i]; |
harada_jpms | 1:d2ebf1c3ae5a | 706 | } |
harada_jpms | 1:d2ebf1c3ae5a | 707 | } |
harada_jpms | 1:d2ebf1c3ae5a | 708 | |
harada_jpms | 1:d2ebf1c3ae5a | 709 | if( pc_cmd_buf.data_cnt == 1 ) |
harada_jpms | 1:d2ebf1c3ae5a | 710 | { |
harada_jpms | 1:d2ebf1c3ae5a | 711 | k = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 712 | } |
harada_jpms | 1:d2ebf1c3ae5a | 713 | |
harada_jpms | 1:d2ebf1c3ae5a | 714 | pc_cmd_buf.data_cnt = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 715 | pc_cmd_buf.buf[0] = '\0'; |
harada_jpms | 1:d2ebf1c3ae5a | 716 | |
harada_jpms | 1:d2ebf1c3ae5a | 717 | if (k > 0) |
harada_jpms | 1:d2ebf1c3ae5a | 718 | { |
harada_jpms | 1:d2ebf1c3ae5a | 719 | int ret; |
harada_jpms | 1:d2ebf1c3ae5a | 720 | int cmd_no = 0xFF; |
harada_jpms | 1:d2ebf1c3ae5a | 721 | |
harada_jpms | 1:d2ebf1c3ae5a | 722 | for( j=0; j<CMD_NUM; j++ ) |
harada_jpms | 1:d2ebf1c3ae5a | 723 | { |
harada_jpms | 1:d2ebf1c3ae5a | 724 | ret = 1; |
harada_jpms | 1:d2ebf1c3ae5a | 725 | |
harada_jpms | 1:d2ebf1c3ae5a | 726 | if( strlen(pc_str[0]) > 0 ) |
harada_jpms | 1:d2ebf1c3ae5a | 727 | { |
harada_jpms | 1:d2ebf1c3ae5a | 728 | for( i=0; i<strlen(pc_str[0]); i++ ) |
harada_jpms | 1:d2ebf1c3ae5a | 729 | { |
harada_jpms | 1:d2ebf1c3ae5a | 730 | if( pc_str[0][i] != pc_str2[j][i] ) |
harada_jpms | 1:d2ebf1c3ae5a | 731 | { |
harada_jpms | 1:d2ebf1c3ae5a | 732 | ret = 0; |
harada_jpms | 1:d2ebf1c3ae5a | 733 | } |
harada_jpms | 1:d2ebf1c3ae5a | 734 | } |
harada_jpms | 1:d2ebf1c3ae5a | 735 | |
harada_jpms | 1:d2ebf1c3ae5a | 736 | if( ret ) |
harada_jpms | 1:d2ebf1c3ae5a | 737 | { |
harada_jpms | 1:d2ebf1c3ae5a | 738 | cmd_no = j; |
harada_jpms | 1:d2ebf1c3ae5a | 739 | break; |
harada_jpms | 1:d2ebf1c3ae5a | 740 | } |
harada_jpms | 1:d2ebf1c3ae5a | 741 | } |
harada_jpms | 1:d2ebf1c3ae5a | 742 | } |
harada_jpms | 1:d2ebf1c3ae5a | 743 | |
harada_jpms | 1:d2ebf1c3ae5a | 744 | // コマンド START |
harada_jpms | 1:d2ebf1c3ae5a | 745 | if (cmd_no == 0) |
harada_jpms | 1:d2ebf1c3ae5a | 746 | { |
harada_jpms | 1:d2ebf1c3ae5a | 747 | ble_write( "START\r" ); |
harada_jpms | 1:d2ebf1c3ae5a | 748 | |
harada_jpms | 1:d2ebf1c3ae5a | 749 | pc.printf( "START OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 750 | } |
harada_jpms | 1:d2ebf1c3ae5a | 751 | // コマンド STOP |
harada_jpms | 1:d2ebf1c3ae5a | 752 | else if( cmd_no == 1 ) |
harada_jpms | 1:d2ebf1c3ae5a | 753 | { |
harada_jpms | 1:d2ebf1c3ae5a | 754 | ble_write( "STOP\r" ); |
harada_jpms | 1:d2ebf1c3ae5a | 755 | |
harada_jpms | 1:d2ebf1c3ae5a | 756 | pc.printf( "STOP OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 757 | } |
harada_jpms | 1:d2ebf1c3ae5a | 758 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 759 | else if (cmd_no == 2) |
harada_jpms | 1:d2ebf1c3ae5a | 760 | { |
harada_jpms | 1:d2ebf1c3ae5a | 761 | |
harada_jpms | 1:d2ebf1c3ae5a | 762 | } |
harada_jpms | 1:d2ebf1c3ae5a | 763 | // コマンド MSET |
harada_jpms | 1:d2ebf1c3ae5a | 764 | else if( cmd_no == 3 ) |
harada_jpms | 1:d2ebf1c3ae5a | 765 | { |
harada_jpms | 1:d2ebf1c3ae5a | 766 | mac_1 = atoh(pc_str[1], strlen(pc_str[1])); |
harada_jpms | 1:d2ebf1c3ae5a | 767 | mac_2 = atoh(pc_str[2], strlen(pc_str[2])); |
harada_jpms | 1:d2ebf1c3ae5a | 768 | mac_3 = atoh(pc_str[3], strlen(pc_str[3])); |
harada_jpms | 1:d2ebf1c3ae5a | 769 | |
harada_jpms | 1:d2ebf1c3ae5a | 770 | pc.printf( "MSET OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 771 | } |
harada_jpms | 1:d2ebf1c3ae5a | 772 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 773 | else if( cmd_no == 4 ) |
harada_jpms | 1:d2ebf1c3ae5a | 774 | { |
harada_jpms | 1:d2ebf1c3ae5a | 775 | |
harada_jpms | 1:d2ebf1c3ae5a | 776 | } |
harada_jpms | 1:d2ebf1c3ae5a | 777 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 778 | else if( cmd_no == 5 ) |
harada_jpms | 1:d2ebf1c3ae5a | 779 | { |
harada_jpms | 1:d2ebf1c3ae5a | 780 | |
harada_jpms | 1:d2ebf1c3ae5a | 781 | } |
harada_jpms | 1:d2ebf1c3ae5a | 782 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 783 | else if( cmd_no == 6 ) |
harada_jpms | 1:d2ebf1c3ae5a | 784 | { |
harada_jpms | 1:d2ebf1c3ae5a | 785 | |
harada_jpms | 1:d2ebf1c3ae5a | 786 | } |
harada_jpms | 1:d2ebf1c3ae5a | 787 | // コマンド ISETL |
harada_jpms | 1:d2ebf1c3ae5a | 788 | else if( cmd_no == 7 ) |
harada_jpms | 1:d2ebf1c3ae5a | 789 | { |
harada_jpms | 1:d2ebf1c3ae5a | 790 | int id_LH = atoh(pc_str[1], strlen(pc_str[1])); |
harada_jpms | 1:d2ebf1c3ae5a | 791 | int id_LL = atoh(pc_str[2], strlen(pc_str[2])); |
harada_jpms | 1:d2ebf1c3ae5a | 792 | |
harada_jpms | 1:d2ebf1c3ae5a | 793 | char buf[BFSIZE]; |
harada_jpms | 1:d2ebf1c3ae5a | 794 | |
harada_jpms | 1:d2ebf1c3ae5a | 795 | sprintf( buf, "L %8X %8X\r", id_LH, id_LL ); |
harada_jpms | 1:d2ebf1c3ae5a | 796 | |
harada_jpms | 1:d2ebf1c3ae5a | 797 | ble_write( buf ); |
harada_jpms | 1:d2ebf1c3ae5a | 798 | |
harada_jpms | 1:d2ebf1c3ae5a | 799 | pc.printf( "ISETL OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 800 | } |
harada_jpms | 1:d2ebf1c3ae5a | 801 | // コマンド ISETR |
harada_jpms | 1:d2ebf1c3ae5a | 802 | else if( cmd_no == 8 ) |
harada_jpms | 1:d2ebf1c3ae5a | 803 | { |
harada_jpms | 1:d2ebf1c3ae5a | 804 | int id_RH = atoh(pc_str[1], strlen(pc_str[1])); |
harada_jpms | 1:d2ebf1c3ae5a | 805 | int id_RL = atoh(pc_str[2], strlen(pc_str[2])); |
harada_jpms | 1:d2ebf1c3ae5a | 806 | |
harada_jpms | 1:d2ebf1c3ae5a | 807 | char buf[BFSIZE]; |
harada_jpms | 1:d2ebf1c3ae5a | 808 | |
harada_jpms | 1:d2ebf1c3ae5a | 809 | sprintf( buf, "R %8X %8X\r", id_RH, id_RL ); |
harada_jpms | 1:d2ebf1c3ae5a | 810 | |
harada_jpms | 1:d2ebf1c3ae5a | 811 | ble_write( buf ); |
harada_jpms | 1:d2ebf1c3ae5a | 812 | |
harada_jpms | 1:d2ebf1c3ae5a | 813 | pc.printf( "ISETR OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 814 | } |
harada_jpms | 1:d2ebf1c3ae5a | 815 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 816 | else if( cmd_no == 9 ) |
harada_jpms | 1:d2ebf1c3ae5a | 817 | { |
harada_jpms | 1:d2ebf1c3ae5a | 818 | |
harada_jpms | 1:d2ebf1c3ae5a | 819 | } |
harada_jpms | 1:d2ebf1c3ae5a | 820 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 821 | else if( cmd_no == 10 ) |
harada_jpms | 1:d2ebf1c3ae5a | 822 | { |
harada_jpms | 1:d2ebf1c3ae5a | 823 | |
harada_jpms | 1:d2ebf1c3ae5a | 824 | } |
harada_jpms | 1:d2ebf1c3ae5a | 825 | // コマンド CONNECT |
harada_jpms | 1:d2ebf1c3ae5a | 826 | else if( cmd_no == 11 ) |
harada_jpms | 1:d2ebf1c3ae5a | 827 | { |
harada_jpms | 1:d2ebf1c3ae5a | 828 | // CONNECT処理追加 |
harada_jpms | 1:d2ebf1c3ae5a | 829 | |
harada_jpms | 1:d2ebf1c3ae5a | 830 | pc.printf( "CONNECT OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 831 | } |
harada_jpms | 1:d2ebf1c3ae5a | 832 | // コマンド DISC |
harada_jpms | 1:d2ebf1c3ae5a | 833 | else if( cmd_no == 12 ) |
harada_jpms | 1:d2ebf1c3ae5a | 834 | { |
harada_jpms | 1:d2ebf1c3ae5a | 835 | // DISC処理追加 |
harada_jpms | 1:d2ebf1c3ae5a | 836 | |
harada_jpms | 1:d2ebf1c3ae5a | 837 | pc.printf( "DISC OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 838 | } |
harada_jpms | 1:d2ebf1c3ae5a | 839 | // コマンド INTVL |
harada_jpms | 1:d2ebf1c3ae5a | 840 | else if( cmd_no == 13 ) |
harada_jpms | 1:d2ebf1c3ae5a | 841 | { |
harada_jpms | 1:d2ebf1c3ae5a | 842 | unsigned int interval = atow(pc_str[1], strlen(pc_str[1])); |
harada_jpms | 1:d2ebf1c3ae5a | 843 | |
harada_jpms | 1:d2ebf1c3ae5a | 844 | char buf[BFSIZE]; |
harada_jpms | 1:d2ebf1c3ae5a | 845 | sprintf( buf, "INTVL %d\r", interval ); |
harada_jpms | 1:d2ebf1c3ae5a | 846 | ble_write( buf ); |
harada_jpms | 1:d2ebf1c3ae5a | 847 | |
harada_jpms | 1:d2ebf1c3ae5a | 848 | pc.printf( "INTVL OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 849 | } |
harada_jpms | 1:d2ebf1c3ae5a | 850 | // コマンド NTC |
harada_jpms | 1:d2ebf1c3ae5a | 851 | else if( cmd_no == 14 ) |
harada_jpms | 1:d2ebf1c3ae5a | 852 | { |
harada_jpms | 1:d2ebf1c3ae5a | 853 | unsigned int not_touch_cnt = atow(pc_str[1], strlen(pc_str[1])); |
harada_jpms | 1:d2ebf1c3ae5a | 854 | |
harada_jpms | 1:d2ebf1c3ae5a | 855 | char buf[BFSIZE]; |
harada_jpms | 1:d2ebf1c3ae5a | 856 | sprintf( buf, "NTC %d\r", not_touch_cnt ); |
harada_jpms | 1:d2ebf1c3ae5a | 857 | ble_write( buf ); |
harada_jpms | 1:d2ebf1c3ae5a | 858 | |
harada_jpms | 1:d2ebf1c3ae5a | 859 | pc.printf( "NTC OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 860 | } |
harada_jpms | 1:d2ebf1c3ae5a | 861 | // コマンド OTC |
harada_jpms | 1:d2ebf1c3ae5a | 862 | else if( cmd_no == 15 ) |
harada_jpms | 1:d2ebf1c3ae5a | 863 | { |
harada_jpms | 1:d2ebf1c3ae5a | 864 | unsigned int touch_cnt = atow(pc_str[1], strlen(pc_str[1])); |
harada_jpms | 1:d2ebf1c3ae5a | 865 | |
harada_jpms | 1:d2ebf1c3ae5a | 866 | char buf[BFSIZE]; |
harada_jpms | 1:d2ebf1c3ae5a | 867 | sprintf( buf, "OTC %d\r", touch_cnt ); |
harada_jpms | 1:d2ebf1c3ae5a | 868 | ble_write( buf ); |
harada_jpms | 1:d2ebf1c3ae5a | 869 | |
harada_jpms | 1:d2ebf1c3ae5a | 870 | pc.printf( "OTC OK\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 871 | } |
harada_jpms | 1:d2ebf1c3ae5a | 872 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 873 | else if( cmd_no == 16 ) |
harada_jpms | 1:d2ebf1c3ae5a | 874 | { |
harada_jpms | 1:d2ebf1c3ae5a | 875 | |
harada_jpms | 1:d2ebf1c3ae5a | 876 | } |
harada_jpms | 1:d2ebf1c3ae5a | 877 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 878 | else if( cmd_no == 17 ) |
harada_jpms | 1:d2ebf1c3ae5a | 879 | { |
harada_jpms | 1:d2ebf1c3ae5a | 880 | |
harada_jpms | 1:d2ebf1c3ae5a | 881 | } |
harada_jpms | 1:d2ebf1c3ae5a | 882 | // コマンド IDGET |
harada_jpms | 1:d2ebf1c3ae5a | 883 | else if( cmd_no == 18 ) |
harada_jpms | 1:d2ebf1c3ae5a | 884 | { |
harada_jpms | 1:d2ebf1c3ae5a | 885 | ble_write( "IDGET\r\n" ); |
harada_jpms | 1:d2ebf1c3ae5a | 886 | } |
harada_jpms | 1:d2ebf1c3ae5a | 887 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 888 | else if( cmd_no == 19 ) |
harada_jpms | 1:d2ebf1c3ae5a | 889 | { |
harada_jpms | 1:d2ebf1c3ae5a | 890 | |
harada_jpms | 1:d2ebf1c3ae5a | 891 | } |
harada_jpms | 1:d2ebf1c3ae5a | 892 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 893 | else if( cmd_no == 20 ) |
harada_jpms | 1:d2ebf1c3ae5a | 894 | { |
harada_jpms | 1:d2ebf1c3ae5a | 895 | |
harada_jpms | 1:d2ebf1c3ae5a | 896 | } |
harada_jpms | 1:d2ebf1c3ae5a | 897 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 898 | else if( cmd_no == 21 ) |
harada_jpms | 1:d2ebf1c3ae5a | 899 | { |
harada_jpms | 1:d2ebf1c3ae5a | 900 | |
harada_jpms | 1:d2ebf1c3ae5a | 901 | } |
harada_jpms | 1:d2ebf1c3ae5a | 902 | // コマンド |
harada_jpms | 1:d2ebf1c3ae5a | 903 | else if( cmd_no == 22 ) |
harada_jpms | 1:d2ebf1c3ae5a | 904 | { |
harada_jpms | 1:d2ebf1c3ae5a | 905 | |
harada_jpms | 1:d2ebf1c3ae5a | 906 | } |
harada_jpms | 1:d2ebf1c3ae5a | 907 | else |
harada_jpms | 1:d2ebf1c3ae5a | 908 | { |
harada_jpms | 2:a88b0b4c4f37 | 909 | //pc.printf("CMD_ERR\r"); |
harada_jpms | 1:d2ebf1c3ae5a | 910 | } |
harada_jpms | 1:d2ebf1c3ae5a | 911 | } |
harada_jpms | 1:d2ebf1c3ae5a | 912 | } |
harada_jpms | 1:d2ebf1c3ae5a | 913 | } |
harada_jpms | 1:d2ebf1c3ae5a | 914 | } |
harada_jpms | 1:d2ebf1c3ae5a | 915 | } |
harada_jpms | 1:d2ebf1c3ae5a | 916 | |
harada_jpms | 1:d2ebf1c3ae5a | 917 | void ble_write(char* dat) |
harada_jpms | 1:d2ebf1c3ae5a | 918 | { |
harada_jpms | 1:d2ebf1c3ae5a | 919 | uint8_t linebf_irq[BFSIZE]; |
harada_jpms | 1:d2ebf1c3ae5a | 920 | |
harada_jpms | 1:d2ebf1c3ae5a | 921 | sprintf( (char *)linebf_irq, "%s", dat); |
harada_jpms | 1:d2ebf1c3ae5a | 922 | |
harada_jpms | 1:d2ebf1c3ae5a | 923 | if( connected2server == true ) |
harada_jpms | 1:d2ebf1c3ae5a | 924 | { |
harada_jpms | 1:d2ebf1c3ae5a | 925 | uartTXCharacteristic.write(NUM_ONCE, linebf_irq); |
harada_jpms | 1:d2ebf1c3ae5a | 926 | } |
harada_jpms | 1:d2ebf1c3ae5a | 927 | } |