#!/usr/bin/env python3

import os
import sys
import random 
import time

# Groesse des Bildschirms bestimmen
Nx = int(sys.argv[1])
Ny = int(sys.argv[2])

# Bestimme den Parameter
time_ms = 100
try:
    time_ms = int(sys.argv[3])
except:
    pass

# Puffer fuer Pixel erstellen und mit 0 initialisieren
buffer = bytearray(b"\x00" * (3 * Nx * Ny))

curPixel = 0

while True:
    # Zufaellige Pixel waeheln
    # rot
    x_r = random.randint(0, Nx-1)
    y_r = random.randint(0, Ny-1)
    i_r = 3*(x_r+Nx*y_r)
    # gruen 
    x_g = random.randint(0, Nx-1)
    y_g = random.randint(0, Ny-1)
    i_g = 3*(x_g+Nx*y_g)
    # blau
    x_b = random.randint(0, Nx-1)
    y_b = random.randint(0, Ny-1)
    i_b = 3*(x_b+Nx*y_b)

    # Pixel in Puffer schreiben
    # rot
    buffer[i_r+0] = 0xff # Rotanteil
    buffer[i_r+1] = 0x00 # Gruenanteil
    buffer[i_r+2] = 0x00 # Blauanteil
    # gruen
    buffer[i_g+0] = 0x00 # Rotanteil
    buffer[i_g+1] = 0xff # Gruenanteil
    buffer[i_g+2] = 0x00 # Blauanteil
    # blau
    buffer[i_b+0] = 0x00 # Rotanteil
    buffer[i_b+1] = 0x00 # Gruenanteil
    buffer[i_b+2] = 0xff # Blauanteil

    

    # Zeige den Puffer an
    os.write(1, buffer)
    # warte time_ms ms
    time.sleep(time_ms*0.001)
