TakwayBoard/takway/board/orangepi.py

122 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from takway.board.base_hd import BaseHardware
import threading
from datetime import datetime
import time
try:
import wiringpi
from wiringpi import GPIO
except:
pass
'''
| GPIO | LED |
| -- | - - |
| 0 | 红色 |
| 1 | 蓝色 |
| 2 | 绿色 |
| 3 | 黄色 |
| 4 | 白色 |
| GPIO | BUTTON |
| -- | ---- |
| 6 | 按键1 |
| 8 | 按键2 |
'''
class OrangePi(BaseHardware):
def __init__(self, hd_trigger='button', hd_detect_threshold=50):
super().__init__(hd_trigger, hd_detect_threshold)
self.LED_PIN_red = 0
self.LED_PIN_blue = 1
self.LED_PIN_green = 2
self.LED_PIN_yellow = 3
self.LED_PIN_white = 4
self.BUTTON_PIN_1 = 6
self.BUTTON_PIN_2 = 8
self.button_status_2 = False
self.led_set_status_2 = False
self.power_status = False # 单次触发按键状态
self.long_power_status = False
# self.short_power_status = False
self.button_init()
self.init_hd_thread()
def button_init(self):
wiringpi.wiringPiSetup()
# GPIO 输出模式
wiringpi.pinMode(self.LED_PIN_red, GPIO.OUTPUT)
wiringpi.pinMode(self.LED_PIN_blue, GPIO.OUTPUT)
wiringpi.pinMode(self.LED_PIN_green, GPIO.OUTPUT)
wiringpi.pinMode(self.LED_PIN_yellow, GPIO.OUTPUT)
wiringpi.pinMode(self.LED_PIN_white, GPIO.OUTPUT)
# GPIO 输入模式
wiringpi.pinMode(self.BUTTON_PIN_1, GPIO.INPUT)
wiringpi.pinMode(self.BUTTON_PIN_2, GPIO.INPUT)
def init_hd_thread(self):
hd_threads = [threading.Thread(target=self.hd_detection_loop)]
for hd_thread in hd_threads:
hd_thread.start()
def hd_detection_loop(self):
last_status_1 = False
press_time_1 = None
self.start_status_light()
while True:
button_status_1 = True if wiringpi.digitalRead(self.BUTTON_PIN_1) == 0 else False
# print(f"{datetime.now()}: BUTTON_PIN_1: {wiringpi.digitalRead(self.BUTTON_PIN_1)}")
if not button_status_1 and last_status_1:
press_time_1 = datetime.now()
elif button_status_1 and not last_status_1:
if press_time_1:
press_duration = (datetime.now() - press_time_1).total_seconds()
# if press_duration > 0.01:
if press_duration > 5:
time.sleep(1) # 防止短按误触发
subprocess.Popen('sudo shutdown now', shell=True)
else:
self.long_power_status = not self.long_power_status
# self.short_power_status = True
print(f"{datetime.now()}: press_duration: {press_duration}")
print(f"{datetime.now()}: button_status: {button_status_1}, last_button_status: {last_status_1}, power_status: {self.long_power_status}, interrupt_status: {self.short_power_status}")
press_time_1 = None
# TODO: 如果button_status_1和上一帧都是True or False则判断为无效按键忽略
last_status_1 = button_status_1
# 更新LED状态
if self.long_power_status:
self.set_led_on('green')
# if self.short_power_status:
# self.set_led_on('blue')
# else:
# self.set_led_off('blue')
else:
self.short_power_status = False
self.set_led_off('green')
self.set_led_off('blue')
self.set_led_off('red')
def set_led_on(self, color='red'):
wiringpi.digitalWrite(getattr(self, f'LED_PIN_{color}'), GPIO.HIGH)
def set_led_off(self, color='red'):
wiringpi.digitalWrite(getattr(self, f'LED_PIN_{color}'), GPIO.LOW)
def start_status_light(self):
for i in range(2):
self.set_led_on("green")
time.sleep(0.5)
self.set_led_off("green")
time.sleep(0.5)
if __name__ == '__main__':
orangepi = OrangePi()
while True:
pass