|
| 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