From dd8bcc2da80d15280b9aa94cec648fbdac576cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Tue, 18 Jul 2017 20:11:12 +0200 Subject: Add matrix --- setup.py | 2 +- turtetris_master/__init__.py | 8 ++++++++ turtetris_master/led_output.py | 31 +++++++++++++++++++++++++++++++ turtetris_slave/__init__.py | 31 +++++++++++++++++++++++++++++++ turtetris_slave/__main__.py | 3 +++ turtetris_slave/leds.py | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 turtetris_slave/__init__.py create mode 100644 turtetris_slave/__main__.py create mode 100644 turtetris_slave/leds.py diff --git a/setup.py b/setup.py index 348f7cf..16f73b5 100755 --- a/setup.py +++ b/setup.py @@ -21,5 +21,5 @@ setup( ], keywords='Turris Tetris', - packages=['turtetris_master'] + packages=['turtetris_master', 'turtetris_slave'] ) diff --git a/turtetris_master/__init__.py b/turtetris_master/__init__.py index 9cf22b1..401accb 100644 --- a/turtetris_master/__init__.py +++ b/turtetris_master/__init__.py @@ -1,11 +1,19 @@ +import time +from .led_output import Matrix from .usb_input import Gamepad def main(): "Main function" inpt = Gamepad() + print('Gamepad initialized') + mtrx = Matrix() + print('Matrix initialized') while True: + print('loop!!!!') print(inpt.check()) + mtrx.display() + time.sleep(1) if __name__ == '__main__': diff --git a/turtetris_master/led_output.py b/turtetris_master/led_output.py index e69de29..7af7a35 100644 --- a/turtetris_master/led_output.py +++ b/turtetris_master/led_output.py @@ -0,0 +1,31 @@ +import zmq +import json + + +class Matrix: + "Leds matrix" + + def __init__(self): + "Establish connection to matrix" + self.__mat__ = [[{ + 'color': '000000', + 'intens': 0 + }]*12]*10 + self.context = zmq.Context() + self.socket = self.context.socket(zmq.PUB) + self.socket.bind('tcp://*:4444') + + def display(self): + "Display matrix to leds" + for i in range(0, 10): + self.socket.send_string('line' + str(i) + ' ' + + json.dumps(self.__mat__[i])) + + def pixel(self, x, y, color=None, bright=None): + "Set pixel in matrix" + if x < 0 or x > 11 or y < 0 or y > 9: + return # just ignore any pixel outside of the matrix + if color is not None: + self.__mat__[y][x]['color'] = color + if bright is not None: + self.__mat__[y][x]['bright'] = bright diff --git a/turtetris_slave/__init__.py b/turtetris_slave/__init__.py new file mode 100644 index 0000000..5779dee --- /dev/null +++ b/turtetris_slave/__init__.py @@ -0,0 +1,31 @@ +import json +import zmq +from subprocess import check_output +from . import leds + + +def socket_init(line): + "Initialize socket for given line" + context = zmq.Context() + socket = context.socket(zmq.SUB) + socket.subscribe('line' + str(line)) + # socket.setsockopt_string(zmq.SUBSCRIBE, 'line' + str(line)) + socket.connect('tcp://192.168.2.1:4444') # TODO change to 192.168.1.1 + return socket + + +def main(): + "Main function" + line = int(check_output("uci get turtetris.line", shell=True)) + print("Starting turtetris client and connecting as line " + str(line)) + sck = socket_init(line) + leds.prepare() + while True: + msg = sck.recv_string() + print(msg) + json0 = msg.find(' ') # First empty char is end of envelope + leds.output(json.loads(msg[json0:])) + + +if __name__ == '__main__': + main() diff --git a/turtetris_slave/__main__.py b/turtetris_slave/__main__.py new file mode 100644 index 0000000..8273c4f --- /dev/null +++ b/turtetris_slave/__main__.py @@ -0,0 +1,3 @@ +from . import main + +main() diff --git a/turtetris_slave/leds.py b/turtetris_slave/leds.py new file mode 100644 index 0000000..cdafca2 --- /dev/null +++ b/turtetris_slave/leds.py @@ -0,0 +1,38 @@ +from subprocess import call + + +def prepare(): + "Prepare leds" + call("rainbow all enable intensity 10 FFFFFF", shell=True) + + +def clear(): + "Clear previous changes" + call("rainbow all auto", shell=True) + + +__MAP__ = [ + 'pwr', + 'lan0', + 'lan1', + 'lan2', + 'lan3', + 'lan4', + 'wan', + 'pci1', + 'pci2', + 'pci3', + 'usr1', + 'usr2' +] + + +def output(data): + "Output received data to leds" + args = ['rainbow'] + for i in range(0, 12): + args.append(__MAP__[i]) + args.append(str(data[i]['color'])) + args.append('intensity') + args.append(str(data[i]['intens'])) + call(args) -- cgit v1.2.3