From acff311f158eaec822ba218f6625a2fccf7b7b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Sat, 26 Mar 2022 20:39:25 +0100 Subject: [PATCH] added threading --- main.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index aa04fab..3a977d4 100644 --- a/main.py +++ b/main.py @@ -6,12 +6,14 @@ try: from rpi_ws281x import * import RPi.GPIO as GPIO import socket + import threading except ImportError: print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"') exit() led_power = True idle_mode = 1 +global power_button_pressed power_button_pressed = False # LED strip configuration: @@ -24,7 +26,9 @@ LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53 +global SOCKET_BIND_IP SOCKET_BIND_IP = '0.0.0.0' +global SOCKET_BIND_PORT SOCKET_BIND_PORT = 5760 def debug_print(message: str): @@ -91,6 +95,14 @@ def power_toggle(channel): led_power = True debug_print('LED Power ON') +def socket_listener(): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((SOCKET_BIND_IP, SOCKET_BIND_PORT)) + s.listen(1) + global conn + global addr + conn, addr = s.accept() + if __name__ == '__main__': strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL) strip.begin() @@ -100,10 +112,8 @@ if __name__ == '__main__': GPIO.add_event_detect(13, GPIO.RISING, callback=power_toggle, bouncetime=300) GPIO.add_event_detect(19, GPIO.RISING, callback=change_idle_mode, bouncetime=300) - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - s.bind((SOCKET_BIND_IP, SOCKET_BIND_PORT)) - s.listen(1) - conn, addr = s.accept() + threading.Thread(target=socket_listener).start() + try: while True: socket_data = conn.recv(1024)