from machine import Pin, ADC
import time 
import math

DIG_1 = 11 
DOT_PIN = 7
last_button_time_stamp = 0
display_timer = machine.Timer()
sensor = ADC(26)
displayCodes = [
    0xC0, #0  -> 0x1100 0000
    0xF9, #1
    0xA4, #2
    0xB0, #3
    0x99, #4
    0x92, #5
    0x82, #6
    0xF8, #7
    0x80, #8
    0x90  #9
]
ADC_RANGE = float((math.pow(2, 16) - 1))


#######################################
# Global variables
#######################################
display_value = 0

def read_analogue_voltage(pin):  # We can use the pin parameter if needed
    global display_value
    global last_button_time_stamp
    cur_button_ts = time.ticks_ms()
    button_press_delta = cur_button_ts - last_button_time_stamp
    if button_press_delta > 200:
        last_button_time_stamp = cur_button_ts
        adc_value = sensor.read_u16()
        x = 3.3/ADC_RANGE  * adc_value * 1000
        print(x)
    # celsius = 1 / (math.log(1 / (ADC_RANGE / adc_value - 1)) / BETA + 1.0 / 298.15) - 273.15;
    # print(celsius)
        display_value = x
        # print(f'pressed button: {PinId(pin) - 16}')
    # Convert ADC value to voltage
    # BETA = 3950; 
    

# Function to disable timer that triggers scanning 7 segment displays
def disable_display_timer():
    global display_timer
    display_timer.deinit()

# Function to enable timer that triggers scanning 7 segment displays
def enable_display_timer():
    global display_timer
    display_timer.init(period=30, mode=machine.Timer.PERIODIC, callback=scan_display)

def enable_button():
    global button
    

# Function to handle scanning 7 segment displays
# Display the value stored in the display_value global variable
# on available 7-segment displays
def scan_display(timer_int):
    read_display_value = display_value
    
    for i in range(3, -1, -1): # 3, 2, 1, 0
        display_digit(int(read_display_value%10), i, i==0)
        time.sleep(1 / 60)
        Pin(DIG_1 - i, Pin.OUT).off()
        Pin(DOT_PIN, Pin.OUT).value(1)
        read_display_value /= 10

# Function display the given value on the display with the specified index
# dp_enable specifies if the decimal pooint should be on or off
def display_digit(digit_value, digit_index, dp_enable=False):
    
    for i in range(7): #0->7
        Pin(i, Pin.OUT).value((1<<i) & displayCodes[digit_value])
    Pin(DIG_1 - digit_index, Pin.OUT).on()
    if dp_enable:
        Pin(DOT_PIN, Pin.OUT).value(0)    

# Function to test avaiable 7-segment displays
def display_value_test():
    # print('ok')
    display_digit(6, 3, 0)

# Function to setup GPIO/ADC pins, timers and interrupts
def setup():
    # global sensor
    print('ok')
    # enable_display_timer()
    
    button = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN)
    button.irq(trigger=machine.Pin.IRQ_FALLING, handler=read_analogue_voltage)
    enable_display_timer()
    
    # while True:
        # read_analogue_voltage(sensor)
    
    

if __name__ == '__main__':
    setup()
    # display_value_test()

$abcdeabcde151015202530354045505560fghijfghij
$abcdeabcde151015202530fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT