Communication Class and Controller Class (ControllerForMbed Class)

Dependencies:   SoftPWM

Files at this revision

API Documentation at this revision

Comitter:
kikuchi8810
Date:
Thu Dec 23 08:56:26 2021 +0000
Parent:
1:6633661058ec
Commit message:
modified

Changed in this revision

Button.cpp Show annotated file Show diff for this revision Revisions of this file
Button.h Show annotated file Show diff for this revision Revisions of this file
SoftPWM.lib Show annotated file Show diff for this revision Revisions of this file
define.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 6633661058ec -r fd0c21600586 Button.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Button.cpp	Thu Dec 23 08:56:26 2021 +0000
@@ -0,0 +1,63 @@
+#include "Button.h"
+
+// コンストラクタ
+Button::Button(PinName pin):input(pin){
+    pre_button_state = get_button_state();
+    initial_flag = true;
+}
+
+bool Button::get_button_state(){
+    return input.read();
+}
+
+bool Button::button_rise(){
+    bool ret = 0;
+    bool button_state = get_button_state();
+    if(initial_flag){
+        if(button_state == 0){
+            pre_button_state = button_state;
+            initial_flag = false;
+        }
+    }else{
+        if(button_state && !pre_button_state){
+            initial_flag = true;
+            ret = 1;
+        }
+        pre_button_state = button_state;
+    }
+    return ret;
+}
+
+bool Button::button_fall(){
+    bool ret = 0;
+    bool button_state = get_button_state();
+    if(initial_flag){
+        if(button_state == 1){
+            pre_button_state = button_state;
+            initial_flag = false;
+        }
+    }else{
+        if(!button_state && pre_button_state){
+            initial_flag = true;
+            ret = 1;
+        }
+        pre_button_state = button_state;
+    }
+    return ret;
+}
+
+bool Button::button_changed(){
+    bool ret = 0;
+    bool button_state = get_button_state();
+    if(initial_flag){
+        pre_button_state = button_state;
+        initial_flag = false;
+    }else{
+        if(button_state != pre_button_state){
+            initial_flag = true;
+            ret = 1;
+        }
+        pre_button_state = button_state;
+    }
+    return ret;
+}
diff -r 6633661058ec -r fd0c21600586 Button.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Button.h	Thu Dec 23 08:56:26 2021 +0000
@@ -0,0 +1,23 @@
+#ifndef _BUTTON_H_INCLUDED
+#define _BUTTON_H_INCLUDED
+
+#include "mbed.h"
+#include "define.h"
+
+class Button{
+  public:
+  Button(PinName pin);
+
+  bool button_rise();
+  bool button_fall();
+  bool button_changed();
+  bool get_button_state();
+
+  private:
+  DigitalIn input;
+  
+  bool pre_button_state;
+  bool initial_flag;
+};
+
+#endif
diff -r 6633661058ec -r fd0c21600586 SoftPWM.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftPWM.lib	Thu Dec 23 08:56:26 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/komaida424/code/SoftPWM/#7918ce37626c
diff -r 6633661058ec -r fd0c21600586 define.h
--- a/define.h	Thu Dec 23 05:52:48 2021 +0000
+++ b/define.h	Thu Dec 23 08:56:26 2021 +0000
@@ -1,6 +1,8 @@
 #ifndef DEFINE_H
 #define DEFINE_H
 
