From f4aa2f8461231f742fb24ca7ed0dafaafa00d712 Mon Sep 17 00:00:00 2001 From: Sebastian Hardt Date: Wed, 14 Jan 2015 23:59:33 +0100 Subject: [PATCH 1/3] Added half step support for rotary --- config.c | 65 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/config.c b/config.c index faf4f2c..6258c4b 100644 --- a/config.c +++ b/config.c @@ -20,7 +20,7 @@ You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. + 02111-1307 USA. */ #include @@ -86,7 +86,7 @@ static int xio_count = 0; static gpio_rotary_s *gpio_rotary[NUM_GPIO]; static gpio_rotary_s *last_gpio_rotary = NULL; static int last_gpio_rotary_idx = NUM_GPIO; - + static int SP; static keyinfo_s KI; @@ -499,15 +499,15 @@ static void setup_xio(int xio) switch(xio_dev[xio].type){ case IO_MCP23008: - write_iic(addr, 0, cfg_dat, 7); + write_iic(addr, 0, cfg_dat, 7); printf("Configuring MCP23008\n"); break; case IO_MCP23017A: - write_iic(addr, 0, cfg_dat, 7); + write_iic(addr, 0, cfg_dat, 7); printf("Configuring MCP23017 port A\n"); break; case IO_MCP23017B: - write_iic(addr, 0x10, cfg_dat, 7); + write_iic(addr, 0x10, cfg_dat, 7); printf("Configuring MCP23017 port B\n"); break; default: @@ -639,33 +639,57 @@ int got_more_rotaries(void) return last_gpio_rotary_idx < NUM_GPIO; } -#define R_START 0x0 -#define R_CW_FINAL 0x1 -#define R_CW_BEGIN 0x2 -#define R_CW_NEXT 0x3 +#define R_START 0x0 +#define DIR_CW 0x10 +#define DIR_CCW 0x20 + + +#define R_CCW_BEGIN_H 0x1 +#define R_CW_BEGIN 0x2 +#define R_START_M 0x3 +#define R_CW_BEGIN_M 0x4 +#define R_CCW_BEGIN_M 0x5 + +const unsigned char half_rotary_states[6][4] = { + // R_START (00) +{R_START_M, R_CW_BEGIN, R_CCW_BEGIN_H, R_START}, +// R_CCW_BEGIN_H +{R_START_M | DIR_CCW, R_START, R_CCW_BEGIN_H, R_START}, +// R_CW_BEGIN +{R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START}, +// R_START_M (11) +{R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START}, +// R_CW_BEGIN_M +{R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW}, +// R_CCW_BEGIN_M +{R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW}, +}; + +#define R_CW_FINAL 0x1 +#define R_CW_BEGIN 0x2 +#define R_CW_NEXT 0x3 #define R_CCW_BEGIN 0x4 #define R_CCW_FINAL 0x5 -#define R_CCW_NEXT 0x6 -#define DIR_CW 0x10 -#define DIR_CCW 0x20 +#define R_CCW_NEXT 0x6 const unsigned char rotary_states[7][4] = { - {R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START}, // R_START - {R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW}, // R_CW_FINAL - {R_CW_NEXT, R_CW_BEGIN, R_START, R_START}, // R_CW_BEGIN - {R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START}, // R_CW_NEXT - {R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START}, // R_CCW_BEGIN - {R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},// R_CCW_FINAL - {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START}, // R_CCW_NEXT +{R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START}, // R_START +{R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW}, // R_CW_FINAL +{R_CW_NEXT, R_CW_BEGIN, R_START, R_START}, // R_CW_BEGIN +{R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START}, // R_CW_NEXT +{R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START}, // R_CCW_BEGIN +{R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},// R_CCW_FINAL +{R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START}, // R_CCW_NEXT }; + static int update_rotary_keys_sm(int gpio_state, gpio_rotary_s* rotary_ctrl) { int bit_a = (gpio_state & rotary_ctrl->gpio_a_mask) ? 1 : 0; int bit_b = (gpio_state & rotary_ctrl->gpio_b_mask) ? 1 : 0; int encoder_bits = bit_a | bit_b << 1; - rotary_ctrl->last_state = rotary_states[rotary_ctrl->last_state & 0xf][encoder_bits]; + rotary_ctrl->last_state = half_rotary_states[rotary_ctrl->last_state & 0xf][encoder_bits]; switch(rotary_ctrl->last_state & 0x30) { @@ -688,4 +712,3 @@ int get_next_rotary_key(int gpio_state) advance_rotary(); return k; } - From 05cbc49e01e1f5931e72c19073f8024c1a7d9915 Mon Sep 17 00:00:00 2001 From: Sebastian Hardt Date: Thu, 15 Jan 2015 00:06:23 +0100 Subject: [PATCH 2/3] Started adding half as a config for rotary --- config.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.c b/config.c index 6258c4b..8382cb7 100644 --- a/config.c +++ b/config.c @@ -108,7 +108,7 @@ int init_config(void) FILE *fp; char ln[MAX_LN]; int lnno = 0; - char name[32], xname[32], keya[32], keyb[32]; + char name[32], xname[32], keya[32], keyb[32], rotHalf[5]; char conffile[80]; int gpio,caddr,regno,gpio2; @@ -148,7 +148,7 @@ int init_config(void) } } else if(strstr(ln, "ROT") == ln){ - n=sscanf(ln, "%s %d %d %s %s", name, &gpio, &gpio2, &keya, &keyb); + n=sscanf(ln, "%s %d %d %s %s %s", name, &gpio, &gpio2, &keya, &keyb, &rotHalf); if (n == 5) { ka = find_key(keya); kb = find_key(keyb); From d5413a54b87199f9a53f7522f41c080c0e871e79 Mon Sep 17 00:00:00 2001 From: Sebastian Hardt Date: Thu, 15 Jan 2015 00:36:06 +0100 Subject: [PATCH 3/3] Finished adding half step --- config.c | 28 +++++++++++++++++++--------- pikeyd.conf | 4 +++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/config.c b/config.c index 8382cb7..d11ca77 100644 --- a/config.c +++ b/config.c @@ -54,6 +54,7 @@ typedef struct _gpio_rotary{ int gpio_b_mask; int last_state; int direction; + int rotHalf; struct _gpio_rotary *next; }gpio_rotary_s; @@ -71,7 +72,7 @@ typedef struct{ static int find_key(const char *name); static void add_event(gpio_key_s **ev, int gpio, int key, int xio); -static void add_rotary(gpio_rotary_s **rot, int gpio_a, int gpio_b, int key_a, int key_b); +static void add_rotary(gpio_rotary_s **rot, int gpio_a, int gpio_b, int key_a, int key_b, int rotHalf); static gpio_key_s *get_event(gpio_key_s *ev, int idx); static int find_xio(const char *name); static void setup_xio(int xio); @@ -108,9 +109,9 @@ int init_config(void) FILE *fp; char ln[MAX_LN]; int lnno = 0; - char name[32], xname[32], keya[32], keyb[32], rotHalf[5]; + char name[32], xname[32], keya[32], keyb[32]; char conffile[80]; - int gpio,caddr,regno,gpio2; + int gpio,caddr,regno,gpio2,rotHalf; for(i=0;i 0) ? 1 : 0; + printf("HALF = %d\n",rotHalf); + //printf("%d ROT = %d/%d %04x:[%s] %04x:[%s]\n", lnno, gpio, gpio2, ka, keya, kb, keyb); - add_rotary(&gpio_rotary[gpio], gpio, gpio2, key_names[ka].code, key_names[kb].code); + add_rotary(&gpio_rotary[gpio], gpio, gpio2, key_names[ka].code, key_names[kb].code,rotHalf); } else { printf("Line %d: unknown key(s) for rotary encoder: %s, %s\n", lnno, keya, keyb); @@ -325,11 +330,11 @@ static void add_event(gpio_key_s **ev, int gpio, int key, int xio) } } -static void add_rotary(gpio_rotary_s **rot, int gpio_a, int gpio_b, int key_a, int key_b) +static void add_rotary(gpio_rotary_s **rot, int gpio_a, int gpio_b, int key_a, int key_b, int rotHalf) { if(*rot){ /* Recursive call to add the next link in the list */ - add_rotary(&(*rot)->next, gpio_a, gpio_b, key_a, key_b); + add_rotary(&(*rot)->next, gpio_a, gpio_b, key_a, key_b, rotHalf); } else{ *rot = malloc(sizeof(gpio_rotary_s)); @@ -346,6 +351,7 @@ static void add_rotary(gpio_rotary_s **rot, int gpio_a, int gpio_b, int key_a, i (*rot)->next = NULL; (*rot)->last_state = 0; (*rot)->direction = 0; + (*rot)->rotHalf = rotHalf; } } } @@ -689,7 +695,11 @@ static int update_rotary_keys_sm(int gpio_state, gpio_rotary_s* rotary_ctrl) int bit_b = (gpio_state & rotary_ctrl->gpio_b_mask) ? 1 : 0; int encoder_bits = bit_a | bit_b << 1; - rotary_ctrl->last_state = half_rotary_states[rotary_ctrl->last_state & 0xf][encoder_bits]; + if(rotary_ctrl->rotHalf == 1) { + rotary_ctrl->last_state = half_rotary_states[rotary_ctrl->last_state & 0xf][encoder_bits]; + } else { + rotary_ctrl->last_state = rotary_states[rotary_ctrl->last_state & 0xf][encoder_bits]; + } switch(rotary_ctrl->last_state & 0x30) { diff --git a/pikeyd.conf b/pikeyd.conf index 1f8810c..0b05b4e 100644 --- a/pikeyd.conf +++ b/pikeyd.conf @@ -32,7 +32,9 @@ KEY_L 21 KEY_O 21 # Set up a rotary encoder generating up & down arrow keys, using GPIOs 4 and 17 for gray code input -ROT 4 17 KEY_DOWN KEY_UP +ROT 4 17 KEY_DOWN KEY_UP 0 +# Same as above only it uses a rotary which works with half steps each click +ROT 5 18 KEY_DOWN KEY_UP 1 #define I/O expanders before using them #XIO(tag) gpio_int_pin/chip_addr:register_no