Add a key mapping table (protocol value vs. raw keycode) and send it draft

Sun, 25 Oct 2020 22:17:15 +0100

author
David Douard <david.douard@sdf3.org>
date
Sun, 25 Oct 2020 22:17:15 +0100
changeset 34
94ab7ff42a1b
parent 33
167010858b8d
child 35
b2dca6b935bb

Add a key mapping table (protocol value vs. raw keycode) and send it

Also reformat the file for 2-wspace-tabs

src/main.cpp file | annotate | diff | comparison | revisions
--- a/src/main.cpp	Sun Oct 25 22:15:35 2020 +0100
+++ b/src/main.cpp	Sun Oct 25 22:17:15 2020 +0100
@@ -56,34 +56,49 @@
 #define KEY_BUF_SIZE 10
 CircularBuffer<keycode_t, KEY_BUF_SIZE> key_buf;
 
-PinName kp_rows[] = {
+#define KP_NROWS 4
+#define KP_NCOLS 5
+DigitalIn kp_rows[KP_NROWS] = {
   KP_R0,
   KP_R1,
   KP_R2,
   KP_R3
 };
 
-PinName kp_colums[] = {
+DigitalOut kp_columns[KP_NCOLS] = {
   KP_C0, KP_C1,
   KP_C2, KP_C3,
   KP_C4
 };
 
-DigitalIn kp_in[] = {
-  KP_R0,
-  KP_R1,
-  KP_R2,
-  KP_R3
+/* mapping (RxC)
+0x2 0x00:  View
+0x1 0x01:  Mon
+3x3 0x02:  Sto/Rcl
+0x0 0x03:  Scan
+1x2 0x04:  Alarm
+1x1 0x05:  Mx+B
+1x0 0x06:  Measure
+2x0 0x07:  Interval
+3x2 0x08:  Card Reset
+3x1 0x09:  Close
+3x0 0x0A:  Open
+0x3 0x0B:  Read
+2x3 0x0C:  Shift
+1x3 0x0D:  Write
+0x4 0x0E:  Left
+1x4 0x0F:  Right
+2x2 0x10:  Advanced
+2x1 0x11:  Step
+*/
+
+uint8_t kp_mapping[KP_NROWS][KP_NCOLS] = { // [row][column]
+  {0x03, 0x01, 0x00, 0x0B, 0x0E},
+  {0x06, 0x05, 0x04, 0x0D, 0x0F},
+  {0x07, 0x11, 0x10, 0x0C, 0xFF},
+  {0x0A, 0x09, 0x08, 0x02, 0xFF}
 };
 
-DigitalOut kp_out[] = {
-  KP_C0, KP_C1,
-  KP_C2, KP_C3,
-  KP_C4
-};
-
-uint8_t kp_nrows = sizeof(kp_in)/sizeof(kp_in[0]);
-uint8_t kp_ncols = sizeof(kp_out)/sizeof(kp_out[0]);
 
 void kp_cb(uint8_t row, uint8_t col);
 void kr_cb(uint8_t row, uint8_t col);
@@ -155,7 +170,7 @@
   uint8_t flag;
   uint8_t x;
   uint8_t y;
-	bool reverse;
+  bool reverse;
   const char* msg;
   const unsigned char* icon;
 } FLAG;
@@ -173,12 +188,12 @@
   {
     // flag, zone, x0, y0, reverse, msg, icon
     // right-side icons area
-	{ 0x00, 246, 0,  false, NULL, icon_alarm}, // F1.0
+  { 0x00, 246, 0,  false, NULL, icon_alarm}, // F1.0
     { 0x01, 246, 11, false, NULL, icon_curve}, // F1.1
 
     // F1.2 == Channel frame
     { 0x03, 197, 27, false, "Channel"},  // F1.3
-  	// F1.7 == Alarm frame
+    // F1.7 == Alarm frame
 
     { 0x34, 0,  28+8,  false, "MON"},    // F4.4
     { 0x33, 0,  28+16, false, "VIEW"},   // F4.3
@@ -280,7 +295,7 @@
 void setup() {
 #if defined(HAVE_PC)
 #if   defined(TARGET_NUCLEO_F446RE)
-	pc.set_baud(115200);
+  pc.set_baud(115200);
 #endif
 
 /*
@@ -293,7 +308,7 @@
 
   printf("\n\nSETUP\n");
   printf("  System Core Clock = %.3f MHZ\r\n",
-  	 (float)SystemCoreClock/1000000);
+     (float)SystemCoreClock/1000000);
 
   /*
 #if defined(HAS_LED)
@@ -329,10 +344,8 @@
 
   printf("  setup Keypad\r\n");
   //cur_keycode.keyevent = KEY_NONE;
-  uint8_t nrows = sizeof(kp_rows)/sizeof(kp_rows[0]);
-  uint8_t ncols = sizeof(kp_colums)/sizeof(kp_colums[0]);
 
-  kpad = new Keypad(nrows, kp_in, ncols, kp_out);
+  kpad = new Keypad(KP_NROWS, kp_rows, KP_NCOLS, kp_columns);
   printf("  attach Keypad callbacks\r\n");
   kpad->attach(&kp_cb, &kr_cb);
   printf("  start Keypad\r\n");
@@ -347,7 +360,7 @@
   printf("     DSP_RST=%d\r\n", DSP_RST);
   printf("     DSP_DC=%d\r\n", DSP_DC);
   dsp = new SSD1322(20000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS,
-		    DSP_RST, DSP_DC, "SSD1322");
+        DSP_RST, DSP_DC, "SSD1322");
 
   printf("  configure DSP\r\n");
   dsp->background(Black);    // set background to black
@@ -374,16 +387,16 @@
   RCC_OscInitTypeDef cfg;
   HAL_RCC_GetOscConfig(&cfg);
   if (cfg.HSEState == RCC_HSE_BYPASS)
-	  dsp->printf("HSE:EXT ");
+    dsp->printf("HSE:EXT ");
   else if (cfg.HSEState == RCC_HSE_ON)
-	  dsp->printf("HSE:XTAL ");
+    dsp->printf("HSE:XTAL ");
   else
-	  dsp->printf("HSE:OFF ");
+    dsp->printf("HSE:OFF ");
 
   if (cfg.HSIState == RCC_HSI_ON)
-	  dsp->printf("HSI:ON ");
+    dsp->printf("HSI:ON ");
   else
-	  dsp->printf("HSI:OFF ");
+    dsp->printf("HSI:OFF ");
 
   dsp->copy_to_lcd();
 
@@ -410,8 +423,8 @@
 
 void end_splashscreen(void)
 {
-	// print is forbidden here because we are in an ISR context here
-	//printf("End of splash screen CB\r\n");
+  // print is forbidden here because we are in an ISR context here
+  //printf("End of splash screen CB\r\n");
   splashscreen = false;
 }
 
@@ -429,7 +442,7 @@
     }
 
     printf("!! RST !! (gstate=%d, state=%d)\r\n",
-	   hp->gstate(), hp->state());
+     hp->gstate(), hp->state());
     //printf("Value is ... %X\n", hp->search());
     hp->startup();
   }
@@ -449,12 +462,12 @@
     /*
     if (must_refresh & mask) {
       for(uint8_t i=0; i<sizeof(zones)/sizeof(zones[0]); i++)
-	if (zones[i].flag == mask) {
-	  dsp->copy_to_lcd(zones[i].x0/4, (zones[i].x1+3)/4,
-			  zones[i].y0,   zones[i].y1);
-	  must_refresh &= ~mask;
-	  break;
-	}
+  if (zones[i].flag == mask) {
+    dsp->copy_to_lcd(zones[i].x0/4, (zones[i].x1+3)/4,
+        zones[i].y0,   zones[i].y1);
+    must_refresh &= ~mask;
+    break;
+  }
     }
     mask = mask << 1;
     if (mask == 0) {
@@ -463,7 +476,7 @@
     */
     pulse(0, false);
 
-	ThisThread::sleep_for(30);
+  ThisThread::sleep_for(30);
   }
 }
 
@@ -480,144 +493,144 @@
 
   if (cmd == 0xFF) // cls
   {
-	  dsp->clrbuff();
+    dsp->clrbuff();
   }
   else
   {
-	  //txt = (char *)malloc(strlen(intxt)+1);
-		strcpy(txt, intxt);
-		txtp = txt;
+    //txt = (char *)malloc(strlen(intxt)+1);
+    strcpy(txt, intxt);
+    txtp = txt;
 
-		pulse(1, true);
+    pulse(1, true);
 
-		// len = MAX_BUFF;
+    // len = MAX_BUFF;
 
-		for (i=0; i<sizeof(table)/sizeof(table[0]); ++i) {
-			if (table[i].cmd == cmd) {
-				bgcolor = table[i].bgcolor;
-				fgcolor = table[i].color;
-				dsp->background(bgcolor);
-				dsp->foreground(fgcolor);
-				dsp->set_font((unsigned char*) table[i].font);
-				oldv = table[i].buffer;
+    for (i=0; i<sizeof(table)/sizeof(table[0]); ++i) {
+      if (table[i].cmd == cmd) {
+        bgcolor = table[i].bgcolor;
+        fgcolor = table[i].color;
+        dsp->background(bgcolor);
+        dsp->foreground(fgcolor);
+        dsp->set_font((unsigned char*) table[i].font);
+        oldv = table[i].buffer;
 
-				dsp->locate(table[i].x0, table[i].y0);
+        dsp->locate(table[i].x0, table[i].y0);
 
-				if (table[i].fmt & 0x01)  // ASCII text
-				{
-					if (strncmp(oldv, txt, table[i].maxsize) != 0)
-					{
-						if (table[i].width > 0)
-							dsp->fillrect(table[i].x0,
-										  table[i].y0,
-										  table[i].x0 + table[i].width,
-										  table[i].y0 + table[i].font[2],
-										  bgcolor);
-						for (uint8_t k=0; ;k++)
-						{
-							if (txtp[k] == 0x00)
-							{
-								dsp->printf(txtp);
-								break;
-							}
-							if (txtp[k] == 0x09)
-							{ // \t is a special char for 'unselected' display value
-								txtp[k] = 0x00;
-								dsp->printf(txtp);
+        if (table[i].fmt & 0x01)  // ASCII text
+        {
+          if (strncmp(oldv, txt, table[i].maxsize) != 0)
+          {
+            if (table[i].width > 0)
+              dsp->fillrect(table[i].x0,
+                      table[i].y0,
+                      table[i].x0 + table[i].width,
+                      table[i].y0 + table[i].font[2],
+                      bgcolor);
+            for (uint8_t k=0; ;k++)
+            {
+              if (txtp[k] == 0x00)
+              {
+                dsp->printf(txtp);
+                break;
+              }
+              if (txtp[k] == 0x09)
+              { // \t is a special char for 'unselected' display value
+                txtp[k] = 0x00;
+                dsp->printf(txtp);
 
-								if (fgcolor == table[i].color)
-									fgcolor /= 2;
-								else
-									fgcolor = table[i].color;
-								dsp->foreground(fgcolor);
-								txtp = &(txtp[k+1]);
-								k = 0;
-							}
-						}
-						if (cmd == 0x00) // main area
-							must_refresh |= 0x01;
-						if (cmd == 0x0C) // channels area
-							must_refresh |= 0x04;
-					}
-				}
-				/*
-					if (table[i].fmt & 0x02 ) {
-					// hex
-					for (uint8_t j=0;; j++) {
-					if (txt[j] == 0x00)
-					break;
-					dsp->printf("%02X ", txt[j]);
-					}
-					for (uint8_t j=3*strlen(txt); j<table[i].maxsize; j++)
-					dsp->printf(" ");
-					}
-				*/
-				if (table[i].fmt & 0x08 )  // flag indicators
-				{
-					// flags
-					for (uint8_t j=0; j<max(nchar, table[i].maxsize) ; j++)
-					{
-						for (uint8_t k=0; k<8; k++)
-						{
-							if (1)
-							{ //(txt[j] & (1 << k) ) != (oldv[j] & (1 << k))) {
-								for (uint8_t l=0;
-										 l<(sizeof(flags)/sizeof(flags[0])); ++l)
-								{
-									if (flags[l].flag == ((j<<4) + k)) {
-										if (txtp[j] & (1 << k))
-										{
-											dsp->foreground(flags[l].reverse ? bgcolor : fgcolor);
-											dsp->background(flags[l].reverse ? fgcolor : bgcolor);
-										}
-										else
-										{
-											dsp->foreground(bgcolor);
-											dsp->background(bgcolor);
-										}
-										if (flags[l].msg != NULL)
-										{ // a string
-											dsp->locate(flags[l].x, flags[l].y);
-											dsp->printf(flags[l].msg);}
-										else
-										{ // an icon
-											Bitmap_s pic = {9, 10, 2, (char*) flags[l].icon};
-											dsp->Bitmap_BW(pic, flags[l].x, flags[l].y);
-										}
-										must_refresh = 1; //|= zones[m].flag;
-										break;
-									}
-								}
-							}
-						}
-					}
+                if (fgcolor == table[i].color)
+                  fgcolor /= 2;
+                else
+                  fgcolor = table[i].color;
+                dsp->foreground(fgcolor);
+                txtp = &(txtp[k+1]);
+                k = 0;
+              }
+            }
+            if (cmd == 0x00) // main area
+              must_refresh |= 0x01;
+            if (cmd == 0x0C) // channels area
+              must_refresh |= 0x04;
+          }
+        }
+        /*
+          if (table[i].fmt & 0x02 ) {
+          // hex
+          for (uint8_t j=0;; j++) {
+          if (txt[j] == 0x00)
+          break;
+          dsp->printf("%02X ", txt[j]);
+          }
+          for (uint8_t j=3*strlen(txt); j<table[i].maxsize; j++)
+          dsp->printf(" ");
+          }
+        */
+        if (table[i].fmt & 0x08 )  // flag indicators
+        {
+          // flags
+          for (uint8_t j=0; j<max(nchar, table[i].maxsize) ; j++)
+          {
+            for (uint8_t k=0; k<8; k++)
+            {
+              if (1)
+              { //(txt[j] & (1 << k) ) != (oldv[j] & (1 << k))) {
+                for (uint8_t l=0;
+                     l<(sizeof(flags)/sizeof(flags[0])); ++l)
+                {
+                  if (flags[l].flag == ((j<<4) + k)) {
+                    if (txtp[j] & (1 << k))
+                    {
+                      dsp->foreground(flags[l].reverse ? bgcolor : fgcolor);
+                      dsp->background(flags[l].reverse ? fgcolor : bgcolor);
+                    }
+                    else
+                    {
+                      dsp->foreground(bgcolor);
+                      dsp->background(bgcolor);
+                    }
+                    if (flags[l].msg != NULL)
+                    { // a string
+                      dsp->locate(flags[l].x, flags[l].y);
+                      dsp->printf(flags[l].msg);}
+                    else
+                    { // an icon
+                      Bitmap_s pic = {9, 10, 2, (char*) flags[l].icon};
+                      dsp->Bitmap_BW(pic, flags[l].x, flags[l].y);
+                    }
+                    must_refresh = 1; //|= zones[m].flag;
+                    break;
+                  }
+                }
+              }
+            }
+          }
 
-					// draw frames (Alarm and Channel)
-					for (uint8_t l=0;
-							 l<(sizeof(frames)/sizeof(frames[0])); ++l)
-					{
-						uint16_t color;
-						if (frames[l].flag & txt[0]) // frame flags are on the 1st byte only
-							color = fgcolor/6;
-						else
-							color = bgcolor;
-						dsp->hline(frames[l].x0+1, frames[l].x0+3, frames[l].y0, color);
-						dsp->hline(frames[l].x1-3, frames[l].x1-1, frames[l].y0, color);
-						dsp->hline(frames[l].x0+1, frames[l].x1-1, frames[l].y1, color);
+          // draw frames (Alarm and Channel)
+          for (uint8_t l=0;
+               l<(sizeof(frames)/sizeof(frames[0])); ++l)
+          {
+            uint16_t color;
+            if (frames[l].flag & txt[0]) // frame flags are on the 1st byte only
+              color = fgcolor/6;
+            else
+              color = bgcolor;
+            dsp->hline(frames[l].x0+1, frames[l].x0+3, frames[l].y0, color);
+            dsp->hline(frames[l].x1-3, frames[l].x1-1, frames[l].y0, color);
+            dsp->hline(frames[l].x0+1, frames[l].x1-1, frames[l].y1, color);
 
-						dsp->vline(frames[l].x0, frames[l].y0+1, frames[l].y1-1, color);
-						dsp->vline(frames[l].x1, frames[l].y0+1, frames[l].y1-1, color);
-					}
-				}
+            dsp->vline(frames[l].x0, frames[l].y0+1, frames[l].y1-1, color);
+            dsp->vline(frames[l].x1, frames[l].y0+1, frames[l].y1-1, color);
+          }
+        }
 
-				for(uint8_t j=0; j<table[i].maxsize; j++)
-					oldv[j] = txt[j];
-				break;
-			}
-		}
-		//free(txt);
-		//dsp->copy_to_lcd();
-		pulse(1, false);
+        for(uint8_t j=0; j<table[i].maxsize; j++)
+          oldv[j] = txt[j];
+        break;
+      }
+    }
+    //free(txt);
+    //dsp->copy_to_lcd();
+    pulse(1, false);
   }
 }
 
@@ -648,7 +661,7 @@
     {
       z = &zones[i];
       printf("Zone %d [%x]: %d, %d, %d, %d\n", i, z->flag,
-		z->x0, z->y0, z->x1, z->y1);
+    z->x0, z->y0, z->x1, z->y1);
       must_refresh = z->flag;
       wait(1);
     }
