Пример #1
0
 def __init__(self, period=500):
     self.state = self.STATE_BUTTON_START
     self.start_time = 0
     self.period = period
     self.click_state = self.STATE_CLICK_START
     self.indicator = Indicator(PinSet(Sonoff.GREEN_LED))
     self.relay = PinSet(Sonoff.RELAY)
     self.smartconfig = SM(self.indicator)
     self.iostate = False
Пример #2
0
class ButtonHandler:
    STATE_BUTTON_START = 0
    STATE_BUTTON_COLLECT = 1

    STATE_CLICK_START = 0
    STATE_CLICK_CONFIRM_CONFIG = 1
    STATE_CLICK_IN_CONFIG = 2

    def __init__(self, period=500):
        self.state = self.STATE_BUTTON_START
        self.start_time = 0
        self.period = period
        self.click_state = self.STATE_CLICK_START
        self.indicator = Indicator(PinSet(Sonoff.GREEN_LED))
        self.relay = PinSet(Sonoff.RELAY)
        self.smartconfig = SM(self.indicator)
        self.iostate = False

    def timer_finish(self):
#        print("Events", self.click_state)
#        for ii in self.events:
#            print(ii)

        zero_count = len([ii for ii in self.events if ii[0] == 0])
        if self.click_state == self.STATE_CLICK_START:
            if zero_count == 2:
                if self.smartconfig.running:
                    self.smartconfig.stop()
                self.indicator.blink(period=100)
                self.click_state = self.STATE_CLICK_CONFIRM_CONFIG
        elif self.click_state == self.STATE_CLICK_CONFIRM_CONFIG:
            if zero_count == 2:
                self.smartconfig.start(self.iostate)
                self.click_state = self.STATE_CLICK_IN_CONFIG
                self.indicator.blink(period=500)
            else:
                self.click_state = self.STATE_CLICK_START
                self.indicator.blink_cancel()
        elif self.click_state == self.STATE_CLICK_IN_CONFIG:
            self.click_state = self.STATE_CLICK_START

        self.state = self.STATE_BUTTON_START

    def handle(self, button_value):
        if button_value == 0:
            if self.iostate is True:
                self.iostate = False
                self.relay.off()
            else:
                self.iostate = True
                self.relay.on()

        if self.state == self.STATE_BUTTON_START:
            self.start_time = pyb.millis()
            self.state = self.STATE_BUTTON_COLLECT
            self.events = list()
            self.timer = esp.os_timer(lambda timer: self.timer_finish(), period=self.period, repeat=False)
            elapsed = 0
        else:
            elapsed = pyb.elapsed_millis(self.start_time)
        self.events.append((button_value, elapsed))