Skip to content

Commit

Permalink
initial support for the luckfox pico
Browse files Browse the repository at this point in the history
resolves #66
  • Loading branch information
Chr157i4n committed May 21, 2024
1 parent e0be16b commit 5e54ce3
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions src/TMC_2209/_TMC_2209_GPIO_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Board(Enum):
RASPBERRY_PI = 1 # all except Pi 5
RASPBERRY_PI5 = 2
NVIDIA_JETSON = 3
LUCKFOX_PICO = 4

class Gpio(IntEnum):
"""GPIO value"""
Expand Down Expand Up @@ -118,6 +119,28 @@ class GpioPUD(IntEnum):
"Exiting..."),
Loglevel.ERROR)
raise
elif "luckfox pico" in model:
try:
from periphery import GPIO
BOARD = Board.LUCKFOX_PICO
except ModuleNotFoundError as err:
dependencies_logger.log(
(f"ModuleNotFoundError: {err}\n"
"Board is Luckfox Pico but module periphery isn`t installed.\n"
"Follow the installation instructions in the link below to resolve the issue:\n"
"https://github.com/vsergeev/python-periphery\n"
"Exiting..."),
Loglevel.ERROR)
raise
except ImportError as err:
dependencies_logger.log(
(f"ImportError: {err}\n"
"Board is Luckfox Pico but module periphery isn`t installed.\n"
"Follow the installation instructions in the link below to resolve the issue:\n"
"https://github.com/vsergeev/python-periphery\n"
"Exiting..."),
Loglevel.ERROR)
raise
else:
# just in case
dependencies_logger.log(
Expand All @@ -134,13 +157,15 @@ class GpioPUD(IntEnum):
class TMC_gpio:
"""TMC_gpio class"""

_gpiozero_dos = [None] * 40
_gpios = [None] * 40

@staticmethod
def init(gpio_mode=None):
"""init"""
if BOARD == Board.RASPBERRY_PI5:
pass
elif BOARD == Board.LUCKFOX_PICO:
pass
else:
GPIO.setwarnings(False)
if gpio_mode is None:
Expand All @@ -159,7 +184,10 @@ def deinit():
def gpio_setup(pin, mode, initial = Gpio.LOW, pull_up_down = GpioPUD.PUD_OFF):
"""setup gpio pin"""
if BOARD == Board.RASPBERRY_PI5:
TMC_gpio._gpiozero_dos[pin] = DigitalOutputDevice(pin)
TMC_gpio._gpios[pin] = DigitalOutputDevice(pin)
elif BOARD == Board.LUCKFOX_PICO:
mode = 'out' if (mode == GpioMode.OUT) else 'in'
TMC_gpio._gpios[pin] = GPIO(pin, mode)
else:
initial = int(initial)
pull_up_down = int(pull_up_down)
Expand All @@ -171,37 +199,45 @@ def gpio_setup(pin, mode, initial = Gpio.LOW, pull_up_down = GpioPUD.PUD_OFF):
def gpio_cleanup(pin):
"""cleanup gpio pin"""
if BOARD == Board.RASPBERRY_PI5:
pass
TMC_gpio._gpios[pin].close()
elif BOARD == Board.LUCKFOX_PICO:
TMC_gpio._gpios[pin].close()
else:
GPIO.cleanup(pin)

@staticmethod
def gpio_input(pin):
"""get input value of gpio pin"""
del pin
return 0
return 0 # TODO: implement

@staticmethod
def gpio_output(pin, value):
"""set output value of gpio pin"""
if BOARD == Board.RASPBERRY_PI5:
TMC_gpio._gpiozero_dos[pin].value = value
TMC_gpio._gpios[pin].value = value
elif BOARD == Board.LUCKFOX_PICO:
TMC_gpio._gpios[pin].write(bool(value))
else:
GPIO.output(pin, value)

@staticmethod
def gpio_remove_event_detect(pin):
"""remove event dectect"""
if BOARD == Board.RASPBERRY_PI5:
pass
pass # TODO: implement for stallguard
elif BOARD == Board.LUCKFOX_PICO:
pass # TODO: implement for stallguard
else:
GPIO.remove_event_detect(pin)

@staticmethod
def gpio_add_event_detect(pin, callback):
"""add event detect"""
if BOARD == Board.RASPBERRY_PI5:
pass
pass # TODO: implement for stallguard
elif BOARD == Board.LUCKFOX_PICO:
pass # TODO: implement for stallguard
else:
GPIO.add_event_detect(pin, GPIO.RISING, callback=callback,
bouncetime=300)

0 comments on commit 5e54ce3

Please sign in to comment.