add pins interface

This commit is contained in:
Alejandro Diaz 2023-09-13 12:43:45 -06:00
parent e841e69b47
commit ee45d97a8b

View file

@ -2,8 +2,8 @@ from django.shortcuts import render
from django.http import HttpResponse
import ctypes
# pin_control = ctypes.CDLL("@pin_control@/lib/libpin_control.so")
# print(f"INIT: {pin_control.init_gpio()}")
pin_control = ctypes.CDLL("@pin_control@/lib/libpin_control.so")
print(f"INIT: {pin_control.init_gpio()}")
leds_state = {
1: False,
@ -13,6 +13,14 @@ leds_state = {
5: False,
}
leds_gpio = {
1: 5,
2: 6,
3: 13,
4: 19,
5: 26
}
def home(request):
return render(request, "home.html")
@ -39,7 +47,11 @@ def turn_on_all():
print("TURN ON ALL")
return HttpResponse("Ok")
def toggle_led(led, state) :
print(f"NEW_STATE ({led}): {not state}")
leds_state[led] = not state
return HttpResponse(f"Turn on {led}" if state else f"Turn off {led}")
def toggle_led(led, turn_on):
gpio_pin = leds_gpio[led]
if turn_on:
pin_control.turn_on_pin(gpio_pin)
else:
pin_control.turn_off_pin(gpio_pin)
leds_state[led] = not turn_on
return HttpResponse(f"Turn on {led}" if turn_on else f"Turn off {led}")