test
Dependencies: BufferedSerial WatchdogTimer
Diff: main.cpp
- Revision:
- 1:d2ebf1c3ae5a
- Parent:
- 0:ad0d1afb4462
- Child:
- 2:a88b0b4c4f37
diff -r ad0d1afb4462 -r d2ebf1c3ae5a main.cpp --- a/main.cpp Fri Jun 01 06:26:08 2018 +0000 +++ b/main.cpp Fri Jun 01 07:44:22 2018 +0000 @@ -35,6 +35,28 @@ uint8_t uart_bf_len; volatile bool rx_isr_busy = false; +unsigned long mac_1; +unsigned long mac_2; +unsigned long mac_3; + +#define SIO_BUF_SIZE 256 /* SIO リングバッファーサイズ (1K byte) */ + +typedef struct { + unsigned short data_cnt; /* リング・バッファ データカウント */ + unsigned short rp; /* リング・バッファ Read ポインタ */ + unsigned short wp; /* リング・バッファ Write ポインタ */ + unsigned char buf[SIO_BUF_SIZE]; /* リング・バッファ */ +} ring_cnt_t; /* リング・バッファ 制御用構造体 */ + +ring_cnt_t pc_uart_buf; // 通信用リングバッファ +ring_cnt_t pc_cmd_buf; // 通信用コマンドバッファ + +ring_cnt_t ble_uart_buf; // 通信用リングバッファ +ring_cnt_t ble_cmd_buf; // 通信用コマンドバッファ + +int timer_cnt; +int timer_cnt_d; + // Function prototypes -------------------------------------------------------- // BLE void advertisementCallback(const Gap::AdvertisementCallbackParams_t *); @@ -48,11 +70,24 @@ bool mac_equals(const Gap::Address_t, const Gap::Address_t); int get_board_index(const Gap::Address_t); +void uart_init(); +void main_timer_proc(); +unsigned int atow(char *str,unsigned short leng); +unsigned long atoh(char *str,unsigned short leng); +void ble_rs232c_cmd(); +void pc_rs232c_cmd(); +void ble_write(char* dat); + //------------------------------------------------------------------------------ // Control Program //------------------------------------------------------------------------------ int main(void) { + uart_init(); + + // メインタイマー設定(1ms) + main_timer.attach(&main_timer_proc, 0.001); + // opening message pc.printf("UART Communication / Client(Central) side\r\n"); pc.printf(" need Server module (run BLE_Uart_Server program)\r\n"); @@ -64,6 +99,8 @@ ble_uart.gattClient().onHVX(onReceivedDataFromDeviceCallback); ble_uart.gap().setScanParams(500, 450); ble_uart.gap().startScan(advertisementCallback); + + while(true) { // allow notifications from Server(Peripheral) if (foundUartRXCharacteristic && @@ -78,14 +115,15 @@ sizeof(uint16_t), reinterpret_cast<const uint8_t *>(&value) ); - - if (received_uart_dat == true) { - received_uart_dat = false; - for(int i = 0; i < uart_bf_len; i++) { - //pc.printf("%c", uart_buffer[i]); - pc.putc(uart_buffer[i]); - } } + + // タイマーカウント更新時(1ms以上経過している) + if( timer_cnt != timer_cnt_d ) + { + timer_cnt_d = timer_cnt; + + ble_rs232c_cmd(); + pc_rs232c_cmd(); } ble_uart.waitForEvent(); @@ -235,3 +273,652 @@ ble_uart.gap().startScan(advertisementCallback);// restart scan } } + +/****************************************************************************/ +/* 関数名 : uart_init */ +/* 概要 : 通信初期化 */ +/* 作成者 : JPMS H.Harada */ +/* 作成日 : 2017.08.30 */ +/****************************************************************************/ +void uart_init() +{ + int k; + + // 初期化時のごみを出力 + while( pc.readable() ) + { + pc.getc(); + } + + // UART バッファ初期化 /////////////////////////////////////////////// + pc_uart_buf.data_cnt = 0; + pc_uart_buf.rp = 0; + pc_uart_buf.wp = 0; + + ble_uart_buf.data_cnt = 0; + ble_uart_buf.rp = 0; + ble_uart_buf.wp = 0; + + for(k=0;k<SIO_BUF_SIZE;k++) { + pc_uart_buf.buf[k] = 0; + ble_uart_buf.buf[k] = 0; + } + + // コマンド バッファ初期化 /////////////////////////////////////////////// + pc_cmd_buf.data_cnt = 0; + pc_cmd_buf.rp = 0; + pc_cmd_buf.wp = 0; + + ble_cmd_buf.data_cnt = 0; + ble_cmd_buf.rp = 0; + ble_cmd_buf.wp = 0; + + for(k=0;k<SIO_BUF_SIZE;k++) { + pc_cmd_buf.buf[k] = 0; + ble_cmd_buf.buf[k] = 0; + } + + ///////////////////////////////////////////////////////////////// +} + +/****************************************************************************/ +/* 関数名 : main_timer_proc */ +/* 概要 : メインタイマ処理 */ +/* 作成者 : JPMS H.Harada */ +/* 作成日 : 2018.02.22 */ +/****************************************************************************/ +void main_timer_proc() +{ + timer_cnt++; // タイマカウントを加算 +} + +/****************************************************************************/ +/* 関数名 : atow */ +/* 概要 : アスキー変換処理 */ +/* 作成者 : JPMS H.Harada */ +/* 作成日 : 2017.08.30 */ +/****************************************************************************/ +unsigned int atow(char *str,unsigned short leng) +{ + register unsigned short i; + unsigned int ret_data = 0; + + /*---------------- データー長エラー -----------------*/ +// if(leng >= 11) +// { +// ret_data = ATOW_ERROR; /* データエラー */ +// } + /*---------------- データー長 OK ------------------*/ +// else +// { + i = leng; + + while(i) + { + ret_data *= 10; /* 10倍 */ + + /*------ データ '0' ~ '9' 範囲内 -----*/ + if( (*str >= '0') && (*str <= '9') ) + { + ret_data += (*str) - '0'; + str++; + } + /*------ データ '0' ~ '9' 範囲外 -----*/ + else + { +// ret_data = ATOW_ERROR; /* データエラー */ + break; + } + i--; + } + /*-------------- データオバーフロー ----------*/ +// if(ret_data & 0x8000) +// { +// ret_data = ATOW_ERROR; /* データエラー */ +// } +// } + return(ret_data); +} + +/****************************************************************************/ +/* 関数名 : atoh */ +/* 概要 : アスキー変換処理 */ +/* 作成者 : JPMS H.Harada */ +/* 作成日 : 2017.08.30 */ +/****************************************************************************/ +unsigned long atoh(char *str,unsigned short leng) +{ + register unsigned short i; + unsigned long ret_data = 0; + + /*---------------- データー長エラー -----------------*/ +// if(leng >= 11) +// { +// ret_data = ATOW_ERROR; /* データエラー */ +// } + /*---------------- データー長 OK ------------------*/ +// else +// { + i = leng; + + while(i) + { + ret_data *= 16; /* 16倍 */ + + /*------ データ '0' ~ '9' 範囲内 -----*/ + if( (*str >= '0') && (*str <= '9') ) + { + ret_data += (*str) - '0'; + str++; + } + /*------ データ 'A' ~ 'F' 範囲内 -----*/ + else if( (*str >= 'A') && (*str <= 'F') ) + { + ret_data += (*str) - 'A' + 10; + str++; + } + /*------ データ '0' ~ '9' 範囲外 -----*/ + else + { +// ret_data = ATOW_ERROR; /* データエラー */ + break; + } + i--; + } + /*-------------- データオバーフロー ----------*/ +// if(ret_data & 0x8000) +// { +// ret_data = ATOW_ERROR; /* データエラー */ +// } +// } + return(ret_data); +} + +char ble_str[32][10]; +char ble_str2[10][8] = { "P ", "U ", " ", "RESP ", " ", " ", " ", " ", " ", " " }; + +/****************************************************************************/ +/* 関数名 : rs232c_cmd */ +/* 概要 : 通信コマンド処理 */ +/* 作成者 : JPMS H.Harada */ +/* 作成日 : 2017.08.30 */ +/****************************************************************************/ +void ble_rs232c_cmd() +{ + unsigned int c, i, j; + int rdat; + + for( i=0; i<uart_bf_len; i++ ) + { + ble_uart_buf.buf[ ble_uart_buf.wp ] = uart_buffer[i]; + ble_uart_buf.data_cnt++; + + if( ble_uart_buf.wp == SIO_BUF_SIZE-1 ) + { + ble_uart_buf.wp = 0; + } + else + { + ble_uart_buf.wp++; + } + } + + if( ble_uart_buf.data_cnt > 0 ) + { + unsigned int len = ble_uart_buf.data_cnt; + for( c=0; c<len; c++ ) + { + rdat = ble_uart_buf.buf[ble_uart_buf.rp]; + ble_uart_buf.data_cnt--; + + if( ble_uart_buf.rp == SIO_BUF_SIZE-1 ) + { + ble_uart_buf.rp = 0; + } + else + { + ble_uart_buf.rp++; + } + + if( rdat != '\n' ) + { + ble_cmd_buf.buf[ble_cmd_buf.data_cnt++] = rdat; + } + + if (rdat == '\r') + { + int k = 0; + int l = 0; + for (i=0;i<ble_cmd_buf.data_cnt;i++) + { + if( ble_cmd_buf.buf[i] == ' ' || + ble_cmd_buf.buf[i] == '\r' || + ble_cmd_buf.buf[i] == '\t' ) + { + ble_str[k++][l] = '\0'; + l = 0; + } + else + { + ble_str[k][l++] = ble_cmd_buf.buf[i]; + } + } + + if( ble_cmd_buf.data_cnt == 1 ) + { + k = 0; + } + + ble_cmd_buf.data_cnt = 0; + ble_cmd_buf.buf[0] = '\0'; + + if (k > 0) + { + int ret; + int cmd_no = 0xFF; + + for( j=0; j<10; j++ ) + { + ret = 1; + + if( strlen(ble_str[0]) > 0 ) + { + for( i=0; i<strlen(ble_str[0]); i++ ) + { + if( ble_str[0][i] != ble_str2[j][i] ) + { + ret = 0; + } + } + + if( ret ) + { + cmd_no = j; + break; + } + } + } + + // コマンド P + if( cmd_no == 0 ) + { + int data_cnt = atow(ble_str[1], strlen(ble_str[1])); + int left = atow(ble_str[2], strlen(ble_str[2])); + int right = atow(ble_str[3], strlen(ble_str[3])); + + pc.printf( "POINT %4d %d %d\r", data_cnt, left, right ); + } + // コマンド U + else if( cmd_no == 1 ) + { + unsigned int high = atoh(ble_str[1], strlen(ble_str[1])); + unsigned int low = atoh(ble_str[2], strlen(ble_str[2])); + + pc.printf( "UID %08X %08X\r\n", high, low ); + } + // コマンド + else if( cmd_no == 2 ) + { + + } + // コマンド RESP + else if( cmd_no == 3 ) + { + pc.printf( "RESP\r" ); + } + // コマンド + else if( cmd_no == 4 ) + { + + } + // コマンド + else if( cmd_no == 5 ) + { + + } + + // コマンド + else if( cmd_no == 6 ) + { + + } + + // コマンド + else if( cmd_no == 7 ) + { + + } + // コマンド + else if( cmd_no == 8 ) + { + + } + // コマンド + else if( cmd_no == 9 ) + { + + } + else + { + pc.printf("CMD_ERR\r\n"); + } + } + } + } + } +} + +#define CMD_NUM 24 + +char pc_str[16][10]; +char pc_str2[CMD_NUM][8] = { "START ", // 0 + "STOP ", // 1 + " ", // 2 + "MSET ", // 3 + " ", // 4 + " ", // 5 + " ", // 6 + "ISETL ", // 7 + "ISETR ", // 8 + " ", // 9 + " ", // 10 + "CONNECT", // 11 + "DISCO ", // 12 + "INTVL ", // 13 + "NTC ", // 14 + "OTC ", // 15 + " ", // 16 + " ", // 17 + "IDGET ", // 18 + " ", // 19 + " ", // 20 + " ", // 21 + " ", // 22 + " "}; // 23 + +/****************************************************************************/ +/* 関数名 : rs232c_cmd */ +/* 概要 : 通信コマンド処理 */ +/* 作成者 : JPMS H.Harada */ +/* 作成日 : 2017.08.30 */ +/****************************************************************************/ +void pc_rs232c_cmd() +{ + unsigned int c, i, j; + int rdat; + + while( pc.readable() ) + { + pc_uart_buf.buf[pc_uart_buf.wp] = pc.getc(); + pc_uart_buf.data_cnt++; + + if( pc_uart_buf.wp == SIO_BUF_SIZE-1 ) + { + pc_uart_buf.wp = 0; + } + else + { + pc_uart_buf.wp++; + } + } + + if( pc_uart_buf.data_cnt > 0 ) + { + unsigned int len = pc_uart_buf.data_cnt; + for( c=0; c<len; c++ ) + { + rdat = pc_uart_buf.buf[pc_uart_buf.rp]; + pc_uart_buf.data_cnt--; + + if( pc_uart_buf.rp == SIO_BUF_SIZE-1 ) + { + pc_uart_buf.rp = 0; + } + else + { + pc_uart_buf.rp++; + } + + if( rdat != '\n' ) + { + pc_cmd_buf.buf[pc_cmd_buf.data_cnt++] = rdat; + } + + if( rdat == '\r' ) + { + int k = 0; + int l = 0; + for (i=0;i<pc_cmd_buf.data_cnt;i++) + { + if( pc_cmd_buf.buf[i] == ' ' || + pc_cmd_buf.buf[i] == '\r' || + pc_cmd_buf.buf[i] == '\t' ) + { + pc_str[k++][l] = '\0'; + l = 0; + } + else + { + pc_str[k][l++] = pc_cmd_buf.buf[i]; + } + } + + if( pc_cmd_buf.data_cnt == 1 ) + { + k = 0; + } + + pc_cmd_buf.data_cnt = 0; + pc_cmd_buf.buf[0] = '\0'; + + if (k > 0) + { + int ret; + int cmd_no = 0xFF; + + for( j=0; j<CMD_NUM; j++ ) + { + ret = 1; + + if( strlen(pc_str[0]) > 0 ) + { + for( i=0; i<strlen(pc_str[0]); i++ ) + { + if( pc_str[0][i] != pc_str2[j][i] ) + { + ret = 0; + } + } + + if( ret ) + { + cmd_no = j; + break; + } + } + } + + // コマンド START + if (cmd_no == 0) + { + ble_write( "START\r" ); + + pc.printf( "START OK\r\n" ); + } + // コマンド STOP + else if( cmd_no == 1 ) + { + ble_write( "STOP\r" ); + + pc.printf( "STOP OK\r\n" ); + } + // コマンド + else if (cmd_no == 2) + { + + } + // コマンド MSET + else if( cmd_no == 3 ) + { + mac_1 = atoh(pc_str[1], strlen(pc_str[1])); + mac_2 = atoh(pc_str[2], strlen(pc_str[2])); + mac_3 = atoh(pc_str[3], strlen(pc_str[3])); + + pc.printf( "MSET OK\r\n" ); + } + // コマンド + else if( cmd_no == 4 ) + { + + } + // コマンド + else if( cmd_no == 5 ) + { + + } + // コマンド + else if( cmd_no == 6 ) + { + + } + // コマンド ISETL + else if( cmd_no == 7 ) + { + int id_LH = atoh(pc_str[1], strlen(pc_str[1])); + int id_LL = atoh(pc_str[2], strlen(pc_str[2])); + + char buf[BFSIZE]; + + sprintf( buf, "L %8X %8X\r", id_LH, id_LL ); + + ble_write( buf ); + + pc.printf( "ISETL OK\r\n" ); + } + // コマンド ISETR + else if( cmd_no == 8 ) + { + int id_RH = atoh(pc_str[1], strlen(pc_str[1])); + int id_RL = atoh(pc_str[2], strlen(pc_str[2])); + + char buf[BFSIZE]; + + sprintf( buf, "R %8X %8X\r", id_RH, id_RL ); + + ble_write( buf ); + + pc.printf( "ISETR OK\r\n" ); + } + // コマンド + else if( cmd_no == 9 ) + { + + } + // コマンド + else if( cmd_no == 10 ) + { + + } + // コマンド CONNECT + else if( cmd_no == 11 ) + { + // CONNECT処理追加 + + pc.printf( "CONNECT OK\r\n" ); + } + // コマンド DISC + else if( cmd_no == 12 ) + { + // DISC処理追加 + + pc.printf( "DISC OK\r\n" ); + } + // コマンド INTVL + else if( cmd_no == 13 ) + { + unsigned int interval = atow(pc_str[1], strlen(pc_str[1])); + + char buf[BFSIZE]; + sprintf( buf, "INTVL %d\r", interval ); + ble_write( buf ); + + pc.printf( "INTVL OK\r\n" ); + } + // コマンド NTC + else if( cmd_no == 14 ) + { + unsigned int not_touch_cnt = atow(pc_str[1], strlen(pc_str[1])); + + char buf[BFSIZE]; + sprintf( buf, "NTC %d\r", not_touch_cnt ); + ble_write( buf ); + + pc.printf( "NTC OK\r\n" ); + } + // コマンド OTC + else if( cmd_no == 15 ) + { + unsigned int touch_cnt = atow(pc_str[1], strlen(pc_str[1])); + + char buf[BFSIZE]; + sprintf( buf, "OTC %d\r", touch_cnt ); + ble_write( buf ); + + pc.printf( "OTC OK\r\n" ); + } + // コマンド + else if( cmd_no == 16 ) + { + + } + // コマンド + else if( cmd_no == 17 ) + { + + } + // コマンド IDGET + else if( cmd_no == 18 ) + { + ble_write( "IDGET\r\n" ); + } + // コマンド + else if( cmd_no == 19 ) + { + + } + // コマンド + else if( cmd_no == 20 ) + { + + } + // コマンド + else if( cmd_no == 21 ) + { + + } + // コマンド + else if( cmd_no == 22 ) + { + + } + else + { + pc.printf("CMD_ERR\r"); + } + } + } + } + } +} + +void ble_write(char* dat) +{ + uint8_t linebf_irq[BFSIZE]; + + sprintf( (char *)linebf_irq, "%s", dat); + + if( connected2server == true ) + { + uartTXCharacteristic.write(NUM_ONCE, linebf_irq); + } +} \ No newline at end of file