teamALI / Mbed 2 deprecated RubyHwTest2

Dependencies:   mbed

Committer:
msimeone234
Date:
Tue Aug 21 01:04:17 2018 +0000
Revision:
5:94b196231ecd
Parent:
3:6898c5bd7fef
now clean, with small motors included

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