from machine import Pin
import display, buttons
from neopixel import NeoPixel
import random
import mch22

colours1=[0xff6347,0xf28500,0xffff00,0x00ff00,0x0000ff,0xff00ff,0xfda4ba,0xff1694]
colours2=[0xff6347,0xf28500,0xffff00,0x00ff00,0x0000ff,0xff00ff,0xffffff,0xadd8e6]
rainbows=[colours1,colours2]
ledColours=[(255,0,0),(0,255,0),(0,0,255),(255,255,0),(255,0,255),(255,128,0)]
powerPin = Pin(19, Pin.OUT)
dataPin = Pin(5,Pin.OUT)
np = NeoPixel(dataPin,5)
powerPin.on()
h=30
numLed = 4

def drawRainbow():
    for i, c in enumerate(colours1): 
        display.drawRect(0,i*h,320,240,1,c)
        display.flush()
        if i <= numLed: 
            np[i]=ledColours[i]
            np.write()

def updateRainbow():
    aRainbow=rainbows[random.randint(0,1)]
    for i, c in enumerate(aRainbow): 
        display.drawRect(0,i*h,320,240,1,c)
        display.flush()

def updateLed():
    j = random.randint(0,numLed)
    k = random.randint(0,(len(ledColours)-1))
    np[j]=ledColours[k]
    np.write()

def aCallback(pressed):
    if pressed: 
        updateRainbow()

def bCallback(pressed):
    if pressed: 
        updateLed()

def homeCallback(pressed): 
    if pressed:
        mch22.exit_python()

buttons.attach(buttons.BTN_A, aCallback)
buttons.attach(buttons.BTN_B, bCallback)
buttons.attach(buttons.BTN_SELECT, homeCallback)

display.drawFill(0xffffff)
drawRainbow()