新交互逻辑,长按/短按

This commit is contained in:
IrvingGao committed 2024-05-23 16:07:23 +08:00
1 parent 8255cc8c68
commit 346559fb90
7 files changed
+280 -117

No files matched your search

+6 -4
View File
@@ -12,7 +12,7 @@ class Keyboard(BaseHardware):
self.keyboard_key = keyboard_key
self.init_hd_thread()
self.power_status = False # 单次触发按键状态
self.long_power_status = False # 单次触发按键状态
def hd_detection_loop(self):
keyboard_status = False
@@ -27,10 +27,12 @@ class Keyboard(BaseHardware):
time.sleep(0.001)
if not self.shared_hd_status and last_status:
self.power_status = ~self.power_status
if self.power_status:
self.long_power_status = ~self.long_power_status
if self.long_power_status:
print("Chating mode.")
else:
print("Slience mode.")
print(f"pres time: {datetime.datetime.now()}")
last_status = self.shared_hd_status
last_status = self.shared_hd_status
+41 -27
View File
@@ -7,7 +7,6 @@ try:
except:
pass
'''
| GPIO | LED |
| -- | - - |
@@ -17,7 +16,7 @@ except:
| 3 | 蓝色 |
| 4 | 白色 |
| GPIO | BUTTOM |
| GPIO | BUTTON |
| -- | ---- |
| 6 | 按键1 |
| 8 | 按键2 |
@@ -40,47 +39,62 @@ class OrangePi(BaseHardware):
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)
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)
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
last_status_1 = False
press_time_1 = None
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)
button_status_1 = wiringpi.digitalRead(self.BUTTON_PIN_1)
if not button_status_1 and last_status_1:
press_time_1 = datetime.datetime.now()
elif button_status_1 and not last_status_1:
if press_time_1:
press_duration = (datetime.datetime.now() - press_time_1).total_seconds()
print(f"press_duration: {press_duration}")
if press_duration > 1:
self.long_power_status = ~self.long_power_status
print(f"long_power_status: {self.long_power_status}")
else:
self.short_power_status = ~self.short_power_status
print(f"short_power_status: {self.short_power_status}")
press_time_1 = None
last_status_1 = button_status_1
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
# 更新LED状态
if self.long_power_status:
self.set_led_on('white')
else:
self.short_power_status = False
self.set_led_off('white')
if self.short_power_status:
self.set_led_on('yellow')
else:
self.set_led_off('yellow')
def set_led_on(self, color='red'):
wiringpi.digitalWrite(getattr(self, f'LED_PIN_{color}'), GPIO.HIGH)