123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import RPi.GPIO as GPIO
- import time
- from lib.mqtt import create_client, control_player, create_config, check_current_status, check_current_disc, update_disc
- client = create_client()
- create_config(client)
- BUTTON_PINS = [17, 18, 27]
- GPIO.setmode(GPIO.BCM)
- for pin in BUTTON_PINS:
- GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- def button_callback_1(channel):
- client = create_client()
- if check_current_status(client) == "PLAY":
- print("Button 1 pressed! Pausing playback.")
- control_player(client, "PAUSE")
- else:
- print("Button 1 pressed! Playing.")
- control_player(client, "PLAY")
- def button_callback_2(channel):
- print("Button 2 pressed!")
- client = create_client()
- control_player(client, "SKIP")
- control_player(client, "PLAY")
- def button_callback_3(channel):
- start_time = time.time()
- client = create_client()
-
- while GPIO.input(BUTTON_PINS[2]) == GPIO.LOW:
- if time.time() - start_time >= 3:
- print("Button 3 held for 3 seconds! Sending RADIO2.")
- disc_data = check_current_disc(client)
- update_disc(client, ["ytube",disc_data['name'],disc_data['type'],"RESET"])
- update_disc(client, ["ytube",disc_data['name'],disc_data['type'],disc_data['id']])
- return
-
-
- print("Button 3 pressed briefly! Sending RADIO.")
- control_player(client, "RADIO")
- GPIO.add_event_detect(BUTTON_PINS[0], GPIO.FALLING, callback=button_callback_1, bouncetime=200)
- GPIO.add_event_detect(BUTTON_PINS[1], GPIO.FALLING, callback=button_callback_2, bouncetime=200)
- GPIO.add_event_detect(BUTTON_PINS[2], GPIO.FALLING, callback=button_callback_3, bouncetime=200)
- try:
-
- while True:
- time.sleep(1)
- except KeyboardInterrupt:
- pass
- finally:
- GPIO.cleanup()
|