python-gpio.py

Arduino Portenta X8 · Archivo : python-gpio.py · Lenguaje : Python · Uso : Script Python complet : bouton, LED et journalisation

El script de aplicación tipo sobre el Linux de la X8: entrada, salida y registro CSV con marca de tiempo usando python3-gpiod. La placa se vuelve un contador autónomo por el lado Linux mientras el core Arduino mantiene el tiempo real.

Hardware

Un Arduino Portenta X8 (módulo ST STM32MP1: quad Cortex-A7 Linux + doble núcleo Cortex-M4 de tiempo real, 8 GB eMMC, 2 GB LPDDR4, Wi-Fi/BT integrados, opción LTE). 1 LED + 330 Ω, 1 botón (hacia el pin, tirada a definir según el esquema).

Cableado

LED y botón en los pines del conector (carrier board), masas comunes. Verifica el nivel lógico máximo de cada pin.

Funcionamiento

El script escruta la línea del botón a 50 Hz, enciende el LED y añade una línea ISO al CSV en el flanco de subida; finally libera las líneas del kernel.

Ejecución

Lado Linux: acceso SSH (USB-C routed o red) sobre la imagen Portenta Linux, scripts bash/Python. Lado tiempo real: Arduino IDE con el core « Arduino Mbed OS Portenta X8 Boards », núcleo M7 de aplicación, grabado vía mboot desde Linux.

Variantes

Sustituir el bucle por espera de evento (edge event de gpiod), o empujar el CSV a la nube con un servicio systemd.

python-gpio.py

#!/usr/bin/env python3
# Bouton + LED + trace CSV en Python avec python3-gpiod (API 1.x).
# sudo apt install python3-gpiod
import gpiod
from time import sleep
from datetime import datetime

PUCE = "gpiochip0"     # gpiodetect
LED = 27               # broche carrier (exemple, voir schema)
BTN = 23

chip = gpiod.Chip(PUCE)
led = chip.get_line(LED)
led.request(consumer="app", type=gpiod.LINE_REQ_DIR_OUT, default_val=0)
btn = chip.get_line(BTN)
btn.request(consumer="app", type=gpiod.LINE_REQ_DIR_IN)

print("Appuis sur le bouton -> LED + /tmp/appuis.csv")
try:
    dernier = btn.get_value()
    while True:
        etat = btn.get_value()
        if etat != dernier and etat == 1:
            led.value(1)
            with open("/tmp/appuis.csv", "a") as f:
                f.write(f"{datetime.now().isoformat()},appui\n")
            print("appui enregistre")
        else:
            led.value(0)
        dernier = etat
        sleep(0.02)
finally:
    led.release()
    btn.release()

← Volver a la ficha