triviOS/ui/main/views.py

59 lines
1.3 KiB
Python
Raw Normal View History

2023-09-11 08:07:53 +02:00
from django.shortcuts import render
2023-09-13 18:38:24 +02:00
from django.http import HttpResponse
2023-09-12 23:51:16 +02:00
import ctypes
2023-09-11 08:07:53 +02:00
2023-09-13 20:43:45 +02:00
pin_control = ctypes.CDLL("@pin_control@/lib/libpin_control.so")
print(f"INIT: {pin_control.init_gpio()}")
2023-09-11 09:59:25 +02:00
2023-09-13 18:38:24 +02:00
leds_state = {
1: False,
2: False,
3: False,
4: False,
5: False,
}
2023-09-12 23:51:16 +02:00
2023-09-13 20:43:45 +02:00
leds_gpio = {
1: 5,
2: 6,
3: 13,
4: 19,
5: 26
}
2023-09-11 09:59:25 +02:00
def home(request):
2023-09-13 18:38:24 +02:00
return render(request, "home.html")
def take_photo(request):
2023-09-13 22:07:26 +02:00
pin_control.capture_image(b'/run/nginx/static/photo.jpg')
2023-09-13 18:38:24 +02:00
return HttpResponse("Ok")
def led(request, led):
if request.method == "GET":
if led == 0: return turn_off_all()
if led == 6: return turn_on_all()
state = leds_state.get(led)
if state is not None: return toggle_led(led, state)
print("UNKNOWN LED")
return HttpResponse("Ok")
def turn_off_all():
2023-09-13 21:11:35 +02:00
for pin in leds_gpio.values():
2023-09-13 21:36:06 +02:00
pin_control.turn_off_pin(pin)
2023-09-13 18:38:24 +02:00
return HttpResponse("Ok")
def turn_on_all():
2023-09-13 21:11:35 +02:00
for pin in leds_gpio.values():
2023-09-13 21:36:06 +02:00
pin_control.turn_on_pin(pin)
2023-09-13 18:38:24 +02:00
return HttpResponse("Ok")
2023-09-13 20:43:45 +02:00
def toggle_led(led, turn_on):
gpio_pin = leds_gpio[led]
if turn_on:
pin_control.turn_off_pin(gpio_pin)
2023-09-13 21:01:49 +02:00
else:
pin_control.turn_on_pin(gpio_pin)
2023-09-13 20:43:45 +02:00
leds_state[led] = not turn_on
2023-09-13 22:07:26 +02:00
return HttpResponse("on" if turn_on else f"Turn on {led}")