triviOS/ui/main/views.py

67 lines
1.4 KiB
Python

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()}")
leds_state = {
1: False,
2: False,
3: False,
4: False,
5: False,
}
leds_gpio = {
1: 5,
2: 6,
3: 13,
4: 19,
5: 26,
7: 21,
8: 20,
9: 16,
10: 12,
}
def home(request):
return render(request, "home.html")
def take_photo(request):
pin_control.capture_image(b'/run/nginx/static/photo.jpg')
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():
for pin in leds_gpio.values():
pin_control.turn_off_pin(pin)
return HttpResponse("Ok")
def turn_on_all():
for pin in leds_gpio.values():
pin_control.turn_on_pin(pin)
return HttpResponse("Ok")
def toggle_led(led, turn_on):
gpio_pin = leds_gpio[led]
if turn_on:
pin_control.turn_off_pin(gpio_pin)
else:
pin_control.turn_on_pin(gpio_pin)
leds_state[led] = not turn_on
return HttpResponse("on" if turn_on else "off")
def button_detect(request, pin):
return HttpResponse(pin_control.probe_pin(leds_gpio[pin]))