TakwayBoard/takway/board/orangepi.py

125 lines
4.3 KiB
Python
Raw Normal View History

2024-05-23 01:27:51 +08:00
from takway.board.base_hd import BaseHardware
import threading
2024-05-24 22:58:16 +08:00
from datetime import datetime
2024-05-27 23:48:35 +08:00
import time
2024-06-18 23:10:05 +08:00
import subprocess
2024-05-23 01:27:51 +08:00
try:
import wiringpi
from wiringpi import GPIO
except:
pass
'''
| GPIO | LED |
| -- | - - |
| 0 | 红色 |
2024-06-09 02:37:17 +08:00
| 1 | 蓝色 |
2024-05-23 01:27:51 +08:00
| 2 | 绿色 |
2024-06-09 02:37:17 +08:00
| 3 | 黄色 |
2024-05-23 01:27:51 +08:00
| 4 | 白色 |
2024-05-23 16:07:23 +08:00
| GPIO | BUTTON |
2024-05-23 01:27:51 +08:00
| -- | ---- |
| 6 | 按键1 |
| 8 | 按键2 |
'''
class OrangePi(BaseHardware):
2024-07-15 14:53:50 +08:00
def __init__(self, hd_trigger='button', hd_detect_threshold=50, enable_start_light=False):
2024-05-23 01:27:51 +08:00
super().__init__(hd_trigger, hd_detect_threshold)
2024-07-15 14:53:50 +08:00
self.enable_start_light = enable_start_light
2024-05-23 01:27:51 +08:00
self.LED_PIN_red = 0
2024-06-09 02:37:17 +08:00
self.LED_PIN_blue = 1
2024-05-23 01:27:51 +08:00
self.LED_PIN_green = 2
2024-06-09 02:37:17 +08:00
self.LED_PIN_yellow = 3
2024-05-23 01:27:51 +08:00
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 # 单次触发按键状态
2024-05-23 16:07:23 +08:00
self.long_power_status = False
2024-06-18 22:04:58 +08:00
# self.short_power_status = False
2024-05-23 16:07:23 +08:00
2024-05-23 01:27:51 +08:00
self.button_init()
self.init_hd_thread()
def button_init(self):
wiringpi.wiringPiSetup()
# GPIO 输出模式
2024-05-23 16:07:23 +08:00
wiringpi.pinMode(self.LED_PIN_red, GPIO.OUTPUT)
wiringpi.pinMode(self.LED_PIN_blue, GPIO.OUTPUT)
2024-06-09 02:37:17 +08:00
wiringpi.pinMode(self.LED_PIN_green, GPIO.OUTPUT)
wiringpi.pinMode(self.LED_PIN_yellow, GPIO.OUTPUT)
2024-05-23 16:07:23 +08:00
wiringpi.pinMode(self.LED_PIN_white, GPIO.OUTPUT)
2024-05-23 01:27:51 +08:00
# GPIO 输入模式
2024-05-23 16:07:23 +08:00
wiringpi.pinMode(self.BUTTON_PIN_1, GPIO.INPUT)
wiringpi.pinMode(self.BUTTON_PIN_2, GPIO.INPUT)
2024-05-23 01:27:51 +08:00
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):
2024-05-23 16:07:23 +08:00
last_status_1 = False
press_time_1 = None
2024-07-15 14:53:50 +08:00
if self.enable_start_light:
self.start_status_light()
2024-05-23 01:27:51 +08:00
while True:
2024-05-30 01:57:15 +08:00
button_status_1 = True if wiringpi.digitalRead(self.BUTTON_PIN_1) == 0 else False
2024-05-30 02:03:22 +08:00
# print(f"{datetime.now()}: BUTTON_PIN_1: {wiringpi.digitalRead(self.BUTTON_PIN_1)}")
2024-05-23 16:07:23 +08:00
if not button_status_1 and last_status_1:
2024-05-24 22:58:16 +08:00
press_time_1 = datetime.now()
2024-05-23 16:07:23 +08:00
elif button_status_1 and not last_status_1:
if press_time_1:
2024-05-24 22:58:16 +08:00
press_duration = (datetime.now() - press_time_1).total_seconds()
2024-06-18 22:04:58 +08:00
# if press_duration > 0.01:
2024-06-18 23:43:25 +08:00
if press_duration > 3:
self.set_led_on('red')
2024-06-09 02:37:17 +08:00
subprocess.Popen('sudo shutdown now', shell=True)
2024-05-23 16:07:23 +08:00
else:
2024-06-18 22:04:58 +08:00
self.long_power_status = not self.long_power_status
# self.short_power_status = True
2024-06-18 21:37:14 +08:00
print(f"{datetime.now()}: press_duration: {press_duration}")
2024-05-30 01:54:27 +08:00
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}")
2024-05-23 16:07:23 +08:00
press_time_1 = None
2024-05-30 02:10:28 +08:00
# TODO: 如果button_status_1和上一帧都是True or False则判断为无效按键忽略
2024-05-23 16:07:23 +08:00
last_status_1 = button_status_1
# 更新LED状态
if self.long_power_status:
2024-06-09 02:37:17 +08:00
self.set_led_on('green')
2024-06-18 22:04:58 +08:00
# if self.short_power_status:
# self.set_led_on('blue')
# else:
# self.set_led_off('blue')
2024-05-23 01:27:51 +08:00
else:
2024-05-23 16:07:23 +08:00
self.short_power_status = False
2024-06-09 02:37:17 +08:00
self.set_led_off('green')
2024-06-18 00:00:27 +08:00
self.set_led_off('blue')
self.set_led_off('red')
2024-05-23 01:27:51 +08:00
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)
2024-05-27 23:47:05 +08:00
def start_status_light(self):
for i in range(2):
2024-06-09 02:37:17 +08:00
self.set_led_on("green")
time.sleep(0.5)
self.set_led_off("green")
time.sleep(0.5)
2024-05-23 01:27:51 +08:00
if __name__ == '__main__':
orangepi = OrangePi()
while True:
pass