2021-04-26 00:34:15 +00:00
|
|
|
#!/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
|
|
|
|
|
2021-04-30 00:11:12 +00:00
|
|
|
Functions "modes" :
|
|
|
|
|
|
|
|
-1 cls
|
|
|
|
0 Scappy
|
|
|
|
1 Rainbow
|
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2021-04-30 00:11:12 +00:00
|
|
|
# Functions button
|
2021-04-26 00:34:15 +00:00
|
|
|
if not funcbutton.value:
|
|
|
|
|
|
|
|
if debug > 0:
|
|
|
|
print("func button pressed : ", funcbutton.value, funcstate )
|
|
|
|
|
|
|
|
sleep(0.2)
|
|
|
|
|
2021-04-30 00:11:12 +00:00
|
|
|
# Change functions on button release
|
2021-04-26 00:34:15 +00:00
|
|
|
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
|
2021-04-30 00:11:12 +00:00
|
|
|
if crtfunc == 2:
|
|
|
|
leds.mode = 2
|
2021-04-26 00:34:15 +00:00
|
|
|
|
|
|
|
sleep(0.2)
|
|
|
|
|
|
|
|
funcstate = funcbutton.value
|
|
|
|
|
2021-04-30 00:11:12 +00:00
|
|
|
|
|
|
|
# Shutdown button
|
2021-04-26 00:34:15 +00:00
|
|
|
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")
|
|
|
|
|
2021-04-30 00:11:12 +00:00
|
|
|
# Animate Rainbow mode
|
|
|
|
if leds.mode == 1:
|
|
|
|
#pass
|
|
|
|
leds.rainbow_mode(wait = 0.001)
|
|
|
|
|
|
|
|
# Animate OSC mode
|
|
|
|
if leds.mode == 2:
|
|
|
|
pass
|
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
sleep(0.005)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
runforever()
|