Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Fjen Undso
nchl
Commits
a8531cd8
Commit
a8531cd8
authored
Jan 23, 2018
by
Fjen Undso
Browse files
lights: support sending to one or many hosts
parent
419c8ddf
Changes
1
Hide whitespace changes
Inline
Side-by-side
lights.py
View file @
a8531cd8
#!/usr/bin/env python3
from
collections
import
defaultdict
import
socket
import
time
UDP_IP
=
"10.0.0.255"
UDP_PORT
=
1234
LED_NUM
=
42
TIMEOUT
=
1
/
120
SEND_RATE
=
1
/
120
VERSION
=
2
SEQUENCE
=
0
SEQUENCE
S
=
defaultdict
(
int
)
SOCKET
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
SOCKET
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
SOCKET
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_BROADCAST
,
1
)
def
send
(
data
):
"""Send data to the esp listening at UDP_IP:UD_PORT"""
global
SEQUENCE
header
=
bytes
([
VERSION
,
SEQUENCE
,
0
,
0
])
SOCKET
.
sendto
(
header
+
data
,
(
UDP_IP
,
UDP_PORT
))
SEQUENCE
+=
1
if
SEQUENCE
==
255
:
SEQUENCE
=
0
time
.
sleep
(
TIMEOUT
)
def
send_broadcast
(
data
):
"""Send data via broadcast"""
send
(
data
,
UDP_IP
)
def
send
(
data
,
host
,
send_rate
=
SEND_RATE
):
"""Send data to the ESP listening at host:UDP_PORT."""
global
SEQUENCES
header
=
bytes
([
VERSION
,
SEQUENCES
[
host
],
0
,
0
])
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
if
host
.
endswith
(
'255'
):
s
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_BROADCAST
,
1
)
s
.
sendto
(
header
+
data
,
(
host
,
UDP_PORT
))
s
.
close
()
SEQUENCES
[
host
]
+=
1
if
SEQUENCES
[
host
]
>
255
:
SEQUENCES
[
host
]
=
0
time
.
sleep
(
send_rate
)
def
send_to_many
(
data
,
hosts
):
"""Send data to a list of ESPs"""
for
h
in
hosts
:
send
(
data
,
h
,
0
)
time
.
sleep
(
SEND_RATE
)
def
pixel2bytes
(
pixel
):
...
...
@@ -50,7 +66,7 @@ def make_stripe_data(pixellist):
if
__name__
==
'__main__'
:
colors
=
make_stripe_data
([
0
,
255
,
255
,
0
]
*
24
)
SEQUENCE
=
0
SEQUENCE
=
0
send
(
colors
)
SEQUENCE
=
128
SEQUENCE
=
128
send
(
colors
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment