Skip to content

Commit c529622

Browse files
jmriegoGO63-samarajmriego
authored
Four hats (#38)
* Up to four HatSwitches are now supported (#36) * More readable creation of HidReportDescriptor * Up to four HatSwitches are now supported * refactoring * fix issues with hid * testing 4 hats * fix for other number of hats * add script for testing arduino with a gamepad * go back to normal sketch * simplify code * fix code for variable number of hats --------- Co-authored-by: GO63-samara <[email protected]> Co-authored-by: jmriego <[email protected]>
1 parent cd8387a commit c529622

File tree

5 files changed

+530
-270
lines changed

5 files changed

+530
-270
lines changed

Diff for: python/libs/robust_serial/robust_serial.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import struct
2+
3+
from enum import Enum
4+
5+
6+
class Order(Enum):
7+
"""
8+
Pre-defined orders
9+
"""
10+
HELLO = 0
11+
ALREADY_CONNECTED = 1
12+
RECEIVED = 2
13+
FORCES = 3
14+
POSITION = 4
15+
ERROR = 5
16+
LOG = 6
17+
VERSION = 7
18+
19+
def read_order(f):
20+
"""
21+
:param f: file handler or serial file
22+
:return: (Order Enum Object)
23+
"""
24+
return Order(read_i8(f))
25+
26+
def read_i8(f):
27+
"""
28+
:param f: file handler or serial file
29+
:return: (int8_t)
30+
"""
31+
return struct.unpack('<b', bytearray(f.read(1)))[0]
32+
33+
34+
def read_i16(f):
35+
"""
36+
:param f: file handler or serial file
37+
:return: (int16_t)
38+
"""
39+
return struct.unpack('<h', bytearray(f.read(2)))[0]
40+
41+
42+
def read_i32(f):
43+
"""
44+
:param f: file handler or serial file
45+
:return: (int32_t)
46+
"""
47+
return struct.unpack('<l', bytearray(f.read(4)))[0]
48+
49+
50+
def write_i8(f, value):
51+
"""
52+
:param f: file handler or serial file
53+
:param value: (int8_t)
54+
"""
55+
if -128 <= value <= 127:
56+
f.write(struct.pack('<b', value))
57+
else:
58+
print("Value error:{}".format(value))
59+
60+
61+
def write_order(f, order):
62+
"""
63+
:param f: file handler or serial file
64+
:param order: (Order Enum Object)
65+
"""
66+
write_i8(f, order.value)
67+
68+
69+
def write_i16(f, value):
70+
"""
71+
:param f: file handler or serial file
72+
:param value: (int16_t)
73+
"""
74+
f.write(struct.pack('<h', value))
75+
76+
77+
def write_i32(f, value):
78+
"""
79+
:param f: file handler or serial file
80+
:param value: (int32_t)
81+
"""
82+
f.write(struct.pack('<l', value))
83+
84+

Diff for: python/libs/robust_serial/utils.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import sys
2+
import glob
3+
4+
try:
5+
import queue
6+
except ImportError:
7+
import Queue as queue
8+
9+
import serial
10+
11+
12+
# From https://stackoverflow.com/questions/6517953/clear-all-items-from-the-queue
13+
class CustomQueue(queue.Queue):
14+
"""
15+
A custom queue subclass that provides a :meth:`clear` method.
16+
"""
17+
18+
def clear(self):
19+
"""
20+
Clears all items from the queue.
21+
"""
22+
23+
with self.mutex:
24+
unfinished = self.unfinished_tasks - len(self.queue)
25+
if unfinished <= 0:
26+
if unfinished < 0:
27+
raise ValueError('task_done() called too many times')
28+
self.all_tasks_done.notify_all()
29+
self.unfinished_tasks = unfinished
30+
self.queue.clear()
31+
self.not_full.notify_all()
32+
33+
34+
# From https://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python
35+
def get_serial_ports():
36+
"""
37+
Lists serial ports.
38+
:return: ([str]) A list of available serial ports
39+
"""
40+
if sys.platform.startswith('win'):
41+
ports = ['COM%s' % (i + 1) for i in range(256)]
42+
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
43+
# this excludes your current terminal "/dev/tty"
44+
ports = glob.glob('/dev/tty[A-Za-z]*')
45+
elif sys.platform.startswith('darwin'):
46+
ports = glob.glob('/dev/tty.*')
47+
else:
48+
raise EnvironmentError('Unsupported platform')
49+
50+
results = []
51+
for port in ports:
52+
try:
53+
s = serial.Serial(port)
54+
s.close()
55+
results.append(port)
56+
except (OSError, serial.SerialException):
57+
pass
58+
return results
59+
60+
61+
def open_serial_port(serial_port=None, baudrate=115200, timeout=0, write_timeout=0):
62+
"""
63+
Try to open serial port with Arduino
64+
If not port is specified, it will be automatically detected
65+
:param serial_port: (str)
66+
:param baudrate: (int)
67+
:param timeout: (int) None -> blocking mode
68+
:param write_timeout: (int)
69+
:return: (Serial Object)
70+
"""
71+
# Open serial port (for communication with Arduino)
72+
if serial_port is None:
73+
serial_port = get_serial_ports()[-1]
74+
# timeout=0 non-blocking mode, return immediately in any case, returning zero or more,
75+
# up to the requested number of bytes
76+
return serial.Serial(port=serial_port, baudrate=baudrate, timeout=timeout, writeTimeout=write_timeout)

0 commit comments

Comments
 (0)