OSDN Git Service

タッチセンサー、Bluetooth周辺をリファクタリング。main.cppに直書きを止めた。
authorkouichi <kkoba@kirari-kobayashi.(none)>
Mon, 16 Sep 2013 07:56:25 +0000 (16:56 +0900)
committerkouichi <kkoba@kirari-kobayashi.(none)>
Mon, 16 Sep 2013 07:56:25 +0000 (16:56 +0900)
ETUtility.cpp [new file with mode: 0644]
ETUtility.h [new file with mode: 0644]
Makefile
main.cpp

diff --git a/ETUtility.cpp b/ETUtility.cpp
new file mode 100644 (file)
index 0000000..b38a653
--- /dev/null
@@ -0,0 +1,72 @@
+#include "ETUtility.h"
+#include "Lcd.h"
+#include "Bluetooth.h"
+#include "BTConnection.h"
+#include "Clock.h"
+#include "TouchSensor.h"
+#include "ETTailControl.h"
+
+using namespace ecrobot;
+
+//#define PASS_KEY
+/* 固有のパスキーを設定してください */
+const char* ecrobot::ETBluetoothString::PASS_KEY = "1234"; 
+/* リモートスタートコマンド */
+const char ecrobot::ETBluetoothString::CMD_START = '1';
+/* リモートストップコマンド */    
+const char ecrobot::ETBluetoothString::CMD_STOP  = '9';    
+
+bool ETUtilityStuff::IsBluetoothReceived(char cmd)
+{
+       int i;
+    /* Bluetooth通信用データ受信バッファ */
+    char rx_buf[BT_MAX_RX_BUF_SIZE];
+       unsigned int rx_len;
+       unsigned char come = 0;
+
+       for (i=0; i<BT_MAX_RX_BUF_SIZE; i++)
+       {
+               rx_buf[i] = 0; /* 受信バッファをクリア */
+       }
+
+       rx_len = ecrobot_read_bt(rx_buf, 0, BT_MAX_RX_BUF_SIZE);
+       if (rx_len > 0)
+       {
+               /* 受信データあり */
+               if (rx_buf[0] == cmd)
+               {
+                       come = 1; /* コマンド受信 */
+               }
+               ecrobot_send_bt(rx_buf, 0, rx_len); /* 受信データをエコーバック */
+       }
+
+       return (BOOL)come;
+}
+
+bool ETUtilityStuff::Istouched(ecrobot::TouchSensor& touch,ecrobot::ETTailControl& tailcontroler)
+{
+    tailcontroler.Control();
+    if(touch.isPressed()){
+           systick_wait_ms(30);
+        while(touch.isPressed()){
+            tailcontroler.Control();
+        }
+           systick_wait_ms(30);
+        return true;
+    }
+    return false;
+}
+
+void ETUtilityStuff::Waitstart(ecrobot::TouchSensor& touch,ecrobot::ETTailControl& tailcontroler,char cmd)
+{
+    /* タッチセンサが押されるまで、待ち続ける */
+    while(1){
+           if (Istouched(touch,tailcontroler)){
+                   break;
+           }
+           if (IsBluetoothReceived(ETBluetoothString::CMD_START)){                                                                     
+                   break;
+           }
+    }
+}
+
diff --git a/ETUtility.h b/ETUtility.h
new file mode 100644 (file)
index 0000000..3362b0e
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef ET_UTILITYSTUFF_H\r
+#define ET_UTILITYSTUFF\r
+\r
+namespace ecrobot{\r
+    class TouchSensor;\r
+    class ETTailControl;\r
+\r
+    struct ETBluetoothString{\r
+        static const char* PASS_KEY;\r
+        static const char CMD_START;\r
+        static const char CMD_STOP;\r
+    };\r
+\r
+    namespace ETUtilityStuff{\r
+        bool IsBluetoothReceived(char cmd);\r
+        bool Istouched(ecrobot::TouchSensor& touch,ecrobot::ETTailControl& tailcontroler);\r
+        void Waitstart(ecrobot::TouchSensor& touch,ecrobot::ETTailControl& tailcontroler,char cmd);\r
+    }\r
+}\r
+#endif /* ET_UTILITYSTUFF */\r
index c302026..3e70001 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,7 @@ context.cpp \
 runstate.cpp \
 exceptionstate.cpp \
 ETTailControl.cpp \
