TakwayBoard/takway/board/orangepi.py

119 lines
4.0 KiB
Python

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_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 # 单次触发按键状态
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_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)
# 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()
print(f"{datetime.now()}: press_duration: {press_duration}")
if press_duration > 1:
self.long_power_status = not self.long_power_status
# print(f"{datetime.now()}: long_power_status: {self.long_power_status} {self.short_power_status}")
else:
self.short_power_status = True
# print(f"{datetime.now()}: short_power_status: {self.short_power_status} {self.long_power_status}")
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
last_status_1 = button_status_1
# 更新LED状态
if self.long_power_status:
self.set_led_on('white')
if self.short_power_status:
self.set_led_on('yellow')
else:
self.set_led_off('yellow')
else:
self.short_power_status = False
self.set_led_off('white')
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("white")
time.sleep(1)
self.set_led_off("white")
time.sleep(1)
if __name__ == '__main__':
orangepi = OrangePi()
while True:
pass