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