Hal component for a matrix keyboard

More
16 Oct 2016 16:37 #81691 by gaston48
Hello,
Why in the real-time space, this component, containing 2 imbricated loops, does not work?
But It works in userspace with some usleep .
It is to decode a matrix of keyboard.
Thank you for help.
/* ********************************************************* */
component keyb2 ;

pin out bit    lin# [4]  ;  
pin in  bit  coltc# [4]   ;  
pin out bit  touch# [16]  ;  

function _;
license "GPL";
;;

unsigned  c = 0 ;
unsigned  l = 0 ;

FUNCTION(_) 
{
 for  (l = 0 ; l < 4 ; l++) 
  {
    lin(l) = 1 ;     
      for (c = 0 ; c < 4 ; c++)
      { 
      if ( coltc(c) ) touch( (l * 4) + c ) = 1;                     
      else            touch( (l * 4) + c ) = 0;
      }
    lin(l) = 0 ;
  }
}
/* ********************************************************* */
/* ********************************************************* */
component keyb3 ;
option userspace yes ;

pin out bit    lin# [4]  ;  
pin in  bit  coltc# [4]   ;  
pin out bit  touch# [16]  ;  

license "GPL";
;;

#include <unistd.h>

void user_mainloop(void) 
{

unsigned  c = 0 ;
unsigned  l = 0 ;

 while (1) 
 {
  FOR_ALL_INSTS()
  { 
  for  (l = 0 ; l < 4 ; l++) 
   {
    lin(l) = 1 ; 
    usleep(1000);    
      for (c = 0 ; c < 4 ; c++)
      { 
      if ( coltc(c) ) touch( (l * 4) + c ) = 1;                     
      else            touch( (l * 4) + c ) = 0;
      }
    lin(l) = 0 ;
    usleep(200);
   }
  }
 }
}
/* ********************************************************* */

Please Log in or Create an account to join the conversation.

More
16 Oct 2016 18:48 - 16 Oct 2016 18:49 #81695 by PCW
The keyb2 real time component cannot work properly since the loop will run to completion (in nanoseconds) every thread invocation so the pins (as seen by other hal components) will only reflect the values at the end of the loop

For this to work it should advance one step in the loop per thread invocation, which means you need to maintain the state (loop variables) between invocations.
Last edit: 16 Oct 2016 18:49 by PCW.

Please Log in or Create an account to join the conversation.

More
16 Oct 2016 19:08 #81697 by cmorley
Note there is already a matrix keyboard decoder in linuxcnc.
One could use it or use it as a starting point.

www.linuxcnc.org/docs/2.7/html/man/man9/matrix_kb.9.html

Chris M

Please Log in or Create an account to join the conversation.

More
18 Oct 2016 09:41 - 18 Oct 2016 09:44 #81763 by gaston48
Thank you for your answers,
I imagine the constraints of real time and
I'm amazed by the performance applied to the servo with Mesa.
Andy component also uses loops "for" imbricated but requires
a slow servo thread. ?
A slow servo thread is not compatible with a control loop ?

git.linuxcnc.org/gitweb?p=linuxcnc.git;a...62b5d63242cd;hb=HEAD

git.linuxcnc.org/gitweb?p=linuxcnc.git;a...346e27306ba8;hb=HEAD

My keyboard also includes two gray switches and somme LEDs
/* ********************************************************* */
component keyb0 ;
option userspace yes ;

pin out bit    lin# [12]  ;  
pin in  bit  coltc# [8]   ;  
pin out bit  touch# [88]  ;  
pin out bit  colld# [3]   ;  
pin in  bit    led# [36]  ;  

pin out float out0 ;   // manivelle
pin out float out1 ;   // incrément de déplacement
pin out float out2 ;   // override

license "GPL";
;; 
#include <unistd.h>

unsigned   tab_gray[16] = {0,1,3,2,7,6,4,5,15,14,12,13,8,9,11,10};
float tab_manivelle[16] = {100,10,1,0,0,0,0,0,0,0,0,0,0,0,0,0};
float tab_increment[16] = {0,0,0,1,10,100,1000,10000,1e6,0,0,0,0,0,0,0};
float tab_override [16] = {0,0.02,0.04,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.1,1.20,0};

void user_mainloop(void) 
{  
unsigned  c = 0 ;
unsigned  l = 0 ;
unsigned  index_commut1 = 0 ;
unsigned  index_commut2 = 0 ;

 while (1) 
 {
  FOR_ALL_INSTS()
  { 
   for  (l = 0 ; l < 12 ; l++) 
   {
    lin(l) = 1 ; 
    usleep(1000);
    
    colld(0) = 1;
    colld(1) = 1;
    colld(2) = 1;

    for (c = 0 ; c < 3 ; c++)
    {
    if ( led ((l * 3) + c ))  { colld(c) = 0 ;  }
    }

    for (c = 0 ; c < 8 ; c++)
    {       
    if ( coltc(c) ) {touch( (l * 8) + c ) = 1;                                  
    else             touch( (l * 8) + c ) = 0;
    }
                
      if ( l == 11)    // ligne 12 on lit les commutateurs
      {
     // récupération de la position du commutateur 1
     index_commut1 = ((unsigned)coltc(0)) +  ((unsigned)coltc(1)<<1) + ((unsigned)coltc(2)<<2) + ((unsigned)coltc(3)<<3);
    
     // récupération de la position du commutateur 2
     index_commut2 = ((unsigned)coltc(4)) +  ((unsigned)coltc(5)<<1) + ((unsigned)coltc(6)<<2) + ((unsigned)coltc(7)<<3);
   
      out0 =  tab_manivelle[tab_gray[index_commut1]] ;
      out1 =  tab_increment[tab_gray[index_commut1]] ;
      out2 =  tab_override [tab_gray[index_commut2]] ;
      }   

     lin(l) = 0 ;
     usleep(200);
   }
  }    
 }
}  
/* ********************************************************* */             
Last edit: 18 Oct 2016 09:44 by gaston48.

Please Log in or Create an account to join the conversation.

More
05 Aug 2021 15:33 - 05 Aug 2021 15:34 #216987 by nkp
what's the point not_to_use a component matrix_kb?
what do we win?
Last edit: 05 Aug 2021 15:34 by nkp.

Please Log in or Create an account to join the conversation.

Time to create page: 0.123 seconds
Powered by Kunena Forum