-
Notifications
You must be signed in to change notification settings - Fork 0
/
led-server.py
executable file
·149 lines (126 loc) · 4.23 KB
/
led-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/pypy
#------------------------------------------------------------------------------
# Sample LED SERVER
# led-server.py - V2.3
#
# DO NOT USE Python to run this script... use pypy !
# Your CPU will thank you.
#------------------------------------------------------------------------------
import time
import random
import socket
import binascii
import threading
import sys
sys.stdout = sys.stderr
# Utilities -------------------------------------------------------------------
def setPattern( id, brightness ):
ptrn = _patterns[id]
for i in xrange(len(ptrn)):
if ptrn[i] != 0:
ptrn[i] = int( round(ptrn[i] * brightness / 100.0) )
return ptrn
# APA102 utilities ----------------------------------------
def createApa102( start, header, pattern, nbLeds ):
lgPtn = len(pattern)
lgLeds = nbLeds * 3
j = start
'''
# Fake test frames
if random.random() > 0.9:
if random.random() > 0.5:
data = [33, 24, 87, 55, 83, 27, 33, 22, 44] + [0] * lgLeds
print "fake packet(+2) :"
else:
data = [33, 24, 87] + [0] * lgLeds
print "fake packet(-4) :"
else:
data = header + [0] * lgLeds
'''
data = header + [0] * lgLeds
for i in xrange(len(header), len(data)):
if j >= lgPtn:
j = 0
data[i] = pattern[j]
j += 1
return "".join( chr( byte ) for byte in data )
# TCP Client ----------------------------------------------
class TcpClient( threading.Thread ):
def __init__( self, cnx, params ):
threading.Thread.__init__(self)
self.cnx = cnx
self.params = params
self.size = len(params["header"]) + (params["nbLeds"] * 3)
def run(self):
params = self.params
start = 0
self.cnx.send( str(params["kHz"]) + "," + str(self.size) + "," + str(self.params["brightness"]) )
try:
while True:
data = createApa102( start, params["header"], params["pattern"], params["nbLeds"] )
start += params["move"]
if start >= params["lg"]:
start -= params["lg"]
elif start < 0:
start += params["lg"]
self.cnx.send( data )
#print "packet(" + str(len(data)) + ")=[" + binascii.hexlify(data) + "] in " + str(params["delay"])
#print "packet(" + str(len(data)) + ") in " + str(params["delay"])
time.sleep( params["delay"] )
'''
# Random network delays
delay = 0.05 + (random.random() * 0.2)
print "packet(" + str(len(data)) + ") in " + str(delay)
time.sleep( delay )
'''
except socket.error as msg:
print 'Connection failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
self.cnx.close()
# MAIN ------------------------------------------------------------------------
if __name__ == '__main__':
_patterns = { # pattern length must be a multiple of 3 (RGB)
"red" : [255,0,0],
"green" : [0,255,0],
"blue" : [0,0,255],
"france": [0,0,255, 255,255,255, 255,0,0],
"orange": [255,128,0],
"black": [0,0,0],
"purple": [0,128,255]
}
# Parameters ----------------------
nbLeds = 2000
kHz = 4000
patternID = "france"
brightness = 50 # 0% <-> 100%
move = 2 # -m < 0 > m, led movement per frame refresh
IPport = 4200
fps = 30 # nb frames per second
# Initialisations -----------------
_params = {
"header" : [65, 80, 65, 49, 48, 50, 95],
"nbLeds" : nbLeds,
"kHz" : kHz,
"brightness": brightness,
"pattern" : setPattern(patternID, brightness),
"move" : (move * 3),
"delay" : (1.0 / fps)
}
_params["lg"] = int( round(len(_params["pattern"])) )
_sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
_sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
_sock.bind( ('', IPport) )
_sock.listen( 5 )
print 'starting up on ' + socket.gethostname() + ' port ' + str(IPport)
# Main loop -----------------------
while True:
print 'waiting for a connection'
connection, client_address = _sock.accept()
print 'connection from %s:%s' % client_address
id = connection.recv( 30 )
print 'client id = <' + str(id) + \
'> at ' + str(kHz) + \
' kHz, brightness = ' + str(_params["brightness"]) + ' %'
client = TcpClient( connection, _params )
client.start()
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------