test

Dependencies:   mbed XBee

Revision:
2:cb2bd28bb66d
Parent:
1:cb2c4d733c1b
--- a/Controller.cpp	Fri Jan 15 08:50:35 2021 +0000
+++ b/Controller.cpp	Fri Apr 16 09:34:17 2021 +0000
@@ -1,42 +1,50 @@
 #include "Controller.h"
 #include "mbed.h"
 #include "RawSerial.h"
+#include "XBee.h"
 
-DigitalOut myled4(LED4);
+DigitalOut myled1(LED1);
+XBeeResponse response = XBeeResponse();
+ZBRxResponse xbee_rx = ZBRxResponse();
 
-Controller::Controller(PinName tx,PinName rx,int baudrate):XBee3(tx,rx)
+XBee_APIMode::XBee_APIMode(PinName tx,PinName rx):xbee(tx,rx)
 {
-    XBee3.baud(baudrate);
     ButtonState = 0;
-    LJoyX = 128;
-    LJoyY = 128;
-    RJoyX = 128;
-    RJoyY = 128;
-    myled4 = 0;
+    myled1 = 0;
 }
 
-void Controller::update()
+XBee_TransparentMode::XBee_TransparentMode(PinName tx,PinName rx,int baudrate) :serialXbee3(tx,rx)
+{
+    serialXbee3.baud(baudrate);
+    serialXbee3.attach(this,&XBee_TransparentMode::update,Serial::RxIrq);
+    ButtonState = 0;
+    LJoyX = 127;
+    LJoyY = 127;
+    RJoyX = 127;
+    RJoyY = 127;
+    myled1 = 0;
+}
+
+void XBee_TransparentMode::update()
 {   
     unsigned int checksum;
-    //uint8_t tmp;
     char c;
-    char recv_msgs[10];
+    char recv_msgs[DATA_NUM_TRANS];
 
-    c = XBee3.getc();
+    c = serialXbee3.getc();
     if (c == '\n') {
 
         if (recv_num == 10) {
             checksum = 0;
 
-            for (int i = 0; i < 9; i++) {
+            for (int i = 0; i < (DATA_NUM_TRANS - 1); i++) {
                 recv_msgs[i] = recv_msgs[i] - 0x20;
                 checksum += (unsigned int)recv_msgs[i];
             }
 
             if ((checksum & 0x3F) == (recv_msgs[9] - 0x20)) {
-                //digitalWrite(PIN_LED_1, !digitalRead(PIN_LED_1));
-                myled4 = !myled4;
-                ButtonState = 0;//, LJoyX = 0, LJoyY = 0, RJoyX = 0, RJoyY = 0;
+                myled1 = !myled1;
+                ButtonState = 0;
 
                 ButtonState |= recv_msgs[0] & 0x3F;
                 ButtonState |= (recv_msgs[1] & 0x3F) << 6;
@@ -53,6 +61,8 @@
 
                 RJoyY = recv_msgs[7];
                 RJoyY |= (recv_msgs[8] & 0x03) << 6;
+                
+                preButtonState = ButtonState;
             }
         }
         recv_num = 0;
@@ -61,68 +71,58 @@
     }
 }
 
-void Controller::DRbegin()
-{
-    XBee3.attach(this,&Controller::update,Serial::RxIrq);
+void XBee_APIMode::begin(int baudrate){
+    xbee.begin(baudrate);
 }
-
-int8_t Controller::readButton(int buttonNum) const
+int XBee_APIMode::update()
 {
-    int8_t ret = 0;
-
-    if ((ButtonState >> (buttonNum - 1)) & 0x01) {
-        ret += 1;
+    uint8_t recv_val[DATA_NUM_API] = {};
+    xbee.readPacket();
+    static int getResponse_getApiId = 0;
+    int getResponse_isAvailable = xbee.getResponse().isAvailable();
+    if (getResponse_isAvailable) {
+        getResponse_getApiId = xbee.getResponse().getApiId();
+        if (getResponse_getApiId == ZB_RX_RESPONSE) {
+            xbee.getResponse().getZBRxResponse(xbee_rx);
+            for(int i = 0; i < DATA_NUM_API; i++){
+                recv_val[i] = (uint8_t)xbee_rx.getData(i);
+            }
+                preButtonState = ButtonState;
+                myled1 = !myled1;
+                
+                ButtonState = 0;
+                ButtonState |= recv_val[0] & 0xFF;
+                ButtonState |= (recv_val[1] & 0xFF) << 8;
+                ButtonState |= (recv_val[2] & 0xFF) << 16;
+                
+        }
+    }else{
+        //getResponse_getApiId = 9999;
     }
-    if (preButtonState >> (buttonNum - 1) & 0x01) {
-        ret -= 1;
-    }
-
-    return ret;
+    return getResponse_getApiId;
 }
-
-unsigned int Controller::getButtonState() const
+unsigned int XBee_TransparentMode::getButtonState()
+{
+    return ButtonState;
+}
+unsigned int XBee_APIMode::getButtonState()
 {
     return ButtonState;
 }
 
-/**コントローラの入力に変化があったらtrueを返す**/
-bool Controller::getButtonChanged() const
-{
-    return buttonChanged;
-}
-
-/**ジョイスティックの値を-1.0~1.0の範囲で返す関数**/
-double Controller::readJoyLX() const
-{
-    return -((double)LJoyX - 127.0) / 127.0;
-}
-double Controller::readJoyLY() const
-{
-    return -((double)LJoyY - 127.0) / 127.0;
-}
-double Controller::readJoyRX() const
-{
-    return -((double)RJoyX - 127.0) / 127.0;
-}
-double Controller::readJoyRY() const
+bool XBee_TransparentMode::readButton(unsigned int button,int status)
 {
-    return -((double)RJoyY - 127.0) / 127.0;
+  int8_t num = 0;
+  if(ButtonState & button)  num += 2;
+  if(preButtonState & button) num -= 1;
+  if(num == status) return true;
+  else return false;
 }
-
-/**ジョイスティックの値を0~255の範囲で返す関数**/
-unsigned int Controller::readJoyLXbyte() const
-{
-    return LJoyX;
-}
-unsigned int Controller::readJoyLYbyte() const
+bool XBee_APIMode::readButton(unsigned int button,int status)
 {
-    return LJoyY;
-}
-unsigned int Controller::readJoyRXbyte() const
-{
-    return RJoyX;
-}
-unsigned int Controller::readJoyRYbyte() const
-{
-    return RJoyY;
+  int8_t num = 0;
+  if(ButtonState & button)  num += 2;
+  if(preButtonState & button) num -= 1;
+  if(num == status) return true;
+  else return false;
 }
\ No newline at end of file