PLANET-Q ES920LR Library
Dependents: IZU2020_GROUND_STATION IZU2020_AVIONICS IZU2020_AVIONICS
Diff: PQES920LR.cpp
- Revision:
- 0:064d3711be83
- Child:
- 1:a5114a32febe
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PQES920LR.cpp Tue Dec 17 09:06:01 2019 +0000 @@ -0,0 +1,62 @@ +#include "mbed.h" +#include "PQES920LR.h" + +ES920LR::ES920LR(Serial &serial) +{ + _serial = &serial; + _serial->attach(callback(this, &ES920LR::receive), Serial::RxIrq); + memset(tx_buf, '\0', 52); + memset(rx_buf, '\0', 52); + index = 0; + flag = 0; + response = false; +} + +void ES920LR::send(char *data, int size) +{ + if(size > 50) { + size = 50; + } + + tx_buf[0] = size; + for(int i = 0; i < size; i++) { + tx_buf[1 + i] = data[i]; + } + for (int i = 0; i < 1 + size; i++) { + _serial->putc(tx_buf[i]); + } + + flag = 0; + response = true; +} + +void ES920LR::attach(void(*func_ptr)(char*)) +{ + func = func_ptr; +} + +void ES920LR::receive() +{ + if(flag == 0) { + rx_size = _serial->getc(); + memset(rx_buf, '\0', 52); + index = 0; + flag = 1; + } + if(flag == 1) { + rx_buf[index] = _serial->getc(); + if(index == rx_size - 1) { + if(!response) { + if(func != NULL) { + (*func)(rx_buf); + } + } else { + response = false; + } + flag = 0; + } else { + index ++; + } + } + +} \ No newline at end of file