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-05-23 01:27:51 +08:00
|
|
|
|
try:
|
|
|
|
|
import wiringpi
|
|
|
|
|
from wiringpi import GPIO
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
| GPIO | LED |
|
|
|
|
|
| -- | - - |
|
|
|
|
|
| 0 | 红色 |
|
|
|
|
|
| 1 | 黄色 |
|
|
|
|
|
| 2 | 绿色 |
|
|
|
|
|
| 3 | 蓝色 |
|
|
|
|
|
| 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):
|
|
|
|
|
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_yellow = 1
|
|
|
|
|
self.LED_PIN_green = 2
|
|
|
|
|
self.LED_PIN_blue = 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 # 单次触发按键状态
|
2024-05-23 16:07:23 +08:00
|
|
|
|
self.long_power_status = False
|
|
|
|
|
self.short_power_status = False
|
|
|
|
|
|
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_yellow, GPIO.OUTPUT)
|
|
|
|
|
wiringpi.pinMode(self.LED_PIN_green, GPIO.OUTPUT)
|
|
|
|
|
wiringpi.pinMode(self.LED_PIN_blue, GPIO.OUTPUT)
|
|
|
|
|
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-05-27 23:48:11 +08:00
|
|
|
|
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()
|
|
|
|
|
print(f"{datetime.now()}: press_duration: {press_duration}")
|
2024-05-23 16:07:23 +08:00
|
|
|
|
if press_duration > 1:
|
2024-05-24 23:06:32 +08:00
|
|
|
|
self.long_power_status = not self.long_power_status
|
2024-05-30 01:43:06 +08:00
|
|
|
|
# print(f"{datetime.now()}: long_power_status: {self.long_power_status} {self.short_power_status}")
|
2024-05-23 16:07:23 +08:00
|
|
|
|
else:
|
2024-05-30 01:43:06 +08:00
|
|
|
|
self.short_power_status = True
|
|
|
|
|
# print(f"{datetime.now()}: short_power_status: {self.short_power_status} {self.long_power_status}")
|
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:
|
|
|
|
|
self.set_led_on('white')
|
2024-05-30 01:43:06 +08:00
|
|
|
|
if self.short_power_status:
|
|
|
|
|
self.set_led_on('yellow')
|
|
|
|
|
else:
|
|
|
|
|
self.set_led_off('yellow')
|
2024-05-23 01:27:51 +08:00
|
|
|
|
else:
|
2024-05-23 16:07:23 +08:00
|
|
|
|
self.short_power_status = False
|
|
|
|
|
self.set_led_off('white')
|
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-05-27 23:52:37 +08:00
|
|
|
|
self.set_led_on("white")
|
2024-05-30 01:43:06 +08:00
|
|
|
|
time.sleep(1)
|
2024-05-27 23:52:37 +08:00
|
|
|
|
self.set_led_off("white")
|
2024-05-30 01:43:06 +08:00
|
|
|
|
time.sleep(1)
|
2024-05-23 01:27:51 +08:00
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
orangepi = OrangePi()
|
|
|
|
|
while True:
|
|
|
|
|
pass
|