Added explaining comments in the code for better readability
This commit is contained in:
parent
1b3c68ddb3
commit
044284b60f
11
counter.py
11
counter.py
@ -2,6 +2,11 @@ from machine import Pin
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
from utime import ticks_ms
|
from utime import ticks_ms
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Counter
|
||||||
|
give a basic counter class with a name and value
|
||||||
|
"""
|
||||||
class Counter:
|
class Counter:
|
||||||
def __init__(self, name="C1", value = 0): # init_value may be smaller than 0
|
def __init__(self, name="C1", value = 0): # init_value may be smaller than 0
|
||||||
self.value = int(value)
|
self.value = int(value)
|
||||||
@ -47,16 +52,22 @@ class CounterArray:
|
|||||||
|
|
||||||
|
|
||||||
def add_counter(self, counter: Counter):
|
def add_counter(self, counter: Counter):
|
||||||
|
# Function
|
||||||
|
# add a counter to the counterarray's internal _counter dict
|
||||||
self._counters[len(self._counters)] = counter
|
self._counters[len(self._counters)] = counter
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def remove_counter(self, key):
|
def remove_counter(self, key):
|
||||||
|
# Function
|
||||||
|
# pop a counter out of the counterarrays internal _counter dict with the given key
|
||||||
self._counters.pop(key)
|
self._counters.pop(key)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_counter(self, key):
|
def get_counter(self, key):
|
||||||
|
# Function
|
||||||
|
# get the Counter class object with the given key
|
||||||
return self._counters[key]
|
return self._counters[key]
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,9 +7,9 @@ class CounterScreen:
|
|||||||
self.lcd = lcd
|
self.lcd = lcd
|
||||||
self.lcd.clear()
|
self.lcd.clear()
|
||||||
self.counterArray = counterArray
|
self.counterArray = counterArray
|
||||||
self._zfill = lambda s, length: '{:0>{w}}'.format(s, w=length)
|
self._zfill = lambda s, length: '{:0>{w}}'.format(s, w=length) # function that fills a given string with a given amount of preceding zeroes
|
||||||
self.last_line1 = ""
|
self.last_line1 = "" # used to prevent screen flickering
|
||||||
self.last_line2 = ""
|
self.last_line2 = "" # used to prevent screen flickering
|
||||||
self.too_many_counters_warning_shown = False
|
self.too_many_counters_warning_shown = False
|
||||||
|
|
||||||
def _too_many_counters_warning(self):
|
def _too_many_counters_warning(self):
|
||||||
|
26
micronec.py
26
micronec.py
@ -5,23 +5,27 @@ from counter import CounterArray
|
|||||||
from lcd_screen import CounterScreen
|
from lcd_screen import CounterScreen
|
||||||
|
|
||||||
|
|
||||||
I2C_ADDR = 0x27
|
# some constants, change on need
|
||||||
I2C_NUM_ROWS = 2
|
I2C_ADDR = 0x27 # for pico w, this should fit
|
||||||
I2C_NUM_COLS = 16
|
I2C_NUM_ROWS = 2 # DON'T CHANGE
|
||||||
WELCOME_CYCLES = 1
|
I2C_NUM_COLS = 16 # DON'T CHANGE
|
||||||
COUNTER_NUMBER = 2
|
WELCOME_CYCLES = 1 # adjusts how often the "Starting..." runs through at startup
|
||||||
COUNTER_NAMES = ["Counter 1", "Counter 2"]
|
COUNTER_NUMBER = 2 # how many counters do you want?
|
||||||
COUNTER_PINS = {0:2,1:3}
|
COUNTER_NAMES = ["Counter 1", "Counter 2"] # names of the counters (MUST contain names for all counters)
|
||||||
|
COUNTER_PINS = {0:2,1:3} # which pins to listen on (form {<COUNTER NUMBER STARTING FROM 0>:<GPIO PIN>}) (there MAY be doubles, you MAY not give pins for all counters)
|
||||||
|
|
||||||
|
# initialize the lcd display with GPIO pins 0 and 1 for data on i2c channel 0
|
||||||
_i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
|
_i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
|
||||||
lcd = I2C_LCD(_i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
|
lcd = I2C_LCD(_i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
|
||||||
|
# initialize the counter array (class managing all the counters)
|
||||||
counterArray = CounterArray(COUNTER_NUMBER, COUNTER_NAMES)
|
counterArray = CounterArray(COUNTER_NUMBER, COUNTER_NAMES)
|
||||||
|
# initialize the welcome screen
|
||||||
ws = WelcomeScreen(lcd)
|
ws = WelcomeScreen(lcd)
|
||||||
|
# initialize the counter screen (display counter values on lcd)
|
||||||
cs = CounterScreen(lcd, counterArray)
|
cs = CounterScreen(lcd, counterArray)
|
||||||
|
|
||||||
# Real program
|
# Real program
|
||||||
ws.show_welcome(WELCOME_CYCLES)
|
ws.show_welcome(WELCOME_CYCLES) # show welcome/startup message
|
||||||
cs.show_screen()
|
cs.show_screen() # DON'T REMOVE; show counter screen one time, may take up to 15s
|
||||||
counterArray.register_listener(COUNTER_PINS, cs.show_screen)
|
counterArray.register_listener(COUNTER_PINS, cs.show_screen) # register the listeners on the given pins
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import time
|
import time
|
||||||
|
"""
|
||||||
|
WelcomeScreen:
|
||||||
|
provide a simple welcome/startup screen
|
||||||
|
"""
|
||||||
class WelcomeScreen:
|
class WelcomeScreen:
|
||||||
def __init__(self, lcd):
|
def __init__(self, lcd):
|
||||||
self.lcd = lcd
|
self.lcd = lcd
|
||||||
|
Loading…
Reference in New Issue
Block a user