From 5e54ce3d39192617a678b7ac694134225ddc43a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6hlke?= Date: Tue, 21 May 2024 18:16:18 +0100 Subject: [PATCH] initial support for the luckfox pico resolves #66 --- src/TMC_2209/_TMC_2209_GPIO_board.py | 50 ++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/TMC_2209/_TMC_2209_GPIO_board.py b/src/TMC_2209/_TMC_2209_GPIO_board.py index ea17e65..2abd68b 100644 --- a/src/TMC_2209/_TMC_2209_GPIO_board.py +++ b/src/TMC_2209/_TMC_2209_GPIO_board.py @@ -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""" @@ -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( @@ -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: @@ -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) @@ -171,7 +199,9 @@ 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) @@ -179,13 +209,15 @@ def gpio_cleanup(pin): 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) @@ -193,7 +225,9 @@ def gpio_output(pin, value): 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) @@ -201,7 +235,9 @@ def gpio_remove_event_detect(pin): 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)