diff --git a/8x8test.py b/8x8test.py deleted file mode 100644 index 45e7c4e..0000000 --- a/8x8test.py +++ /dev/null @@ -1,119 +0,0 @@ -from threading import Thread - -import pygame -import sys -import random -import time -from pygame.locals import * - - -pygame.init() -length = 600 -width = 600 -tile_size = 75 -window_front = pygame.display.set_mode((length, width)) - -pygame.display.set_caption("8x8 Test") - -window_front.fill((255, 255, 255)) -pygame.display.update() - - -# Unnötige Scheiße von Leon -class Color: - r: int = 0 - g: int = 0 - b: int = 0 - - def __init__(self, r, g, b): - self.r = r - self.g = g - self.b = b - - -# 1 Feld -class Tile: - pos_x: int = 0 - pos_y: int = 0 - size: int = 0 - color = Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) - - def __init__(self, pos_x, pos_y, size): - self.pos_x = pos_x - self.pos_y = pos_y - self.size = size - - def draw(self): - pygame.draw.rect(window_front, (self.color.r, self.color.g, self.color.b), - (self.pos_x * tile_size, self.pos_y * tile_size, self.size, self.size)) - - def change_color(self, color: Color): - self.color = color - self.draw() - - -class GameTicks(Thread): - k = 0 - - def __init__(self, tick_speed, g_tiles): - pass - - Thread.__init__(self) - self.tick_speed = tick_speed - self.g_tiles = g_tiles - - def run(self): - while True: - time.sleep(self.tick_speed) - - -BLACK = Color(0, 0, 0) -WHITE = Color(255, 255, 255) - -tiles = [Tile(m, n, tile_size) for n in range(0, length // tile_size) for m in range(0, width // tile_size)] - - -def grid(): - gridcolor = BLACK - for i in range(tile_size): - pygame.draw.rect(window_front, (gridcolor.r, gridcolor.g, gridcolor.b), (i * tile_size, 0, 1, length)) - for i in range(tile_size): - pygame.draw.rect(window_front, (gridcolor.r, gridcolor.g, gridcolor.b), (0, i * tile_size, width, 1)) - - -def keypressed(key): - return pygame.key.get_pressed()[key] - - -k = 0 -grid() - -### MAIN LOOP ### -while True: - if 64 > k > 0: - tiles[k].draw() - grid() - else: - k = 0 - - pygame.display.update() - - # Event Handler - for event in pygame.event.get(): - if event.type == KEYDOWN: - if event.key == K_d or event.key == K_RIGHT: - k += 1 - elif event.key == K_a or event.key == K_LEFT: - k -= 1 - elif event.key == K_w or event.key == K_UP: - k -= 8 - elif event.key == K_s or event.key == K_DOWN: - k += 8 - elif event.key == K_r: - window_front.fill((255, 255, 255)) - - if event.type == MOUSEBUTTONDOWN: - tiles[k].change_color(Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) - if event.type == QUIT: - pygame.quit() - sys.exit() diff --git a/api.py b/api.py index 201828b..e7af21e 100644 --- a/api.py +++ b/api.py @@ -1,81 +1,69 @@ -leds = [[[False for x in range(8)] for y in range(8)] for z in range(8)] - - -class Vector: - x: int = None - y: int = None - z: int = None - - def __init__(self, x, y, z): - self.x = x - self.y = y - self.z = z - - def __add__(self, other): - self.x = self.x + other.x - self.y = self.y + other.y - self.z = self.z + other.z - - def add(self, x, y, z): - self.x = self.x + x - self.y = self.y + y - self.z = self.z + z - - -class LED: - x: int = None - y: int = None - z: int = None - state: bool = False - lifetime: float = None - - def __init__(self, x, y, z): - self.x = x - self.y = y - self.z = z - - def turn_on(self): - self.state = True - turn_on(self.x, self.y, self.z) - - def turn_off(self): - self.state = False - turn_off(self.x, self.y, self.z) - - def toggle_state(self): - self.state = not self.state - - -def turn_on(x, y, z): - leds[x][y][z] = True - - -def turn_off(x, y, z): - leds[x][y][z] = False - - -def move_all(vector: Vector): - pass - - -def move_led(x: int, y: int, z: int, vector: Vector): - leds[x + vector.x][y + vector.y][z + vector.z] = leds[x][y][z] - leds[x][y][z] = False - - -def move_leds(m_led, vector: Vector): - pass - - -def turn_on_all(): - for x in range(8): - for y in range(8): - for z in range(8): - leds[x][y][z] = True - - -def turn_off_all(): - for x in range(8): - for y in range(8): - for z in range(8): - leds[x][y][z] = False +import time + +import RPi.GPIO as GPIO + +import led + +# Temporäre Dummy-Werte für die Pins +data = 1 +clock = 2 +latch = 3 + + +def initialise(): + """LED-Objekt mit Cube- sowie Buffer-Array erstellen""" + return led.LED() + + +def test_all(led): + """Durchläuft alle Layer einmal.""" + for layer in range(0, 8): + led.nth_layer(layer) + shift_write_all(led) + time.sleep(.01) + + +def shift_write_layer(led, layer): + for x in range(0, 8): + if x == layer: + led.cube_array[x+512] = 1 + else: + led.cube_array[x+512] = 0 + shift_write_all(led) + + +def shift_write_all(led): + """ + aktuelles Bitmuster wird in die Shift-Register geschoben. + Soweit nur Implementation für 8 zusammenhängende Register. + TO-DO (falls gewünscht): Funktion erweitern, sodass abhhängig von der aktuellen + Position im Array der Pin für die nächste Shift-Registerreihe angesprochen wird. + """ + for x in range(0, 520): + temp = led.cube_array[x] + if temp == 1: + GPIO.output(data, GPIO.HIGH) + else: + GPIO.output(data, GPIO.LOW) + pulse_clock() + trigger_latch() + return + + +def pulse_clock(): + """manueller Taktgeber""" + GPIO.output(clock, GPIO.HIGH) + time.sleep(.01) + GPIO.output(clock, GPIO.LOW) + + +def trigger_latch(): + """ + Gespeichertes Bitmuster ins latch übertragen. + TO-DO: Falls nicht alle Shift-Register aneinander: Funktion erweitern. + (parallel zu "shiftWrite(led)") + """ + GPIO.output(latch, GPIO.HIGH) + time.sleep(.01) + GPIO.output(latch, GPIO.LOW) + return diff --git a/led.py b/led.py index c6f71fe..9b95fc3 100644 --- a/led.py +++ b/led.py @@ -2,39 +2,71 @@ class LED: - x: int = None - y: int = None - z: int = None - state: bool = False - lifetime: float = None + def __init__(self): + """Cube- und Buffer-Array initialisieren""" + self.cube_array = np.zeros(520, dtype=int) + self.buffer_array = np.zeros(520, dtype=int) - cube_array = np.ones(512, dtype=int) - buffer_array = np.zeros(512, dtype=int) + ''' + Zählweise LED's: Man fängt hinten links unten an zu zählen. + Gezählt wird von links nach rechts. + :param x: Zeilenposition (insg. 8 pro Reihe) + :param y: Höhe/Layer (Ansteuerung: 64 Elemente des Arrays weitergehen) + :param z: Tiefe (Ansteuerung: Eine Zeile weitergehen <=> 8 Schritte) + ''' - def transmission(self, cube_array, buffer_array): - for x in range(buffer_array.size): - if buffer_array[x] != cube_array[x]: - cube_array[x] = buffer_array[x] + def nth_layer(self, layer): + self.cube_array = np.ones(520, dtype=int) + for x in range(8): + if x == layer: + self.cube_array[x+512] = 1 + else: + self.cube_array[x+512] = 0 - """def __init__(self, x, y, z): - self.x = x - self.y = y - self.z = z""" + def transmission(self): + """Buffer-Array mit aktualisiertem Muster füllen.""" + self.buffer_array=self.cube_array def turn_on(self, x, y, z): - cord = x * y * z - self.state = True - self.buffer_array[cord] = 1 - + """Über die Parameter angesprochenes LED-Bit unabhängig vom derzeitigen Status auf 1 setzen.""" + self.cube_array[x + 64 * y + 8 * z] = 1 def turn_off(self, x, y, z): - cord = x * y * z - self.state = False - self.buffer_array[cord] = 0 - - # turn_off(self.x, self.y, self.z) + """Über die Parameter angesprochenes LED-Bit unabhängig vom derzeitigen Status auf 0 setzen.""" + self.cube_array[x + 64 * y + 8 * z] = 0 def toggle_state(self, x, y, z): - cord = x * y * z - self.state = not self.state - self.buffer_array[cord] = not self.buffer_array[cord] + """ + Zustand wechseln. (1 => 0 bzw. 0 => 1) + """ + if self.cube_array[x + 64 * y + 8 * z] == 0: + self.cube_array[x + 64 * y + 8 * z] = 1 + else: + self.cube_array[x + 64 * y + 8 * z] = 0 + + def turn_off_all(self): + """Alle LED's aus.""" + for x in range(0, 512): + self.cube_array[x] = 0 + + def turn_on_all(self): + """Alle LED's an.""" + for x in range(0, 512): + self.cube_array[x] = 1 + + def move_led(self, x1, y1, z1, x2, y2, z2): + """ + Falls Ursprungs-LED aus: return ohne weitere Verarbeitung. + Ansonsten: Schalte Urpsrung auf 0, Ziel auf 1 + :param x1: Pos. x-Achse Ursprung + :param y1: Pos. y-Achse Ursprung + :param z1: Pos. z-Achse Ursprung + :param x2: Pos. x-Achse Ziel + :param y2: Pos. y-Achse Ziel + :param z2: Pos. z-Achse Ziel + :return: + """ + + if self.cube_array[x1 + 64 * y1 + 8 * z1] == 1: + self.cube_array[x1 + 64 * y1 + 8 * z1] = 0 + self.cube_array[x2 + 64 * y2 + 8 * z2] = 1 diff --git a/multSource_RGB_simulation/RGB.py b/multSource_RGB_simulation/RGB.py deleted file mode 100644 index dd862b9..0000000 --- a/multSource_RGB_simulation/RGB.py +++ /dev/null @@ -1,52 +0,0 @@ -import time -class LED(object): - ''' - Einfache LED-Klasse mit (privaten) RGB-Variablen - ''' - def __init__(self, r, g, b): - self.r=r - self.g=g - self.b=b - def setR(self,val): - self.r=val - def getR(self): - return self.r - def setG(self,val): - self.g=val - def getG(self): - return self.g - def setB(self,val): - self.b=val - def getB(self): - return self.b - def getAll(self): - tmp = [self.r,self.g,self.b] - return tmp - def incToMax(self, light): - ''' - 1=R, 2=G, 3=B - :param light: - :return: - ''' - if light==1: - while self.r!=255: - self.r+=1 - print("Setting red to",self.r) - time.sleep(0.3) - print("Done increasing.") - if light==2: - while self.g!=255: - self.g+=1 - print("Setting green to",self.g) - time.sleep(0.3) - print("Done increasing.") - if light==3: - while self.b!=255: - self.b+=1 - print("Setting blue to",self.b) - time.sleep(0.3) - print("Done increasing.") - def resetAll(self): - self.r=self.g=self.b=0 - def maxAll(self): - self.r=self.g=self.b=255 \ No newline at end of file diff --git a/multSource_RGB_simulation/rasp.py b/multSource_RGB_simulation/rasp.py deleted file mode 100644 index 809d8d2..0000000 --- a/multSource_RGB_simulation/rasp.py +++ /dev/null @@ -1,9 +0,0 @@ -import RGB - -myFirstLED=RGB.LED(0,222,255) -mySecondLED=RGB.LED(200,200,200) -myFirstLED.resetAll() -print(myFirstLED.getAll()) -myFirstLED.maxAll() -print(myFirstLED.getAll()) - diff --git a/snake/snake.py b/snake/snake.py deleted file mode 100644 index d5762c1..0000000 --- a/snake/snake.py +++ /dev/null @@ -1,131 +0,0 @@ -from enum import Enum -from threading import Thread -import time -import keyboard - - -class Color(Enum): - BLACK = 1 - WHITE = 2 - - -class Direction(Enum): - UP = 1 - DOWN = 2 - RIGHT = 3 - LEFT = 4 - - -class Tile: - pos_x = 0 - pos_y = 0 - color = Color.WHITE - - def __init__(self, pos_x, pos_y): - self.pos_x = pos_x - self.pos_y = pos_y - - def draw(self): - pass - - def change_color(self, color: Color): - self.color = color - self.draw() - - -class GameTicks(Thread): - snake = None - - def __init__(self, tick_speed, snake): - pass - - Thread.__init__(self) - self.tick_speed = tick_speed - self.snake = snake - - def run(self): - while True: - # draw etc. - print(self.snake.current_direction) # testing - time.sleep(self.tick_speed) - - -class KeyCheck(Thread): - snake = None - - def __init__(self, snake): - pass - - Thread.__init__(self) - self.snake = snake - - def run(self): - while True: - try: - if keyboard.is_pressed('w'): - self.snake.change_direction(Direction.UP) - elif keyboard.is_pressed('a'): - self.snake.change_direction(Direction.LEFT) - elif keyboard.is_pressed('s'): - self.snake.change_direction(Direction.DOWN) - elif keyboard.is_pressed('d'): - self.snake.change_direction(Direction.RIGHT) - except: - break - - -class Snake: - current_direction = Direction.UP - s_length: int = 1 - current_pos = (0, 0) - - def __init__(self, starting_pos_x, starting_pos_y, tiles): - tiles[starting_pos_x * starting_pos_y].change_color(Color.BLACK) - self.current_pos = (starting_pos_x, starting_pos_y) - - def change_direction(self, direction: Direction): - self.current_direction = direction - - def move(self): - if self.current_direction == Direction.UP: - if self.current_pos[1] == 0: - return False - else: - self.current_pos = (self.current_pos[0], self.current_pos[1] - 1) - return True - elif self.current_direction == Direction.LEFT: - if self.current_pos[0] == 0: - return False - else: - self.current_pos = (self.current_pos[0] - 1, self.current_pos[1]) - return True - elif self.current_direction == Direction.DOWN: - if self.current_pos[1] == sy - 1: - return False - else: - self.current_pos = (self.current_pos[0], self.current_pos[1] + 1) - return True - elif self.current_direction == Direction.RIGHT: - if self.current_pos[0] == sx - 1: - return False - else: - self.current_pos = (self.current_pos[0] + 1, self.current_pos[1]) - return True - - -def main(tick_speed): - tiles = [Tile(n, m) for n in range(0, sx) for m in range(0, sy)] - snake = Snake(sx // 2, sy - 1, tiles) - for tile in tiles: - print(tile.pos_x, " ", tile.pos_y) # just for testing purpose - tile.draw() # initial ... - - game_ticks = GameTicks(tick_speed, snake) - game_ticks.start() - - key_check = KeyCheck(snake) - key_check.start() - - -sx, sy = 5, 5 -main(0.5) diff --git a/test_main.py b/test_main.py new file mode 100644 index 0000000..b08bc5f --- /dev/null +++ b/test_main.py @@ -0,0 +1,100 @@ +import unittest + +import numpy as np + +import api + + +class TestLED(unittest.TestCase): + def test_initialise(self): + led = api.initialise() + vgl = np.zeros(520, dtype=int) + self.assertEqual(led.cube_array.all(), vgl.all()) + + def test_nth_layer(self): + led = api.initialise() + led.nth_layer(1) + self.assertFalse(led.cube_array[0 + 512], 1) + self.assertEqual(led.cube_array[1+512], 1) + self.assertFalse(led.cube_array[2 + 512], 1) + + def test_transmission(self): + led = api.initialise() + led.cube_array = np.ones(520, dtype=int) + vgl = np.ones(520, dtype=int) + led.transmission() + self.assertEqual(led.buffer_array.all(), vgl.all()) + + def test_turn_on(self): + led = api.initialise() + led.turn_on(1, 0, 0) + self.assertEqual(led.cube_array[1], 1) + led.turn_on(0, 2, 0) + self.assertEqual(led.cube_array[2*64], 1) + led.turn_on(0, 0, 3) + self.assertEqual(led.cube_array[3 * 8], 1) + led.turn_on(1, 2, 3) + self.assertEqual(led.cube_array[1 + 2 * 64 + 3 * 8], 1) + led.turn_on(7, 7, 7) + self.assertEqual(led.cube_array[7 + 7 * 64 + 7 * 8], 1) + + def test_turn_off(self): + led = api.initialise() + led.cube_array = np.ones(520, dtype=int) + led.turn_off(1, 0, 0) + self.assertEqual(led.cube_array[1], 0) + led.turn_off(0, 2, 0) + self.assertEqual(led.cube_array[2*64], 0) + led.turn_off(0, 0, 3) + self.assertEqual(led.cube_array[3 * 8], 0) + led.turn_off(1, 2, 3) + self.assertEqual(led.cube_array[1 + 2 * 64 + 3 * 8], 0) + led.turn_off(7, 7, 7) + self.assertEqual(led.cube_array[7 + 7 * 64 + 7 * 8], 0) + + def test_toggle_state(self): + led = api.initialise() + led.cube_array = np.ones(520, dtype=int) + led.toggle_state(1, 0, 0) + self.assertEqual(led.cube_array[1], 0) + led.toggle_state(0, 2, 0) + self.assertEqual(led.cube_array[2*64], 0) + led.toggle_state(0, 0, 3) + self.assertEqual(led.cube_array[3 * 8], 0) + led.toggle_state(1, 2, 3) + self.assertEqual(led.cube_array[1 + 2 * 64 + 3 * 8], 0) + led.toggle_state(1, 0, 0) + self.assertEqual(led.cube_array[1], 1) + led.toggle_state(0, 2, 0) + self.assertEqual(led.cube_array[2 * 64], 1) + led.toggle_state(0, 0, 3) + self.assertEqual(led.cube_array[3 * 8], 1) + + def test_turn_off_all(self): + led = api.initialise() + led.cube_array = np.ones(520, dtype=int) + list1 = led.cube_array + led.turn_off_all() + list2 = led.cube_array + self.assertEqual(list1.all(), list2.all()) + + def test_turn_on_all(self): + led = api.initialise() + list1 = led.cube_array + led.turn_on_all() + list2 = led.cube_array + self.assertEqual(list1.all(), list2.all()) + + def test_move_led(self): + led = api.initialise() + led.cube_array[0] = 1 + led.move_led(0, 0, 0, 7, 7, 7) + self.assertEqual(led.cube_array[0], 0) + self.assertEqual(led.cube_array[7 + 7 * 64 + 7 * 8], 1) + +def main(): + unittest.main() + + +if __name__ == "__main__": + main()