Skip to content

Commit

Permalink
Applied a few patches from the original pyFirmata
Browse files Browse the repository at this point in the history
  • Loading branch information
berndporr committed Aug 24, 2021
1 parent f98ba95 commit d9ffb8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pyfirmata2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __str__(self):

class ArduinoNano(Board):
"""
A board that will set itself up as an Arduino Due.
A board that will set itself up as an Arduino Nano.
"""
def __init__(self, *args, **kwargs):
args = list(args)
Expand Down
39 changes: 20 additions & 19 deletions pyfirmata2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from .boards import BOARDS


def get_the_board(layout=BOARDS['arduino'], base_dir='/dev/', identifier='tty.usbserial',):
def get_the_board(
layout=BOARDS["arduino"], base_dir="/dev/", identifier="tty.usbserial"
):
"""
Helper function to get the one and only board connected to the computer
running this. It assumes a normal arduino layout, but this can be
Expand Down Expand Up @@ -50,7 +52,7 @@ def run(self):
while self.board.bytes_available():
self.board.iterate()
time.sleep(0.001)
except (AttributeError, serial.SerialException, OSError) as e:
except (AttributeError, serial.SerialException, OSError):
# this way we can kill the thread by setting the board object
# to None, or when the serial port is closed by board.exit()
break
Expand All @@ -66,7 +68,7 @@ def run(self):
except (TypeError, IndexError):
pass
raise
except (KeyboardInterrupt) as e:
except (KeyboardInterrupt):
sys.exit()

def stop(self):
Expand Down Expand Up @@ -169,35 +171,34 @@ def pin_list_to_board_dict(pinlist):
"""

board_dict = {
'digital': [],
'analog': [],
'pwm': [],
'servo': [], # 2.2 specs
# 'i2c': [], # 2.3 specs
'disabled': [],
"digital": [],
"analog": [],
"pwm": [],
"servo": [],
"disabled": [],
}
for i, pin in enumerate(pinlist):
pin.pop() # removes the 0x79 on end
if not pin:
board_dict['disabled'] += [i]
board_dict['digital'] += [i]
board_dict["disabled"] += [i]
board_dict["digital"] += [i]
continue

for j, _ in enumerate(pin):
# Iterate over evens
if j % 2 == 0:
# This is safe. try: range(10)[5:50]
if pin[j:j + 4] == [0, 1, 1, 1]:
board_dict['digital'] += [i]
board_dict["digital"] += [i]

if pin[j:j + 2] == [2, 10]:
board_dict['analog'] += [i]
board_dict["analog"] += [i]

if pin[j:j + 2] == [3, 8]:
board_dict['pwm'] += [i]
board_dict["pwm"] += [i]

if pin[j:j + 2] == [4, 14]:
board_dict['servo'] += [i]
board_dict["servo"] += [i]

# Desable I2C
if pin[j:j + 2] == [6, 1]:
Expand All @@ -206,16 +207,16 @@ def pin_list_to_board_dict(pinlist):
# We have to deal with analog pins:
# - (14, 15, 16, 17, 18, 19)
# + (0, 1, 2, 3, 4, 5)
diff = set(board_dict['digital']) - set(board_dict['analog'])
board_dict['analog'] = [n for n, _ in enumerate(board_dict['analog'])]
diff = set(board_dict["digital"]) - set(board_dict["analog"])
board_dict["analog"] = [n for n, _ in enumerate(board_dict["analog"])]

# Digital pin problems:
# - (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
# + (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)

board_dict['digital'] = [n for n, _ in enumerate(diff)]
board_dict["digital"] = [n for n, _ in enumerate(diff)]
# Based on lib Arduino 0017
board_dict['servo'] = board_dict['digital']
board_dict["servo"] = board_dict["digital"]

# Turn lists into tuples
# Using dict for Python 2.6 compatibility
Expand Down

0 comments on commit d9ffb8c

Please sign in to comment.