import machine rtc = machine.RTC() class State: def __init__(self): self._auto_mode = False self._day_on = False self._day_on_time = [6, 30] self._day_off_time = [19, 0] rtc.datetime((2018, 10, 15, 1, 13, 11, 0, 0)) #(year, month, day, weekday, hours, minutes, seconds, subseconds) self._current_time = rtc.datetime() self._current_time = (2018, 20, 15, 1, 13, 11, 0, 0) self._night_on = False self._day_pins = [ machine.Pin("LED", machine.Pin.OUT) ] self._night_pins = [] for pin in self._day_pins + self._night_pins: pin.off() def is_day_on(self): return self._day_on def is_night_on(self): return self._night_on def is_auto_mode(self): return self._auto_mode def set_auto_mode(self, is_set): self._auto_mode = is_set def toggle_day(self): print("=> Toggle day") for pin in self._day_pins: pin.toggle() self._day_on = not self._day_on def toggle_night(self): print("=> Toggle night") for pin in self._night_pins: pin.toggle() self._night_on = not self._night_on def set_time(self, h, m, s): rtc.datetime((2018, 10, 15, 1, h, m, s, 0)) def set_times(self, is_day, h, m): if is_day: self._day_on_time = [h, m] else: self._day_off_time = [h, m] def switch_to_day(self, is_day): if is_day: if not self._day_on: self.toggle_day() if self._night_on: self.toggle_night() else: if self._day_on: self.toggle_day() if not self._night_on: self.toggle_night() def get_time_h(self): return self._current_time[4] def get_time_m(self): return self._current_time[5] def get_time_s(self): return self._current_time[6] def get_day_on_time(self): return self._day_on_time def get_day_off_time(self): return self._day_off_time def update(self): self._current_time = rtc.datetime() if self._auto_mode: if self.is_day_time(): self.switch_to_day(True) else: self.switch_to_day(False) else: pass def is_day_time(self): c_h = self._current_time[4] c_m = self._current_time[5] day_h = self._day_on_time[0] day_m = self._day_on_time[1] night_h = self._day_off_time[0] night_m = self._day_off_time[1] return day_h < c_h < night_h or (day_h == c_h and day_m <= c_m) or (night_h == c_h and c_m < night_m) state = State()