added threading

master
Ebbe Baß 2022-03-26 20:39:25 +01:00
parent 376edc47be
commit acff311f15
1 changed files with 14 additions and 4 deletions

18
main.py
View File

@ -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)