明石高専ロボ研 mbedライブラリ
Dependencies: mbed
Diff: scrp_slave.cpp
- Revision:
- 8:82727add54ce
- Parent:
- 7:4ad54efe2fdd
- Child:
- 10:25af94dd1668
diff -r 4ad54efe2fdd -r 82727add54ce scrp_slave.cpp --- a/scrp_slave.cpp Sun Dec 06 08:09:01 2020 +0000 +++ b/scrp_slave.cpp Wed Feb 24 12:34:27 2021 +0000 @@ -20,7 +20,7 @@ :port1(TX1,RX1,115200),port2(TX2,RX2,115200),address_(addr){ mode_ = 2; serial_[1] = &port2; - serial_[1]->attach(callback(this,&ScrpSlave::receive2),Serial::RxIrq); + serial_[1]->attach(callback(this,&ScrpSlave::receive1),Serial::RxIrq); init(); } @@ -29,7 +29,7 @@ mode_ = 3; rede_ = new DigitalOut(REDE1,0); serial_[1] = &port2; - serial_[1]->attach(callback(this,&ScrpSlave::receive2),Serial::RxIrq); + serial_[1]->attach(callback(this,&ScrpSlave::receive1),Serial::RxIrq); init(); } @@ -44,7 +44,7 @@ procs_[i] = NULL; } serial_[0] = &port1; - serial_[0]->attach(callback(this,&ScrpSlave::receive1),Serial::RxIrq); + serial_[0]->attach(callback(this,&ScrpSlave::receive0),Serial::RxIrq); if(address_ < 255){ my_id_ = address_; return; @@ -53,20 +53,20 @@ flash_ = new FlashIAP; if(flash_->init()==0){ if(flash_->read(&my_id_,address_,1) != 0){ - send(222,222,222); + send2(222,222,222); my_id_ = 10; } }else{ - send(111,111,111); + send2(111,111,111); my_id_ = 10; } } -void ScrpSlave::receive1(){ +void ScrpSlave::receive0(){ check(0); } -void ScrpSlave::receive2(){ +void ScrpSlave::receive1(){ check(1); } @@ -83,12 +83,12 @@ flash_->program(&id,address_,1); } -bool ScrpSlave::send(uint8_t id,uint8_t cmd,int16_t tx_data){ +bool ScrpSlave::send1(uint8_t id,uint8_t cmd,int16_t tx_data){ return sending(0,id,cmd,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)); + return sending((mode_ > 1),id,cmd,tx_data); } bool ScrpSlave::sending(int port,uint8_t id,uint8_t cmd,int16_t tx_data){ @@ -181,12 +181,17 @@ uint8_t tx_sum = my_id_ + rx_cmd + tx_dataL + tx_dataH; const uint8_t data[8] = {DMY, STX, my_id_, rx_cmd, tx_dataL, tx_dataH, tx_sum, DMY}; - memcpy(this->send_data_[port],data,8); + memcpy(send_data_[port],data,8); prime(port); } }else if(stx_flag_[port]){ uint8_t get_data = serial_[port]->getc(); - if(get_data == my_id_ || get_data == 255 || wait_data_[port]){ + if(get_data == my_id_ || get_data == 255){ + id_ok_[port] = true; + wait_data_[port] = false; + tmp_data_[port][0] = get_data; + data_count_[port]++; + }else if(wait_data_[port]){ id_ok_[port] = true; tmp_data_[port][0] = get_data; data_count_[port]++; @@ -202,7 +207,7 @@ return; } -void ScrpSlave::data_send1(){ +void ScrpSlave::data_send0(){ while(serial_[0]->writeable()){ if(data_count_[0] < 8){ serial_[0]->putc(send_data_[0][data_count_[0]++]); @@ -216,7 +221,7 @@ } } -void ScrpSlave::data_send2(){ +void ScrpSlave::data_send1(){ while(serial_[1]->writeable()){ if(data_count_[1] < 8){ serial_[1]->putc(send_data_[1][data_count_[1]++]); @@ -230,15 +235,15 @@ void ScrpSlave::prime(int port){ serial_[port]->attach(NULL, Serial::TxIrq); data_count_[port] = 0; - if(port == 0){ + if(port == 0){ if(mode_%2 == 1){ rede_->write(1); } - data_send1(); - serial_[0]->attach(callback(this, &ScrpSlave::data_send1), Serial::TxIrq); + data_send0(); + serial_[0]->attach(callback(this, &ScrpSlave::data_send0), Serial::TxIrq); }else{ - data_send2(); - serial_[1]->attach(callback(this, &ScrpSlave::data_send2), Serial::TxIrq); + data_send1(); + serial_[1]->attach(callback(this, &ScrpSlave::data_send1), Serial::TxIrq); } }