PyTmcStepper 0.15.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytmcstepper-0.15.0.dist-info/METADATA +338 -0
- pytmcstepper-0.15.0.dist-info/RECORD +56 -0
- pytmcstepper-0.15.0.dist-info/WHEEL +5 -0
- pytmcstepper-0.15.0.dist-info/licenses/LICENSE +683 -0
- pytmcstepper-0.15.0.dist-info/top_level.txt +1 -0
- tmc_driver/__init__.py +96 -0
- tmc_driver/_tmc_exceptions.py +21 -0
- tmc_driver/_tmc_math.py +122 -0
- tmc_driver/_tmc_stallguard.py +340 -0
- tmc_driver/_tmc_stepperdriver.py +210 -0
- tmc_driver/_tmc_validation.py +29 -0
- tmc_driver/_tmc_xxxx.py +489 -0
- tmc_driver/com/__init__.py +64 -0
- tmc_driver/com/_tmc_com.py +148 -0
- tmc_driver/com/_tmc_com_spi.py +81 -0
- tmc_driver/com/_tmc_com_spi_base.py +159 -0
- tmc_driver/com/_tmc_com_spi_ftdi.py +48 -0
- tmc_driver/com/_tmc_com_spi_micropython.py +99 -0
- tmc_driver/com/_tmc_com_uart.py +113 -0
- tmc_driver/com/_tmc_com_uart_base.py +386 -0
- tmc_driver/com/_tmc_com_uart_micropython.py +115 -0
- tmc_driver/enable_control/__init__.py +55 -0
- tmc_driver/enable_control/_tmc_ec.py +59 -0
- tmc_driver/enable_control/_tmc_ec_pin.py +48 -0
- tmc_driver/enable_control/_tmc_ec_toff.py +30 -0
- tmc_driver/motion_control/__init__.py +111 -0
- tmc_driver/motion_control/_tmc_mc.py +280 -0
- tmc_driver/motion_control/_tmc_mc_int_ramp_generator.py +218 -0
- tmc_driver/motion_control/_tmc_mc_step_dir.py +415 -0
- tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py +78 -0
- tmc_driver/motion_control/_tmc_mc_step_reg.py +36 -0
- tmc_driver/motion_control/_tmc_mc_vactual.py +221 -0
- tmc_driver/platform_utils.py +13 -0
- tmc_driver/reg/__init__.py +0 -0
- tmc_driver/reg/_tmc2209_reg.py +55 -0
- tmc_driver/reg/_tmc220x_reg.py +261 -0
- tmc_driver/reg/_tmc224x_reg.py +414 -0
- tmc_driver/reg/_tmc5160_reg.py +526 -0
- tmc_driver/reg/_tmc_reg.py +202 -0
- tmc_driver/reg/_tmc_shared_regs.py +314 -0
- tmc_driver/tmc_2208.py +17 -0
- tmc_driver/tmc_2209.py +75 -0
- tmc_driver/tmc_220x.py +383 -0
- tmc_driver/tmc_2240.py +340 -0
- tmc_driver/tmc_5160.py +344 -0
- tmc_driver/tmc_gpio/__init__.py +101 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_base.py +106 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py +162 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py +135 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py +108 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_periphery.py +87 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py +182 -0
- tmc_driver/tmc_logger/__init__.py +15 -0
- tmc_driver/tmc_logger/_tmc_logger_base.py +111 -0
- tmc_driver/tmc_logger/_tmc_logger_cpython.py +155 -0
- tmc_driver/tmc_logger/_tmc_logger_micropython.py +52 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# pylint: disable=unused-import
|
|
2
|
+
# pylint: disable=import-outside-toplevel
|
|
3
|
+
"""
|
|
4
|
+
Module for GPIO handling across different boards and libraries.
|
|
5
|
+
Automatically detects the board type and imports the appropriate GPIO library.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
from enum import Enum, IntEnum
|
|
10
|
+
from ..tmc_logger import TmcLogger, Loglevel
|
|
11
|
+
from ._tmc_gpio_board_base import *
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
MICROPYTHON = sys.implementation.name == "micropython"
|
|
15
|
+
|
|
16
|
+
# ------------------------------
|
|
17
|
+
# LIB | BOARD
|
|
18
|
+
# ------------------------------
|
|
19
|
+
# RPi.GPIO | Pi4, Pi3 etc.
|
|
20
|
+
# Jetson.GPIO | Nvidia Jetson
|
|
21
|
+
# gpiozero | Pi5
|
|
22
|
+
# pheriphery | Luckfox Pico
|
|
23
|
+
# OPi.GPIO | Orange Pi
|
|
24
|
+
# machine | MicroPython
|
|
25
|
+
# ------------------------------
|
|
26
|
+
|
|
27
|
+
if MICROPYTHON:
|
|
28
|
+
from ._tmc_gpio_board_micropython import MicroPythonGPIOWrapper
|
|
29
|
+
|
|
30
|
+
tmc_gpio = MicroPythonGPIOWrapper()
|
|
31
|
+
BOARD = Board.MICROPYTHON
|
|
32
|
+
else:
|
|
33
|
+
import os
|
|
34
|
+
from ._tmc_gpio_board_ftdi import FtdiWrapper
|
|
35
|
+
|
|
36
|
+
# Board mapping: (module_path, class_name, Board enum)
|
|
37
|
+
board_mapping = {
|
|
38
|
+
"raspberry pi 5": (
|
|
39
|
+
"._tmc_gpio_board_gpiozero",
|
|
40
|
+
"GpiozeroWrapper",
|
|
41
|
+
Board.RASPBERRY_PI5,
|
|
42
|
+
),
|
|
43
|
+
"raspberry": (
|
|
44
|
+
"._tmc_gpio_board_rpi_gpio",
|
|
45
|
+
"RPiGPIOWrapper",
|
|
46
|
+
Board.RASPBERRY_PI,
|
|
47
|
+
),
|
|
48
|
+
"jetson": (
|
|
49
|
+
"._tmc_gpio_board_rpi_gpio",
|
|
50
|
+
"JetsonGPIOWrapper",
|
|
51
|
+
Board.NVIDIA_JETSON,
|
|
52
|
+
),
|
|
53
|
+
"luckfox": (
|
|
54
|
+
"._tmc_gpio_board_periphery",
|
|
55
|
+
"peripheryWrapper",
|
|
56
|
+
Board.LUCKFOX_PICO,
|
|
57
|
+
),
|
|
58
|
+
"orange": ("._tmc_gpio_board_rpi_gpio", "OPiGPIOWrapper", Board.ORANGE_PI),
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
# Determine the board and instantiate the appropriate GPIO class
|
|
62
|
+
def get_board_model_name():
|
|
63
|
+
"""get board model name from /proc/device-tree/model file"""
|
|
64
|
+
if not os.path.exists("/proc/device-tree/model"): # type: ignore[attr-defined]
|
|
65
|
+
return "mock"
|
|
66
|
+
with open("/proc/device-tree/model", encoding="utf-8") as f:
|
|
67
|
+
return f.readline().lower()
|
|
68
|
+
|
|
69
|
+
def initialize_gpio():
|
|
70
|
+
"""initialize GPIO"""
|
|
71
|
+
from importlib import import_module
|
|
72
|
+
|
|
73
|
+
model = get_board_model_name()
|
|
74
|
+
dependencies_logger.log(f"Board model: {model}", Loglevel.INFO)
|
|
75
|
+
|
|
76
|
+
if model == "mock":
|
|
77
|
+
from ._tmc_gpio_board_rpi_gpio import MockGPIOWrapper
|
|
78
|
+
|
|
79
|
+
return MockGPIOWrapper(), Board.UNKNOWN
|
|
80
|
+
|
|
81
|
+
for key, (module_path, class_name, board_enum) in board_mapping.items():
|
|
82
|
+
if key in model:
|
|
83
|
+
# Import module dynamically only when needed
|
|
84
|
+
module = import_module(module_path, package=__name__)
|
|
85
|
+
wrapper_class = getattr(module, class_name)
|
|
86
|
+
return wrapper_class(), board_enum
|
|
87
|
+
|
|
88
|
+
dependencies_logger.log(
|
|
89
|
+
"The board is not recognized. Trying import default RPi.GPIO module...",
|
|
90
|
+
Loglevel.INFO,
|
|
91
|
+
)
|
|
92
|
+
try:
|
|
93
|
+
from ._tmc_gpio_board_rpi_gpio import RPiGPIOWrapper
|
|
94
|
+
|
|
95
|
+
return RPiGPIOWrapper(), Board.UNKNOWN
|
|
96
|
+
except ImportError:
|
|
97
|
+
from ._tmc_gpio_board_rpi_gpio import MockGPIOWrapper
|
|
98
|
+
|
|
99
|
+
return MockGPIOWrapper(), Board.UNKNOWN
|
|
100
|
+
|
|
101
|
+
tmc_gpio, BOARD = initialize_gpio()
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Many boards have RaspberryPI-compatible PinOut,
|
|
3
|
+
but require to import special GPIO module instead RPI.GPIO
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from enum import IntEnum
|
|
7
|
+
from abc import abstractmethod
|
|
8
|
+
import types
|
|
9
|
+
from ..tmc_logger import TmcLogger, Loglevel
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Board(IntEnum):
|
|
13
|
+
"""board"""
|
|
14
|
+
|
|
15
|
+
UNKNOWN = 0
|
|
16
|
+
RASPBERRY_PI = 1 # all except Pi 5
|
|
17
|
+
RASPBERRY_PI5 = 2
|
|
18
|
+
NVIDIA_JETSON = 3
|
|
19
|
+
LUCKFOX_PICO = 4
|
|
20
|
+
ORANGE_PI = 5
|
|
21
|
+
MICROPYTHON = 6
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Gpio(IntEnum):
|
|
25
|
+
"""GPIO value"""
|
|
26
|
+
|
|
27
|
+
LOW = 0
|
|
28
|
+
HIGH = 1
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class GpioMode(IntEnum):
|
|
32
|
+
"""GPIO mode"""
|
|
33
|
+
|
|
34
|
+
OUT = 0
|
|
35
|
+
IN = 1
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class GpioPUD(IntEnum):
|
|
39
|
+
"""Pull up Down"""
|
|
40
|
+
|
|
41
|
+
PUD_OFF = 20
|
|
42
|
+
PUD_UP = 22
|
|
43
|
+
PUD_DOWN = 21
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
BOARD = Board.UNKNOWN
|
|
47
|
+
dependencies_logger = TmcLogger(Loglevel.DEBUG, "DEPENDENCIES")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class BaseGPIOWrapper:
|
|
51
|
+
"""Base class for GPIO wrappers"""
|
|
52
|
+
|
|
53
|
+
@abstractmethod
|
|
54
|
+
def init(self, gpio_mode=None):
|
|
55
|
+
"""initialize GPIO library"""
|
|
56
|
+
|
|
57
|
+
@abstractmethod
|
|
58
|
+
def deinit(self):
|
|
59
|
+
"""deinitialize GPIO library"""
|
|
60
|
+
|
|
61
|
+
@abstractmethod
|
|
62
|
+
def gpio_setup(
|
|
63
|
+
self,
|
|
64
|
+
pin: int,
|
|
65
|
+
mode: GpioMode,
|
|
66
|
+
initial: Gpio = Gpio.LOW,
|
|
67
|
+
pull_up_down: GpioPUD = GpioPUD.PUD_OFF,
|
|
68
|
+
):
|
|
69
|
+
"""setup GPIO pin"""
|
|
70
|
+
|
|
71
|
+
@abstractmethod
|
|
72
|
+
def gpio_cleanup(self, pin: int):
|
|
73
|
+
"""cleanup GPIO pin"""
|
|
74
|
+
|
|
75
|
+
@abstractmethod
|
|
76
|
+
def gpio_input(self, pin: int) -> int:
|
|
77
|
+
"""read GPIO pin"""
|
|
78
|
+
|
|
79
|
+
@abstractmethod
|
|
80
|
+
def gpio_output(self, pin: int, value):
|
|
81
|
+
"""write GPIO pin"""
|
|
82
|
+
|
|
83
|
+
@abstractmethod
|
|
84
|
+
def gpio_pwm_setup(self, pin: int, frequency: int = 10, duty_cycle: int = 0):
|
|
85
|
+
"""setup PWM"""
|
|
86
|
+
|
|
87
|
+
@abstractmethod
|
|
88
|
+
def gpio_pwm_set_frequency(self, pin: int, frequency: int):
|
|
89
|
+
"""set PWM frequency"""
|
|
90
|
+
|
|
91
|
+
@abstractmethod
|
|
92
|
+
def gpio_pwm_set_duty_cycle(self, pin: int, duty_cycle: int):
|
|
93
|
+
"""set PWM duty cycle
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
pin (int): pin number
|
|
97
|
+
duty_cycle (int): duty cycle in percent (0-100)
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
@abstractmethod
|
|
101
|
+
def gpio_add_event_detect(self, pin: int, callback: types.FunctionType):
|
|
102
|
+
"""add event detect"""
|
|
103
|
+
|
|
104
|
+
@abstractmethod
|
|
105
|
+
def gpio_remove_event_detect(self, pin: int):
|
|
106
|
+
"""remove event detect"""
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# pylint: disable=wildcard-import
|
|
2
|
+
# pylint: disable=unused-wildcard-import
|
|
3
|
+
"""
|
|
4
|
+
FTDI GPIO module for FT232H and similar chips using pyftdi.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from pyftdi.spi import SpiGpioPort
|
|
8
|
+
from ._tmc_gpio_board_base import *
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FtdiWrapper(BaseGPIOWrapper):
|
|
12
|
+
"""FTDI GPIO wrapper for FT232H and similar chips using pyftdi.
|
|
13
|
+
|
|
14
|
+
Pin mapping (directly accent next to SPI pins):
|
|
15
|
+
- Pin 0-3: Reserved for SPI (SCK, MOSI, MISO, CS)
|
|
16
|
+
- Pin 4-7: Available as GPIO (directly accent 4-7 directly accent AD4-AD7)
|
|
17
|
+
|
|
18
|
+
When using with SPI, only pins 4-7 are available as GPIO.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, gpio_port: SpiGpioPort):
|
|
22
|
+
"""constructor, imports pyftdi
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
ftdi_url: FTDI device URL, default 'ftdi://ftdi:232h/1'
|
|
26
|
+
"""
|
|
27
|
+
self._gpio_port = gpio_port
|
|
28
|
+
self._gpio_direction = 0x00 # All inputs by default
|
|
29
|
+
self._gpio_state = 0x00 # All LOW by default
|
|
30
|
+
self._pin_modes = {} # Track pin modes
|
|
31
|
+
dependencies_logger.log("using pyftdi for GPIO control", Loglevel.INFO)
|
|
32
|
+
|
|
33
|
+
def init(self, gpio_mode=None):
|
|
34
|
+
"""initialize GPIO library and configure FTDI device"""
|
|
35
|
+
|
|
36
|
+
def deinit(self):
|
|
37
|
+
"""deinitialize GPIO library and close FTDI connection"""
|
|
38
|
+
|
|
39
|
+
def _update_gpio_direction(self):
|
|
40
|
+
"""update GPIO direction on the device"""
|
|
41
|
+
if self._gpio_port is not None:
|
|
42
|
+
# Pins 4-7 are available (directly accent 0xF0), apply direction directly accent
|
|
43
|
+
self._gpio_port.set_direction(
|
|
44
|
+
pins=0xF0, direction=self._gpio_direction & 0xF0
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
def gpio_setup(
|
|
48
|
+
self,
|
|
49
|
+
pin: int,
|
|
50
|
+
mode: GpioMode,
|
|
51
|
+
initial: Gpio = Gpio.LOW,
|
|
52
|
+
pull_up_down: GpioPUD = GpioPUD.PUD_OFF,
|
|
53
|
+
):
|
|
54
|
+
"""setup GPIO pin
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
pin: Pin number (4-7 for AD4-AD7, pins 0-3 reserved for SPI)
|
|
58
|
+
mode: GpioMode.OUT or GpioMode.IN
|
|
59
|
+
initial: Initial value for output pins
|
|
60
|
+
pull_up_down: Not supported on FTDI, ignored
|
|
61
|
+
"""
|
|
62
|
+
if pin < 4 or pin > 7:
|
|
63
|
+
dependencies_logger.log(
|
|
64
|
+
f"FTDI GPIO: Pin {pin} not available (use pins 4-7, 0-3 reserved for SPI)",
|
|
65
|
+
Loglevel.WARNING,
|
|
66
|
+
)
|
|
67
|
+
return
|
|
68
|
+
|
|
69
|
+
self._pin_modes[pin] = mode
|
|
70
|
+
pin_mask = 1 << pin
|
|
71
|
+
|
|
72
|
+
if mode == GpioMode.OUT:
|
|
73
|
+
self._gpio_direction |= pin_mask # Set bit for output
|
|
74
|
+
if initial == Gpio.HIGH:
|
|
75
|
+
self._gpio_state |= pin_mask
|
|
76
|
+
else:
|
|
77
|
+
self._gpio_state &= ~pin_mask
|
|
78
|
+
else:
|
|
79
|
+
self._gpio_direction &= ~pin_mask # Clear bit for input
|
|
80
|
+
|
|
81
|
+
self._update_gpio_direction()
|
|
82
|
+
|
|
83
|
+
if mode == GpioMode.OUT:
|
|
84
|
+
self.gpio_output(pin, initial)
|
|
85
|
+
|
|
86
|
+
def gpio_cleanup(self, pin: int):
|
|
87
|
+
"""cleanup GPIO pin - set to input"""
|
|
88
|
+
if pin < 4 or pin > 7:
|
|
89
|
+
return
|
|
90
|
+
pin_mask = 1 << pin
|
|
91
|
+
self._gpio_direction &= ~pin_mask # Set to input
|
|
92
|
+
self._gpio_state &= ~pin_mask # Set LOW
|
|
93
|
+
self._pin_modes.pop(pin, None)
|
|
94
|
+
self._update_gpio_direction()
|
|
95
|
+
|
|
96
|
+
def gpio_input(self, pin: int) -> int:
|
|
97
|
+
"""read GPIO pin
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
pin: Pin number (4-7)
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
Pin state (0 or 1)
|
|
104
|
+
"""
|
|
105
|
+
if pin < 4 or pin > 7:
|
|
106
|
+
dependencies_logger.log(
|
|
107
|
+
f"FTDI GPIO: Pin {pin} not available", Loglevel.WARNING
|
|
108
|
+
)
|
|
109
|
+
return 0
|
|
110
|
+
if self._gpio_port is None:
|
|
111
|
+
return 0
|
|
112
|
+
value = self._gpio_port.read()
|
|
113
|
+
return (value >> pin) & 0x01
|
|
114
|
+
|
|
115
|
+
def gpio_output(self, pin: int, value):
|
|
116
|
+
"""write GPIO pin
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
pin: Pin number (4-7)
|
|
120
|
+
value: Gpio.HIGH/LOW or 1/0
|
|
121
|
+
"""
|
|
122
|
+
if pin < 4 or pin > 7:
|
|
123
|
+
dependencies_logger.log(
|
|
124
|
+
f"FTDI GPIO: Pin {pin} not available", Loglevel.WARNING
|
|
125
|
+
)
|
|
126
|
+
return
|
|
127
|
+
if self._gpio_port is None:
|
|
128
|
+
return
|
|
129
|
+
|
|
130
|
+
pin_mask = 1 << pin
|
|
131
|
+
if value:
|
|
132
|
+
self._gpio_state |= pin_mask
|
|
133
|
+
else:
|
|
134
|
+
self._gpio_state &= ~pin_mask
|
|
135
|
+
|
|
136
|
+
# Write only the GPIO pins (directly accent 4-7)
|
|
137
|
+
self._gpio_port.write(self._gpio_state & 0xF0)
|
|
138
|
+
|
|
139
|
+
def gpio_pwm_setup(self, pin: int, frequency: int = 10, duty_cycle: int = 0):
|
|
140
|
+
"""setup PWM"""
|
|
141
|
+
raise NotImplementedError
|
|
142
|
+
|
|
143
|
+
def gpio_pwm_set_frequency(self, pin: int, frequency: int):
|
|
144
|
+
"""set PWM frequency"""
|
|
145
|
+
raise NotImplementedError
|
|
146
|
+
|
|
147
|
+
def gpio_pwm_set_duty_cycle(self, pin: int, duty_cycle: int):
|
|
148
|
+
"""set PWM duty cycle
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
pin (int): pin number
|
|
152
|
+
duty_cycle (int): duty cycle in percent (0-100)
|
|
153
|
+
"""
|
|
154
|
+
raise NotImplementedError
|
|
155
|
+
|
|
156
|
+
def gpio_add_event_detect(self, pin: int, callback: types.FunctionType):
|
|
157
|
+
"""add event detect"""
|
|
158
|
+
raise NotImplementedError
|
|
159
|
+
|
|
160
|
+
def gpio_remove_event_detect(self, pin: int):
|
|
161
|
+
"""remove event detect"""
|
|
162
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# pylint: disable=wildcard-import
|
|
2
|
+
# pylint: disable=unused-wildcard-import
|
|
3
|
+
"""
|
|
4
|
+
Many boards have RaspberryPI-compatible PinOut,
|
|
5
|
+
but require to import special GPIO module instead RPI.GPIO
|
|
6
|
+
|
|
7
|
+
This module determines the type of board
|
|
8
|
+
and import the corresponding GPIO module
|
|
9
|
+
|
|
10
|
+
Can be extended to support BeagleBone or other boards
|
|
11
|
+
Supports MicroPython
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from gpiozero import (
|
|
15
|
+
DigitalInputDevice,
|
|
16
|
+
DigitalOutputDevice,
|
|
17
|
+
PWMOutputDevice,
|
|
18
|
+
GPIODevice,
|
|
19
|
+
)
|
|
20
|
+
from ._tmc_gpio_board_base import *
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class GpiozeroWrapper(BaseGPIOWrapper):
|
|
24
|
+
"""gpiozero GPIO wrapper"""
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
"""constructor, imports gpiozero"""
|
|
28
|
+
self._gpios: list[GPIODevice | None] = [None] * 200
|
|
29
|
+
self._gpios_pwm: list[PWMOutputDevice | None] = [None] * 200
|
|
30
|
+
dependencies_logger.log("using gpiozero for GPIO control", Loglevel.INFO)
|
|
31
|
+
|
|
32
|
+
def init(self, gpio_mode=None):
|
|
33
|
+
"""initialize GPIO library. pass on gpiozero"""
|
|
34
|
+
|
|
35
|
+
def deinit(self):
|
|
36
|
+
"""deinitialize GPIO library. pass on gpiozero"""
|
|
37
|
+
|
|
38
|
+
def gpio_setup(
|
|
39
|
+
self,
|
|
40
|
+
pin: int,
|
|
41
|
+
mode: GpioMode,
|
|
42
|
+
initial: Gpio = Gpio.LOW,
|
|
43
|
+
pull_up_down: GpioPUD = GpioPUD.PUD_OFF,
|
|
44
|
+
):
|
|
45
|
+
"""setup GPIO pin"""
|
|
46
|
+
if mode == GpioMode.OUT:
|
|
47
|
+
gpio = self._gpios[pin]
|
|
48
|
+
if gpio is None or gpio.closed:
|
|
49
|
+
self._gpios[pin] = DigitalOutputDevice(pin, initial_value=bool(initial))
|
|
50
|
+
else:
|
|
51
|
+
gpio = self._gpios[pin]
|
|
52
|
+
if gpio is None or gpio.closed:
|
|
53
|
+
self._gpios[pin] = DigitalInputDevice(pin)
|
|
54
|
+
|
|
55
|
+
def gpio_cleanup(self, pin: int):
|
|
56
|
+
"""cleanup GPIO pin"""
|
|
57
|
+
gpio = self._gpios[pin]
|
|
58
|
+
if gpio is not None:
|
|
59
|
+
gpio.close()
|
|
60
|
+
self._gpios[pin] = None
|
|
61
|
+
gpio_pwm = self._gpios_pwm[pin]
|
|
62
|
+
if gpio_pwm is not None:
|
|
63
|
+
gpio_pwm.close()
|
|
64
|
+
self._gpios_pwm[pin] = None
|
|
65
|
+
|
|
66
|
+
def gpio_input(self, pin: int) -> int:
|
|
67
|
+
"""read GPIO pin"""
|
|
68
|
+
gpio = self._gpios[pin]
|
|
69
|
+
if not isinstance(gpio, DigitalInputDevice):
|
|
70
|
+
raise RuntimeError(f"GPIO pin {pin} not configured as input")
|
|
71
|
+
return gpio.value
|
|
72
|
+
|
|
73
|
+
def gpio_output(self, pin: int, value):
|
|
74
|
+
"""write GPIO pin"""
|
|
75
|
+
gpio = self._gpios[pin]
|
|
76
|
+
if not isinstance(gpio, DigitalOutputDevice):
|
|
77
|
+
raise RuntimeError(f"GPIO pin {pin} not configured as output")
|
|
78
|
+
if gpio.closed:
|
|
79
|
+
return
|
|
80
|
+
gpio.value = value
|
|
81
|
+
|
|
82
|
+
def gpio_pwm_enable(self, pin: int, enable: bool):
|
|
83
|
+
"""switch to PWM"""
|
|
84
|
+
if enable:
|
|
85
|
+
if self._gpios[pin] is not None:
|
|
86
|
+
self._gpios[pin] = None
|
|
87
|
+
self._gpios_pwm[pin] = PWMOutputDevice(pin)
|
|
88
|
+
else:
|
|
89
|
+
if self._gpios_pwm[pin] is not None:
|
|
90
|
+
self._gpios_pwm[pin] = None
|
|
91
|
+
self._gpios[pin] = DigitalOutputDevice(pin)
|
|
92
|
+
|
|
93
|
+
def gpio_pwm_setup(self, pin: int, frequency: int = 10, duty_cycle: int = 0):
|
|
94
|
+
"""setup PWM"""
|
|
95
|
+
# self._gpios_pwm[pin] = PWMOutputDevice(pin)
|
|
96
|
+
|
|
97
|
+
def gpio_pwm_set_frequency(self, pin: int, frequency: int):
|
|
98
|
+
"""set PWM frequency"""
|
|
99
|
+
self.gpio_pwm_enable(pin, True)
|
|
100
|
+
|
|
101
|
+
gpio = self._gpios_pwm[pin]
|
|
102
|
+
if not isinstance(gpio, PWMOutputDevice):
|
|
103
|
+
raise RuntimeError(f"GPIO pin {pin} not configured as PWM")
|
|
104
|
+
gpio.frequency = frequency
|
|
105
|
+
|
|
106
|
+
def gpio_pwm_set_duty_cycle(self, pin: int, duty_cycle: int):
|
|
107
|
+
"""set PWM duty cycle
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
pin (int): pin number
|
|
111
|
+
duty_cycle (int): duty cycle in percent (0-100)
|
|
112
|
+
"""
|
|
113
|
+
if duty_cycle == 0:
|
|
114
|
+
self.gpio_pwm_enable(pin, False)
|
|
115
|
+
return
|
|
116
|
+
self.gpio_pwm_enable(pin, True)
|
|
117
|
+
|
|
118
|
+
gpio = self._gpios_pwm[pin]
|
|
119
|
+
if not isinstance(gpio, PWMOutputDevice):
|
|
120
|
+
raise RuntimeError(f"GPIO pin {pin} not configured as PWM")
|
|
121
|
+
gpio.value = duty_cycle / 100
|
|
122
|
+
|
|
123
|
+
def gpio_add_event_detect(self, pin: int, callback: types.FunctionType):
|
|
124
|
+
"""add event detect"""
|
|
125
|
+
gpio = self._gpios_pwm[pin]
|
|
126
|
+
if not isinstance(gpio, DigitalInputDevice):
|
|
127
|
+
raise RuntimeError(f"GPIO pin {pin} not configured as input")
|
|
128
|
+
gpio.when_activated = callback
|
|
129
|
+
|
|
130
|
+
def gpio_remove_event_detect(self, pin: int):
|
|
131
|
+
"""remove event detect"""
|
|
132
|
+
gpio = self._gpios_pwm[pin]
|
|
133
|
+
if not isinstance(gpio, DigitalInputDevice):
|
|
134
|
+
raise RuntimeError(f"GPIO pin {pin} not configured as input")
|
|
135
|
+
gpio.when_activated = None
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# pylint: skip-file
|
|
2
|
+
"""
|
|
3
|
+
MicroPython GPIO module
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from machine import Pin, PWM
|
|
7
|
+
from ._tmc_gpio_board_base import *
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MicroPythonGPIOWrapper(BaseGPIOWrapper):
|
|
11
|
+
"""MicroPython GPIO wrapper for Raspberry Pi Pico (RP2040) and similar boards.
|
|
12
|
+
|
|
13
|
+
Uses machine.Pin and machine.PWM for GPIO control.
|
|
14
|
+
Only available when running under MicroPython.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self):
|
|
18
|
+
"""constructor"""
|
|
19
|
+
self._pins = {} # pin_num -> Pin object
|
|
20
|
+
self._pwms = {} # pin_num -> PWM object
|
|
21
|
+
|
|
22
|
+
def init(self, gpio_mode=None):
|
|
23
|
+
"""initialize GPIO library - not needed for MicroPython"""
|
|
24
|
+
|
|
25
|
+
def deinit(self):
|
|
26
|
+
"""deinitialize GPIO library"""
|
|
27
|
+
for pin_num in list(self._pwms.keys()):
|
|
28
|
+
self.gpio_cleanup(pin_num)
|
|
29
|
+
for pin_num in list(self._pins.keys()):
|
|
30
|
+
self.gpio_cleanup(pin_num)
|
|
31
|
+
|
|
32
|
+
def gpio_setup(self, pin, mode, initial=0, pull_up_down=0):
|
|
33
|
+
"""setup GPIO pin
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
pin: GPIO pin number
|
|
37
|
+
mode: GpioMode.OUT or GpioMode.IN
|
|
38
|
+
initial: Initial value for output pins
|
|
39
|
+
pull_up_down: Pull-up/down configuration
|
|
40
|
+
"""
|
|
41
|
+
if mode == GpioMode.OUT:
|
|
42
|
+
self._pins[pin] = Pin(pin, Pin.OUT, value=initial)
|
|
43
|
+
else:
|
|
44
|
+
# Configure pull-up/down
|
|
45
|
+
if pull_up_down == GpioPUD.PUD_UP:
|
|
46
|
+
self._pins[pin] = Pin(pin, Pin.IN, Pin.PULL_UP)
|
|
47
|
+
elif pull_up_down == GpioPUD.PUD_DOWN:
|
|
48
|
+
self._pins[pin] = Pin(pin, Pin.IN, Pin.PULL_DOWN)
|
|
49
|
+
else:
|
|
50
|
+
self._pins[pin] = Pin(pin, Pin.IN)
|
|
51
|
+
|
|
52
|
+
def gpio_cleanup(self, pin):
|
|
53
|
+
"""cleanup GPIO pin"""
|
|
54
|
+
if pin in self._pwms:
|
|
55
|
+
self._pwms[pin].deinit()
|
|
56
|
+
del self._pwms[pin]
|
|
57
|
+
if pin in self._pins:
|
|
58
|
+
del self._pins[pin]
|
|
59
|
+
|
|
60
|
+
def gpio_input(self, pin):
|
|
61
|
+
"""read GPIO pin"""
|
|
62
|
+
if pin in self._pins:
|
|
63
|
+
return self._pins[pin].value()
|
|
64
|
+
return 0
|
|
65
|
+
|
|
66
|
+
def gpio_output(self, pin, value):
|
|
67
|
+
"""write GPIO pin"""
|
|
68
|
+
if pin in self._pins:
|
|
69
|
+
self._pins[pin].value(value)
|
|
70
|
+
|
|
71
|
+
def gpio_pwm_setup(self, pin, frequency=10, duty_cycle=0):
|
|
72
|
+
"""setup PWM
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
pin: GPIO pin number
|
|
76
|
+
frequency: PWM frequency in Hz
|
|
77
|
+
duty_cycle: Duty cycle in percent (0-100)
|
|
78
|
+
"""
|
|
79
|
+
self._pwms[pin] = PWM(Pin(pin))
|
|
80
|
+
self._pwms[pin].freq(frequency)
|
|
81
|
+
# MicroPython uses 0-65535 for duty, convert from percent
|
|
82
|
+
self._pwms[pin].duty_u16(int(duty_cycle / 100 * 65535))
|
|
83
|
+
|
|
84
|
+
def gpio_pwm_set_frequency(self, pin, frequency):
|
|
85
|
+
"""set PWM frequency"""
|
|
86
|
+
if pin in self._pwms:
|
|
87
|
+
self._pwms[pin].freq(frequency)
|
|
88
|
+
|
|
89
|
+
def gpio_pwm_set_duty_cycle(self, pin, duty_cycle):
|
|
90
|
+
"""set PWM duty cycle
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
pin: GPIO pin number
|
|
94
|
+
duty_cycle: Duty cycle in percent (0-100)
|
|
95
|
+
"""
|
|
96
|
+
if pin in self._pwms:
|
|
97
|
+
# Convert percent to 0-65535
|
|
98
|
+
self._pwms[pin].duty_u16(int(duty_cycle * 655.35))
|
|
99
|
+
|
|
100
|
+
def gpio_add_event_detect(self, pin, callback):
|
|
101
|
+
"""add event detect (rising edge interrupt)"""
|
|
102
|
+
if pin in self._pins:
|
|
103
|
+
self._pins[pin].irq(trigger=Pin.IRQ_RISING, handler=callback)
|
|
104
|
+
|
|
105
|
+
def gpio_remove_event_detect(self, pin):
|
|
106
|
+
"""remove event detect"""
|
|
107
|
+
if pin in self._pins:
|
|
108
|
+
self._pins[pin].irq(handler=None)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# pylint: disable=wildcard-import
|
|
2
|
+
# pylint: disable=unused-wildcard-import
|
|
3
|
+
"""
|
|
4
|
+
Many boards have RaspberryPI-compatible PinOut,
|
|
5
|
+
but require to import special GPIO module instead RPI.GPIO
|
|
6
|
+
|
|
7
|
+
This module determines the type of board
|
|
8
|
+
and import the corresponding GPIO module
|
|
9
|
+
|
|
10
|
+
Can be extended to support BeagleBone or other boards
|
|
11
|
+
Supports MicroPython
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from periphery import GPIO
|
|
15
|
+
from ._tmc_gpio_board_base import *
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class peripheryWrapper(BaseGPIOWrapper):
|
|
19
|
+
"""periphery GPIO wrapper"""
|
|
20
|
+
|
|
21
|
+
def __init__(self):
|
|
22
|
+
"""constructor, imports periphery"""
|
|
23
|
+
dependencies_logger.log("using periphery for GPIO control", Loglevel.INFO)
|
|
24
|
+
self._gpios: list[GPIO | None] = [None] * 200
|
|
25
|
+
|
|
26
|
+
def init(self, gpio_mode=None):
|
|
27
|
+
"""initialize GPIO library. pass on periphery"""
|
|
28
|
+
|
|
29
|
+
def deinit(self):
|
|
30
|
+
"""deinitialize GPIO library. pass on periphery"""
|
|
31
|
+
|
|
32
|
+
def gpio_setup(
|
|
33
|
+
self,
|
|
34
|
+
pin: int,
|
|
35
|
+
mode: GpioMode,
|
|
36
|
+
initial: Gpio = Gpio.LOW,
|
|
37
|
+
pull_up_down: GpioPUD = GpioPUD.PUD_OFF,
|
|
38
|
+
):
|
|
39
|
+
"""setup GPIO pin"""
|
|
40
|
+
mode_str = "out" if (mode == GpioMode.OUT) else "in"
|
|
41
|
+
self._gpios[pin] = GPIO(pin, mode_str)
|
|
42
|
+
|
|
43
|
+
def gpio_cleanup(self, pin: int):
|
|
44
|
+
"""cleanup GPIO pin"""
|
|
45
|
+
gpio = self._gpios[pin]
|
|
46
|
+
if isinstance(gpio, GPIO):
|
|
47
|
+
gpio.close()
|
|
48
|
+
self._gpios[pin] = None
|
|
49
|
+
|
|
50
|
+
def gpio_input(self, pin: int) -> int:
|
|
51
|
+
"""read GPIO pin"""
|
|
52
|
+
gpio = self._gpios[pin]
|
|
53
|
+
if not isinstance(gpio, GPIO):
|
|
54
|
+
raise RuntimeError(f"GPIO pin {pin} not configured")
|
|
55
|
+
return gpio.read()
|
|
56
|
+
|
|
57
|
+
def gpio_output(self, pin: int, value):
|
|
58
|
+
"""write GPIO pin"""
|
|
59
|
+
gpio = self._gpios[pin]
|
|
60
|
+
if not isinstance(gpio, GPIO):
|
|
61
|
+
raise RuntimeError(f"GPIO pin {pin} not configured")
|
|
62
|
+
gpio.write(bool(value))
|
|
63
|
+
|
|
64
|
+
def gpio_pwm_setup(self, pin: int, frequency: int = 10, duty_cycle: int = 0):
|
|
65
|
+
"""setup PWM"""
|
|
66
|
+
raise NotImplementedError
|
|
67
|
+
|
|
68
|
+
def gpio_pwm_set_frequency(self, pin: int, frequency: int):
|
|
69
|
+
"""set PWM frequency"""
|
|
70
|
+
raise NotImplementedError
|
|
71
|
+
|
|
72
|
+
def gpio_pwm_set_duty_cycle(self, pin: int, duty_cycle: int):
|
|
73
|
+
"""set PWM duty cycle
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
pin (int): pin number
|
|
77
|
+
duty_cycle (int): duty cycle in percent (0-100)
|
|
78
|
+
"""
|
|
79
|
+
raise NotImplementedError
|
|
80
|
+
|
|
81
|
+
def gpio_add_event_detect(self, pin: int, callback: types.FunctionType):
|
|
82
|
+
"""add event detect"""
|
|
83
|
+
raise NotImplementedError
|
|
84
|
+
|
|
85
|
+
def gpio_remove_event_detect(self, pin: int):
|
|
86
|
+
"""remove event detect"""
|
|
87
|
+
raise NotImplementedError
|