NagaokaRoboticsClub_mbedTeam / 2021BconTX

Bcon.cpp

Committer:
piroro4560
Date:
2021-10-16
Revision:
0:4e42396b06ed

File content as of revision 0:4e42396b06ed:

/**
 *  @file   Bcon.cpp
 *  @brief  Bcontroller送信用FEPライブラリ
 *  @author 安澤瑠
 *  @date   21/10/15
 */
#include "Bcon.h"

Bcon::Bcon(PinName tx, PinName rx, uint8_t addr_, int baud) :
    RawSerial(tx, rx, baud)
{
    addr     = addr_;
}

int8_t Bcon::SendData(uint8_t *data)
{
    uint8_t sendindex; // index of 'data'
    printf("@TBN%03d%03d", addr, DATANUM); // send comand
    for (sendindex=0; sendindex<DATANUM; sendindex++) { 
        putc(data[sendindex]); // 
    }
    printf("\r\n"); // <cr><lf>
    return GetResponse(); // check response
}

int8_t Bcon::GetResponse()
{
    char res[256]; // array of storing response
    for (uint8_t resindex=0; resindex<256; resindex++) {
        res[resindex] = getc();
        if (!strncmp(res+resindex-1, "\r\n", 2)) {
            if (!strncmp(res+resindex-3, "P0", 2))  return 0;
            else if (!strncmp(res+resindex-3, "N0", 2)) return 2;
            else if (!strncmp(res+resindex-3, "N1", 2)) return 3;
            else if (!strncmp(res+resindex-3, "N3", 2)) return 4;
        }
    }
    return -1;
}