Make sure to send pending keys draft

Thu, 07 Oct 2021 22:26:33 +0200

author
David Douard <david.douard@sdfa3.org>
date
Thu, 07 Oct 2021 22:26:33 +0200
changeset 70
7b4735e9c2c1
parent 69
516acbbce05a
child 71
d193c24a078a

Make sure to send pending keys

even if the FP is flood by the CPU: hijack the started transmission
(just after receiving a SoT (0x66) datagram).

src/hp34comm.cpp file | annotate | diff | comparison | revisions
src/hp34comm.h file | annotate | diff | comparison | revisions
--- a/src/hp34comm.cpp	Thu Oct 07 22:24:45 2021 +0200
+++ b/src/hp34comm.cpp	Thu Oct 07 22:26:33 2021 +0200
@@ -64,34 +64,40 @@
 
 
 void HPSerial::send_pending_key() {
-  uint8_t c;
-
+  // body of the send_trhead: dedicated to sending pending keys
+  // Note that a key can also be sent after receiving a SoT packet
   while(true)
   {
 	  if (!sendbuf.empty())
+		do_send_key();
+	  else // prevent from flooding the main unit
+	    ThisThread::sleep_for(1ms);
+  }
+}
+
+void HPSerial::do_send_key() {
+  uint8_t c;
+  if (!sendbuf.empty())
+  {
+	  if (cur_gstate == GSTATE_IDLE)
 	  {
-		  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?
+		  serial.attach(0, SerialBase::RxIrq);
+		  cur_gstate = GSTATE_TX;
 
-			  sendbuf.pop(c);
-			  serial.write(&c, 1);
-			  if (!wait_for(0x00)) {}
+		  c = 0x66;
+		  serial.write(&c, 1);
+		  if (!wait_for(0x99)) {}
+		  // break; // XXX what to do?
 
-			  c = 0x55;
-			  serial.write(&c, 1);
-			  cur_gstate = GSTATE_IDLE;
-			  serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
-		  }
+		  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(1ms);
   }
 }
 
@@ -187,7 +193,7 @@
 }
 
 
-void HPSerial::pushCmd(uint8_t cmd, uint8_t size, char *payload) {
+void HPSerial::push_cmd(uint8_t cmd, uint8_t size, char *payload) {
   CMD val;
   uint8_t i;
   val.id = ncmd++;
@@ -220,8 +226,16 @@
     return HPSerial::STATE_IDLE;
     break;
   case 0x66:
-    send_ack(0x99);
-    return HPSerial::STATE_COMMAND;
+	  if (!sendbuf.empty()) {
+		  // hijack the transmission to send a keycode
+		  do_send_key();
+		  return HPSerial::STATE_IDLE;
+	  }
+	  else
+	  {
+		  send_ack(0x99);
+		  return HPSerial::STATE_COMMAND;
+	  }
     break;
   case 0xFF:
     return HPSerial::STATE_IDLE;
@@ -244,7 +258,7 @@
   send_ack(0x00);
 
   if (c == 0x86) { // shutdown
-    pushCmd(tr_data.cmd, tr_data.size, tr_data.payload);
+    push_cmd(tr_data.cmd, tr_data.size, tr_data.payload);
     return HPSerial::STATE_IDLE;
   }
   return STATE_PAYLOAD_SIZE;
@@ -265,7 +279,7 @@
   tr_data.payload[tr_data.pos++] = c;
   send_ack(0x00);
   if (tr_data.pos >= tr_data.size) {
-    pushCmd(tr_data.cmd, tr_data.size, tr_data.payload);
+    push_cmd(tr_data.cmd, tr_data.size, tr_data.payload);
     return STATE_IDLE;
   }
   return STATE_PAYLOAD;
--- a/src/hp34comm.h	Thu Oct 07 22:24:45 2021 +0200
+++ b/src/hp34comm.h	Thu Oct 07 22:26:33 2021 +0200
@@ -36,7 +36,7 @@
   void send_startup_seq(uint8_t keycode);
 
 private:
-  void pushCmd(uint8_t cmd, uint8_t size, char *payload);
+  void push_cmd(uint8_t cmd, uint8_t size, char *payload);
   void rx_irq(void);
   void timeout(void);
   void set_timer(Kernel::Clock::duration_u32 v=0ms) {
@@ -57,6 +57,7 @@
 
   Thread send_thread;
   void send_pending_key();
+  void do_send_key();
   bool wait_for(uint8_t);
 
 

mercurial