Skip to content

Commit be1a211

Browse files
authored
Merge pull request #120 from adafruit/pc_joystick_example
Adding example for pc joystick
2 parents b317568 + 9a2df8c commit be1a211

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

examples/seesaw_pc_joystick.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SPDX-FileCopyrightText: 2023 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
from micropython import const
8+
from adafruit_seesaw.seesaw import Seesaw
9+
10+
BUTTON_1 = const(3)
11+
BUTTON_2 = const(13)
12+
BUTTON_3 = const(2)
13+
BUTTON_4 = const(14)
14+
15+
JOY1_X = const(1)
16+
JOY1_Y = const(15)
17+
JOY2_X = const(0)
18+
JOY2_Y = const(16)
19+
20+
button_mask = const(
21+
(1 << BUTTON_1) | (1 << BUTTON_2) | (1 << BUTTON_3) | (1 << BUTTON_4)
22+
)
23+
24+
i2c_bus = board.STEMMA_I2C() # The built-in STEMMA QT connector on the microcontroller
25+
# i2c_bus = board.I2C() # Uses board.SCL and board.SDA. Use with breadboard.
26+
27+
seesaw = Seesaw(i2c_bus, addr=0x49)
28+
29+
seesaw.pin_mode_bulk(button_mask, seesaw.INPUT_PULLUP)
30+
31+
last_x = 0
32+
last_y = 0
33+
x = 0
34+
y = 0
35+
36+
while True:
37+
# These joysticks are really jittery so let's take 4 samples of each axis
38+
for i in range(4):
39+
x += seesaw.analog_read(JOY1_X)
40+
y += seesaw.analog_read(JOY1_Y)
41+
42+
# take average reading
43+
x /= 4
44+
y /= 4
45+
46+
# PC joysticks aren't true voltage divider because we have a fixed 10K
47+
# we dont know the normalized value so we're just going to give you
48+
# the result in 'Kohms' for easier printing
49+
50+
x = 1024 / x - 1
51+
y = 1024 / y - 1
52+
53+
if (abs(x - last_x) > 3) or (abs(y - last_y) > 3):
54+
print(x, y)
55+
last_x = x
56+
last_y = y
57+
58+
buttons = seesaw.digital_read_bulk(button_mask)
59+
60+
if not buttons & (1 << BUTTON_1):
61+
print("Button 1 pressed")
62+
63+
if not buttons & (1 << BUTTON_2):
64+
print("Button 2 pressed")
65+
66+
if not buttons & (1 << BUTTON_3):
67+
print("Button 3 pressed")
68+
69+
if not buttons & (1 << BUTTON_4):
70+
print("Button 4 pressed")
71+
72+
time.sleep(0.01)

0 commit comments

Comments
 (0)