teamALI / Mbed 2 deprecated RubyHwTest2

Dependencies:   mbed

Committer:
duchung2603
Date:
Mon Aug 20 07:37:12 2018 +0000
Revision:
1:c002cfb55315
Parent:
0:a8339cee32b7
Child:
2:ff609fd0c51a
2version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
duchung2603 0:a8339cee32b7 1 #include "uart.h"
duchung2603 0:a8339cee32b7 2 #include "mbed.h"
duchung2603 0:a8339cee32b7 3 #include <string.h>
duchung2603 0:a8339cee32b7 4 #include "motor.h"
duchung2603 0:a8339cee32b7 5
duchung2603 0:a8339cee32b7 6 #define BS 0x08
duchung2603 0:a8339cee32b7 7 #define CR 0x0D
duchung2603 0:a8339cee32b7 8 #define LF 0x0A
duchung2603 0:a8339cee32b7 9
duchung2603 0:a8339cee32b7 10 static Serial sp(p9, p10, 115200) ;// シリアルポートインスタンス:tx , rx ,ボーレート
duchung2603 0:a8339cee32b7 11 static char msgBuf[63] ={0,} ;//
duchung2603 0:a8339cee32b7 12 static UCHAR bp =0 ;// バッファポインタ
duchung2603 0:a8339cee32b7 13
duchung2603 1:c002cfb55315 14 //int value = 0;
duchung2603 0:a8339cee32b7 15 DigitalOut led4(LED4);
duchung2603 0:a8339cee32b7 16
duchung2603 0:a8339cee32b7 17 //-------------------------------------------------------------
duchung2603 0:a8339cee32b7 18 //受信コマンドバッファクリア
duchung2603 0:a8339cee32b7 19 //-------------------------------------------------------------
duchung2603 0:a8339cee32b7 20 static void bufClear(){
duchung2603 0:a8339cee32b7 21 //コマンドバッファをゼロクリア
duchung2603 0:a8339cee32b7 22 memset(&msgBuf[0] , 0x00 , sizeof(msgBuf) );
duchung2603 0:a8339cee32b7 23 //バッファポインタを先頭に戻す
duchung2603 0:a8339cee32b7 24 bp=0;
duchung2603 0:a8339cee32b7 25 }
duchung2603 0:a8339cee32b7 26
duchung2603 0:a8339cee32b7 27 //-------------------------------------------------------------
duchung2603 0:a8339cee32b7 28 //バックスペースが来た時の処理
duchung2603 0:a8339cee32b7 29 //-------------------------------------------------------------
duchung2603 0:a8339cee32b7 30 static void xBs(){
duchung2603 0:a8339cee32b7 31 msgBuf[bp] = 0x0; //1文字消す
duchung2603 0:a8339cee32b7 32 if(bp>0) bp--; //インデックスを戻す(先頭でなければ)
duchung2603 0:a8339cee32b7 33 }
duchung2603 0:a8339cee32b7 34 //-------------------------------------------------------------
duchung2603 0:a8339cee32b7 35 //エンターを押したときの処理
duchung2603 0:a8339cee32b7 36 //-------------------------------------------------------------
duchung2603 0:a8339cee32b7 37 static void xEnter(){
duchung2603 0:a8339cee32b7 38 sp.printf("\r\n");
duchung2603 0:a8339cee32b7 39 sp.printf("%s\r\n" , msgBuf);
duchung2603 0:a8339cee32b7 40
duchung2603 0:a8339cee32b7 41 if(strcmp(msgBuf , "i")==0 ){
duchung2603 1:c002cfb55315 42 //value += 10;
duchung2603 1:c002cfb55315 43 //if (value<250){
duchung2603 1:c002cfb55315 44 motorUp();
duchung2603 1:c002cfb55315 45 //}
duchung2603 0:a8339cee32b7 46 }
duchung2603 0:a8339cee32b7 47
duchung2603 0:a8339cee32b7 48 if(strcmp(msgBuf , "x")==0 ){ motorStop(value);}
duchung2603 0:a8339cee32b7 49
duchung2603 0:a8339cee32b7 50 //バッファクリア
duchung2603 0:a8339cee32b7 51 bufClear();
duchung2603 0:a8339cee32b7 52 }
duchung2603 0:a8339cee32b7 53
duchung2603 0:a8339cee32b7 54 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
duchung2603 0:a8339cee32b7 55 // UART 受信割り込みのコールバック
duchung2603 0:a8339cee32b7 56 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
duchung2603 0:a8339cee32b7 57 void rxCallback()
duchung2603 0:a8339cee32b7 58 {
duchung2603 0:a8339cee32b7 59 led4 = !led4;
duchung2603 0:a8339cee32b7 60 //1Byte抜き出す
duchung2603 0:a8339cee32b7 61 UCHAR buf = sp.getc();
duchung2603 0:a8339cee32b7 62
duchung2603 0:a8339cee32b7 63 //エコーバック
duchung2603 0:a8339cee32b7 64 sp.putc(buf);
duchung2603 0:a8339cee32b7 65
duchung2603 0:a8339cee32b7 66 //
duchung2603 0:a8339cee32b7 67 switch(buf){
duchung2603 0:a8339cee32b7 68 case BS : xBs(); break;
duchung2603 0:a8339cee32b7 69 case CR : break;
duchung2603 0:a8339cee32b7 70 case LF : xEnter(); break; // Enter
duchung2603 0:a8339cee32b7 71 default :
duchung2603 0:a8339cee32b7 72 //コマンドバッファ格納
duchung2603 0:a8339cee32b7 73 msgBuf[bp] = buf;
duchung2603 0:a8339cee32b7 74 bp++;
duchung2603 0:a8339cee32b7 75 break;
duchung2603 0:a8339cee32b7 76 }
duchung2603 0:a8339cee32b7 77 }
duchung2603 0:a8339cee32b7 78
duchung2603 0:a8339cee32b7 79 //=============================================================
duchung2603 0:a8339cee32b7 80 //初期化
duchung2603 0:a8339cee32b7 81 //=============================================================
duchung2603 0:a8339cee32b7 82 void uartInit(){
duchung2603 0:a8339cee32b7 83 //受信割り込みハンドラ登録
duchung2603 0:a8339cee32b7 84 sp.attach(rxCallback, Serial::RxIrq);
duchung2603 0:a8339cee32b7 85
duchung2603 0:a8339cee32b7 86 //シリアルポート開通確認メッセージ表示
duchung2603 0:a8339cee32b7 87 sp.printf("***************\n");
duchung2603 0:a8339cee32b7 88 sp.printf("start!!!!!\n");
duchung2603 0:a8339cee32b7 89 sp.printf("***************\n");
duchung2603 0:a8339cee32b7 90
duchung2603 0:a8339cee32b7 91 //受信コマンドバッファクリア
duchung2603 0:a8339cee32b7 92 bufClear();
duchung2603 0:a8339cee32b7 93 }
duchung2603 0:a8339cee32b7 94