2022NHKAチーム(射出、紙飛行機折り、昇降)

Dependencies:   OmniPosition PID R1370 SerialMultiByte Servo ikarashiMDC_2byte_ver mbed omni_wheel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FEP_RX22.cpp Source File

FEP_RX22.cpp

Go to the documentation of this file.
00001 /**
00002  *  @file   FEP_RX22.cpp
00003  *  @brief  FEP受信用ライブラリ
00004  *  @author 安澤瑠
00005  *  @date   22/10/6
00006  */
00007 #include "FEP_RX22.h"
00008 
00009 FEP_RX22::FEP_RX22(PinName tx, PinName rx, uint8_t addr_, int baud) :
00010     RawSerial(tx, rx, baud)
00011 {
00012     addr     = addr_;
00013     timeout  = TIMEOUT_COUNT;
00014     bufindex = 0;
00015 }
00016 
00017 void FEP_RX22::StartReceive()
00018 {
00019     attach(callback(this, &FEP_RX22::ReceiveBytes));
00020     timeoutTimer.attach(callback(this, &FEP_RX22::TimeoutLoop), 0.1);
00021 }
00022 
00023 void FEP_RX22::TimeoutLoop()
00024 {
00025     if (timeout >= TIMEOUT_COUNT) {
00026         status = false;
00027     } else {
00028         status = true;
00029         timeout++;
00030     }
00031 }
00032 
00033 void FEP_RX22::ReceiveBytes()
00034 {
00035     buffer[bufindex] = getc(); // Receive 1byte
00036     timeout = 0;
00037 
00038     if ( (!strncmp((char*)(buffer + ((256 + bufindex - 1)%256) ), "\r\n", 2)) ) { // <CR><LF> bufindex = <LF>(='\n')
00039         CheckData();
00040     }
00041     bufindex++;
00042 }
00043 
00044 void FEP_RX22::CheckData()
00045 {
00046     indexofR=0;
00047     for (uint16_t i_Rbn=0; i_Rbn<256; i_Rbn++) {
00048         indexofR = (256 + bufindex - i_Rbn) % 256;
00049         if ( !strncmp((char*)(buffer + indexofR) , "RBN", 3) ) { // check header  indexofR is address of 'R'BN
00050 #if ControllerMode
00051             getControllerState();
00052 #else
00053             datalen = (buffer[(indexofR+6)%256]-48)*100 + (buffer[(indexofR+7)%256]-48)*10 + (buffer[(indexofR+8)%256]-48);
00054             for (int i_msg=0; i_msg<datalen; i_msg++) {
00055                 msgdata[i_msg] = buffer[(indexofR+9+i_msg)%256];
00056             }
00057 #endif
00058             
00059             return;
00060         }
00061     }
00062 }
00063 
00064 uint8_t FEP_RX22::getData(uint8_t *data)
00065 {
00066     for(int i=0; i<128; i++) data[i] = msgdata[i];
00067     return datalen;
00068 }
00069 
00070 void FEP_RX22::getControllerState()
00071 {
00072     /* コントローラの仕様変更時はここを変える */
00073     for (int i_button=0; i_button<2; i_button++) {
00074         button[i_button] = buffer[(indexofR+9+i_button)%256];
00075     }
00076     for (int i_stick=0; i_stick<4; i_stick++) {
00077         stick[i_stick] = buffer[(indexofR+11+i_stick)%256];
00078     }
00079     for (int i_trigger=0; i_trigger<2; i_trigger++) {
00080         trigger[i_trigger] = buffer[(indexofR+15+i_trigger)%256];
00081     }
00082     
00083     return;
00084 }
00085 
00086 bool FEP_RX22::getButton(uint8_t n)
00087 {
00088     if (n<8) {
00089         return (button[0]>>n) & 1;
00090     } else {
00091         return (button[1]>>(n-8)) & 1;
00092     }
00093 }
00094 
00095 int16_t FEP_RX22::getStick(uint8_t n)
00096 {
00097     if (n%2) return 128-stick[n];
00098     else return stick[n]-128;
00099 }
00100 
00101 int16_t FEP_RX22::getTrigger(uint8_t n)
00102 {
00103     return trigger[n];
00104 }
00105 
00106 bool FEP_RX22::getStatus()
00107 {
00108     return status;
00109 }