Files
sumbrella/tests/check_pins.py
T
Andre Neto 5d4b62c40b Build the two sketches with PlatformIO, add offline checks and rewrite the README
Sumbrella had no build definition: it was two .ino files to open in the Arduino
IDE. This adds one PlatformIO project per sketch (PlatformIO only converts a
*.ino sitting at the top of its own src_dir) and there is now a real, repeated
compile for the boards the project actually used:

  Projeto.ino       -> Arduino Nano        206 B RAM, 3508 B flash of 30720
  Fetch_rain.ino    -> ESP8266 ESP-01     28972 B RAM, 280119 B of 434160 flash

The boards come from the course poster kept in the repository (Sumbrella.pdf,
"Listagem de material": Arduino Nano and ESP8226 ESP-01), not from the code:
neither sketch names its board. The ESP-01 flash size is not given anywhere, so
the 512 KB variant is targeted.

Dependencies are now reproducible. LiquidCrystal_I2C comes from the PlatformIO
registry by name and version. DS3231 1.01 cannot: all 87 registry hits were
checked and none exposes class Time / getTimeStr() / DS3231(data, sclk), so it is
pinned to tag v1.01 of a mirror whose files were verified byte-for-byte equal to
the libs/DS3231 removed in 009066d. A clean clone compiles as it is.

The unused LCD stays unused and no sensor is invented: the only thing this commit
does to the sketches is compile them.

tests/ holds the checks that need no board, each with a negative selftest that
proves the check bites: secrets hygiene (secrets.h not tracked and ignored, the
example complete and placeholder-only, no credential literal in any tracked file)
and pin validity against the Nano and ESP-01 pin maps. run.sh builds both targets
and then runs them, exiting non-zero on any failure. .gitignore now covers the
PlatformIO artefacts (.pio/, *.bin, *.elf and friends).

README.md is rewritten in the common format, with the real build and test output.
Two things it no longer says, because they are not true of this repository: no
revision reachable from main ever contained a credential literal (the deleted
ESP8266_Fetching_Data.ino carried the string "<REDACTED>"), and the DS3231
dependency is not installable from the PlatformIO registry.

Known, left unfixed on purpose: the LCD is constructed and never written to; the
rain values are printed and discarded; loop() calls setup() on failure; and
libs/DS3231 is still reachable in the history before 009066d.
2026-10-08 16:11:49 +01:00

171 lines
5.2 KiB
Python

#!/usr/bin/env python3
"""Offline check: every MCU pin used by the sketches exists on the target board.
Board data is the published pin map of each board:
- Arduino Nano (ATmega328P, DIP-30): digital pins 0-19, where A0-A5 are 14-19,
PWM on 3, 5, 6, 9, 10 and 11, and SDA/SCL are A4/A5 = 18/19.
- ESP-01 (ESP8266EX, 8-pin module): GPIO0-GPIO3 are the pins broken out; PWM is
available on every ESP8266 GPIO except GPIO16.
The board of each sketch is the one named on the course poster kept in this
repository (Sumbrella.pdf, "Listagem de material": Arduino Nano and ESP8266
ESP-01) and declared in platformio.ini.
Run with --selftest to prove the check bites: copies with an out-of-range pin
and with analogWrite() on a non-PWM pin must both be rejected.
"""
import os
import re
import shutil
import subprocess
import sys
import tempfile
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BOARDS = {
"nano": {
"env": "nano",
"sketch": "Projeto.ino",
"pins": set(range(0, 20)),
"pwm": {3, 5, 6, 9, 10, 11},
"macros": {"SDA": 18, "SCL": 19},
"dn_pins": False,
},
"esp01": {
"env": "esp8266",
"sketch": os.path.join("Fetch_rain", "Fetch_rain.ino"),
"pins": {0, 1, 2, 3},
"pwm": {0, 1, 2, 3},
"macros": {},
"dn_pins": True,
},
}
CALL_RE = re.compile(
r"\b(pinMode|digitalWrite|digitalRead|analogWrite|analogRead)\s*\(\s*"
r"([A-Za-z_][A-Za-z0-9_]*|\d+)"
)
ESPNUM_RE = re.compile(r"\bD(\d{1,2})\b")
def read(path):
with open(path, encoding="utf-8") as fh:
return fh.read()
def used_pins(text, macros):
"""Return [(function, raw_pin_text, resolved_int)] for a sketch."""
found = []
for func, raw in CALL_RE.findall(text):
if raw.isdigit():
found.append((func, raw, int(raw)))
elif raw in macros:
found.append((func, raw, macros[raw]))
return found
def check(root):
errors = []
for board, cfg in BOARDS.items():
path = os.path.join(root, cfg["sketch"])
if not os.path.isfile(path):
errors.append("%s: sketch %s is missing" % (board, cfg["sketch"]))
continue
text = read(path)
for func, raw, pin in used_pins(text, cfg["macros"]):
if pin not in cfg["pins"]:
errors.append("%s: %s(%s) -> pin %s does not exist on %s "
"(valid: 0-%d)"
% (board, func, raw, pin, board,
max(cfg["pins"])))
elif func == "analogWrite" and pin not in cfg["pwm"]:
errors.append("%s: analogWrite(%s) -> pin %s is not PWM-capable"
% (board, raw, pin))
# A stray D0..D16 style pin on the AVR target would not compile.
if not cfg["dn_pins"] and ESPNUM_RE.search(text):
errors.append("%s: %s uses an ESP8266 Dn pin name"
% (board, cfg["sketch"]))
return errors
def run_selftest():
failures = []
def copy():
tmp = tempfile.mkdtemp(prefix="sumbrella-pin-selftest-")
dst = os.path.join(tmp, "repo")
shutil.copytree(REPO, dst, symlinks=True)
return tmp, dst
cases = [
("pin outside the board range",
"Projeto.ino",
lambda t: t.replace("pinMode(5, OUTPUT)", "pinMode(23, OUTPUT)")),
("analogWrite on a non-PWM pin",
"Projeto.ino",
lambda t: t.replace("analogWrite(5, redValue)",
"analogWrite(4, redValue)")),
("ESP8266 Dn pin name in an AVR sketch",
"Projeto.ino",
lambda t: t.replace("pinMode(6, OUTPUT)", "pinMode(D6, OUTPUT)")),
]
for name, rel, mutate in cases:
tmp, dst = copy()
try:
p = os.path.join(dst, rel)
text = read(p)
new = mutate(text)
if new == text:
print(" FAIL selftest could not mutate %s (%s)" % (rel, name))
failures.append(name)
continue
with open(p, "w", encoding="utf-8") as fh:
fh.write(new)
if check(dst):
print(" PASS negative case detected: %s" % name)
else:
print(" FAIL negative case NOT detected: %s" % name)
failures.append(name)
finally:
shutil.rmtree(tmp, ignore_errors=True)
tmp, dst = copy()
try:
if check(dst):
print(" FAIL control copy is not clean: %s" % "; ".join(check(dst)))
failures.append("control")
else:
print(" PASS control copy is clean")
finally:
shutil.rmtree(tmp, ignore_errors=True)
return failures
def main():
if "--selftest" in sys.argv:
print("check_pins.py --selftest")
failures = run_selftest()
if failures:
print("SELFTEST FAILED: %s" % ", ".join(failures))
return 1
print("SELFTEST OK")
return 0
errors = check(REPO)
for err in errors:
print(" FAIL %s" % err)
if errors:
return 1
print(" PASS all pins used are valid and PWM-capable where required")
return 0
if __name__ == "__main__":
sys.exit(main())