Skip to content

Commit b406de4

Browse files
committed
libs/hardware: Add PLC I/O support.
Signed-off-by: lbuque <[email protected]>
1 parent 9e9a743 commit b406de4

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

m5stack/libs/hardware/__init__.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,37 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
from machine import *
6-
from .rgb import RGB
7-
from .button import Button
8-
from .ir import IR
9-
from .rfid import RFID
10-
from .rotary import Rotary
11-
from .keyboard import Keyboard
12-
from .matrix_keyboard import MatrixKeyboard
13-
from .scd40 import SCD40
14-
from .sen55 import SEN55
5+
_attrs = {
6+
"Keyboard": "keyboard",
7+
"Button": "button",
8+
"IR": "ir",
9+
"MatrixKeyboard": "matrix_keyboard",
10+
"DigitalInput": "plcio",
11+
"Relay": "plcio",
12+
"RFID": "rfid",
13+
"RGB": "rgb",
14+
"Rotary": "rotary",
15+
"SCD40": "scd40",
16+
"SEN55": "sen55",
17+
}
18+
19+
20+
def __getattr__(attr):
21+
if attr == "sdcard":
22+
value = __import__("sdcard", None, None, True, 1)
23+
globals()[attr] = value
24+
return value
25+
26+
mod = _attrs.get(attr, None)
27+
if mod is None:
28+
import machine
29+
30+
value = getattr(machine, attr)
31+
if value is None:
32+
raise AttributeError(attr)
33+
else:
34+
globals()[attr] = value
35+
return value
36+
value = getattr(__import__(mod, None, None, True, 1), attr)
37+
globals()[attr] = value
38+
return value

m5stack/libs/hardware/manifest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"button.py",
1212
"ir.py",
1313
"matrix_keyboard.py",
14+
"plcio.py",
1415
"rfid.py",
1516
"rgb.py",
1617
"rotary.py",
@@ -19,5 +20,5 @@
1920
"sen55.py",
2021
),
2122
base_path="..",
22-
opt=0,
23+
# opt=0,
2324
)

m5stack/libs/hardware/plcio.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
from driver import aw9523
6+
import M5
7+
import machine
8+
9+
10+
def _plc_closure():
11+
aw = None
12+
13+
def _aw9523_init():
14+
_board_map = {
15+
# boardid: (i2c, scl, sda, irq_pin)
16+
M5.BOARD.M5StampPLC: (1, 15, 13, 14),
17+
}
18+
i2c, scl, sda, irq_pin = _board_map.get(M5.getBoard(), (None, None, None, None))
19+
if i2c is None:
20+
raise NotImplementedError("AW9523 is not supported on this board")
21+
i2c = machine.I2C(i2c, scl=machine.Pin(scl), sda=machine.Pin(sda))
22+
return aw9523.AW9523(i2c, irq_pin=irq_pin)
23+
24+
class DigitalInput(aw9523.Pin):
25+
_pin_map = {
26+
# input id: pin id
27+
1: 4,
28+
2: 5,
29+
3: 6,
30+
4: 7,
31+
5: 12,
32+
6: 13,
33+
7: 14,
34+
8: 15,
35+
}
36+
37+
def __init__(self, id) -> None:
38+
nonlocal aw
39+
if aw is None:
40+
aw = _aw9523_init()
41+
pid = self._pin_map.get(id, None)
42+
if pid is None:
43+
raise ValueError("Invalid DigitalInput ID")
44+
super().__init__(pid)
45+
46+
def get_status(self) -> bool:
47+
return bool(self.value())
48+
49+
class Relay(aw9523.Pin):
50+
_pin_map = {
51+
# relay id: pin id
52+
1: 0,
53+
2: 1,
54+
3: 2,
55+
4: 3,
56+
}
57+
58+
def __init__(self, id) -> None:
59+
nonlocal aw
60+
if aw is None:
61+
aw = _aw9523_init()
62+
pid = self._pin_map.get(id, None)
63+
if pid is None:
64+
raise ValueError("Invalid Relay ID")
65+
super().__init__(pid, mode=aw9523.Pin.OUT)
66+
67+
def get_status(self) -> bool:
68+
return bool(self.value())
69+
70+
return DigitalInput, Relay
71+
72+
73+
DigitalInput, Relay = _plc_closure()

0 commit comments

Comments
 (0)