restructure code base

This commit is contained in:
Lucca Ketterer 2020-06-01 00:12:32 +02:00
parent 41ea1007dd
commit eae0d18bd1
10 changed files with 153 additions and 0 deletions

4
README Normal file
View File

@ -0,0 +1,4 @@
SPI has to be enabled:
sudo raspi-config -> Interface -> SPI

21
client/client.py Normal file
View File

@ -0,0 +1,21 @@
import socket
def client_program():
host = "192.168.188.61"
port = 5000
client_socket = socket.socket() # instantiate
client_socket.connect((host, port)) # connect to the server
message = input(" -> ") # take input
while message.lower().strip() != 'bye':
client_socket.send(message.encode()) # send message
message = input(" -> ") # again take input
client_socket.close() # close the connection
if __name__ == '__main__':
client_program()

2
install_on_pi.sh Executable file
View File

@ -0,0 +1,2 @@
scp server/* pi@192.168.188.61:~/led_controll/
#ws2801.py --daemon

5
requirements Normal file
View File

@ -0,0 +1,5 @@
RPi.GPIO
Adafruit-WS2801
Adafruit-GPIO

30
server/color_mode.py Normal file
View File

@ -0,0 +1,30 @@
def color_cycle(speed):
if int(speed) == 0:
delay = 0
else:
delay = 1/int(speed) * 10
r = 255
g = 0
b = 0
while True:
while r > 0:
r = r-1
g = g+1
set_color(r,g,b)
time.sleep(delay)
while g > 0:
g = g-1
b = b+1
set_color(r,g,b)
time.sleep(delay)
while b > 0:
b = b-1
r = r+1
set_color(r,g,b)
time.sleep(delay)
def visualizer():
for volume in sys.stdin:
volume = int(volume)
set_color(volume,0,0)

45
server/led_controll.py Executable file
View File

@ -0,0 +1,45 @@
#!/usr/bin/env python3
import sys
import getopt
import time
import "ws2801"
import "server"
def helpmenu():
print("Options:")
print("-h show help")
print("-s <color> color in hex")
print("-c <speed> colorcycle")
def main(argv):
if not sys.stdin.isatty():
visualizer()
sys.exit()
try:
opts, args = getopt.getopt(argv,"hds:c:r")
except getopt.GetoptError:
print("ws2801.py: invalid option")
print("Try 'ws2801.py -h' for help")
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
helpmenu()
sys.exit()
elif opt == "-s":
r,g,b = hex_to_rgb(arg)
set_color(r,g,b)
sys.exit()
elif opt == "-c":
color_cycle(arg)
elif opt == "-d":
start_server()
helpmenu()
sys.exit()
if __name__ == "__main__":
main(sys.argv[1:])

21
server/server.py Normal file
View File

@ -0,0 +1,21 @@
import socket
def start_server():
host = socket.gethostname()
port = 5000
server_socket = socket.socket()
server_socket.bind((host, port))
# configure how many client the server can listen simultaneously
server_socket.listen(1)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
while True:
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(1024).decode()
print(str(data))
conn.close() # close the connection

25
server/ws2801.py Normal file
View File

@ -0,0 +1,25 @@
import RPi.GPIO as GPIO
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI
PIXEL_COUNT = 32
SPI_PORT = 0
SPI_DEVICE = 0
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO)
def hex_to_rgb(hex):
r = int(hex[0:2],16)
g = int(hex[2:4],16)
b = int(hex[4:6],16)
print(r,g,b)
return r,g,b
def set_color(r,g,b):
for i in range(pixels.count()):
pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(r,g,b))
pixels.show()
def clear_pixels():
pixels.clear()
pixels.show()