pyenttec
pyenttec copied to clipboard
Use a static packet buffer in DMXConnection
Hello, and thanks for this very useful library!
This pull request should be completely backward compatible.
If you construct DMXConnection
with the new use_numpy
parameter set to True
, then numpy.array
is used to construct the buffers instead of array.array
.
There are many advantages in general to numpy.array
and two ones specific to this project, using the fact that taking a slice of a numpy.array
creates a reference, not a copy, so modifying the slice modifies the original.
In the existing array
code, a new buffer is created on each DMX frame update and then discarded.
In the new numpy
codepath, a fixed np.array
buffer called _packet
is constructed with _packet_begin
and PACKET_END
already written into it, and then dmx_frame
is just a slice out of that.
So when you manipulate dmx_frame
, you are directly writing into the final packet which is sent to the controller. No copies or allocations are performed.
The second advantage is that you can now construct "lighting instruments" by taking out little slices from DMXConnection.dmx_frame
, which is what I'm doing in my code!
Both of these take advantage of the fact that taking a slice of a numpy.array
creates a reference, not a copy.
I tested all four cases, with and without numpy installed, and with and withoutuse_numpy=True
.
I both ran the nose
tests, and lighted a lamp on my table. :-D