This commit is contained in:
IrvingGao committed 2024-05-23 01:27:51 +08:00
commit 8255cc8c68
36 files changed
+4800

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
from .base_hd import BaseHardware
from .keyboard import Keyboard
from .sipeed import V329
from .orangepi import OrangePi
+32
View File
@@ -0,0 +1,32 @@
import threading
import time
class BaseHardware:
def __init__(self, hd_trigger=None, hd_detect_threshold=50):
self.hd_trigger = hd_trigger
self.hd_detect_threshold = hd_detect_threshold
self.hd_lock = threading.Lock()
self.shared_hd_status = False
def init_hd_thread(self):
hd_thread = threading.Thread(target=self.hd_detection_loop)
hd_thread.start()
# hd_thread.join()
print("HD detection thread started.")
def hd_detection_loop(self):
pass
@property
def is_hardware_pressed(self):
return self.shared_hd_status
def wait_for_hardware_pressed(self):
print("Waiting for hardware trigger.")
while True:
if self.is_hardware_pressed:
time.sleep(0.01)
break
return True
+36
View File
@@ -0,0 +1,36 @@
import keyboard
import time
from takway.board.base_hd import BaseHardware
import datetime
#
class Keyboard(BaseHardware):
def __init__(self, hd_trigger='keyboard', keyboard_key='space', hd_detect_threshold=50):
super().__init__(hd_trigger, hd_detect_threshold)
self.keyboard_key = keyboard_key
self.init_hd_thread()
self.power_status = False # 单次触发按键状态
def hd_detection_loop(self):
keyboard_status = False
last_status = False
while True:
'''
keyboard_status = keyboard.is_pressed(self.keyboard_key)
with self.hd_lock:
self.shared_hd_status = keyboard_status
'''
self.shared_hd_status = keyboard.is_pressed(self.keyboard_key)
time.sleep(0.001)
if not self.shared_hd_status and last_status:
self.power_status = ~self.power_status
if self.power_status:
print("Chating mode.")
else:
print("Slience mode.")
print(f"pres time: {datetime.datetime.now()}")
last_status = self.shared_hd_status
+94
View File
@@ -0,0 +1,94 @@
from takway.board.base_hd import BaseHardware
import threading
import datetime
try:
import wiringpi
from wiringpi import GPIO
except:
pass
'''
| GPIO | LED |
| -- | - - |
| 0 | 红色 |
| 1 | 黄色 |
| 2 | 绿色 |
| 3 | 蓝色 |
| 4 | 白色 |
| GPIO | BUTTOM |
| -- | ---- |
| 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.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),
# threading.Thread(target=self.hd_detection_loop_2)]
hd_threads = [threading.Thread(target=self.hd_detection_loop)]
for hd_thread in hd_threads:
hd_thread.start()
def hd_detection_loop(self):
keyboard_status = False
last_status = False
while True:
self.button_status = wiringpi.digitalRead(self.BUTTON_PIN_1)
if self.button_status:
wiringpi.digitalWrite(self.LED_PIN_red, GPIO.LOW)
else:
wiringpi.digitalWrite(self.LED_PIN_red,GPIO.HIGH)
if not self.button_status and last_status:
self.power_status = ~self.power_status
if self.power_status:
print("Chating mode.")
else:
print("Slience mode.")
print(f"pres time: {datetime.datetime.now()}")
last_status = self.button_status
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)
if __name__ == '__main__':
orangepi = OrangePi()
while True:
pass
+58
View File
@@ -0,0 +1,58 @@
import sys
import warnings
import threading
import time
from collections import deque
from takway.board.base_hd import BaseHardware
if "gpiod" in sys.modules:
# sipeed MaixSense V329
import gpiod as gpio
else:
# 如果所有库都不存在,执行默认操作或抛出异常
# raise ImportError("gpiod package is not available.")
pass
class V329(BaseHardware):
def __init__(self, hd_trigger='button', hd_detect_threshold=50):
super().__init__(hd_trigger, hd_detect_threshold)
self.button = self.button_init()
self.init_hd_thread()
def button_init(self):
PH_BASE = (8-1)*32 #PH
gpiochip1 = gpio.chip("gpiochip1")
button = gpiochip1.get_line((PH_BASE+5))
config = gpio.line_request()
config.request_type = gpio.line_request.DIRECTION_INPUT
config.flags = gpio.line_request.FLAG_BIAS_PULL_UP
button.request(config)
return button
@property
def button_status(self):
return True if self.button.get_value() == 1 else False
def hd_detection_loop(self):
self.shared_hd_status = False
button_value_list = deque(maxlen=self.hd_detect_threshold)
while True:
if len(button_value_list) > button_value_list.maxlen:
button_value_list.popleft()
button_value_list.append(self.button_status)
# 记录50个值,如果连续50个值都是True,则认为按钮被按下
if button_value_list.count(True) == button_value_list.maxlen:
with self.hd_lock:
self.shared_hd_status = True
# 记录50个值,如果连续50个值都是False,则认为按钮被松开
if button_value_list.count(False) == button_value_list.maxlen:
with self.hd_lock:
self.shared_hd_status = False