def _handler(self): self.beeptime -= 1 if self.beeptime % 2 == 0: self.buzzer.value(0) else: self.buzzer.value(1) if self.beeptime == 0: return AddTask(mode='once', function=self._handler, time=self.speed)
def push_handler(self): if self.button.value() == 0: pushed = len(ButtonHistory) // 2 for i in range(len(ButtonTaskList[2])): if ButtonTaskList[2][i][0] == pushed: try: ButtonTaskList[2][i][1]() except: pass ButtonHistory.clear() else: AddTask(name='button' + str(self.p), period=500)
def event(type, time, function): if type == 'press': self.ButtonTaskList[0] = (time, function) elif type == 'release': self.ButtonTaskList[1] = (time, function) elif type == 'push': self.ButtonTaskList[2].append((time, function)) self.ButtonTaskList[2].sort() AddTask(name='Button', function=self.push_handler, mode='once', period=999999999) elif type == 'hold': self.ButtonTaskList[3].append((time * 1000, function)) self.ButtonTaskList[3].sort()
def _handler(self, source): state = self.button.value() now = time() if state != self.last_state: self.last_state = state self.ButtonHistory.append((time(), state)) if self.ButtonTaskList[0]: if state == 1: try: self.ButtonTaskList[0][1]() except: pass if self.ButtonTaskList[1]: if state == 0: try: self.ButtonTaskList[1][1]() except: pass if len(self.ButtonTaskList[3]): if len(self.ButtonHistory ) > 1 and self.ButtonHistory[-2][1] == 1: timegap = now - self.ButtonHistory[-2][0] if timegap > 1000: min = 9999999 target = 0 for i in range(len(self.ButtonTaskList[3])): if abs(timegap - self.ButtonTaskList[3][i][0]) < min: target = i min = abs(timegap - self.ButtonTaskList[3][i][0]) if min < 3000: # min is tolerance try: self.ButtonTaskList[3][target][1]() self.ButtonHistory.clear() except: pass DeleteTask('button' + str(self.p)) AddTask(name='button' + str(self.p), period=1000, mode='once', function=self.push_handler)
def __init__(self, port, uart_baudrate=115200): self.last = None self.status = False self.message = "" pin = getPin(port) self.pin = pin[0] self.cb = None try: from machine import UART self.ser = UART(1, tx=pin[0], rx=pin[1], baudrate=115200) self.status = True self.begin() self.SAM_configuration() AddTask(name='rfid' + str(self.pin), mode='repeat', period=500, function=self._handler) except Exception: self.status = False