from machine import Pin from time import sleep from utime import ticks_ms class Counter: def __init__(self, name="C1", value = 0): # init_value may be smaller than 0 self.value = int(value) self.name = str(name) def __str__(self): return str(self.name); def __int__(self): return int(self.value); def __add__(self, x): counter = Counter(value=self.value+int(x)) return counter def __sub__(self, x): counter = Counter(value=self.value-int(x)) return counter def increment(self): self.value += 1 """ CounterArray: provides an easy-to-use interface for the counter class. """ class CounterArray: def __init__(self, counter_number = 1, counter_names = ["Counter 1"]): self._counters = {} if len(counter_names) < counter_number: raise IndexError("counter_names list MUST contain names for all counters.") elif len(counter_names) > counter_number: print("[WARN] [CounterArray] More counter names are given than counters wanted. Ignoring the overflow.") for i in range(counter_number): self._counters[i] = Counter(counter_names[i]) def add_counter(self, counter: Counter): self._counters[len(self._counters)] = counter return True def remove_counter(self, key): self._counters.pop(key) return True def get_counter(self, key): return self._counters[key] def register_listener(self, pins: dict, changed_callback=lambda: None): # Function: # listens for btn presses on specified pins # Parameter: pins # should be a dict of the form {COUNTER_ID:PIN, ...} # where COUNTER_ID is the key of the counter to increment in the # internal _counters dict and PIN is the pin to listen on # Parameter: changed_callback # is called without parameters, every time a button was pressed inputs = {} for counter_id, pin in pins.items(): inputs[counter_id] = Pin(pin, Pin.IN, Pin.PULL_DOWN) self.last_time = 0 def press_handler(pin, inputs, _counters, counterarray, changed_callback): for counter_id, p in inputs.items(): if p == pin: # now we have the pin where the button got pressed new_time = ticks_ms() if (new_time - counterarray.last_time) > 150: _counters[counter_id] += 1 counterarray.last_time = new_time changed_callback() for counter_id, pin in inputs.items(): try: self._counters[counter_id] pin.irq(trigger=Pin.IRQ_FALLING, handler=lambda p: press_handler(p, inputs, self._counters, self, changed_callback)) except KeyError: print("[ERROR] [Counter().register_listener] Error while registering counter-pin-pair (counter_id=" + str(counter_id) + ", pin=" + str(pin) + "). Ignoring this pair.")