TakwayBoard/takway/board/orangepi.py

116 lines
3.7 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-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()
2024-05-27 23:47:05 +08:00
self.start_status_light()
2024-05-23 01:27:51 +08:00
def hd_detection_loop(self):
2024-05-23 16:07:23 +08:00
last_status_1 = False
press_time_1 = None
2024-05-23 01:27:51 +08:00
while True:
2024-05-23 16:07:23 +08:00
button_status_1 = wiringpi.digitalRead(self.BUTTON_PIN_1)
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-24 22:58:16 +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-24 23:04:09 +08:00
self.short_power_status = not self.short_power_status
2024-05-24 22:58:16 +08:00
print(f"{datetime.now()}: short_power_status: {self.short_power_status} {self.long_power_status}")
2024-05-23 16:07:23 +08:00
press_time_1 = None
last_status_1 = button_status_1
# 更新LED状态
if self.long_power_status:
self.set_led_on('white')
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
2024-05-23 16:07:23 +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
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):
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