from power import HUSB238
import board
import neopixel

VM_GREEN = (32,255,32)
VM_RED = (255,16,16)

LED1 = 1 << 0   # 1
LED2 = 1 << 1   # 2
LED3 = 1 << 2   # 4
LED4 = 1 << 3   # 8
LED5 = 1 << 4   # 16
LED6 = 1 << 5   # 32
LED7 = 1 << 6   # 64
LED8 = 1 << 7   # 128
LED9 = 1 << 8   # 256
LED10 = 1 << 9  # 512
LED11 = 1 << 10 # 1024
LED12 = 1 << 11 # 2048
LED13 = 1 << 12 # 4096
LED14 = 1 << 13 # 8192
LED15 = 1 << 14 # 16384
LED16 = 1 << 15 # 32768
LED17 = 1 << 16 # 65536
LED18 = 1 << 17 # 131072
LED19 = 1 << 18 # 262144
LED20 = 1 << 19 # 524288
LED21 = 1 << 20 # 1048576
LED22 = 1 << 21 # 2097152
LED23 = 1 << 22 # 4194304
LED24 = 1 << 23 # 8388608
LED25 = 1 << 24 # 16777216
LED26 = 1 << 25 # 33554432
LED27 = 1 << 26 # 67108864
LED28 = 1 << 27 # 134217728
LED29 = 1 << 28 # 268435456
LED30 = 1 << 29 # 536870912
LED31 = 1 << 30 # 1073741824
LED32 = 1 << 31 # 2147483648
LED33 = 1 << 32 # 4294967296
LED34 = 1 << 33 # 8589934592
LED35 = 1 << 34 # 17179869184
LED36 = 1 << 35 # 34359738368
LED37 = 1 << 36 # 68719476736
LED38 = 1 << 37 # 137438953472
LED39 = 1 << 38 # 274877906944
LED40 = 1 << 39 # 549755813888
LED41 = 1 << 40 # 1099511627776
LED42 = 1 << 41 # 2199023255552
LED43 = 1 << 42 # 4398046511104
LED44 = 1 << 43 # 8796093022208
LED45 = 1 << 44 # 17592186044416

leds = [
    LED1, LED2, LED3, LED4, LED5,
    LED6, LED7, LED8, LED9, LED10,
    LED11, LED12, LED13, LED14, LED15,
    LED16, LED17, LED18, LED19, LED20,
    LED21, LED22, LED23, LED24, LED25,
    LED26, LED27, LED28, LED29, LED30,
    LED31, LED32, LED33, LED34, LED35,
    LED36, LED37, LED38, LED39, LED40,
    LED41, LED42, LED43, LED44, LED45
]

def get_led_states(value):
    return [(value & led) != 0 for led in leds]


_COLORS = [
	VM_RED, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, 
	VM_RED, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, 
	VM_RED, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, 
	VM_RED, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, 
	VM_RED, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN, VM_GREEN
]

class MixerLeds:
	def __init__(self, i2c):
		self._i2c = i2c
		self._pd = HUSB238(i2c)
		self._pixels = neopixel.NeoPixel(board.D5, 45, auto_write=False)
		self._known_wattage = 0

	def _init_pd(self):
		if not self._pd.connect():
			print('Init PD failed to start connection')
			return False
		if self._known_wattage == self._pd.wattage:
			return
		available_wattage = self._pd.attempt_wattage(30)
		self._known_wattage = available_wattage[1]
		if not available_wattage[0]:
			self._pixels.deinit()
			available_brightness = available_wattage[1]/30.0
			self._pixels = neopixel.NeoPixel(board.D5, 45, brightness=available_brightness, auto_write=False)

		return True

	def set_state(self, state):
		if not self._init_pd():
			return

		for idx, led_state in enumerate(get_led_states(state)):
			if (led_state):
				self._pixels[idx] = _COLORS[idx]
			else:
				self._pixels[idx] = (0,0,0)
		self._pixels.show()
