95 lines
2.6 KiB
Python
95 lines
2.6 KiB
Python
"""
|
|
test_gesture.py — Tests del mòdul gesture.py (sensor PAJ7620U2).
|
|
Executa des del directori Rasp/: python tests/test_gesture.py
|
|
|
|
Descomenta la funció que vols provar al final del fitxer.
|
|
Assegura't que el venv està activat i pigpiod en marxa (sudo pigpiod -s 1).
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
import time
|
|
from gesture import (
|
|
gesture_setup, gesture_cleanup,
|
|
read_gesture,
|
|
GS_NONE, GS_FORWARD, GS_LEFT, GS_RIGHT,
|
|
GS_UP, GS_DOWN, GS_CLOCKWISE, GS_ANTICLOCKWISE, GS_WAVE,
|
|
)
|
|
|
|
_GESTURE_NAMES = {
|
|
GS_NONE: "Cap (GS_NONE)",
|
|
GS_FORWARD: "Endavant (GS_FORWARD)",
|
|
GS_LEFT: "Esquerra (GS_LEFT)",
|
|
GS_RIGHT: "Dreta (GS_RIGHT)",
|
|
GS_UP: "Amunt (GS_UP)",
|
|
GS_DOWN: "Avall (GS_DOWN)",
|
|
GS_CLOCKWISE: "Horari (GS_CLOCKWISE)",
|
|
GS_ANTICLOCKWISE: "Antihorari (GS_ANTICLOCKWISE)",
|
|
GS_WAVE: "Wave (GS_WAVE)",
|
|
}
|
|
|
|
|
|
# ==================
|
|
# TEST 1 — Connexió i inicialització
|
|
# ==================
|
|
|
|
def test_connection():
|
|
"""
|
|
Comprova que el sensor PAJ7620U2 és accessible via I2C.
|
|
Ha de mostrar 'Gesture sensor init OK' sense errors.
|
|
"""
|
|
print("=== TEST CONNEXIÓ PAJ7620U2 ===")
|
|
|
|
gesture_setup()
|
|
time.sleep(0.5)
|
|
|
|
gesture_cleanup()
|
|
print("Test connexió completat.\n")
|
|
|
|
|
|
# ==================
|
|
# TEST 2 — Lectura de gestos
|
|
# ==================
|
|
|
|
def test_read_gestures():
|
|
"""
|
|
Llegeix gestos durant 30 segons i els mostra per pantalla.
|
|
Fes gestos davant del sensor per verificar que els detecta correctament:
|
|
- Mà cap endavant/enrere → GS_FORWARD
|
|
- Mà cap a l'esquerra → GS_LEFT
|
|
- Mà cap a la dreta → GS_RIGHT
|
|
- Mà cap amunt → GS_UP
|
|
- Mà cap avall → GS_DOWN
|
|
- Rotació horària → GS_CLOCKWISE
|
|
- Rotació antihorària → GS_ANTICLOCKWISE
|
|
- Sacsejada (wave) → GS_WAVE
|
|
"""
|
|
print("=== TEST LECTURA GESTOS PAJ7620U2 ===")
|
|
print("Fes gestos davant del sensor durant 30 segons...")
|
|
|
|
gesture_setup()
|
|
time.sleep(0.5)
|
|
|
|
inici = time.time()
|
|
while time.time() - inici < 30:
|
|
gest = read_gesture()
|
|
if gest != GS_NONE:
|
|
nom = _GESTURE_NAMES.get(gest, f"Desconegut ({gest})")
|
|
print(f" Gest detectat: {nom}")
|
|
time.sleep(0.05)
|
|
|
|
gesture_cleanup()
|
|
print("Test lectura gestos completat.\n")
|
|
|
|
|
|
# ==================
|
|
# Execució
|
|
# ==================
|
|
|
|
if __name__ == "__main__":
|
|
# Descomenta el test que vols executar:
|
|
test_connection()
|
|
# test_read_gestures()
|