added oleds and docs
This commit is contained in:
parent
6ef20ee042
commit
caab7a4a15
90
docs/peg_oled_display.md
Normal file
90
docs/peg_oled_display.md
Normal file
@ -0,0 +1,90 @@
|
||||
# Peg Oled Display
|
||||
To use this you need to make some changes to your kb.py as well as you main.py I will break it into two sections for you.
|
||||
|
||||
### What you can and cant do
|
||||
|
||||
#### Can do
|
||||
* display images
|
||||
* display text
|
||||
* set images or text to react to your layer
|
||||
|
||||
|
||||
#### Cant do yet / on the way
|
||||
* react to battery percentage
|
||||
* react to WPM
|
||||
|
||||
## kb.py
|
||||
Your chosen board may already have these changes done If not you will need to add them.
|
||||
|
||||
you need to add SCL and SDA to your keyboard. The pins the oled are connected to on your controller may not be called "SCL"and "SDA" they could be "GP21" and "GP13" for example. The best way to find out what they are is look at the boards pinout.
|
||||
```python
|
||||
SCL=board.SCL
|
||||
SDA=board.SDA
|
||||
```
|
||||
|
||||
|
||||
## Main.py
|
||||
These are the changes that need to be made / added to your main.py
|
||||
### Config
|
||||
no mater how you are going to use the oled you need this part
|
||||
```python
|
||||
from kmk.extensions.peg_oledDisplay import oled
|
||||
keyboard = KMKKeyboard()
|
||||
# ... Other oled code
|
||||
keyboard.extensions.append(oled_ext)
|
||||
|
||||
```
|
||||
### Photos
|
||||
So the config for photos is quite simple. Getting the photos maybe not so much. I will explain.
|
||||
|
||||
oled takes 3-4 arguments
|
||||
1. keyboard
|
||||
* this is where we get the config from
|
||||
2. array of data
|
||||
* this array can have a length of 1 or 4. For images we use a length of 1.
|
||||
* Every item in the array has 2 fields
|
||||
* 0: this is the reaction type right now it can be "LAYER" or "STATIC"
|
||||
* 1: the items you want to show for the reaction. In this example 4 images to switch on the 4 layers
|
||||
3. toDisplay this takes a string "TXT" or "IMG".
|
||||
* this tells the extension to load images or text.
|
||||
4. flip Boolean this will simply flip your display.
|
||||
|
||||
![code layout](https://i.imgur.com/MrRYzo8.png)
|
||||
|
||||
```python
|
||||
oled_ext = oled(keyboard, [{0:"LAYER",1:["1.bmp","2.bmp","3.bmp","4.bmp"]}],toDisplay='IMG',flip=True)
|
||||
```
|
||||
In this code example we are saying to react to the layer change and load a image filed named "1.bmp" for layer one and "2.bmp" for layer two and so on.
|
||||
### Preparing the images
|
||||
So you need to include all the images in your circuitpython drive in the root along side main.py
|
||||
|
||||
Your images need to be 128x32px and should only be black and white. After you have made your image you can save as a "monochrome bmp" this will save you a lot of space.
|
||||
|
||||
### Text
|
||||
Ok now we get into something that looks a lot more complicated but we will get though it.
|
||||
|
||||
Almost everything is the same we are still passing in the keyboard for pin config. We swap toDisplay to "TXT" lets get into that array.
|
||||
|
||||
So each item in the array is a corner of the oled display
|
||||
|
||||
0. top left
|
||||
1. top right
|
||||
2. bottom left
|
||||
3. bottom right
|
||||
After that is the same as the previous example. Each one has two fields 0:the reaction type. 1:what to display
|
||||
|
||||
In this code we will always display the word "layer" in the top left corner of the screen then the other 3 corners will swap depending on the layer.
|
||||
|
||||
![code layout](https://i.imgur.com/wXl8nmZ.png)
|
||||
|
||||
|
||||
```python
|
||||
oled_ext = oled(keyboard,[
|
||||
{0:"STATIC",1:["layer"]},
|
||||
{0:"LAYER",1:["1","2","3","4"]},
|
||||
{0:"LAYER",1:["base","raise","lower","adjust"]},
|
||||
{0:"LAYER",1:["qwerty","nums","shifted","leds"]}
|
||||
],toDisplay='TXT',flip=False)
|
||||
```
|
||||
![example](https://boardsource.imgix.net/a4f155e0-bc83-11ec-a4ed-79d542ba3127.gif)
|
||||
|
110
kmk/extensions/peg_oledDisplay.py
Normal file
110
kmk/extensions/peg_oledDisplay.py
Normal file
@ -0,0 +1,110 @@
|
||||
import busio
|
||||
import displayio
|
||||
import terminalio
|
||||
from adafruit_display_text import label
|
||||
import adafruit_displayio_ssd1306
|
||||
from kmk.extensions import Extension
|
||||
import gc
|
||||
|
||||
class oled(Extension):
|
||||
def __init__(
|
||||
self,
|
||||
board,
|
||||
views,
|
||||
toDisplay: str = "TXT",
|
||||
oWidth: int = 128,
|
||||
oHeight: int = 32,
|
||||
flip:bool = False,
|
||||
|
||||
):
|
||||
displayio.release_displays()
|
||||
self._views=views
|
||||
self._toDisplay = toDisplay
|
||||
i2c = busio.I2C(board.SCL, board.SDA)
|
||||
self.width=oWidth,
|
||||
self.height=oHeight,
|
||||
self.rotation = 180 if flip else 0
|
||||
self._display = adafruit_displayio_ssd1306.SSD1306(displayio.I2CDisplay(i2c, device_address=0x3C),
|
||||
width=oWidth,
|
||||
height=oHeight,rotation=self.rotation)
|
||||
self._prevLayers = 0
|
||||
gc.collect()
|
||||
|
||||
def returnCurrectRenderText(self,layer,singleView):
|
||||
# for now we only have static things and react to layers. But when we react to battery % and wpm we can handle the logic here
|
||||
if singleView[0]=="STATIC":
|
||||
return singleView[1][0]
|
||||
if singleView[0]=="LAYER":
|
||||
return singleView[1][layer]
|
||||
|
||||
|
||||
def renderOledTextLayer(self, layer):
|
||||
splash = displayio.Group()
|
||||
self._display.show(splash)
|
||||
splash.append(
|
||||
label.Label(terminalio.FONT,
|
||||
text=self.returnCurrectRenderText(layer,self._views[0]),
|
||||
color=0xFFFFFF, x=0, y=10))
|
||||
splash.append(
|
||||
label.Label(terminalio.FONT,
|
||||
text=self.returnCurrectRenderText(layer,self._views[1]),
|
||||
color=0xFFFFFF, x=64, y=10))
|
||||
splash.append(
|
||||
label.Label(terminalio.FONT,
|
||||
text=self.returnCurrectRenderText(layer,self._views[2]),
|
||||
color=0xFFFFFF, x=0, y=25))
|
||||
splash.append(
|
||||
label.Label(terminalio.FONT,
|
||||
text=self.returnCurrectRenderText(layer,self._views[3]),
|
||||
color=0xFFFFFF, x=64, y=25))
|
||||
gc.collect()
|
||||
|
||||
def renderOledImgLayer(self, layer):
|
||||
splash = displayio.Group()
|
||||
self._display.show(splash)
|
||||
odb = displayio.OnDiskBitmap('/'+self.returnCurrectRenderText(layer,self._views[0]))
|
||||
image = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader)
|
||||
splash.append(image)
|
||||
gc.collect()
|
||||
|
||||
|
||||
|
||||
def updateOLED(self, sandbox):
|
||||
if self._toDisplay == "TXT":
|
||||
self.renderOledTextLayer(sandbox.active_layers[0])
|
||||
if self._toDisplay == "IMG":
|
||||
self.renderOledImgLayer(sandbox.active_layers[0])
|
||||
gc.collect()
|
||||
|
||||
|
||||
def on_runtime_enable(self, sandbox):
|
||||
return
|
||||
|
||||
def on_runtime_disable(self, sandbox):
|
||||
return
|
||||
|
||||
def during_bootup(self, sandbox):
|
||||
self.renderOledImgLayer(0)
|
||||
return
|
||||
|
||||
def before_matrix_scan(self, sandbox):
|
||||
if sandbox.active_layers[0] != self._prevLayers:
|
||||
self._prevLayers = sandbox.active_layers[0]
|
||||
self.updateOLED(sandbox)
|
||||
return
|
||||
|
||||
def after_matrix_scan(self, sandbox):
|
||||
|
||||
return
|
||||
|
||||
def before_hid_send(self, sandbox):
|
||||
return
|
||||
|
||||
def after_hid_send(self, sandbox):
|
||||
return
|
||||
|
||||
def on_powersave_enable(self, sandbox):
|
||||
return
|
||||
|
||||
def on_powersave_disable(self, sandbox):
|
||||
return
|
Loading…
x
Reference in New Issue
Block a user