明石高専ロボ研 mbedライブラリ

Dependencies:   mbed

Dependents:   MDD_L432KC USB2RS485 pathtracking odometry ... more

Revision:
10:25af94dd1668
Parent:
8:82727add54ce
Child:
11:eaf2e3166d20
diff -r 9f43b2ead060 -r 25af94dd1668 scrp_slave.cpp
--- a/scrp_slave.cpp	Thu Feb 25 05:37:29 2021 +0000
+++ b/scrp_slave.cpp	Thu Feb 25 07:20:15 2021 +0000
@@ -43,6 +43,7 @@
     for(int i = 1;i<256;++i){
         procs_[i] = NULL;
     }
+    responceFunc_ = NULL;
     serial_[0] = &port1;
     serial_[0]->attach(callback(this,&ScrpSlave::receive0),Serial::RxIrq);
     if(address_ < 255){
@@ -70,6 +71,10 @@
     check(1);
 }
 
+void ScrpSlave::attachResponce(void (*func)(uint8_t id, uint8_t cmd, int16_t responce)){
+    responceFunc_ = func;
+}
+
 void ScrpSlave::addCMD(uint8_t cmd, bool (*proc)(int rx_data, int& tx_data)){
     if(cmd == 0 || cmd == 254 || cmd == 253)return;
     procs_[cmd] = proc;
@@ -101,6 +106,7 @@
         return false;
     }
     wait_data_[port] = true;//データ返信待ち
+    get_responce_[port] = false;
     if(mode_%2 == 1 && port == 0){
         rede_->write(1);
     }
@@ -114,6 +120,13 @@
     return true;
 }
 
+bool ScrpSlave::getResponce(uint8_t port){
+    if(port > 1 || (port == 1 && mode_ < 2)){
+        return false;
+    }
+    return get_responce_[port];
+}
+
 bool ScrpSlave::isWaiting(uint8_t port){
     if(port > 1 || (port == 1 && mode_ < 2)){
         return false;
@@ -153,6 +166,9 @@
             if(wait_data_[port]){//データ返信待ち時
                 wait_data_[port] = false;
                 get_responce_[port] = true;
+                if(responceFunc_ != NULL){
+                    responceFunc_(tmp_data_[port][0],tmp_data_[port][1],rx_data_[port]);
+                }
                 return;
             }else if(get_responce_[port]){
                 get_responce_[port] = false;
@@ -207,7 +223,7 @@
     return;
 }
 
-void ScrpSlave::data_send0(){
+void ScrpSlave::dataSend0(){
     while(serial_[0]->writeable()){
         if(data_count_[0] < 8){
             serial_[0]->putc(send_data_[0][data_count_[0]++]);
@@ -221,7 +237,7 @@
     }
 }
 
-void ScrpSlave::data_send1(){
+void ScrpSlave::dataSend1(){
     while(serial_[1]->writeable()){
         if(data_count_[1] < 8){
             serial_[1]->putc(send_data_[1][data_count_[1]++]);
@@ -239,11 +255,11 @@
         if(mode_%2 == 1){
             rede_->write(1);
         }
-        data_send0();
-        serial_[0]->attach(callback(this, &ScrpSlave::data_send0), Serial::TxIrq);
+        dataSend0();
+        serial_[0]->attach(callback(this, &ScrpSlave::dataSend0), Serial::TxIrq);
     }else{
-        data_send1();
-        serial_[1]->attach(callback(this, &ScrpSlave::data_send1), Serial::TxIrq);
+        dataSend1();
+        serial_[1]->attach(callback(this, &ScrpSlave::dataSend1), Serial::TxIrq);
     }
 }