@@ -665,7 +678,7 @@
 
 void loop()
 { // run over and over
-	keycode_t key;
+  keycode_t key;
 
   unsigned int err[8];
   for (uint8_t i=0; i<8; i++)
@@ -677,90 +690,92 @@
   while(1) {
     p = qenc.getPulses();
     if (p != pp)
-	{
-		dsp->locate(0, 0);
-		dsp->printf("Pulses = %d  ", p);
-		  dsp->copy_to_lcd();
-		pp = p;
+  {
+    dsp->locate(0, 0);
+    dsp->printf("Pulses = %d  ", p);
+      dsp->copy_to_lcd();
+    pp = p;
     }
 
     if (knob != 0)
+  {
+    if (hp != NULL)
+    {
+    printf("Sending keycode %X\r\n", knob);
+    hp->sendkey(knob);
+    printf("   DONE\r\n");
+    }
+    else
+    {
+      dsp->locate(70, 0);
+      dsp->printf("Knob = %X  ", knob);
+      dsp->copy_to_lcd();
+    }
+    knob = 0;
+  }
+
+    if (!key_buf.empty()) //cur_keycode.keyevent != KEY_NONE)
 	{
-	  if (hp != NULL)
-	  {
-		printf("Sending keycode %X\r\n", knob);
-		hp->sendkey(knob);
-		printf("   DONE\r\n");
+	  key_buf.pop(key);
+	  printf("Keycode %dx%d: %s\r\n",
+			 key.row, key.col, key.keyevent==KEY_PRESSED?"pressed":"released");
+	  if (hp != NULL) {
+		hp->sendkey(kp_mapping[key.row][key.col]);
 	  }
 	  else
 	  {
-		  dsp->locate(70, 0);
-		  dsp->printf("Knob = %X  ", knob);
-		  dsp->copy_to_lcd();
+		dsp->locate(140, 0);
+		dsp->printf("KC: %dx%d[%X] %s",
+					key.row, key.col, kp_mapping[key.row][key.col],
+					key.keyevent==KEY_PRESSED?"PRE":"REL");
+		dsp->copy_to_lcd();
 	  }
-	  knob = 0;
-	}
-
-    if (!key_buf.empty()) //cur_keycode.keyevent != KEY_NONE)
-	{
-		key_buf.pop(key);
-		printf("Keycode %dx%d: %s\r\n",
-			   key.row, key.col, key.keyevent==KEY_PRESSED?"pressed":"released");
-		if (hp != NULL) {
-		}
-		else
-		{
-			dsp->locate(140, 0);
-			dsp->printf("KC: %dx%d: %s",
-						key.row, key.col,key.keyevent==KEY_PRESSED?"PRE":"REL");
-		  dsp->copy_to_lcd();
-		}
-		// cur_keycode.keyevent = KEY_NONE;
-	}
+	  // cur_keycode.keyevent = KEY_NONE;
+  }
 
 
     if ((hp != NULL) && (hp->cmd_available()))
       {
-		HPSerial::CMD cmd;
-		if (hp->pop(cmd))
-    	  {
+    HPSerial::CMD cmd;
+    if (hp->pop(cmd))
+        {
 #if defined(HAS_LED)
-			led = 1;
+      led = 1;
 #endif
-    		for (uint8_t i=0; i<7; i++)
-    	      if (hp->nerrors(i) > err[i]) {
-    			  printf("ERR: %d/%d/%d/%d/%d/%d/%d\r\n",
-    					 hp->nerrors(0),
-    					 hp->nerrors(1),
-    					 hp->nerrors(2),
-    					 hp->nerrors(3),
-    					 hp->nerrors(4),
-    					 hp->nerrors(5),
-    					 hp->nerrors(6)
-    					 );
-    			  break;
-    		  }
-    	    for (uint8_t i=0; i<7; i++)
-    	      err[i] = hp->nerrors(i);
+        for (uint8_t i=0; i<7; i++)
+            if (hp->nerrors(i) > err[i]) {
+            printf("ERR: %d/%d/%d/%d/%d/%d/%d\r\n",
+               hp->nerrors(0),
+               hp->nerrors(1),
+               hp->nerrors(2),
+               hp->nerrors(3),
+               hp->nerrors(4),
+               hp->nerrors(5),
+               hp->nerrors(6)
+               );
+            break;
+          }
+          for (uint8_t i=0; i<7; i++)
+            err[i] = hp->nerrors(i);
 
-    	    printf("CMD[%d] %02X", (int)cmd.id, cmd.cmd);
+          printf("CMD[%d] %02X", (int)cmd.id, cmd.cmd);
 
-    	    if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C))
-    	      printf(": '%s'\r\n", cmd.value);
-    	    else {
-    		  printf(":");
-    	      for (uint8_t i=0; i<cmd.size; i++)
-    			  printf("%02x ", cmd.value[i]);
-    	      printf("\r\n");
-    		}
-    	    show(cmd.cmd, cmd.value, cmd.size);
+          if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C))
+            printf(": '%s'\r\n", cmd.value);
+          else {
+          printf(":");
+            for (uint8_t i=0; i<cmd.size; i++)
+            printf("%02x ", cmd.value[i]);
+            printf("\r\n");
+        }
+          show(cmd.cmd, cmd.value, cmd.size);
 #if defined(HAS_LED)
-			led = 0;
+      led = 0;
 #endif
-    	  }
-	  }
-	//else
-	ThisThread::sleep_for(1);
+        }
+    }
+  //else
+  ThisThread::sleep_for(1);
   }
 }
 
@@ -769,27 +784,27 @@
   if(dir == 1) // turn right
     knob = 0x80;
   else         // turn left
-    knob = 0x83;
+  knob = 0x81;  // 83?
 }
 
 void kp_cb(uint8_t row, uint8_t col)
 {
-	keycode_t key;
+  keycode_t key;
     key.row = row;
     key.col = col;
     key.keyevent = KEY_PRESSED;
-	if(!key_buf.full())
-		key_buf.push(key);
+  if(!key_buf.full())
+    key_buf.push(key);
 }
 
 void kr_cb(uint8_t row, uint8_t col)
 {
-	keycode_t key;
+  keycode_t key;
     key.row = row;
     key.col = col;
     key.keyevent = KEY_RELEASED;
-	if(!key_buf.full())
-		key_buf.push(key);
+  if(!key_buf.full())
+    key_buf.push(key);
 }
 
 int main()
@@ -798,7 +813,7 @@
   printf("Main loop (noop)\r\n");
   while(1)
   {
-	  timeout_h();
-	  ThisThread::sleep_for(1);
+    timeout_h();
+    ThisThread::sleep_for(1);
   }
 }

mercurial