Jelajahi Sumber

some ipy experiments

dirkse 8 tahun lalu
induk
melakukan
4feb8f1519
4 mengubah file dengan 49 tambahan dan 9 penghapusan
  1. 3 2
      matrix/Dockerfile
  2. 28 0
      matrix/lib.py
  3. 13 3
      matrix/pixelreceiver.py
  4. 5 4
      matrix/sender.py

+ 3 - 2
matrix/Dockerfile

@@ -35,7 +35,7 @@ RUN sudo dpkg-reconfigure -f noninteractive tzdata
 RUN pip install --upgrade pip
 RUN pip install readline notebook==5.2.0 ipywidgets path.py requests cython
 RUN jupyter nbextension enable --py --sys-prefix widgetsnbextension
-
+RUN apt-get install -y python-numpy python-scipy
 # Configure jupyter
 # we run this manually after mounting /root/.jupyter
 # RUN jupyter notebook --generate-config
@@ -76,4 +76,5 @@ RUN make -C examples-api-use
 RUN cd bindings/python && make build-python install-python
 
 COPY *.py /
-CMD python -u /pixelreceiver.py > /pixel/receive.log & jupyter notebook --allow-root
+# CMD python -u /pixelreceiver.py > /pixel/receive.log & jupyter notebook --allow-root
+CMD jupyter notebook --allow-root

+ 28 - 0
matrix/lib.py

@@ -0,0 +1,28 @@
+from __future__ import print_function, absolute_import, division
+
+from rgbmatrix import RGBMatrix, RGBMatrixOptions
+from rgbmatrix import graphics
+
+def setup_matrix(**kwargs):
+    o = RGBMatrixOptions()
+    o.chain_length = 4
+    for k, v in kwargs.iteritems():
+        setattr(o, k, v)
+
+    matrix = RGBMatrix(options=o)
+    matrix.Clear()
+    return matrix
+
+def scipy_demo():
+    from PIL import Image
+    import numpy as np
+    from scipy.ndimage.interpolation import rotate
+    from scipy.ndimage.filters import gaussian_filter
+    draw = lambda pixels: matrix.SetImage(Image.fromarray(pixels.swapaxes(0, 1), 'RGB'))
+
+    pixels = np.zeros(DIM, dtype=np.uint8)
+    pixels[:, :, :] = (np.random.random(DIM) > 0.95) * 255
+
+    for i in range(361):
+        smooth = (180 - abs(i - 180)) / 80
+        draw(gaussian_filter(rotate(pixels, i, order=0), [smooth, smooth, 0]))

+ 13 - 3
matrix/pixelreceiver.py

@@ -6,6 +6,7 @@ import time
 
 import socket
 import traceback
+from collections import defaultdict
 
 from path import Path
 
@@ -40,6 +41,8 @@ class Renderer(object):
         self._color = textColor
         self.setup()
 
+        self.progress_tracker = defaultdict(lambda: -1)
+
         self._brightness = brightness
         self.set_brightness(brightness)
 
@@ -63,7 +66,11 @@ class Renderer(object):
         self.canvas = self.matrix.SwapOnVSync(self.canvas)
         self.canvas.Clear()
 
-    def progress(self, step=0, total=0):
+    def progress(self, step=0, total=0, id=None):
+        last_step = self.progress_tracker[id]
+        if step < last_step:
+            return
+
         c = self.canvas
         w, h = c.width, c.height
         col = Color('#AACCFF')
@@ -74,9 +81,12 @@ class Renderer(object):
 
             stepsize = max(1, int(w / total))
             for x in range(w):
-                modulate = asc_desc_modulation(x, stepsize)
+                modulate_x = asc_desc_modulation(x, 32)
+
                 for y in range(h):
-                    c.SetPixel(s_len + x, y, *modulate(col))
+                    #~ modulate_y = asc_desc_modulation(y, 16)
+                    modulate_y = lambda x: x
+                    c.SetPixel(s_len + x, y, *modulate_y(modulate_x(col)))
                 if  x > ((step + 1) / total) * w:
                     break
         self.swap()

+ 5 - 4
matrix/sender.py

@@ -64,9 +64,9 @@ def scroll_text(text, loop=1):
 def fit_text(text, width=0, fade=0):
     return text.encode('utf-8'), width, fade
 
-@matrix_api(10, 'hh', 'step total')
-def progress(text, step=0, total=0):
-    return step, total
+@matrix_api(10, '32shh', 'step total')
+def progress(text, id, step=0, total=0):
+    return id.encode('utf-8'), step, total
 
 @matrix_api(255, '', '')
 def destroy():
@@ -80,4 +80,5 @@ if __name__ == '__main__':
 
 
     sender = Sender('blaubeere.fritz.box', 10000)
-    sender.print_text(u'Christine ♥')
+    #~ sender.print_text(u'Christine ♥')
+    sender.destroy()