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