First version running with One led

This commit is contained in:
Nicolas Bertrand 2025-10-09 20:01:45 +02:00
parent 35b039a39a
commit 5fba1ce900
2 changed files with 26 additions and 42 deletions

View File

@ -35,10 +35,10 @@ class GpioLed:
self.led = None
def on(self):
self.led.set_value(1)
self.led.set_value(0)
def off(self):
self.led.set_value(0)
self.led.set_value(1)
def __str__(self):
return f'LED{self.gpio_pin:02}'
@ -141,4 +141,4 @@ _leds = GpioLeds(config.GPIO_CHIP, config.LEDS_UUIDS) if config.GPIO_CHIP is not
def get() -> Leds:
return _leds
return _leds

View File

@ -18,40 +18,26 @@ def _get_gpio_leds():
app.extensions[ext_key] = leds.get().enter()
return app.extensions[ext_key]
@blueprint.record_once
def _register_cleanup(state):
"""
Register a teardown handler on the app that will try to exit the leds controller
when the app context is torn down.
"""
app = state.app
ext_key = 'nenuscanner_gpio_leds'
@app.teardown_appcontext
def _cleanup(exception=None):
gpio = app.extensions.get(ext_key)
if gpio:
try:
gpio.exit()
except Exception:
# best effort cleanup, don't raise during teardown
pass
# WARNING : how to release
#@blueprint.record_once
#def _register_cleanup(state):
# """
#Register a teardown handler on the app that will try to exit the leds controller
# when the app context is torn down.
# """
# app = state.app
# ext_key = 'nenuscanner_gpio_leds'
# @app.teardown_appcontext
# def _cleanup(exception=None):
# gpio = app.extensions.get(ext_key)
# if gpio:
# try:
# gpio.exit()
# except Exception:
# # best effort cleanup, don't raise during teardown
# pass
# Routes for object management
# Nom du chip GPIO (sur Raspberry Pi 5, généralement "gpiochip4")
CHIP = "gpiochip4"
LINE = 14 # Numéro BCM du GPIO (ici GPIO14)
# Ouvrir le chip
chip = gpiod.Chip(CHIP)
# Demander laccès à la ligne GPIO14 en sortie
line = chip.get_line(LINE)
line.request(consumer="Small Led 1", type = gpiod.LINE_REQ_DIR_OUT)
line.set_value(1)
# Routes for object management
@ -60,12 +46,7 @@ def get():
"""
Returns the pages showing all leds.
"""
gpio_leds = _get_gpio_leds()
print(gpio_leds)
for i, led in enumerate(gpio_leds.leds):
print(f"LED {i}: {led}, is_on={led.is_on}")
return render_template(
'leds.html', leds= config.LEDS_UUIDS)
@ -74,7 +55,7 @@ def get():
def set_led():
"""
Reçoit une commande pour allumer ou éteindre une LED.
Attend un JSON : { "led": "led1", "state": "on" } ou { "led": "led2", "state": "off" }
Attend un JSON : { "led": "14", "state": "on" } ou { "led": "15, "state": "off" }
"""
data = request.get_json()
led = data.get('led')
@ -90,8 +71,11 @@ def set_led():
gpio_led.on()
else:
gpio_led.off()
except Exception as e:
return jsonify({'status': 'error', 'error': str(e)}), 400
raise
print(f'{e}')
return jsonify({'status': 'error', 'error': 'error'}), 400
print(f"Commande reçue pour {led} : {state}")