Skip to content

Adding example for pc joystick #120

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

Merged
merged 1 commit into from
Aug 11, 2023
Merged
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
72 changes: 72 additions & 0 deletions examples/seesaw_pc_joystick.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# SPDX-FileCopyrightText: 2023 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import board
from micropython import const
from adafruit_seesaw.seesaw import Seesaw

BUTTON_1 = const(3)
BUTTON_2 = const(13)
BUTTON_3 = const(2)
BUTTON_4 = const(14)

JOY1_X = const(1)
JOY1_Y = const(15)
JOY2_X = const(0)
JOY2_Y = const(16)

button_mask = const(
(1 << BUTTON_1) | (1 << BUTTON_2) | (1 << BUTTON_3) | (1 << BUTTON_4)
)

i2c_bus = board.STEMMA_I2C() # The built-in STEMMA QT connector on the microcontroller
# i2c_bus = board.I2C() # Uses board.SCL and board.SDA. Use with breadboard.

seesaw = Seesaw(i2c_bus, addr=0x49)

seesaw.pin_mode_bulk(button_mask, seesaw.INPUT_PULLUP)

last_x = 0
last_y = 0
x = 0
y = 0

while True:
# These joysticks are really jittery so let's take 4 samples of each axis
for i in range(4):
x += seesaw.analog_read(JOY1_X)
y += seesaw.analog_read(JOY1_Y)

# take average reading
x /= 4
y /= 4

# PC joysticks aren't true voltage divider because we have a fixed 10K
# we dont know the normalized value so we're just going to give you
# the result in 'Kohms' for easier printing

x = 1024 / x - 1
y = 1024 / y - 1

if (abs(x - last_x) > 3) or (abs(y - last_y) > 3):
print(x, y)
last_x = x
last_y = y

buttons = seesaw.digital_read_bulk(button_mask)

if not buttons & (1 << BUTTON_1):
print("Button 1 pressed")

if not buttons & (1 << BUTTON_2):
print("Button 2 pressed")

if not buttons & (1 << BUTTON_3):
print("Button 3 pressed")

if not buttons & (1 << BUTTON_4):
print("Button 4 pressed")

time.sleep(0.01)