# imports
from machine import pin, ADC
import math
import time
#######################################
# Pin and constant definitions
SEVEN_SEGMENT_START_PIN = 0
DISPLAY_COUNT = 4
DECIMAL_PRECISION = 1
seg_A = ADC(0)
seg_B = ADC(1)
seg_C = ADC(2)
seg_D = ADC(3)
seg_E = ADC(4)
seg_F = ADC(5)
seg_G = ADC(6)
seg_DP = ADC(7)
DIG_1 = ADC(8)
DIG_2 = ADC(9)
DIG_3 = ADC(10)
DIG_4 = ADC(11)
temp_sensor_pin = ADC(26)
light_sensor_pin = ADC(27)
slide_potentiometer_pin = ADC(28)
button_pin = pin(16, pin.OUT)
#######################################
#######################################
# Global variables
#######################################
display_value = 0
segment_pins = []
display_select_pins = []
current_display_index = DISPLAY_COUNT -1
display_timer = None
counter_timer = None
#######################################
# Function definitions
#######################################
# Function to read the ADC pin and
# to convert the digital value to a voltage level in the 0-3.3V range
# This function updates the value of the display_value global variable
def read_analogue_voltage(pin): # We can use the pin parameter if needed
pass
# 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)
# 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):
global current_display_index, display_value
# 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):
if digit_value < 0 or digit_value > len(digit_list_hex):
return
# Function to test avaiable 7-segment displays
def display_value_test():
global display_value
disable_display_timer()
current_display_index = 0
for i in range(0, len(digit_list_hex)):
display_digit(i, -1, i % 2 != 0)
time.sleep(0.5)
for i in range(0, len(digit_list_hex)):
display_digit(i, DISPLAY_COUNT - 1 - (i % DISPLAY_COUNT), True)
time.sleep(0.5)
display_digit(16, -1, False)
enable_display_timer()
def count_display_value(timer_int):
global display_value
display_value += 1
if display_value > 9999:
display_value = 0
#print(display_value)
# Function to setup GPIO/ADC pins, timers and interrupts
def setup():
global segment_pins, display_select_pins
global display_timer, counter_timer
# Set up display select pins
for i in range(SEVEN_SEGMENT_START_PIN + 8, SEVEN_SEGMENT_START_PIN + 8 + DISPLAY_COUNT):
pin = machine.Pin(i, machine.Pin.OUT)
pin.value(0)
display_select_pins.append(pin)
# Set up seven segment pins
for i in range(SEVEN_SEGMENT_START_PIN, SEVEN_SEGMENT_START_PIN + 8):
pin = machine.Pin(i, machine.Pin.OUT)
pin.value(1)
segment_pins.append(pin)
# Start the timer interrupt for scanning
display_timer = machine.Timer()
enable_display_timer()
counter_timer = machine.Timer()
counter_timer.init(period=100, mode=machine.Timer.PERIODIC, callback=count_display_value)
if __name__ == '__main__':
setup()
#display_value_test()