aboutsummaryrefslogtreecommitdiff
path: root/turtetris_master
diff options
context:
space:
mode:
Diffstat (limited to 'turtetris_master')
-rw-r--r--turtetris_master/__init__.py21
-rw-r--r--turtetris_master/led_output.py20
-rw-r--r--turtetris_master/screen_checker.py33
-rw-r--r--turtetris_master/state_machine.py4
4 files changed, 62 insertions, 16 deletions
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"
+
+