Skip to content

Commit 7f33856

Browse files
committed
libgpio 2.x support for odroid c4
1 parent f9b4015 commit 7f33856

File tree

4 files changed

+89
-22
lines changed

4 files changed

+89
-22
lines changed

src/adafruit_blinka/microcontroller/amlogic/meson_g12_common/pin.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,13 @@
1515
from adafruit_blinka.agnostic import detector
1616
from adafruit_blinka.microcontroller.alias import get_dts_alias, get_pwm_chipid
1717
from adafruit_blinka.microcontroller.generic_linux.libgpiod_pin import Pin
18+
from adafruit_blinka.microcontroller.generic_linux.libgpiod_chip import Chip
1819

19-
try:
20-
import gpiod
21-
except ImportError:
22-
raise ImportError(
23-
"libgpiod Python bindings not found, please install and try again!"
24-
) from ImportError
20+
chip0 = Chip("0")
21+
chip1 = Chip("1")
2522

26-
if hasattr(gpiod, "Chip"):
27-
chip0 = gpiod.Chip("0")
28-
chip1 = gpiod.Chip("1")
29-
else:
30-
chip0 = gpiod.chip("0")
31-
chip1 = gpiod.chip("1")
32-
33-
if callable(chip0.num_lines):
34-
chip0lines = chip0.num_lines()
35-
else:
36-
chip0lines = chip0.num_lines
37-
38-
if callable(chip1.num_lines):
39-
chip1lines = chip1.num_lines()
40-
else:
41-
chip1lines = chip1.num_lines
23+
chip0lines = chip0.num_lines
24+
chip1lines = chip1.num_lines
4225

4326
if chip0lines < 20:
4427
aobus = 0
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""A Chip class for use with libgpiod 1.x."""
5+
import gpiod
6+
7+
8+
# pylint: disable=too-many-branches,too-many-statements
9+
class Chip:
10+
"""Abstraction for handling all breaking changes over the lifecycle of gpiod"""
11+
12+
_CONSUMER = "adafruit_blinka"
13+
14+
id: str = None
15+
num_lines: int
16+
17+
def __init__(self, chip_id: str):
18+
self.id = chip_id
19+
if hasattr(gpiod, "Chip"):
20+
self._chip = gpiod.Chip(self.id)
21+
else:
22+
self._chip = gpiod.chip(self.id)
23+
24+
if callable(self._chip.num_lines):
25+
self.num_lines = self._chip.num_lines()
26+
else:
27+
self.num_lines = self.num_lines
28+
29+
def __repr__(self):
30+
return self.id
31+
32+
def __eq__(self, other):
33+
return self.id == other
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""A Chip class for use with libgpiod 2.x."""
5+
import gpiod
6+
7+
8+
# pylint: disable=too-many-branches,too-many-statements
9+
class Chip:
10+
"""Abstraction for handling all breaking changes over the lifecycle of gpiod"""
11+
12+
_CONSUMER = "adafruit_blinka"
13+
14+
id: str = None
15+
num_lines: int
16+
17+
def __init__(self, chip_id: str):
18+
self.id = chip_id
19+
path = f"/dev/gpiochip{self.id}"
20+
self._chip = gpiod.Chip(path)
21+
22+
info = self._chip.get_info()
23+
self.num_lines = info.num_lines
24+
25+
def __repr__(self):
26+
return self.id
27+
28+
def __eq__(self, other):
29+
return self.id == other
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""A Chip class for use with libgpiod."""
5+
try:
6+
import gpiod
7+
except ImportError:
8+
raise ImportError(
9+
"libgpiod Python bindings not found, please install and try again! See "
10+
"https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/main/libgpiod.py"
11+
) from ImportError
12+
13+
# Versions 1.5.4 and earlier have no __version__ attribute
14+
if hasattr(gpiod, "__version__"):
15+
version = gpiod.__version__
16+
else:
17+
version = "1.x"
18+
19+
if version.startswith("1."):
20+
from .libgpiod.libgpiod_chip_1_x import Chip # pylint: disable=unused-import
21+
else:
22+
from .libgpiod.libgpiod_chip_2_x import Chip # pylint: disable=unused-import

0 commit comments

Comments
 (0)