-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbutton_detect
executable file
·94 lines (82 loc) · 3.37 KB
/
button_detect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import smbus
import syslog
import time
import os
#Var decloration:
bus=1
battery_loop_sleep = .3
tmp_dir = "/tmp/"
shut_file = "shut"
address = 0x75 # pisugar2 i2c address is 0x75 for battery management.
TAP_ARRAY = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
#Setup logging: Work in progress
syslog.openlog(ident="pishugar_button_mon",logoption=syslog.LOG_PID, facility=syslog.LOG_LOCAL7)
syslog.syslog(syslog.LOG_INFO, ' button_detect was started')
#Begin Function defenition:
######################################################################################################
#This module detects the type of press - single tap; double tap, or long press:
def gpio_tap_detect(tap):
global TAP_ARRAY
if tap > 0:
tap = 1
del TAP_ARRAY[0]
TAP_ARRAY.append(tap)
string = "".join([str(x) for x in TAP_ARRAY])
# print (string)
# print (tap)
should_refresh = False
current_tap_type = ""
if string.find("111111110") >= 0:
print("long tap event")
current_tap_type = "long"
should_refresh = True
if string.find("1010") >= 0 \
or string.find("10010") >= 0 \
or string.find("10110") >= 0\
or string.find("100110") >= 0\
or string.find("101110") >= 0\
or string.find("1001110") >= 0:
print("double tap event")
current_tap_type = "double"
should_refresh = True
if string.find("1000") >= 0:
print("single tap event")
current_tap_type = "single"
should_refresh = True
if should_refresh:
TAP_ARRAY = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
return (current_tap_type)
#End Function defenition:
######################################################################################################
try: #Try setting the following attributes, to turn on the button.
i2c_bus=smbus.SMBus(bus) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
i2c_bus.write_byte_data(address, 0x51, 0b0000_0100)
i2c_bus.write_byte_data(address, 0x53, 0b0000_0010)
except: #if we hit except, we exit.
syslog.syslog(syslog.LOG_INFO, 'Did not find the PiSugar2, exiting....')
exit(1)
syslog.syslog(syslog.LOG_INFO, 'Found the PiSugar2 button.')
while True:
time.sleep(battery_loop_sleep) #sleep for for the duration of battery_loop_sleep, before we loop again.
button_press = i2c_bus.read_byte_data(address, 0x55) #Wait for the button press
if button_press >= 0: #Once the button is pressed...
tap_type=gpio_tap_detect(button_press)
if tap_type == "single": #maximum 2 second press
file = open(tmp_dir+shut_file,'w')
file.write(str("p"))
file.close()
syslog.syslog(syslog.LOG_INFO, 'Button-press detected, shutting down...')
os.system('wall -n "shutdown by way of the power button being pressed!"')
os.system("poweroff -p") #shut the pi down.
if tap_type == "long": #minimum of 3 second press
file = open(tmp_dir+shut_file,'w')
file.write(str("r"))
file.close()
syslog.syslog(syslog.LOG_INFO, 'Button-press detected, rebooting...')
os.system('wall -n "reboot by way of the power button being long pressed!"')
os.system("reboot") #reboot the pi.
#exit routine:
syslog.syslog(syslog.LOG_INFO, 'button_detect terminated, exiting...')
exit(0)