from machine import Pin, ADC, PWM, I2C
from time import sleep
from neopixel import NeoPixel 
from onewire import OneWire
import ssd1306 
from ssd1306 import SSD1306_I2C

#Bouton
BP1=Pin(25,Pin.IN)
BP2=Pin(34,Pin.IN)
BP3=Pin(39,Pin.IN)
BP4=Pin(36,Pin.IN)

#Led
LED1=Pin(23,Pin.OUT)
LED2=Pin(19,Pin.OUT)
LED3=Pin(18,Pin.OUT)
LED4=Pin(2,Pin.OUT)

#Potentiometre
POT1=ADC(Pin(35))
POT2=ADC(Pin(33))

POT1.atten(ADC.ATTN_11DB)
POT2.atten(ADC.ATTN_11DB)


#neopixel led
np=NeoPixel(Pin(26),16)

#Photo-elec
LDR=ADC(Pin(32))
LDR.atten(ADC.ATTN_11DB)             

#oled
i2c = I2C(0, scl=Pin(22), sda=Pin(21),freq=400000)  
display = ssd1306.SSD1306_I2C(128, 64, i2c) 

class Vumetre:

  def __init__(self,PBinf,PVal,PBsup):
    self.inf=PBinf
    self.val=PVal
    self.sup=PBsup

  def Cal(self):
    A=int(((self.val-self.inf)/(self.sup-self.inf))*16)
    return(A)

  def vu(self):
      display.fill(0) 
      for i in range (0,16):
          np[i]=(0,0,0)

      for i in range (0,self.Cal()):
        if i>=0 and i<4:
          np[i]=(0,0,200)
        elif i>=4 and i<8:
          np[i]=(0,200,0)
        elif i>=8 and i<12:
          np[i]=(200,200,0)
        elif i>=12 and i<=16:
          np[i]=(200,0,0)
      display.text(str(self.val),10,10,1)
      display.show()
      np.write()
      
    
      np.write()

print("Move the upper potentiometer and see its 12-bit value on the screen.")
print("Press BPA (green) to stop.")
E1=Vumetre(0,POT1.read(),4095)

while not BP1.value():
  E1.val=POT1.read()
  E1.vu()
  sleep(0.2)