From b934596cfd5415ce83b5a7b42120e0f0e229937a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Wed, 19 Jul 2017 11:06:04 +0200 Subject: Add screen checker --- turtetris_master/__init__.py | 21 +++++++++++++++------ turtetris_master/led_output.py | 20 ++++++++++---------- turtetris_master/screen_checker.py | 33 +++++++++++++++++++++++++++++++++ turtetris_master/state_machine.py | 4 ++++ turtetris_slave/__init__.py | 2 +- turtetris_slave/leds.py | 6 ++---- 6 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 turtetris_master/screen_checker.py create mode 100644 turtetris_master/state_machine.py diff --git a/turtetris_master/__init__.py b/turtetris_master/__init__.py index 401accb..e7f9fab 100644 --- a/turtetris_master/__init__.py +++ b/turtetris_master/__init__.py @@ -1,19 +1,28 @@ import time from .led_output import Matrix from .usb_input import Gamepad +from .screen_checker import ScreenChecker def main(): "Main function" inpt = Gamepad() - print('Gamepad initialized') mtrx = Matrix() - print('Matrix initialized') + sc = ScreenChecker(mtrx) + mtrx.display() # Display first state while True: - print('loop!!!!') - print(inpt.check()) - mtrx.display() - time.sleep(1) + tstart = time.time() + ## + #print(inpt.check()) + if sc.tick(): + mtrx.display() + #print(mtrx.__mat__[2]) + ## + trest = (1/60) - (time.time() - tstart) + if trest > 0: + time.sleep(trest) + else: + print("Output took too long!!") if __name__ == '__main__': diff --git a/turtetris_master/led_output.py b/turtetris_master/led_output.py index 7af7a35..443c44f 100644 --- a/turtetris_master/led_output.py +++ b/turtetris_master/led_output.py @@ -7,10 +7,13 @@ class Matrix: def __init__(self): "Establish connection to matrix" - self.__mat__ = [[{ - 'color': '000000', - 'intens': 0 - }]*12]*10 + self.width = 12 + self.height = 10 + self.__mat__ = [None]*10 + for x in range(10): + self.__mat__[x] = [None]*12 + for y in range(12): + self.__mat__[x][y] = '000000' self.context = zmq.Context() self.socket = self.context.socket(zmq.PUB) self.socket.bind('tcp://*:4444') @@ -21,11 +24,8 @@ class Matrix: self.socket.send_string('line' + str(i) + ' ' + json.dumps(self.__mat__[i])) - def pixel(self, x, y, color=None, bright=None): + def pixel(self, x, y, color): "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 + raise Exception('Pixel out of matrix') + self.__mat__[y][x] = color diff --git a/turtetris_master/screen_checker.py b/turtetris_master/screen_checker.py new file mode 100644 index 0000000..d8ed327 --- /dev/null +++ b/turtetris_master/screen_checker.py @@ -0,0 +1,33 @@ +class ScreenChecker: + "Simple screen checker showing lines and updates" + + def __init__(self, matrix): + "Initialize it for given matrix" + if matrix.height >= matrix.width: + raise Exception('ScreenChecker doesn\'t support wide matrix.') + self.matrix = matrix + self.__tick__ = 0 + self.__pos__ = [[0, 0] for _ in range(matrix.height)] + for i in range(0, matrix.height): + self.__pos__[i][1] = i + for y in range(0, i): + matrix.pixel(i, y, 'green') + + def __pos_inc__(self, p): + p += 1 + if p >= self.matrix.width: + p = 0 + return p + + def tick(self): + self.__tick__ += 1 + if self.__tick__ > 60: + self.__tick__ = 0 + for i in range(0, self.matrix.height): + self.matrix.pixel(self.__pos__[i][0], i, '000000') + self.__pos__[i][0] = self.__pos_inc__(self.__pos__[i][0]) + self.__pos__[i][1] = self.__pos_inc__(self.__pos__[i][1]) + self.matrix.pixel(self.__pos__[i][1], i, color='green') + return True + else: + return False diff --git a/turtetris_master/state_machine.py b/turtetris_master/state_machine.py new file mode 100644 index 0000000..880a582 --- /dev/null +++ b/turtetris_master/state_machine.py @@ -0,0 +1,4 @@ +#class StateMachine: + #"Game state machine" + + diff --git a/turtetris_slave/__init__.py b/turtetris_slave/__init__.py index 5779dee..d5dd129 100644 --- a/turtetris_slave/__init__.py +++ b/turtetris_slave/__init__.py @@ -23,7 +23,7 @@ def main(): while True: msg = sck.recv_string() print(msg) - json0 = msg.find(' ') # First empty char is end of envelope + json0 = msg.find(' ') # First empty char is end of envelope leds.output(json.loads(msg[json0:])) diff --git a/turtetris_slave/leds.py b/turtetris_slave/leds.py index cdafca2..3075827 100644 --- a/turtetris_slave/leds.py +++ b/turtetris_slave/leds.py @@ -3,7 +3,7 @@ from subprocess import call def prepare(): "Prepare leds" - call("rainbow all enable intensity 10 FFFFFF", shell=True) + call("rainbow all enable FFFFFF", shell=True) def clear(): @@ -32,7 +32,5 @@ def output(data): 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'])) + args.append(str(data[i])) call(args) -- cgit v1.2.3