Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagonal Directions for Analog Joy Stick on the Joy Bonnet #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 89 additions & 25 deletions joyBonnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 #################################
Expand Down Expand Up @@ -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)