if machine.reset_cause() == machine.DEEPSLEEP_RESET: print("I woke from a deep sleep") white_noise_color = 255, 147, 41 pixel = neupixel.create_neupixel(pin=16, num_pixels=3) def light_up(duration): pixel.set_color(*white_noise_color) utime.sleep(duration) pixel.clear() ns = Nightshift(begin=Time(18, 21), end=Time(22, 15)) print("Nightshift(begin={}, end={})".format(ns.begin, ns.end)) print("Localtime: {}".format(utime.localtime())) current_time = localtime2time() if ns.is_at_end(current_time, delta=Time(0, 20)): print("I am at the end of the nighttime... clearing neopixel") pixel.clear() elif ns.is_at_begin(current_time) or ns.is_within(current_time): print("I set neopixel with {}".format(white_noise_color)) # Within night shift, light up pixel.set_color(*white_noise_color) else: print("Clearing neopixel's color") # Otherwise don't show anything.
def localtime2time(): current_time = utime.localtime() return Time(hour=current_time[3], minute=current_time[4])
def setUp(self): self.ns = Nightshift(begin=Time(13, 30), end=Time(15, 45))
def test_cut_off_time(self): sleeptime = self.ns.sleep_time(self.ns.begin, max_sleep=Time(1, 15)) self.assertEqual(75, sleeptime)
def test_sleep_time_for_time_during_nightshift(self): self.assertEqual( 90, self.ns.sleep_time(Time(14, 15), max_sleep=Time(10, 0)))
def test_sleep_time_for_end_time(self): self.assertEqual( 45 + 21 * 60 - 1, self.ns.sleep_time(Time(15, 45), max_sleep=Time(100, 0)))
def test_sleep_time_before_20m(self): self.assertEqual( 20, self.ns.sleep_time(Time(13, 10), max_sleep=Time(10, 0)))
def test_sleep_time_for_start_time(self): self.assertEqual( 135, self.ns.sleep_time(self.ns.begin, max_sleep=Time(10, 0)))
def test_within_yields_false(self): self.assertFalse(self.ns.is_within(Time(15, 55)))
def test_within_yields_true_for_end_time(self): self.assertTrue(self.ns.is_within(Time(15, 45)))
def test_within_yields_true_for_start_time(self): self.assertTrue(self.ns.is_within(Time(13, 30)))
def test_within_before_yields_false(self): self.assertFalse(self.ns.is_within(Time(13, 14)))
def test_within_yields_true(self): self.assertTrue(self.ns.is_within(Time(14, 5)))