nerves/buttons.py

102 lines
2.0 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
Functions "modes" :
-1 cls
0 Scappy
1 Rainbow
'''
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:
# Functions button
if not funcbutton.value:
if debug > 0:
print("func button pressed : ", funcbutton.value, funcstate )
sleep(0.2)
# Change functions 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
if crtfunc == 2:
leds.mode = 2
sleep(0.2)
funcstate = funcbutton.value
# Shutdown button
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")
# Animate Rainbow mode
if leds.mode == 1:
#pass
leds.rainbow_mode(wait = 0.001)
# Animate OSC mode
if leds.mode == 2:
pass
sleep(0.005)
if __name__ == '__main__':
runforever()