diff -r fc55f6eaa8bc -r c146d19101a3 src/hp34comm.cpp --- a/src/hp34comm.cpp Sat Nov 07 19:24:11 2020 +0100 +++ b/src/hp34comm.cpp Mon Nov 09 23:05:24 2020 +0100 @@ -32,6 +32,7 @@ serial.baud(187500); serial.format(8, BufferedSerial::Even, 1); cur_state = STATE_IDLE; + send_thread.start(callback(this, &HPSerial::send_pending_key)); serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq); } @@ -69,6 +70,118 @@ sendbuf.push(keycode); } + +bool HPSerial::wait_for(uint8_t value) +{ + char c; + + for(uint8_t i=0; i<2; i++) + { + while(!serial.readable()) + wait_us(10); + //ThisThread::sleep_for(0.1ms); + serial.read(&c, 1); + if (value == c) + return true; + } + return false; +} + +void HPSerial::send_pending_key() { + uint8_t c; + + while(true) + { + if (!sendbuf.empty()) + { + if (cur_gstate == GSTATE_IDLE) + { + serial.attach(0, SerialBase::RxIrq); + cur_gstate = GSTATE_TX; + + c = 0x66; + serial.write(&c, 1); + if (!wait_for(0x99)) {} + // break; // XXX what to do? + + sendbuf.pop(c); + serial.write(&c, 1); + if (!wait_for(0x00)) {} + + c = 0x55; + serial.write(&c, 1); + cur_gstate = GSTATE_IDLE; + serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq); + } + } + //else // prevent from flooding the main unit + ThisThread::sleep_for(5ms); + } +} + +void HPSerial::send_startup_seq(uint8_t keycode) { + uint8_t c; + + while (cur_gstate != GSTATE_IDLE) { + ThisThread::sleep_for(1ms); + } + + serial.attach(0, SerialBase::RxIrq); + cur_gstate = GSTATE_TX; + + // Send the init seq 0x33 0x02 0xFF 0x55 + c = 0x33; + serial.write(&c, 1); + if (!wait_for(0xCC)) {} + + c = 0x02; + serial.write(&c, 1); + if (!wait_for(0x00)) {} + + c = 0xFF; + serial.write(&c, 1); + if (!wait_for(0x00)) {} + + c = keycode; + serial.write(&c, 1); + if (!wait_for(0x00)) {} + + c = 0x55; + serial.write(&c, 1); + cur_gstate = GSTATE_IDLE; + serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq); + +} + +void HPSerial::send_startup_seq() { + uint8_t c; + + while (cur_gstate != GSTATE_IDLE) { + ThisThread::sleep_for(1ms); + } + + serial.attach(0, SerialBase::RxIrq); + cur_gstate = GSTATE_TX; + + // Send the init seq 0x33 0x02 0x00 0x55 + c = 0x33; + serial.write(&c, 1); + if (!wait_for(0xCC)) {} + + c = 0x02; + serial.write(&c, 1); + if (!wait_for(0x00)) {} + + c = 0x00; + serial.write(&c, 1); + if (!wait_for(0x00)) {} + + c = 0x55; + serial.write(&c, 1); + cur_gstate = GSTATE_IDLE; + serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq); +} + void HPSerial::send_key_when_idle() { if (!sendbuf.empty() && cur_gstate == GSTATE_IDLE) {