明石高専ロボ研 mbedライブラリ
Dependencies: mbed
Diff: scrp_slave.cpp
- Revision:
- 5:a7894e6982ea
- Parent:
- 4:39ef4d91dc34
- Child:
- 7:4ad54efe2fdd
--- a/scrp_slave.cpp Fri Jul 24 14:12:15 2020 +0000 +++ b/scrp_slave.cpp Thu Nov 12 16:38:23 2020 +0000 @@ -30,14 +30,22 @@ } void ScrpSlave::init(PinName TX,PinName RX){ - timeout_ = 500; for(int i = 0;i<2;i++){ wait_data_[i] = false; stx_flag_[i] = false; id_ok_[i] = false; + get_responce_[i] = false; + } + for(int i = 1;i<256;++i){ + procs_[i] = NULL; } serial_[0] = new Serial(TX,RX,115200); serial_[0]->attach(callback(this,&ScrpSlave::port1),Serial::RxIrq); + if(address_ < 255){ + my_id_ = address_; + return; + } + //フラッシュメモリーのアクセスにエラーが出たら、アドレスは10に設定される。 flash_ = new FlashIAP; if(flash_->init()==0){ if(flash_->read(&my_id_,address_,1) != 0){ @@ -48,9 +56,6 @@ send(111,111,111); my_id_ = 10; } - for(int i = 1;i<256;++i){ - procs_[i] = NULL; - } } void ScrpSlave::port1(){ @@ -66,32 +71,30 @@ procs_[cmd] = proc; } -void ScrpSlave::setTimeout(int time){ - timeout_ = time; -} - void ScrpSlave::changeID(uint8_t id){ + if(address_ < 255){ + return; + } flash_->erase(address_,flash_->get_sector_size(address_)); flash_->program(&id,address_,1); } -int16_t ScrpSlave::send(uint8_t id,uint8_t cmd,int16_t tx_data){ +bool ScrpSlave::send(uint8_t id,uint8_t cmd,int16_t tx_data){ return sending(0,id,cmd,tx_data); } -int16_t ScrpSlave::send2(uint8_t id,uint8_t cmd,int16_t tx_data){ +bool ScrpSlave::send2(uint8_t id,uint8_t cmd,int16_t tx_data){ return ((mode_ < 2) ? -1 : sending(1,id,cmd,tx_data)); } -int16_t ScrpSlave::sending(int port,uint8_t id,uint8_t cmd,int16_t tx_data){ +bool ScrpSlave::sending(int port,uint8_t id,uint8_t cmd,int16_t tx_data){ uint8_t tx_dataL = tx_data; uint8_t tx_dataH = tx_data >> 8; uint8_t tx_sum = id + cmd + tx_dataL + tx_dataH; - Timer out; const uint8_t data[8] = {DMY, STX, id, cmd, tx_dataL, tx_dataH, tx_sum, DMY}; if(!serial_[port]->writeable()){ - return -1; + return false; } wait_data_[port] = true;//データ返信待ち if(mode_%2 == 1 && port == 0){ @@ -104,17 +107,27 @@ if(mode_%2 == 1 && port == 0){ rede_->write(0); } - - out.reset(); - out.start(); - while(wait_data_[port]){ - if(out.read_ms() > timeout_){ - rx_data_[port] = -1; - wait_data_[port] = false; - } + return true; +} + +bool ScrpSlave::isWaiting(uint8_t port){ + if(port > 1 || (port == 1 && mode_ < 2)){ + return false; } - out.stop(); - return rx_data_[port]; + return wait_data_[port]; +} + +int16_t ScrpSlave::receiveData(uint8_t port){ + //ポート指定が正しいかどうか。 + if(port > 1 || (port == 1 && mode_ < 2)){ + return -1; + } + //データがあるか確認。 + if(get_responce_[port]){ + return rx_data_[port]; + }else{ + return -1; + } } void ScrpSlave::check(int port){ @@ -135,7 +148,10 @@ rx_data_[port] = (int16_t)(tmp_data_[port][2] + ((int16_t)tmp_data_[port][3] << 8)); if(wait_data_[port]){//データ返信待ち時 wait_data_[port] = false; + get_responce_[port] = true; return; + }else if(get_responce_[port]){ + get_responce_[port] = false; } uint8_t rx_cmd = tmp_data_[port][1]; bool broadcast = (tmp_data_[port][0] == 255);