Skip to content

Commit 63fb7ed

Browse files
authored
Merge pull request #1040 from makermelissa/jsonify
Jsonify Boards and Chips for easier importing
2 parents 45a24c6 + e9a340f commit 63fb7ed

13 files changed

Lines changed: 466 additions & 983 deletions

File tree

src/adafruit_blinka/importing.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
`adafruit_blinka.importing` - Import utilities that run on Linux and MicroPython
7+
======================================================================================
8+
9+
* Author(s): Melissa LeBlanc-Williams
10+
"""
11+
12+
13+
try:
14+
from importlib import import_module
15+
except ImportError as e:
16+
17+
def import_module(module_name: str, package: str = None):
18+
"""importlib not available, define an alternate import_module function"""
19+
package_list = []
20+
if package is not None:
21+
package_list.append(package)
22+
return __import__(module_name, globals(), locals(), package_list)
23+
24+
25+
from adafruit_blinka.agnostic import detector
26+
27+
28+
def get_import_file(json_file_name, script_file_location):
29+
"""Get the full path to the microcontroller imports file."""
30+
try:
31+
from pathlib import Path
32+
33+
script_folder = Path(script_file_location).parent.absolute()
34+
return script_folder / json_file_name
35+
except ImportError:
36+
# MicroPython doesn't have pathlib, so we have to do it manually
37+
if script_file_location.startswith("/"):
38+
script_folder = "/".join(script_file_location.split("/")[:-1])
39+
else:
40+
script_folder = "."
41+
return f"{script_folder}/{json_file_name}"
42+
43+
44+
def import_mod(caller_globals, module_name: str, package_name: str = "*"):
45+
"""Function to allow importing with * or specific package name."""
46+
if package_name == "*":
47+
module = import_module(module_name)
48+
caller_globals.update(
49+
{name: getattr(module, name) for name in module.__all__}
50+
if hasattr(module, "__all__")
51+
else {
52+
key: value
53+
for (key, value) in module.__dict__.items()
54+
if not key.startswith("_")
55+
}
56+
)
57+
else:
58+
module = import_module(module_name, package=package_name)
59+
caller_globals[package_name] = getattr(module, package_name)
60+
61+
62+
def import_microcontroller(
63+
caller_globals,
64+
microcontroller_imports,
65+
module_extension: str = "",
66+
package_name: str = "*",
67+
):
68+
"""Detect and import the microcontroller module and package based on detected hardware"""
69+
if module_extension[0:1] != "." and module_extension != "":
70+
# Make sure the module extension starts with a dot if it's not empty
71+
module_extension = f".{module_extension}"
72+
for chip_key, chip_module in microcontroller_imports.items():
73+
if getattr(detector.chip, chip_key):
74+
if isinstance(chip_module, dict):
75+
# Loop through the children and import the first one that matches
76+
for board_key, board_chip_module in chip_module.items():
77+
if board_key.startswith("any_") and getattr(
78+
detector.board, board_key
79+
):
80+
# import Pin from the microcontroller module
81+
import_mod(
82+
caller_globals,
83+
f"{board_chip_module}{module_extension}",
84+
package_name,
85+
)
86+
return True
87+
if board_key == getattr(detector.board, board_key):
88+
print(f"Detected board: {board_key}")
89+
import_mod(
90+
caller_globals,
91+
f"{board_chip_module}{module_extension}",
92+
package_name,
93+
)
94+
return True
95+
import_mod(
96+
caller_globals,
97+
f"{chip_module['default']}{module_extension}",
98+
package_name,
99+
)
100+
return True
101+
import_mod(caller_globals, f"{chip_module}{module_extension}", package_name)
102+
return True
103+
return False

src/adafruit_blinka/microcontroller/generic_micropython/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
#
33
# SPDX-License-Identifier: MIT
44
"""Generic Pin class for use with MicroPython boards"""
5-
from adafruit_blinka import Enum
5+
# from adafruit_blinka import Enum
6+
try:
7+
from machine import Pin as MachinePin
8+
except ImportError:
9+
# Fall back to a simple Pin class if machine.Pin is not available for CI testing
10+
from adafruit_blinka import Enum as MachinePin
611

712

8-
class Pin(Enum):
13+
class Pin(MachinePin):
914
"""
1015
Identifies an IO pin on the microcontroller.
1116

src/adafruit_blinka/microcontroller/nova/pwmout.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _open(self, pin, duty=0, freq=750, variable_frequency=False):
8585
if variable_frequency:
8686
print("Variable Frequency is not supported, continuing without it...")
8787

88-
PWMOut._nova.setIOpinMode(self._pwmpin, Pin.PWM)
88+
PWMOut._nova.setIOpinMode(self._pwmpin, Pin.PWM) # pylint: disable=no-member
8989

9090
# set frequency
9191
self.frequency = freq
@@ -149,7 +149,7 @@ def _set_period(self, period):
149149
"""
150150

151151
def _get_duty_cycle(self):
152-
duty_cycle = Pin._nova.getIOpinValue(self._pwmpin)
152+
duty_cycle = Pin._nova.getIOpinValue(self._pwmpin) # pylint: disable=no-member
153153

154154
# Convert duty cycle to ratio from 0.0 to 1.0
155155
duty_cycle = duty_cycle / PWMOut.MAX_CYCLE_LEVEL
@@ -172,7 +172,7 @@ def _set_duty_cycle(self, duty_cycle):
172172

173173
# Set duty cycle
174174
# pylint: disable=protected-access
175-
Pin._nova.setIOpinValue(self._pwmpin, duty_cycle)
175+
Pin._nova.setIOpinValue(self._pwmpin, duty_cycle) # pylint: disable=no-member
176176
# pylint: enable=protected-access
177177

178178
duty_cycle = property(_get_duty_cycle, _set_duty_cycle)

src/adafruit_blinka/microcontroller/rp2040/pin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: MIT
44
"""RP2040 pins"""
55

6-
from machine import Pin
6+
from adafruit_blinka.microcontroller.generic_micropython import Pin
77

88
GP0 = Pin(0)
99
GP1 = Pin(1)

src/adafruit_blinka/microcontroller/stm32/stm32f405/pin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: MIT
44
"""STM32F405 pins"""
55

6-
from machine import Pin
6+
from adafruit_blinka.microcontroller.generic_micropython import Pin
77

88
A0 = Pin("A0")
99
A1 = Pin("A1")

0 commit comments

Comments
 (0)