#!/usr/bin/python3

import evdev
import periphery

import subprocess
import time

GPIO_LINE = 168
KEY = evdev.ecodes.KEY_F15

FALLING_EDGE = "falling"
RISING_EDGE = "rising"

gpio_in = periphery.GPIO("/dev/gpiochip0", GPIO_LINE, "in")

first_edge = FALLING_EDGE
second_edge = RISING_EDGE

while True:
    print("Check poll falling 1 -> 0 interrupt on line", GPIO_LINE)

    value = gpio_in.read()
    print(value)
    
    if not value:
        first_edge = RISING_EDGE

    gpio_in.edge = first_edge
    gpio_in.poll(timeout=None)

    subprocess.run("pkexec /usr/lib/astra-mobile/t8s_scan_trig", shell=True)

    if (first_edge == FALLING_EDGE):
        second_edge = RISING_EDGE
    else:
        second_edge = FALLING_EDGE

    value = gpio_in.read()

    if value:
        first_edge = second_edge
    else:
        gpio_in.edge = second_edge
        gpio_in.poll(timeout=None)

    time.sleep(0.5)
    subprocess.run("pkexec /usr/lib/astra-mobile/t8s_scan_trig 0", shell=True)

gpio_in.close()
