from machine import Pin
import machine, neopixel, time
import buttons
import mch22
import display


class ColorKite:
    mode = 2
    n = 5
    p = 5
    idx = 0
    colors_rgb=[(128,0,0),(0,128,0),(0,0,128), (128,128,0),(0,128,128),(128,0,128), (128,64,64),(64,128,64),(64,64,128)]
    np = neopixel.NeoPixel(machine.Pin(5),n)


    def btn_a(self,pressed):
        pass
    def btn_b(self,pressed):
        pass

    def btn_up(self,pressed):
        if pressed:
            if self.idx == (len(self.colors_rgb)-1):
                self.idx = 0
            else:
                self.idx += 1
    def btn_down(self,pressed):
        if pressed:
            if self.idx == 0:
                self.idx = len(self.colors_rgb)-1
            else:
                self.idx -= 1
            
    
    def btn_left(self,pressed):
        if pressed:
            if self.mode < 1:
                self.mode = 4
            else:
                self.mode -= 1
    def btn_right(self,pressed):
        if pressed:
            if self.mode > 4:
                self.mode = 1
            else:
                self.mode += 1

    def btn_home(self,pressed):
        if pressed:
            mch22.exit_python()    
    def btn_select(self,pressed):
        pass
    def btn_start(self,pressed):
        pass
    
            

    def __init__(self):
        # configure pushbuttons as interrupts
        buttons.attach(buttons.BTN_A, self.btn_a)
        buttons.attach(buttons.BTN_B, self.btn_b)
               
        buttons.attach(buttons.BTN_START, self.btn_start)
        buttons.attach(buttons.BTN_SELECT, self.btn_select)                
        buttons.attach(buttons.BTN_HOME, self.btn_home)
                
        buttons.attach(buttons.BTN_DOWN, self.btn_down)
        buttons.attach(buttons.BTN_RIGHT, self.btn_right)
        buttons.attach(buttons.BTN_UP, self.btn_up)
        buttons.attach(buttons.BTN_LEFT, self.btn_left)
        
        while True:
            if self.mode == 1:
                self.clear()
            elif self.mode == 2:
                rgb = self.colors_rgb[self.idx]
                self.bounce(rgb, 70)
            elif self.mode == 3:
                rgb = self.colors_rgb[self.idx]
                self.cycle(rgb, 50)
            elif self.mode == 4:
                self.rainbow_cycle(1)
        



    # FUNCTIONS FOR LIGHTING EFFECTS
    # bounce
    def bounce(self, rgb, wait):
      for i in range(2 * self.n):
        for j in range(self.n):
          self.np[j] = rgb
        if (i // self.n) % 2 == 0:
          self.np[i % self.n] = (0, 0, 0)
        else:
          self.np[self.n - 1 - (i % self.n)] = (0, 0, 0)
        self.np.write()
        time.sleep_ms(wait)

    # cycle
    def cycle(self, rgb, wait):
      for i in range(self.n):
        for j in range(self.n):
          self.np[j] = (0, 0, 0)
          self.np[i % self.n] = rgb
          self.np.write()
        time.sleep_ms(wait)

    # function to go through all colors 
    def wheel(self, pos):
      # Input a value 0 to 255 to get a color value.
      # The colours are a transition r - g - b - back to r.
      if pos < 0 or pos > 255:
        return (0, 0, 0)
      if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
      if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
      pos -= 170
      return (pos * 3, 0, 255 - pos * 3)

    # rainbow 
    def rainbow_cycle(self, wait):
      for j in range(255):
        for i in range(self.n):
          rc_index = (i * 256 // self.n) + j
          self.np[i] = self.wheel(rc_index & 255)
          self.np.write()
          time.sleep_ms(wait)

    # turn off all pixels
    def clear(self):
      for i in range(self.n):
        self.np[i] = (0, 0, 0)
        self.np.write()


            
font="roboto_regular12"

try:
    display.drawFill(0x000000)
    display.flush()
    fl = ColorKite()
except:
    display.drawText(0, 0, "No Fancy Lights v2!", 0x000000, font)