Browse Source

added matrix udp server

dirkse 8 năm trước cách đây
mục cha
commit
b213c7fe7f
6 tập tin đã thay đổi với 113 bổ sung13 xóa
  1. 1 1
      alexa/app/domo.py
  2. 1 1
      hosts.yml
  3. 1 7
      jupyter/Dockerfile
  4. 2 2
      matrix/Dockerfile
  5. 102 0
      matrix/pixelreceiver.py
  6. 6 2
      services.yml

+ 1 - 1
alexa/app/domo.py

@@ -65,7 +65,7 @@ def toggle_switch(OnOff):
 
     log.info('alexa: room %s switch: %s' % (room, OnOff))
 
-    assert OnOff in ON_OFF_SLOT
+    assert OnOff in ON_OFF_SLOT, OnOff
     switchcmd = ON_OFF_SLOT[OnOff]
 
     domo_idx = ROOM2POWER_DOMO_IDX[room]

+ 1 - 1
hosts.yml

@@ -23,7 +23,7 @@ all:
       #~ docker_engine_port: 10243
       ansible_ssh_host: 192.168.178.43
     blaubeere:
-      ansible_ssh_host: 192.168.178.49
+      ansible_ssh_host: 192.168.178.24
     erdbeere:
       ansible_ssh_host: 192.168.178.29
 

+ 1 - 7
jupyter/Dockerfile

@@ -1,10 +1,4 @@
-# This file creates a container that runs a jupyter notebook server on Raspberry Pi
-#
-# Author: Max Jiang
-# Date 13/02/2017
-
 FROM resin/rpi-raspbian:jessie-20170531
-MAINTAINER Max Jiang <maxjiang@hotmail.com>
 
 # Set the variables
 ENV DEBIAN_FRONTEND noninteractive
@@ -42,7 +36,7 @@ RUN tar zxvf "Python-${PHTHON_VERSION}.tgz" \
 # Update pip and install jupyter
 RUN apt-get install -y libncurses5-dev
 RUN pip3 install --upgrade pip
-RUN pip3 install readline notebook==5.1.0
+RUN pip3 install readline notebook==5.2.0 pillow
 
 # Configure jupyter
 # we run this manually after mounting /root/.jupyter

+ 2 - 2
matrix/Dockerfile

@@ -86,11 +86,11 @@ RUN git clone https://github.com/hzeller/rpi-rgb-led-matrix.git /matrix
 WORKDIR /matrix
 RUN sed -i "s/=regular/=adafruit-hat-pwm/g" lib/Makefile
 
-# RUN  apt-get install -y
 # for C++-demos
 RUN make -C examples-api-use
 
 # for python interface
 RUN cd bindings/python && make build-python install-python
 
-
+COPY pixelreceiver.py /
+CMD python -u /pixelreceiver.py > /pixel/receive.log & jupyter notebook --allow-root

+ 102 - 0
matrix/pixelreceiver.py

@@ -0,0 +1,102 @@
+from __future__ import print_function, absolute_import, division
+
+print('receiver start')
+import sys
+
+import socket
+import traceback
+
+from path import Path
+
+from rgbmatrix import RGBMatrix, RGBMatrixOptions
+from rgbmatrix import graphics
+
+class Renderer(object):
+    def __init__(self):
+        o = RGBMatrixOptions()
+        o.chain_length = 4
+        self.matrix = RGBMatrix(options=o)
+        self.canvas = self.matrix.CreateFrameCanvas()
+
+    def swap(self):
+        self.canvas = self.matrix.SwapOnVSync(self.canvas)
+        self.canvas.Clear()
+
+    def print_text(self, text):
+        graphics.DrawText(self.canvas, fonts['10x20'], 0, 20, textColor, text)
+        self.swap()
+
+    def scroll_text(self, text, loop=1):
+        pos = self.canvas.width
+        i = 1
+        while True:
+            len = graphics.DrawText(self.canvas, fonts['10x20'], pos, 20, textColor, text)
+            pos -= 1
+            if (pos + len < 0):
+                pos = self.canvas.width
+                i += 1
+            if i > loop:
+                break
+
+            time.sleep(0.02)
+            self.swap()
+
+
+    def fit_text(self, text, width=128):
+        ff = reversed(sorted(fonts.items(), key=lambda e: int(e[0].split('x')[0])))
+        temp_canvas = self.matrix.CreateFrameCanvas()
+        for name, font in ff:
+            w = graphics.DrawText(temp_canvas, font, 0, -100, textColor, text)
+            if w < width:
+                break
+        else:
+            print('str len %s exceeds space' % w)
+
+        graphics.DrawText(self.canvas, font, 0, 20, textColor, text)
+        self.swap()
+
+
+try:
+    fonts = {}
+    for f in Path('/matrix/fonts').files('*.bdf'):
+        if not f.namebase.split('x')[0].isdigit() or not f.namebase[-1].isdigit():
+            continue
+        fo = graphics.Font()
+        fo.LoadFont(f)
+        fonts[f.namebase] = fo
+
+    textColor = graphics.Color(96, 128, 255)
+    r = Renderer()
+
+    # Create a TCP/IP socket
+    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+
+    # Bind the socket to the port
+    server_address = ('0.0.0.0', 10000)
+    print('starting up on %s port %s' % server_address)
+    sock.bind(server_address)
+    r.fit_text('Matrix ready.')
+
+    r.fit_text('111')
+
+
+    while True:
+        print('waiting to receive message')
+        data, address = sock.recvfrom(4096)
+
+        print('received %s bytes from %s' % (len(data), address))
+        method = ord(data[0])
+        data = data[1:]
+
+        if method == 0:
+            r.print_text(data.decode('utf-8'))
+        elif method == 1:
+            r.scroll_text(data.decode('utf-8'))
+        elif method == 2:
+            r.fit_text(data.decode('utf-8'))
+
+except Exception:
+    print(traceback.format_exc())
+
+finally:
+    print('receiver exit')

+ 6 - 2
services.yml

@@ -60,7 +60,7 @@ svn:
     ports: 3690:3690
     stop_signal: SIGKILL
 
-ngp:
+ngp: # nginx proxy with lets encrypt
   host: himbeere
   docker:
     - name: nginx-proxy
@@ -176,12 +176,14 @@ matrix: # interface with RGB LED-Matrix
     build: matrix
     # matrix driver needs access to /dev/mem
     privileged: true
-    ports: 17888:8888
+    ports: 17888:8888, 10000:10000/udp, 10001:10001/udp
     volumes:
       - /opt/jupyter/notebooks:/root/notebooks
       - /opt/jupyter/config:/root/.local
       - /opt/jupyter/config_root:/root/.jupyter
       - /opt/jupyter/config_ipy_root:/root/.ipython
+      - /opt/pixel:/pixel
+
   commands:
     # some rotating square
     demo00: examples-api-use/demo -c 4 --led-show-refresh -D0
@@ -207,6 +209,8 @@ matrix: # interface with RGB LED-Matrix
     pydemo2: bindings/python/samples/image-scroller.py -c 4 --led-show-refresh -i examples-api-use/runtext.ppm
     # python sample: rotatatin block
     pydemo3: bindings/python/samples/simple-square.py -c 4 --led-show-refresh
+    # tail logs of pixelreceiver (udp server)
+    pixel_logs: tail -f /pixel/receive.log
 
 vaping:
   host: erdbeere