From 47e6026e0bcac3ef0aca6df1d107f78d2fda1d62 Mon Sep 17 00:00:00 2001 From: Joshua Reed Barney Date: Mon, 8 Jun 2020 17:44:31 -0600 Subject: [PATCH 1/3] Added ability to enable key presses for the diagonal directions on the analog joy stick --- joyBonnet.py | 114 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 25 deletions(-) diff --git a/joyBonnet.py b/joyBonnet.py index 9db1802..2f0688c 100644 --- a/joyBonnet.py +++ b/joyBonnet.py @@ -46,7 +46,12 @@ ANALOG_THRESH_NEG = -600 ANALOG_THRESH_POS = 600 -analog_states = [False, False, False, False] # up down left right +analog_states = [False, False, False, False, False, False, False, False] # up down left right up-left up-right down-right down-left +ENABLE_DIAGONAL_DIRECTIONS = False + +if ENABLE_DIAGONAL_DIRECTIONS: + ANALOG_THRESH_NEG = -300 + ANALOG_THRESH_POS = 300 KEYS= { # EDIT KEYCODES IN THIS TABLE TO YOUR PREFERENCES: # See /usr/include/linux/input.h for keycode names @@ -63,6 +68,10 @@ 1001: e.KEY_DOWN, # Analog down 1002: e.KEY_LEFT, # Analog left 1003: e.KEY_RIGHT, # Analog right + 1004: e.KP7, # Analog up-left + 1005: e.KP9, # Analog up-right + 1006: e.KP3, # Analog down-right + 1007: e.KP1, # Analog down-left } ###################################### ADS1015 microdriver ################################# @@ -167,29 +176,84 @@ def handle_button(pin): continue #print("(%d , %d)" % (x, y)) - if (y > ANALOG_THRESH_POS) and not analog_states[0]: - analog_states[0] = True - handle_button(1000) # send UP press - if (y < ANALOG_THRESH_POS) and analog_states[0]: - analog_states[0] = False - handle_button(1000) # send UP release - if (y < ANALOG_THRESH_NEG) and not analog_states[1]: - analog_states[1] = True - handle_button(1001) # send DOWN press - if (y > ANALOG_THRESH_NEG) and analog_states[1]: - analog_states[1] = False - handle_button(1001) # send DOWN release - if (x < ANALOG_THRESH_NEG) and not analog_states[2]: - analog_states[2] = True - handle_button(1002) # send LEFT press - if (x > ANALOG_THRESH_NEG) and analog_states[2]: - analog_states[2] = False - handle_button(1002) # send LEFT release - if (x > ANALOG_THRESH_POS) and not analog_states[3]: - analog_states[3] = True - handle_button(1003) # send RIGHT press - if (x < ANALOG_THRESH_POS) and analog_states[3]: - analog_states[3] = False - handle_button(1003) # send RIGHT release + if ENABLE_DIAGONAL_DIRECTIONS: # Cardinal and Diagonal directions + + # Cardinal directions + if (y > ANALOG_THRESH_POS) and (not analog_states[0]) and not (x > ANALOG_THRESH_POS or x < ANALOG_THRESH_NEG): + analog_states[0] = True + handle_button(1000) # send UP press + if (y < ANALOG_THRESH_POS) and analog_states[0]: + analog_states[0] = False + handle_button(1000) # send UP release + if (y < ANALOG_THRESH_NEG) and (not analog_states[1]) and not (x > ANALOG_THRESH_POS or x < ANALOG_THRESH_NEG): + analog_states[1] = True + handle_button(1001) # send DOWN press + if (y > ANALOG_THRESH_NEG) and analog_states[1]: + analog_states[1] = False + handle_button(1001) # send DOWN release + if (x < ANALOG_THRESH_NEG) and (not analog_states[2]) and not (y > ANALOG_THRESH_POS or y < ANALOG_THRESH_NEG): + analog_states[2] = True + handle_button(1002) # send LEFT press + if (x > ANALOG_THRESH_NEG) and analog_states[2]: + analog_states[2] = False + handle_button(1002) # send LEFT release + if (x > ANALOG_THRESH_POS) and (not analog_states[3]) and not (y > ANALOG_THRESH_POS or y < ANALOG_THRESH_NEG): + analog_states[3] = True + handle_button(1003) # send RIGHT press + if (x < ANALOG_THRESH_POS) and analog_states[3]: + analog_states[3] = False + handle_button(1003) # send RIGHT release + + # Diagonal directions + if (y > ANALOG_THRESH_POS and x < ANALOG_THRESH_NEG) and not analog_states[4]: + analog_states[4] = True + handle_button(1004) # send UP-LEFT press + if (y < ANALOG_THRESH_POS or x > ANALOG_THRESH_NEG) and analog_states[4]: + analog_states[4] = False + handle_button(1004) # send UP-LEFT release + if (y > ANALOG_THRESH_POS and x > ANALOG_THRESH_POS) and not analog_states[5]: + analog_states[5] = True + handle_button(1005) # send UP-RIGHT press + if (y < ANALOG_THRESH_POS or x < ANALOG_THRESH_POS) and analog_states[5]: + analog_states[5] = False + handle_button(1005) # send UP-RIGHT release + if (x > ANALOG_THRESH_POS and y < ANALOG_THRESH_NEG) and not analog_states[6]: + analog_states[6] = True + handle_button(1006) # send DOWN-RIGHT press + if (x < ANALOG_THRESH_POS or y > ANALOG_THRESH_NEG) and analog_states[6]: + analog_states[6] = False + handle_button(1006) # send DOWN-RIGHT release + if (x < ANALOG_THRESH_NEG and y < ANALOG_THRESH_NEG) and not analog_states[7]: + analog_states[7] = True + handle_button(1007) # send DOWN-LEFT press + if (x > ANALOG_THRESH_NEG or y > ANALOG_THRESH_NEG) and analog_states[7]: + analog_states[7] = False + handle_button(1007) # send DOWN-LEFT release + + else: # Only Cardinal directions + if (y > ANALOG_THRESH_POS) and not analog_states[0]: + analog_states[0] = True + handle_button(1000) # send UP press + if (y < ANALOG_THRESH_POS) and analog_states[0]: + analog_states[0] = False + handle_button(1000) # send UP release + if (y < ANALOG_THRESH_NEG) and not analog_states[1]: + analog_states[1] = True + handle_button(1001) # send DOWN press + if (y > ANALOG_THRESH_NEG) and analog_states[1]: + analog_states[1] = False + handle_button(1001) # send DOWN release + if (x < ANALOG_THRESH_NEG) and not analog_states[2]: + analog_states[2] = True + handle_button(1002) # send LEFT press + if (x > ANALOG_THRESH_NEG) and analog_states[2]: + analog_states[2] = False + handle_button(1002) # send LEFT release + if (x > ANALOG_THRESH_POS) and not analog_states[3]: + analog_states[3] = True + handle_button(1003) # send RIGHT press + if (x < ANALOG_THRESH_POS) and analog_states[3]: + analog_states[3] = False + handle_button(1003) # send RIGHT release time.sleep(0.01) From bf0930f8f6b3ff7476ee3d3ecfd59a4e2c61db21 Mon Sep 17 00:00:00 2001 From: Joshua Reed Barney Date: Mon, 8 Jun 2020 18:54:27 -0600 Subject: [PATCH 2/3] Fixed indentation --- joyBonnet.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/joyBonnet.py b/joyBonnet.py index 2f0688c..24a3e3d 100644 --- a/joyBonnet.py +++ b/joyBonnet.py @@ -54,20 +54,20 @@ ANALOG_THRESH_POS = 300 KEYS= { # EDIT KEYCODES IN THIS TABLE TO YOUR PREFERENCES: - # See /usr/include/linux/input.h for keycode names - # Keyboard Bonnet EmulationStation - BUTTON_A: e.KEY_LEFTCTRL, # 'A' button - BUTTON_B: e.KEY_LEFTALT, # 'B' button - BUTTON_X: e.KEY_Z, # 'X' button - BUTTON_Y: e.KEY_X, # 'Y' button - SELECT: e.KEY_SPACE, # 'Select' button - START: e.KEY_ENTER, # 'Start' button - PLAYER1: e.KEY_1, # '#1' button - PLAYER2: e.KEY_2, # '#2' button - 1000: e.KEY_UP, # Analog up - 1001: e.KEY_DOWN, # Analog down - 1002: e.KEY_LEFT, # Analog left - 1003: e.KEY_RIGHT, # Analog right + # See /usr/include/linux/input.h for keycode names + # Keyboard Bonnet EmulationStation + BUTTON_A: e.KEY_LEFTCTRL, # 'A' button + BUTTON_B: e.KEY_LEFTALT, # 'B' button + BUTTON_X: e.KEY_Z, # 'X' button + BUTTON_Y: e.KEY_X, # 'Y' button + SELECT: e.KEY_SPACE, # 'Select' button + START: e.KEY_ENTER, # 'Start' button + PLAYER1: e.KEY_1, # '#1' button + PLAYER2: e.KEY_2, # '#2' button + 1000: e.KEY_UP, # Analog up + 1001: e.KEY_DOWN, # Analog down + 1002: e.KEY_LEFT, # Analog left + 1003: e.KEY_RIGHT, # Analog right 1004: e.KP7, # Analog up-left 1005: e.KP9, # Analog up-right 1006: e.KP3, # Analog down-right From 973c01bf122813dd99bc49870b960bf6061eca0b Mon Sep 17 00:00:00 2001 From: Joshua Reed Barney Date: Mon, 8 Jun 2020 18:55:39 -0600 Subject: [PATCH 3/3] Added tabs for KEYS dict --- joyBonnet.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/joyBonnet.py b/joyBonnet.py index 24a3e3d..16ca446 100644 --- a/joyBonnet.py +++ b/joyBonnet.py @@ -54,24 +54,24 @@ ANALOG_THRESH_POS = 300 KEYS= { # EDIT KEYCODES IN THIS TABLE TO YOUR PREFERENCES: - # See /usr/include/linux/input.h for keycode names - # Keyboard Bonnet EmulationStation - BUTTON_A: e.KEY_LEFTCTRL, # 'A' button - BUTTON_B: e.KEY_LEFTALT, # 'B' button - BUTTON_X: e.KEY_Z, # 'X' button - BUTTON_Y: e.KEY_X, # 'Y' button - SELECT: e.KEY_SPACE, # 'Select' button - START: e.KEY_ENTER, # 'Start' button - PLAYER1: e.KEY_1, # '#1' button - PLAYER2: e.KEY_2, # '#2' button - 1000: e.KEY_UP, # Analog up - 1001: e.KEY_DOWN, # Analog down - 1002: e.KEY_LEFT, # Analog left - 1003: e.KEY_RIGHT, # Analog right - 1004: e.KP7, # Analog up-left - 1005: e.KP9, # Analog up-right - 1006: e.KP3, # Analog down-right - 1007: e.KP1, # Analog down-left + # See /usr/include/linux/input.h for keycode names + # Keyboard Bonnet EmulationStation + BUTTON_A: e.KEY_LEFTCTRL, # 'A' button + BUTTON_B: e.KEY_LEFTALT, # 'B' button + BUTTON_X: e.KEY_Z, # 'X' button + BUTTON_Y: e.KEY_X, # 'Y' button + SELECT: e.KEY_SPACE, # 'Select' button + START: e.KEY_ENTER, # 'Start' button + PLAYER1: e.KEY_1, # '#1' button + PLAYER2: e.KEY_2, # '#2' button + 1000: e.KEY_UP, # Analog up + 1001: e.KEY_DOWN, # Analog down + 1002: e.KEY_LEFT, # Analog left + 1003: e.KEY_RIGHT, # Analog right + 1004: e.KP7, # Analog up-left + 1005: e.KP9, # Analog up-right + 1006: e.KP3, # Analog down-right + 1007: e.KP1, # Analog down-left } ###################################### ADS1015 microdriver #################################