writing current status to /tmp/status
This commit is contained in:
parent
fb1ebf77f3
commit
41cd53cb0f
1 changed files with 24 additions and 3 deletions
|
@ -2,7 +2,9 @@ import pifacedigitalio
|
||||||
import pifacecommon.mcp23s17
|
import pifacecommon.mcp23s17
|
||||||
import time
|
import time
|
||||||
from subprocess import call
|
from subprocess import call
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# dirty workaround to disable reset of output pins
|
||||||
def init_board_no_power_off(self):
|
def init_board_no_power_off(self):
|
||||||
ioconfig = (
|
ioconfig = (
|
||||||
pifacecommon.mcp23s17.BANK_OFF |
|
pifacecommon.mcp23s17.BANK_OFF |
|
||||||
|
@ -27,7 +29,7 @@ def init_board_no_power_off(self):
|
||||||
|
|
||||||
setattr(pifacedigitalio.PiFaceDigital, 'init_board', init_board_no_power_off)
|
setattr(pifacedigitalio.PiFaceDigital, 'init_board', init_board_no_power_off)
|
||||||
|
|
||||||
button_count = 8
|
button_count = 2
|
||||||
longpress_delay = 0.4
|
longpress_delay = 0.4
|
||||||
verylongpress_delay = 2
|
verylongpress_delay = 2
|
||||||
shortpress_count_max_delay = 0.5
|
shortpress_count_max_delay = 0.5
|
||||||
|
@ -37,10 +39,23 @@ pin_last_shortpress = [0]*button_count
|
||||||
pin_shortpress_count = [0]*button_count
|
pin_shortpress_count = [0]*button_count
|
||||||
pifacedigital = pifacedigitalio.PiFaceDigital()
|
pifacedigital = pifacedigitalio.PiFaceDigital()
|
||||||
|
|
||||||
|
def chch_status_output(pfd):
|
||||||
|
f = open("/root/chch-power/status", 'w')
|
||||||
|
status = "geschlossen"
|
||||||
|
for i in range(button_count):
|
||||||
|
if pfd.output_pins[i].value != 0:
|
||||||
|
status = "offen"
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
f.truncate(0)
|
||||||
|
f.write(status)
|
||||||
|
except OSError:
|
||||||
|
sys.stderr("Can not write to status file\n")
|
||||||
|
|
||||||
def switch_pressed(event):
|
def switch_pressed(event):
|
||||||
pin_last_rise_date[event.pin_num] = 0
|
pin_last_rise_date[event.pin_num] = 0
|
||||||
time.sleep(0.02)
|
time.sleep(0.02)
|
||||||
if pifacedigitalio.digital_read(event.pin_num) != 0:
|
if event.chip.input_pins[event.pin_num].value != 0:
|
||||||
pin_last_rise_date[event.pin_num] = time.time()
|
pin_last_rise_date[event.pin_num] = time.time()
|
||||||
|
|
||||||
def switch_unpressed(event):
|
def switch_unpressed(event):
|
||||||
|
@ -57,6 +72,7 @@ def switch_unpressed(event):
|
||||||
# toggle current room's power with a short and a long press
|
# toggle current room's power with a short and a long press
|
||||||
# call a script with two short and a long press
|
# call a script with two short and a long press
|
||||||
def switch_longpressed(event):
|
def switch_longpressed(event):
|
||||||
|
chch_status_output()
|
||||||
shortpress_delay = pin_last_rise_date[event.pin_num] - pin_last_shortpress[event.pin_num]
|
shortpress_delay = pin_last_rise_date[event.pin_num] - pin_last_shortpress[event.pin_num]
|
||||||
if shortpress_delay > inter_press_max_delay:
|
if shortpress_delay > inter_press_max_delay:
|
||||||
pin_shortpress_count[event.pin_num] = 0
|
pin_shortpress_count[event.pin_num] = 0
|
||||||
|
@ -65,10 +81,12 @@ def switch_longpressed(event):
|
||||||
elif pin_shortpress_count[event.pin_num] == 2:
|
elif pin_shortpress_count[event.pin_num] == 2:
|
||||||
if event.pin_num == 0:
|
if event.pin_num == 0:
|
||||||
call(["/root/chch-power/light.sh"])
|
call(["/root/chch-power/light.sh"])
|
||||||
|
chch_status_output(event.chip)
|
||||||
|
|
||||||
# switch all power off with a very long press
|
# switch all power off with a very long press
|
||||||
# switch all power on with a short and a very long press
|
# switch all power on with a short and a very long press
|
||||||
def switch_verylongpressed(event):
|
def switch_verylongpressed(event):
|
||||||
|
chch_status_output()
|
||||||
shortpress_delay = pin_last_rise_date[event.pin_num] - pin_last_shortpress[event.pin_num]
|
shortpress_delay = pin_last_rise_date[event.pin_num] - pin_last_shortpress[event.pin_num]
|
||||||
if shortpress_delay > inter_press_max_delay:
|
if shortpress_delay > inter_press_max_delay:
|
||||||
pin_shortpress_count[event.pin_num] = 0
|
pin_shortpress_count[event.pin_num] = 0
|
||||||
|
@ -77,6 +95,7 @@ def switch_verylongpressed(event):
|
||||||
elif pin_shortpress_count[event.pin_num] == 1:
|
elif pin_shortpress_count[event.pin_num] == 1:
|
||||||
for i in range(button_count):
|
for i in range(button_count):
|
||||||
event.chip.output_pins[i].turn_on()
|
event.chip.output_pins[i].turn_on()
|
||||||
|
chch_status_output(event.chip)
|
||||||
|
|
||||||
def switch_shortpressed(event):
|
def switch_shortpressed(event):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
|
@ -89,12 +108,14 @@ def switch_shortpressed(event):
|
||||||
else:
|
else:
|
||||||
pin_shortpress_count[event.pin_num] += 1
|
pin_shortpress_count[event.pin_num] += 1
|
||||||
pin_last_shortpress[event.pin_num] = now
|
pin_last_shortpress[event.pin_num] = now
|
||||||
|
chch_status_output(event.chip)
|
||||||
print(["count:", pin_shortpress_count[event.pin_num]])
|
print(["count:", pin_shortpress_count[event.pin_num]])
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
pifacedigitalio.init()
|
|
||||||
listener = pifacedigitalio.InputEventListener(chip=pifacedigital)
|
listener = pifacedigitalio.InputEventListener(chip=pifacedigital)
|
||||||
for i in range(button_count):
|
for i in range(button_count):
|
||||||
listener.register(i, pifacedigitalio.IODIR_ON, switch_pressed)
|
listener.register(i, pifacedigitalio.IODIR_ON, switch_pressed)
|
||||||
listener.register(i, pifacedigitalio.IODIR_OFF, switch_unpressed)
|
listener.register(i, pifacedigitalio.IODIR_OFF, switch_unpressed)
|
||||||
listener.activate()
|
listener.activate()
|
||||||
|
pfd = pifacedigitalio.PiFaceDigital()
|
||||||
|
chch_status_output(pfd)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue