Hayato Kikuchi
/
XBee3-API_test
test
Revision 2:cb2bd28bb66d, committed 2021-04-16
- Comitter:
- kikuchi8810
- Date:
- Fri Apr 16 09:34:17 2021 +0000
- Parent:
- 1:cb2c4d733c1b
- Commit message:
- test
Changed in this revision
diff -r cb2c4d733c1b -r cb2bd28bb66d Controller.cpp --- 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
diff -r cb2c4d733c1b -r cb2bd28bb66d Controller.h --- a/Controller.h Fri Jan 15 08:50:35 2021 +0000 +++ b/Controller.h Fri Apr 16 09:34:17 2021 +0000 @@ -2,84 +2,122 @@ #define CONTROLLER_H #include "mbed.h" +#include "XBee.h" #include "RawSerial.h" -#define BUTTON_SIKAKU 0x01 -#define BUTTON_SANKAKU 0x02 -#define BUTTON_BATU 0x04 -#define BUTTON_MARU 0x08 -#define BUTTON_L1 0x10 -#define BUTTON_R1 0x20 -#define BUTTON_L2 0x40 -#define BUTTON_R2 0x80 -#define BUTTON_PAD 0x100 -#define BUTTON_PS 0x200 -#define BUTTON_SHARE 0x400 -#define BUTTON_OPTION 0x800 -#define BUTTON_UP 0x1000 -#define BUTTON_RIGHT 0x2000 -#define BUTTON_DOWN 0x4000 -#define BUTTON_LEFT 0x8000 +#define APIMODE 1 +#define TRANSPARENT_MODE 2 + +#define XBEE_MODE ( APIMODE ) //カッコの中を変えて使う + +#if XBEE_MODE == TRANSPARENT_MODE + + #define BUTTON_X 0x0001 + #define BUTTON_Y 0x0002 + #define BUTTON_A 0x0004 + #define BUTTON_B 0x0008 + + #define BUTTON_L1 0x0010 + #define BUTTON_R1 0x0020 + #define BUTTON_L2 0x0040 + #define BUTTON_R2 0x0080 + + // #define BUTTON_JOY_L 0x0100 + // #define BUTTON_JOY_R 0x0200 + // #define BUTTON_SHARE 0x0400 + // #define BUTTON_OPTION 0x0800 + #define BUTTON_PAD 0x0100 + #define BUTTON_PS 0x0200 + #define BUTTON_JOY_L 0x0400 + #define BUTTON_JOY_R 0x0800 + + #define BUTTON_UP 0x1000 + #define BUTTON_RIGHT 0x2000 + #define BUTTON_DOWN 0x4000 + #define BUTTON_LEFT 0x8000 + +#elif XBEE_MODE == API_MODE -class Controller + #define BUTTON_SQUARE 0x0001 + #define BUTTON_TRIANGLE 0x0002 + #define BUTTON_CROSS 0x0004 + #define BUTTON_CIRCLE 0x0008 + + #define BUTTON_L1 0x0010 + #define BUTTON_R1 0x0020 + #define BUTTON_L2 0x0040 + #define BUTTON_R2 0x0080 + + #define BUTTON_UP 0x0100 + #define BUTTON_RIGHT 0x0200 + #define BUTTON_DOWN 0x0400 + #define BUTTON_LEFT 0x0800 + + #define BUTTON_SHARE 0x1000 + #define BUTTON_OPTION 0x2000 + #define BUTTON_PAD 0x4000 + #define BUTTON_PS 0x8000 + + #define BUTTON_JOY_L 0x0100 + #define BUTTON_JOY_R 0x0200 + +#endif + +#define DATA_NUM_TRANS 10 +#define DATA_NUM_API 3 +#define BOUDRATE_TRANS 115200 +#define BOUDRATE_API 230400 + +#define PUSHE 1 +#define RELEASE 0 +#define PUSHED 2 +#define RELEASED -1 + +class XBee_TransparentMode { public: - Controller(PinName tx, PinName rx, int baudrate); - RawSerial XBee3; - //Controller(RawSerial* serial_xbee); + XBee_TransparentMode(PinName tx,PinName rx,int baudrate); void update(); - void DRbegin(); - - /** - * コントローラのボタン入力状態を返す. - * - * @param buttonNum ボタン番号 1~12 - * @return 1:押されている 0:離されている 2:押された -1:離された - * - */ - int8_t readButton(int buttonNum) const; - unsigned int getButtonState() const; - - /** - * ジョイスティックの値を読む - * @return -1.0 ~ 1.0 - * - * X - * ^ - * | - * Y<---+---- - * | - * | - */ + bool readButton(unsigned int button,int status); + unsigned int getButtonState(); - double readJoyLX() const; - double readJoyLY() const; - double readJoyRX() const; - double readJoyRY() const; - - unsigned int readJoyLXbyte() const; - unsigned int readJoyLYbyte() const; - unsigned int readJoyRXbyte() const; - unsigned int readJoyRYbyte() const; - - bool getButtonChanged() const; //スイッチの状態を確認する + unsigned int ButtonState, preButtonState; + unsigned int LJoyX, LJoyY, RJoyX, RJoyY; private: - unsigned int ButtonState, preButtonState; - unsigned int LJoyX, LJoyY, RJoyX, RJoyY; int boudrate; + RawSerial serialXbee3; bool buttonChanged; private: int recv_num; +}; - char recv_msgs[9]; +class XBee_APIMode +{ +public: + XBee_APIMode(PinName tx,PinName rx); + + void begin(int baudrate); + int update(); + + bool readButton(unsigned int button,int status); + unsigned int getButtonState(); + + unsigned int ButtonState, preButtonState; + +private: + + int boudrate; + XBee xbee; + + bool buttonChanged; }; #endif \ No newline at end of file
diff -r cb2c4d733c1b -r cb2bd28bb66d XBee.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XBee.lib Fri Apr 16 09:34:17 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/okini3939/code/XBee/#b36422ef864f
diff -r cb2c4d733c1b -r cb2bd28bb66d main.cpp --- a/main.cpp Fri Jan 15 08:50:35 2021 +0000 +++ b/main.cpp Fri Apr 16 09:34:17 2021 +0000 @@ -1,21 +1,51 @@ #include "mbed.h" #include "Controller.h" -Controller Con(p28,p27,115200); Serial pc(USBTX,USBRX,115200); +Ticker timer; +DigitalOut myled2(LED2); + +XBee_APIMode Con(p28,p27); -int main() -{ - Con.DRbegin(); - while(1) { - double joyRX = Con.readJoyRXbyte(); - double joyRY = Con.readJoyRYbyte(); - double joyLX = Con.readJoyLXbyte(); - double joyLY = Con.readJoyLYbyte(); - unsigned int buttonstate = Con.getButtonState(); - if(buttonstate & BUTTON_MARU) pc.printf("%d\n",Con.getButtonState()); - else if(buttonstate & BUTTON_L2) pc.printf("%3.0lf %3.0lf %3.0lf %3.0lf\n",joyRX,joyRY,joyLX,joyLY); - else pc.printf("PUSH!\n"); - //wait(0.01); +bool flag_10ms = false; +bool flag_1s = false; + +void warikomi(){ + flag_10ms = true; + static int a = 0; + if(a++ > 100){ + flag_1s = true; + a = 0; } } + +void setup() +{ + wait(1); + myled2.write(0); + Con.begin(115200); + timer.attach(warikomi,0.01); +} + +void loop() +{ + int serial_num = Con.update(); + if(flag_10ms) + { + pc.printf("%d\t%d\n",serial_num,Con.getButtonState()); + + flag_10ms = false; + } + if(flag_1s) + { + myled2.write(!myled2.read()); + flag_1s = false; + } +} + +int main(void) +{ + setup(); + for(;;)loop(); + return 0; +}