+#include "platform/mbed_thread.h"
+
 struct ControllerData{
     unsigned int ButtonState;
     uint8_t RJoyX, RJoyY, LJoyX, LJoyY;
diff -r 6633661058ec -r fd0c21600586 main.cpp
--- a/main.cpp	Thu Dec 23 05:52:48 2021 +0000
+++ b/main.cpp	Thu Dec 23 08:56:26 2021 +0000
@@ -3,13 +3,15 @@
 //contoller(conrollerForMbed)クラス内のタイムアウト機能を使わないようにしている
 
 #include "mbed.h"
-#include "platform/mbed_thread.h"
+#include "SoftPWM.h" //ソフトフェアでPWM出力を行うクラス
+#include "Button.h"
 #include "Controller.h"// DS4のみ通信が確認できているバージョンで,プロジェクトに公開しているクラス
 #include "ControllerForMbed.h"//宇井コンとの通信をmbedで行えるように変更を加えたクラスでプロジェクトに公開していない.
 #include "CommunicationMonitoring.h" //複数のデバイスの通信状況を確認するクラス(通信監視クラス)
 #include "define.h" //コントローラクラスにのみ対応
  
 #define INT_TIME 0.001
+#define CONTROL_PERIOD INT_TIME*10.0
 #define INT_TIME_MS INT_TIME*1000
  
 Serial pc(USBTX, USBRX);
@@ -22,17 +24,41 @@
 
 CommunicationMonitoring commMonitor;
 int contoller_num; //通信監視クラス内でのコントローラのデバイス番号
- 
+
+/*ハードウェアのPWMは非対応*/
+SoftPWM led_red(LED_RED);
+SoftPWM led_green(LED_GREEN);
+SoftPWM led_blue(LED_BLUE);
+
 DigitalOut led_user(LED_USER);
-DigitalOut led_red(LED_RED);
-DigitalOut led_green(LED_GREEN);
-DigitalOut led_blue(LED_BLUE);
 DigitalOut pin_emergency(D0); //非常停止信号
 
+Button user_sw(USER_BUTTON0);
+
 Ticker interrupt;
 bool flag_10ms = false;
 bool flag_1s = false;
- 
+
+void RGB_LED() //RGBのLEDをいい感じに光らせる
+{
+    static float counts = 0.0;
+    counts += CONTROL_PERIOD; // ここで光る周期を変えられる(はず)
+
+    if(counts < CONTROL_PERIOD*100.0){
+        led_red.write(counts);
+        led_blue.write(1.0 - counts);
+        
+    }else if(counts < CONTROL_PERIOD*100.0*2.0){
+        led_green.write(counts - 1.0);
+        led_red.write(1.0*2.0 - counts);
+    }else if(counts < CONTROL_PERIOD*100.0*3.0){
+        led_blue.write(counts - 1.0*2.0);
+        led_green.write(1.0*3.0 - counts);
+    }else{
+        counts = 0.0;
+    }
+}
+
 void interrupt_func()
 {
     static int count_10ms = 0;
@@ -62,12 +88,22 @@
     #endif
     contoller_num = commMonitor.init(1000, INT_TIME_MS*10); //コントローラのタイムアウトと受信周期を設定し,デバイス番号を取得
     
+    led_red.write(0.0);
+    led_green.write(0.0);
+    led_blue.write(0.0);
+    led_user.write(1);
+    
+    while(!user_sw.button_fall())pc.printf("waiting\r\n"); //user_swが押されるまで待機
+    led_user.write(0);
+    
     interrupt.attach(&interrupt_func, INT_TIME);
     
     while(1) 
     {
         if(flag_10ms)
         {
+            RGB_LED();
+            
             #if CON_TYPE == CON_DS4
                 //con.update(); //main関数のflag内で呼び出す.
                 commMonitor.Monitoring(con.update(), contoller_num); //コントローラの通信状況を監視
@@ -98,9 +134,10 @@
 
             #elif CON_TYPE == CON_ADACHI
                 //conmbed.update(); //main関数のflag内で呼び出す.
-                commMonitor.Monitoring(conmbed.update(), contoller_num);
+                commMonitor.Monitoring(conmbed.update(), contoller_num); //コントローラの通信状況を監視
+                
                 //if(conmbed.available())
-                if(commMonitor.getAvailable(contoller_num))
+                if(commMonitor.getAvailable(contoller_num))//コントローラと通信しているかを確認
                 {
                     int buttonState = conmbed.getButtonState();
                     uint8_t joyRx = conmbed.readJoyRXbyte();
@@ -123,8 +160,6 @@
                     led_user.write(1);
                     pin_emergency.write(0); //非常停止を作動させる
                 }
-
-
             #endif
             
             flag_10ms = false;
@@ -132,18 +167,10 @@
         
         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;
-
+            // code here!
             flag_1s = false;
         }
         
-        thread_sleep_for(1);
+        thread_sleep_for(1); //眠らせないとなんか変
     }
 }