#BSD 2-clause
#Inspired by the lsd-nickname card10 app + code
#

import utime, urandom, display, color, leds, buttons
NUMBERS =  [ i for i in range(0,10) ]
SPACES = [ " "*i for i in range(0,5)  ]
items = NUMBERS + SPACES

def rain(seq):
  s = [str(urandom.choice(seq)) for i in range(1,67)]
  return "".join(s)
#get nickname
nick = 'matrix'
try:
    with open('/nickname.txt') as f:
        nick = f.read()
except:
    pass

def matrix(bgc, fgc):
   with display.open() as d:
       #print characters 
       s = rain(items)
       d.print(s, fg = bgc)
       #print nickname
       nx = 80 - round(len(nick)/2 * 14)
       d.print(nick, fg=fgc, bg=None, posx=nx + urandom.randrange(-2,2), posy=25 + urandom.randrange(-3,2))
       d.update()
       d.close()
   leds.set(urandom.randrange(11), bgc)
   leds.set(urandom.randrange(11), fgc)
   utime.sleep(0.01)

def random_col():
    red = urandom.randrange(0,255)
    green = urandom.randrange(0,255)
    blue = urandom.randrange(0,255)
    return (red, green, blue)
    
last_fg = color.GREEN
last_bg = color.WHITE
while True:
    matrix(last_fg, last_bg)
    pressed = buttons.read(buttons.TOP_RIGHT | buttons.BOTTOM_RIGHT)
    if pressed & buttons.TOP_RIGHT != 0:
        last_fg = color.GREEN
        last_bg = color.WHITE
    if pressed & buttons.BOTTOM_RIGHT != 0:
        last_fg = random_col()
        last_bg = random_col()
       
