Skip to content

Commit

Permalink
0.8.2:
Browse files Browse the repository at this point in the history
- Using nonblocking sockets
- Added option to send frames to a broadcast address (closes #9)
  • Loading branch information
spacemanspiff2007 committed Mar 14, 2021
1 parent 55603e2 commit 6a4ef9d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pyartnet/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.8.1'
__version__ = '0.8.2'
47 changes: 28 additions & 19 deletions pyartnet/artnet_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import struct
import time
import typing
from traceback import format_exc

from .dmx_universe import DmxUniverse

Expand All @@ -14,13 +15,14 @@

class ArtNetNode:
def __init__(self, host: str, port: int = 0x1936, max_fps: int = 25,
refresh_every: int = 2, sequence_counter: bool = True):
refresh_every: int = 2, sequence_counter: bool = True, broadcast: bool = False):
"""
:param host: IP of the Art-Net Node
:param port: Port of the Art-Net Node
:param max_fps: How many packets per sec shall max be send
:param refresh_every: Resend the data every x seconds, 0 to deactivate
:param sequence_counter: activate the sequence counter in the packages
:param broadcast: activate if you want to send the frames to a broadcast address
"""
self.__host = host
self.__port = port
Expand All @@ -30,6 +32,9 @@ def __init__(self, host: str, port: int = 0x1936, max_fps: int = 25,
self.__universe = {} # type: typing.Dict[int, DmxUniverse]

self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
self._socket.setblocking(False)
if broadcast:
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

packet = bytearray()
packet.extend(map(ord, "Art-Net"))
Expand Down Expand Up @@ -67,20 +72,25 @@ async def __worker(self):
while True:
await asyncio.sleep(self.sleep_time)

fades_running = False
for u in self.__universe.values():
if u.process():
fades_running = True

if fades_running:
self.update()
last_update = time.time()
else:
if self.refresh_every > 0:
# refresh data all 2 secs
if time.time() - last_update > self.refresh_every:
self.update()
last_update = time.time()
try:
fades_running = False
for u in self.__universe.values():
if u.process():
fades_running = True

if fades_running:
self.update()
last_update = time.time()
else:
if self.refresh_every > 0:
# refresh data all 2 secs
if time.time() - last_update > self.refresh_every:
self.update()
last_update = time.time()
except Exception:
log.error(f'Error in worker for {self.__host}:')
for line in format_exc().splitlines():
log.error(line)

async def start(self):
if self.__task:
Expand All @@ -101,7 +111,7 @@ async def stop(self):
return None

def update(self):
"Send an update to the artnet device. This normally happens automatically"
"""Send an update to the artnet device. This normally happens automatically"""

for universe_nr, universe in self.__universe.items():
assert isinstance(universe_nr, int), type(universe_nr)
Expand Down Expand Up @@ -133,8 +143,8 @@ def update(self):

return None

def __log_artnet_frame(self, p):
"Log Artnet Frame"
def __log_artnet_frame(self, p: bytearray):
"""Log Artnet Frame"""
assert isinstance(p, bytearray)

# runs the first time
Expand Down Expand Up @@ -195,7 +205,6 @@ def __log_artnet_frame(self, p):
if show_description:
out_desc += ' '.join(_block_desc)


if show_description:
log.debug(out_desc)
log.debug(out)
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ channel.add_fade([0xFFFF,0,0], 5000)


# Changelog
#### 0.8.2 (14.03.2021)
- Using nonblocking sockets
- Added option to send frames to a broadcast address

#### 0.8.1 (26.02.2021)
- Fixed an issue with the max value for channels with 16bits and more
Expand Down
10 changes: 10 additions & 0 deletions tests/test_fades.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pyartnet


def test_single_step():
fade = pyartnet.fades.LinearFade(255)
fade.factor = 255
assert not fade.is_done()

fade.calc_next_value()
assert fade.is_done()

0 comments on commit 6a4ef9d

Please sign in to comment.