This repository was archived by the owner on Jan 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjack.py
More file actions
69 lines (58 loc) · 1.98 KB
/
jack.py
File metadata and controls
69 lines (58 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from machine import Pin
class Jack:
def __init__(self, jack):
if jack == 'L': # Left (ground is brown)
self.pins = {
't': Pin(3), # Green wire
'r': Pin(4), # White wire
's': Pin(2) # Purple wire
}
elif jack == 'R': # Right (ground is brown)
self.pins = {
't': Pin(7), # Green wire
'r': Pin(8), # White wire
's': Pin(6) # Purple wire
}
elif jack == 'C': # USB-C (red is 5v, brown is ground)
self.pins = {
'plus': Pin(21), # Orange wire
'min': Pin(22) # Green wire
}
else:
raise ValueError("Invalid interface type. Choose 'L' (for Left jack), 'R' (for Right jack), or 'C' (for USB-C port).")
self.toggle_state = False
@staticmethod
def set_pin_mode(pin, mode, pull=None):
if pull:
pin.init(mode=mode, pull=pull)
else:
pin.init(mode=mode)
def _read(self, pin, pull=None):
self.set_pin_mode(pin, Pin.IN, pull)
return pin.value()
def _write(self, pin):
self.set_pin_mode(pin, Pin.OUT)
pin.on()
def _off(self, pin):
self.set_pin_mode(pin, Pin.OUT)
pin.off()
def read(self, pull=None):
results = {k: self._read(v, pull) for k, v in self.pins.items()}
print(f"Read {self.__class__.__name__}:", results)
def write(self):
for pin in self.pins.values():
self._write(pin)
print(f"{self.__class__.__name__} on")
def off(self):
for pin in self.pins.values():
self._off(pin)
print(f"{self.__class__.__name__} off")
def toggle_write(self):
if self.toggle_state:
self.off()
else:
self.write()
self.toggle_state = not self.toggle_state
return str(self.toggle_state)
def shutdown(self):
self.off()