aboutsummaryrefslogtreecommitdiff
path: root/oled.py
blob: 4db04364249c006a913fd40db235969a0f7b0722 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import board
import busio

from kmk.extensions.display import Display, TextEntry, ImageEntry
from kmk.extensions.display.ssd1306 import SSD1306
from supervisor import ticks_ms

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 CustomDisplay(Display):
    """ Create a custom Display object which refresh the display everytime the
        layout is updated
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._prevLayers = None

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

    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.timer_start = ticks_ms()
            self._prevLayers = self.keyboard.keymap[0].name
            self.render(sandbox.active_layers[0])
        return



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),
    ]
    keyboard.extensions.append(display)