summaryrefslogtreecommitdiff
path: root/src/sleep_mode.py
blob: 9c56c247c887af397932f075abfd084d06534da6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# The firmware I’m using does not have the function display_sleep available. So
# I’m sending the raw code directly.
#
# https://docs.circuitpython.org/projects/macropad/en/latest/api.html#adafruit_macropad.MacroPad.display_sleep
#

timeout = 15 * 60 * 1000
from supervisor import ticks_ms
import ticks

class Power(object):

    def __init__(self, macropad):
        self.macropad = macropad
        self.last_update = ticks_ms()
        self.configuration = None
        self.sleep = False
        self.macropad.display.brightness = 0
        self.macropad.display.bus.send(0xAF, b"")

    def start(self):
        self.last_update = ticks_ms()
        self._wake()

    def set_configuration(self, configuration):
        self.configuration = configuration
        self.start()

    def _sleep(self):
        if self.sleep:
            return
        self.sleep = True
        self.macropad.pixels.fill((0, 0, 0))
        self.macropad.pixels.show()
        self.macropad.display.bus.send(0xAE, b"")

    def _wake(self):
        if not self.sleep:
            return
        self.sleep = False
        #self.macropad.display_sleep = False
        for index, value in self.configuration.keys.items():
            if value is None or index >= 12:
                continue
            self.macropad.pixels[index] = value.color
        self.macropad.pixels.show()
        self.macropad.display.bus.send(0xAF, b"")

    def tick(self):
        if ticks.diff(ticks_ms(), self.last_update) > timeout:
            self._sleep()

    def execute_action(self, key, pressed):
        self.start()