Library for receiving 2021NHK Bteam Controller

Dependents:   2021NHK_Bcon_RX 2021NHK_B_syudo 2021NHK_B_main 2022kouroboBv2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers controller.cpp Source File

controller.cpp

00001 /**
00002  *  @file   FEP.cpp
00003  *  @author 安澤瑠
00004  *  @date   21/10/16
00005  */
00006 #include "controller.h"
00007 
00008 Bcon::Bcon(PinName tx, PinName rx, uint8_t addr_, int baud) :
00009     RawSerial(tx, rx, baud)
00010 {
00011     addr     = addr_;
00012     timeout  = TIMEOUT_COUNT;
00013     bufindex = 0;
00014     retindex = 0;
00015 }
00016 
00017 void Bcon::StartReceive()
00018 {
00019     attach(callback(this, &Bcon::ReceiveBytes));
00020     timeoutTimer.attach(callback(this, &Bcon::TimeoutLoop), 0.1);
00021 }
00022 
00023 void Bcon::TimeoutLoop()
00024 {
00025     if (timeout >= TIMEOUT_COUNT) {
00026         status = false;
00027     } else {
00028         status = true;
00029         timeout++;
00030     }
00031 }
00032 
00033 void Bcon::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(); // check and extract message
00040     }
00041     bufindex++;
00042 }
00043 
00044 void Bcon::CheckData()
00045 {
00046     uint8_t temp=0;
00047     for (uint16_t i=0; i<256; i++) {
00048         temp = (256 + bufindex - i) % 256;
00049         if ( !strncmp((char*)(buffer + temp) , "RBN", 3) ) { // check header  temp='R'
00050         /* コントローラの仕様変更時はここを変える */
00051             sum = buffer[(temp+9)%256];
00052             for (int j=0; j<4; j++) {
00053                 stick[j] = buffer[(temp+10+j)%256];
00054             }
00055             
00056             return;
00057         }
00058     }
00059 }
00060 
00061 
00062 void Bcon::GetData(uint8_t *data)
00063 {
00064     for (int i=0; i<DATANUM; i++) data[i] = retdata[i];
00065 }
00066 
00067 bool Bcon::getButton(uint8_t n)
00068 {
00069     return (sum>>n) & 1;
00070 }
00071 
00072 int16_t Bcon::getStick(uint8_t n)
00073 {
00074     if (n%2) return 128-stick[n];
00075     else return stick[n]-128;
00076 }