+ETUtility.cpp
 
 
 TARGET_SOURCES = tsprintf.c balancer_param.c
index 0711261..f49dc61 100644 (file)
--- a/main.cpp
+++ b/main.cpp
 #include "ETBalanceRunner.h"\r
 #include "ETTailControl.h"\r
 #include "context.h"\r
+#include "ETUtility.h"\r
 \r
 using namespace ecrobot;\r
 \r
-/* sample_c4\83}\83N\83\8d */\r
-#define DEVICE_NAME       "ET277"  /* Bluetooth\92Ê\90M\97p\83f\83o\83C\83X\96¼ */                                     /* \83`\81[\83\80ID\82É\95Ï\8dX\82µ\82Ä\82­\82¾\82³\82¢ */\r
-#define PASS_KEY          "1234" /* Bluetooth\92Ê\90M\97p\83p\83X\83L\81[ */                                         /* \8cÅ\97L\82Ì\83p\83X\83L\81[\82ð\90Ý\92è\82µ\82Ä\82­\82¾\82³\82¢ */\r
-#define CMD_START         '1'    /* \83\8a\83\82\81[\83g\83X\83^\81[\83g\83R\83}\83\93\83h */\r
-#define CMD_STOP          '9'    /* \83\8a\83\82\81[\83g\83X\83g\83b\83v\83R\83}\83\93\83h */\r
-\r
-\r
 extern "C"\r
 {\r
 #include "kernel.h"\r
@@ -84,87 +78,23 @@ void user_1ms_isr_type2(void)
     }\r
 }\r
 \r
