aboutsummaryrefslogtreecommitdiff
path: root/oled.py
blob: 91a68184d5376f3bf5a59900e2e1012afe4031fd (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import board
import busio

from kmk.extensions.lock_status import LockStatus
from kmk.extensions.display import Display, TextEntry, ImageEntry
from kmk.extensions.display.ssd1306 import SSD1306


class NamedLayer(TextEntry):
    """ This is a named layer which return the name of the current mappng as
        text.
    """

    def __init__(self, keyboard, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._text = None
        self.keyboard = keyboard

    @property
    def text(self):
        try:
            return self.keyboard.keymap[0].name
        except _:
            return self._text

    @text.setter
    def text(self, value):
        self._text = value


class StatusLayer(TextEntry):
    """ This layer display a status
    """

    def __init__(self, text, display, prop, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._text = text
        self.display = display
        self.property = prop

    @property
    def text(self):
        """ Return the name of the given property
        """
        if getattr(self.display, self.property):
            return ""
        return f"{self._text}:Off"

    @text.setter
    def text(self, value):
        """ Ignore the text given
        """
        pass


class CustomDisplay(Display):
    """ Create a custom Display object which refresh the display everytime the
        layout is updated
    """
    def __init__(self, *args, **kwargs):
        self.keyboard = None
        super().__init__(*args, **kwargs)
        self.status = LockStatus()
        self._prevLayers = None

        self.numlock = False

    def during_bootup(self, keyboard):
        super().during_bootup(keyboard)
        self.status.during_bootup(keyboard)
        self.keyboard = keyboard

    def after_hid_send(self, sandbox):
        self.status.after_hid_send(sandbox)
        if self.status.report_updated:
            self.numlock = self.status.get_num_lock()
            self.render(sandbox.active_layers[0])

    def before_matrix_scan(self, sandbox):
        """ Override the default function with the layer name as a comparaison
            key
        """
        super().before_matrix_scan(sandbox)
        if self.keyboard.keymap[0].name != self._prevLayers:
            self._prevLayers = self.keyboard.keymap[0].name
            self.render(sandbox.active_layers[0])

    @property
    def timer_start(self):
        if self.keyboard:
            return self.keyboard.keymap[0].timer_start

    @timer_start.setter
    def timer_start(self, value):
        if self.keyboard:
            self.keyboard.keymap[0].timer_start = value


def main(keyboard):

    driver = SSD1306(
        i2c=busio.I2C(board.SCL, board.SDA),
    )

    display = CustomDisplay(
        display=driver,
    )
    display.entries = [
        NamedLayer(keyboard, text="", x=0, y=0),
        StatusLayer(text="Num", display=display, prop="numlock", x=0, y=20),
    ]
    keyboard.extensions.append(display)