85 lines
1.8 KiB
Python
85 lines
1.8 KiB
Python
#!/usr/bin/python3
|
|
# -*- coding: utf-8 -*-
|
|
# -*- mode: Python -*-
|
|
|
|
'''
|
|
|
|
Physical buttons handler for Nerves v0.1b
|
|
|
|
Func button : GPIO 23 (->pin 16) / ground pin 14
|
|
Down button : GPIO 24 (->pin 18) / ground 20
|
|
|
|
When button is pressed : button.value -> False
|
|
|
|
|
|
'''
|
|
|
|
from time import sleep
|
|
import board
|
|
import digitalio
|
|
import leds
|
|
|
|
crtfunc = 0
|
|
funcs = [0,1,2]
|
|
|
|
debug = 0
|
|
|
|
print("Loading Nerves buttons handler v0.1b")
|
|
|
|
funcbutton = digitalio.DigitalInOut(board.D23)
|
|
funcbutton.direction = digitalio.Direction.INPUT
|
|
funcbutton.pull = digitalio.Pull.UP
|
|
|
|
funcstate = True
|
|
|
|
downbutton = digitalio.DigitalInOut(board.D24)
|
|
downbutton.direction = digitalio.Direction.INPUT
|
|
downbutton.pull = digitalio.Pull.UP
|
|
|
|
def runforever():
|
|
global crtfunc, funcstate
|
|
|
|
while True:
|
|
|
|
#print(funcbutton.value, funcstate, crtfunc, downbutton.value)
|
|
#print(funcbutton.value, funcstate)
|
|
|
|
if not funcbutton.value:
|
|
|
|
if debug > 0:
|
|
print("func button pressed : ", funcbutton.value, funcstate )
|
|
|
|
sleep(0.2)
|
|
|
|
# Launch on button release
|
|
if funcbutton.value and funcstate == False :
|
|
|
|
crtfunc +=1
|
|
if crtfunc == len(funcs):
|
|
crtfunc = 0
|
|
print('Launch func', crtfunc)
|
|
if crtfunc == 0:
|
|
leds.mode = 0
|
|
if crtfunc == 1:
|
|
leds.mode = 1
|
|
|
|
sleep(0.2)
|
|
|
|
funcstate = funcbutton.value
|
|
|
|
if not downbutton.value:
|
|
if debug > 0:
|
|
print("down button pressed")
|
|
|
|
import os
|
|
print('will shutdown...')
|
|
leds.mode = -1
|
|
leds.cls()
|
|
os.system("systemctl poweroff")
|
|
|
|
sleep(0.005)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
runforever() |