-
Notifications
You must be signed in to change notification settings - Fork 0
/
hard_switch.py
executable file
·53 lines (44 loc) · 1.34 KB
/
hard_switch.py
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
#! /usr/bin/env python
# coding: utf-8
# coding=utf-8
# -*- coding: utf-8 -*-
# vim: fileencoding=utf-8
import time
import RPi.GPIO as GPIO
import os
watch_pin = 17
wait_sec = 5
internal_green_led_pin = 16
GPIO.setmode(GPIO.BCM)
# LEDコントロール用に16pinを出力モードに
GPIO.setup(internal_green_led_pin, GPIO.OUT)
# GPIO17pinを入力モードとし、pull up設定とします
GPIO.setup(watch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
# ボタンが押されるまで待つ
GPIO.wait_for_edge(watch_pin, GPIO.FALLING)
sw_counter = 0
led_is_on = False
while True:
sw_status = GPIO.input(watch_pin)
if sw_status == 0:
sw_counter = sw_counter + 1
if led_is_on:
# LED On
GPIO.output(internal_green_led_pin, GPIO.LOW)
else:
# LED Off
GPIO.output(internal_green_led_pin, GPIO.HIGH)
led_is_on = not led_is_on
if sw_counter >= wait_sec:
print("長押し検知-shutdown")
os.system("sudo shutdown -h now")
break
else:
print("短押し検知-reboot")
os.system("sudo reboot")
break
time.sleep(1)
print(sw_counter)
# LED Off
GPIO.output(internal_green_led_pin, GPIO.HIGH)