Communication Class and Controller Class (ControllerForMbed Class)

Dependencies:   SoftPWM

Revision:
0:a33375289d79
Child:
1:6633661058ec
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 22 11:15:12 2021 +0000
@@ -0,0 +1,149 @@
+//CommunicationMonitoringクラスの動作確認用プログラム
+//宇井コンのmbed環境での動作確認をするプログラム(ControllerForMbed内で実装し,controllerクラスと区別している)
+//contoller(conrollerForMbed)クラス内のタイムアウト機能を使わないようにしている
+
+#include "mbed.h"
+#include "platform/mbed_thread.h"
+#include "Controller.h"
+#include "ControllerForMbed.h"
+#include "CommunicationMonitoring.h"
+#include "define.h"
+ 
+#define INT_TIME 0.001
+#define INT_TIME_MS INT_TIME*1000
+ 
+Serial pc(USBTX, USBRX);
+
+#if CON_TYPE == CON_DS4
+    Controller con(P7_4,P7_5, 115200); //TXpin, RXpin, baudrateを設定
+#elif CON_TYPE == CON_ADACHI
+    ControllerForMbed conmbed(P7_4,P7_5, 115200);
+#endif
+
+CommunicationMonitoring commMonitor;
+int contoller_num;
+ 
+DigitalOut led_user(LED_USER);
+DigitalOut led_red(LED_RED);
+DigitalOut led_green(LED_GREEN);
+DigitalOut led_blue(LED_BLUE);
+DigitalOut pin_emergency(D0); //非常停止信号
+
+Ticker interrupt;
+bool flag_10ms = false;
+bool flag_1s = false;
+ 
+void interrupt_func()
+{
+    static int count_10ms = 0;
+    if(count_10ms++ > (INT_TIME_MS*10 - 1))
+    {
+        flag_10ms = true;
+        count_10ms = 0;
+        
+        static int count_1s = 0;
+        if(count_1s++ > (INT_TIME_MS*100 -1))
+        {
+            flag_1s = true;
+            count_1s = 0;
+        }
+    }
+}
+ 
+int main()
+{   
+    pc.baud(115200);
+    
+    /* 第一引数はタイムアウト時間[ms],第二引数はupdate関数の呼び出し周期[ms] */
+    #if CON_TYPE == CON_DS4
+        con.init(1000, INT_TIME_MS*10); //init関数を呼び出さなければタイムアウトの処理は行われない(available関数は常にtrueを返す)
+    #elif CON_TYPE == CON_ADACHI
+        conmbed.inti(1000, INT_TIME_MS*10);
+    #endif
+    contoller_num = commMonitor.init(1000, INT_TIME_MS*10);
+    
+    interrupt.attach(&interrupt_func, INT_TIME);
+    
+    while(1) 
+    {
+        if(flag_10ms)
+        {
+            #if CON_TYPE == CON_DS4
+                //con.update(); //main関数のflag内で呼び出す.
+                commMonitor.Monitoring(con.update(), contoller_num);
+                //if(con.available())
+                if(commMonitor.getAvailable(contoller_num))
+                {
+                    int buttonState = con.getButtonState();
+                    uint8_t joyRx = con.readJoyRXbyte();
+                    uint8_t joyRy = con.readJoyRYbyte();
+                    uint8_t joyLx = con.readJoyLXbyte();
+                    uint8_t joyLy = con.readJoyLYbyte();
+                    
+                    pc.printf("%d\t", buttonState);
+                    pc.printf("%d\t", joyRx);
+                    pc.printf("%d\t", joyRy);
+                    pc.printf("%d\t", joyLx);
+                    pc.printf("%d\r\n", joyLy);
+                    
+                    led_user.write(0);
+                    pin_emergency.write(1); //非常停止を解除する
+                }
+                else
+                {
+                    pc.printf("disconnected\r\n");
+                    led_user.write(1);
+                    pin_emergency.write(0); //非常停止を作動させる
+                }
+
+            #elif CON_TYPE == CON_ADACHI
+                //conmbed.update(); //main関数のflag内で呼び出す.
+                commMonitor.Monitoring(conmbed.update(), contoller_num);
+                //if(conmbed.available())
+                if(commMonitor.getAvailable(contoller_num))
+                {
+                    int buttonState = conmbed.getButtonState();
+                    uint8_t joyRx = conmbed.readJoyRXbyte();
+                    uint8_t joyRy = conmbed.readJoyRYbyte();
+                    uint8_t joyLx = conmbed.readJoyLXbyte();
+                    uint8_t joyLy = conmbed.readJoyLYbyte();
+                    
+                    pc.printf("%d\t", buttonState);
+                    pc.printf("%d\t", joyRx);
+                    pc.printf("%d\t", joyRy);
+                    pc.printf("%d\t", joyLx);
+                    pc.printf("%d\r\n", joyLy);
+                    
+                    led_user.write(0);
+                    pin_emergency.write(1); //非常停止を解除する
+                }
+                else
+                {
+                    pc.printf("disconnected\r\n");
+                    led_user.write(1);
+                    pin_emergency.write(0); //非常停止を作動させる
+                }
+
+
+            #endif
+            
+            flag_10ms = false;
+        }
+        
+        if(flag_1s)
+        {
+            static int led_count = 0;
+
+            led_red.write(led_count & 0x01);
+            led_green.write(led_count & 0x02);
+            led_blue.write(led_count & 0x04);
+
+            led_count++;
+            if(led_count == 0x05) led_count = 0;
+
+            flag_1s = false;
+        }
+        
+        thread_sleep_for(1);
+    }
+}