feat: v0.3.5 — PN532 driver, NFC operational, pinout from config

This commit is contained in:
2026-06-07 16:27:15 +02:00
parent 2720233964
commit 23bbbb4ed2
3 changed files with 267 additions and 64 deletions

View File

@@ -14,37 +14,15 @@ import machine
import ubinascii
from machine import Pin, I2C
# ---- Optional OLED ----
try:
from ssd1306 import SSD1306_I2C
_i2c = I2C(0, sda=Pin(4), scl=Pin(5))
_oled = SSD1306_I2C(128, 64, _i2c)
HAS_OLED = True
except Exception:
_oled = None
HAS_OLED = False
# ---- Optional NFC (PN532 I2C) ----
try:
from pn532 import PN532_I2C
HAS_NFC = True
except Exception:
HAS_NFC = False
# ---- Optional sensor (DHT22 on GP15) ----
try:
import dht
_dht = dht.DHT22(Pin(15))
HAS_SENSOR = True
except Exception:
HAS_SENSOR = False
# ---- Optional relay (GP16) ----
try:
_relay = Pin(16, Pin.OUT)
HAS_RELAY = True
except Exception:
HAS_RELAY = False
# Hardware instances — initialised in detect_capabilities() after config load
_oled = None
_nfc = None
_dht = None
_relay = None
HAS_OLED = False
HAS_NFC = False
HAS_SENSOR = False
HAS_RELAY = False
CONFIG_PATH = 'config.json'
@@ -82,7 +60,56 @@ def _show_state(state, detail=''):
# Capability detection
# =============================================================================
def detect_capabilities():
def detect_capabilities(cfg=None):
global _oled, _nfc, _dht, _relay
global HAS_OLED, HAS_NFC, HAS_SENSOR, HAS_RELAY
pinout = cfg.get('pinout', {}) if cfg else {}
i2c0_sda = pinout.get('i2c0_sda', 4)
i2c0_scl = pinout.get('i2c0_scl', 5)
i2c1_sda = pinout.get('i2c1_sda', 6)
i2c1_scl = pinout.get('i2c1_scl', 7)
dht_pin = pinout.get('dht_pin', 15)
relay_pin = pinout.get('relay_pin', 16)
# OLED
try:
from ssd1306 import SSD1306_I2C
_i2c0 = I2C(0, sda=Pin(i2c0_sda), scl=Pin(i2c0_scl))
_oled = SSD1306_I2C(128, 64, _i2c0)
HAS_OLED = True
except Exception:
_oled = None
HAS_OLED = False
# NFC
try:
from pn532 import PN532_I2C
_i2c1 = I2C(1, sda=Pin(i2c1_sda), scl=Pin(i2c1_scl))
_nfc = PN532_I2C(_i2c1)
HAS_NFC = True
except Exception as e:
print('[nfc] error:', e)
_nfc = None
HAS_NFC = False
# Sensor
try:
import dht
_dht = dht.DHT22(Pin(dht_pin))
HAS_SENSOR = True
except Exception:
_dht = None
HAS_SENSOR = False
# Relay
try:
_relay = Pin(relay_pin, Pin.OUT)
HAS_RELAY = True
except Exception:
_relay = None
HAS_RELAY = False
caps = {
'has_oled': HAS_OLED,
'has_nfc': HAS_NFC,
@@ -271,21 +298,6 @@ def run():
if state == 'BOOT':
_show(['Identity IoT', 'WIDE / AEL', '', 'Booting...', ''])
utime.sleep(1)
state = 'CAPABILITY_DETECT'
# ------------------------------------------------------------------
elif state == 'CAPABILITY_DETECT':
caps = detect_capabilities()
_show([
'Identity IoT',
'NFC:' + ('Y' if caps['has_nfc'] else 'N') +
' SNS:' + ('Y' if caps['has_sensor'] else 'N') +
' RLY:' + ('Y' if caps['has_relay'] else 'N'),
'',
'Checking config...',
'',
])
utime.sleep(1)
state = 'CONFIG_CHECK'
# ------------------------------------------------------------------
@@ -293,11 +305,26 @@ def run():
cfg = load_config()
if cfg and validate_config(cfg):
print('[config] found and valid')
state = 'WIFI_CONNECT'
state = 'CAPABILITY_DETECT'
else:
print('[config] missing or invalid — provisioning mode')
state = 'PROVISIONING'
# ------------------------------------------------------------------
elif state == 'CAPABILITY_DETECT':
caps = detect_capabilities(cfg)
_show([
'Identity IoT',
'NFC:' + ('Y' if caps['has_nfc'] else 'N') +
' SNS:' + ('Y' if caps['has_sensor'] else 'N') +
' RLY:' + ('Y' if caps['has_relay'] else 'N'),
'',
'READY',
'',
])
utime.sleep(1)
state = 'WIFI_CONNECT'
# ------------------------------------------------------------------
elif state == 'PROVISIONING':
if caps.get('has_nfc'):