summaryrefslogtreecommitdiff
path: root/src/sleep_mode.py
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2023-08-13 10:45:56 +0200
committerSébastien Dailly <sebastien@dailly.me>2023-08-13 10:46:58 +0200
commit6716fbc7a1ec5d14887d54b7d0286a149d15d46a (patch)
tree4b79c10ce838142ae9ac41222fd73d99aeba5228 /src/sleep_mode.py
parent7357e1f9671cbaef34fc934ce52b78f25c74f34e (diff)
Added a sleep function in the keyboard
Diffstat (limited to 'src/sleep_mode.py')
-rw-r--r--src/sleep_mode.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/sleep_mode.py b/src/sleep_mode.py
new file mode 100644
index 0000000..9c56c24
--- /dev/null
+++ b/src/sleep_mode.py
@@ -0,0 +1,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()