-/* Bluetooth\92Ê\90M\97p\83f\81[\83^\8eó\90M\83o\83b\83t\83@ */\r
-static char rx_buf[BT_MAX_RX_BUF_SIZE];\r
-\r
-/*****************************************************************************\r
\8aÖ\90\94\96¼ : Bluetooth_isReceived\r
\88ø  \90\94 : \8eó\90M\91Ò\82¿\83R\83}\83\93\83h\r
\95Ô\82è\92l : 1(\83X\83^\81[\83g)/0(\91Ò\8b@)\r
\8aT  \97v : Bluetooth\92Ê\90M\82É\82æ\82é\83\8a\83\82\81[\83g\83X\83^\81[\83g\81B Tera Term\82È\82Ç\82Ì\83^\81[\83~\83i\83\8b\83\\83t\83g\82©\82ç\81A\r
-          ASCII\83R\81[\83h\82Å1\82ð\91\97\90M\82·\82é\82Æ\81A\83\8a\83\82\81[\83g\83X\83^\81[\83g\82·\82é\81B\r
- *****************************************************************************/\r
-BOOL Bluetooth_isReceived(char cmd)\r
-{\r
-       int i;\r
-       unsigned int rx_len;\r
-       unsigned char come = 0;\r
-\r
-       for (i=0; i<BT_MAX_RX_BUF_SIZE; i++)\r
-       {\r
-               rx_buf[i] = 0; /* \8eó\90M\83o\83b\83t\83@\82ð\83N\83\8a\83A */\r
-       }\r
-\r
-       rx_len = ecrobot_read_bt(rx_buf, 0, BT_MAX_RX_BUF_SIZE);\r
-       if (rx_len > 0)\r
-       {\r
-               /* \8eó\90M\83f\81[\83^\82 \82è */\r
-               if (rx_buf[0] == cmd)\r
-               {\r
-                       come = 1; /* \83R\83}\83\93\83h\8eó\90M */\r
-               }\r
-               ecrobot_send_bt(rx_buf, 0, rx_len); /* \8eó\90M\83f\81[\83^\82ð\83G\83R\81[\83o\83b\83N */\r
-       }\r
-\r
-       return (BOOL)come;\r
-}\r
-\r
-bool is_touched(TouchSensor& touch,ETTailControl& tailcontroler)\r
-{\r
-    tailcontroler.Control();\r
-    if(touch.isPressed()){\r
-           systick_wait_ms(30);\r
-        while(touch.isPressed()){\r
-            tailcontroler.Control();\r
-        }\r
-           systick_wait_ms(30);\r
-        return true;\r
-    }\r
-    return false;\r
-}\r
-\r
-\r
-void waitstart(TouchSensor& touch,ETTailControl& tailcontroler,char cmd)\r
-{\r
-    /* \83^\83b\83`\83Z\83\93\83T\82ª\89\9f\82³\82ê\82é\82Ü\82Å\81A\91Ò\82¿\91±\82¯\82é */\r
-    while(1){\r
-           if (is_touched(touch,tailcontroler)){\r
-                   break;\r
-           }\r
-           if (Bluetooth_isReceived(CMD_START)){                                                                       \r
-                   break;\r
-           }\r
-    }\r
-}\r
-\r
 TASK(TaskMain)\r
 {\r
     //bt.setFriendlyName(DEVICE_NAME);\r
        BTConnection btConnection(bt, lcd, nxt);\r
-       btConnection.connect(PASS_KEY);\r
+       btConnection.connect(ETBluetoothString::PASS_KEY);\r
        lcd.clear();\r
        lcd.putf("s", "Hello World");\r
        lcd.disp();\r
     tailcontroler.Init();\r
     tailcontroler.SetAngle(ETTailControl::ANGLE_STAND_UP);\r
 \r
-\r
        // TODO:\82Æ\82è\82 \82¦\82¸\82Ì\83L\83\83\83\8a\83u\83\8c\81[\83V\83\87\83\93\r
        static const int sum_num = 256;\r
        unsigned long sum = 0;\r
        char str[16+1];\r
        // \83W\83\83\83C\83\8d\r
-       while(!is_touched(touch,tailcontroler)) {}\r
+       while(!ETUtilityStuff::Istouched(touch,tailcontroler)) {}\r
        for(int i=0;i<sum_num;i++){\r
                sum += gyro.get();\r
                clock.wait(10);\r
@@ -178,7 +108,7 @@ TASK(TaskMain)
        speaker.playTone(1000,100,50);\r
 \r
        // \94\92\r
-       while(!is_touched(touch,tailcontroler)) {}\r
+       while(!ETUtilityStuff::Istouched(touch,tailcontroler)) {}\r
        sum = 0;\r
        for(int i=0;i<sum_num;i++){\r
                sum += light.getBrightness();\r
@@ -193,7 +123,7 @@ TASK(TaskMain)
        speaker.playTone(1200,100,50);\r
 \r
        // \8d\95\r
-       while(!is_touched(touch,tailcontroler)) {}\r
+       while(!ETUtilityStuff::Istouched(touch,tailcontroler)) {}\r
        sum = 0;\r
        for(int i=0;i<sum_num;i++){\r
                sum += light.getBrightness();\r
@@ -208,7 +138,7 @@ TASK(TaskMain)
        speaker.playTone(1400,100,50);\r
 \r
     //wait for touch or bluetooth.\r
-    waitstart(touch,tailcontroler,CMD_START);\r
+    ETUtilityStuff::Waitstart(touch,tailcontroler,ETBluetoothString::CMD_START);\r
 \r
        tailcontroler.SetAngle(ETTailControl::ANGLE_DRIVE);\r
     context.SetETLineTracer(&tracer);\r