Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of vialrgb #47

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7063b9b
implemented kbexpander
PercyJW-2 Jun 17, 2024
ff6ae1c
stuff
PercyJW-2 Jul 10, 2024
efb31c4
Merge branch 'sago35:main' into main
PercyJW-2 Jul 11, 2024
1f4c656
updated stuff
PercyJW-2 Jul 11, 2024
28d016d
more stuff
PercyJW-2 Jul 11, 2024
665cbb1
Merge branch 'main' of github.com:PercyJW-2/tinygo-keyboard
PercyJW-2 Jul 11, 2024
c84952c
Merge branch 'sago35:main' into main
PercyJW-2 Jul 25, 2024
34cba97
updated module
PercyJW-2 Jul 25, 2024
e195de3
Fixed kbexpander and reduced debounce time
PercyJW-2 Jul 28, 2024
059d332
Removed own keyboard
PercyJW-2 Jul 28, 2024
40757fc
Removed debug log prints
PercyJW-2 Jul 28, 2024
e222842
Added explicitly ignoring possible errors
PercyJW-2 Jul 28, 2024
2020ea4
Started to implement vialrgb
PercyJW-2 Jul 29, 2024
60417d3
Continued implementation, need to finish writing part
PercyJW-2 Jul 30, 2024
a8bdcd2
Finished untested vialrgb communication
PercyJW-2 Aug 1, 2024
e3941c8
Finished untested vialrgb implementation with a few animations
PercyJW-2 Aug 2, 2024
49a8c32
Made struct elements public
PercyJW-2 Aug 2, 2024
1bf0544
Fixed RGB communication
PercyJW-2 Aug 7, 2024
30bfea4
Finished RGB implementation
PercyJW-2 Aug 13, 2024
6704554
Fixed saving and loading rgb settings
PercyJW-2 Aug 15, 2024
40d471d
Implemented new rgb animation
PercyJW-2 Aug 15, 2024
567a997
Merge branch 'sago35:main' into main
PercyJW-2 Aug 15, 2024
9f797ef
Implemented gradient rgb effect
PercyJW-2 Aug 15, 2024
02dff8c
implemented Breathing effect, TODO: rework animation speed
PercyJW-2 Aug 16, 2024
d920ff4
adding comment to rgb.go to pass ci
PercyJW-2 Aug 18, 2024
094577e
reworkted animation speed for breathing animation
PercyJW-2 Oct 7, 2024
0a7da42
Merge branch 'sago35:main' into main
PercyJW-2 Oct 7, 2024
8f1eb30
Merge remote-tracking branch 'origin/main'
PercyJW-2 Oct 7, 2024
5c16bf8
implemented 4/6 Band Animations
PercyJW-2 Oct 7, 2024
f874931
implemented 6/6 Band Animations
PercyJW-2 Oct 7, 2024
ef7aafb
implemented All Cycle Animations
PercyJW-2 Oct 7, 2024
39bb188
implemented All Beacon Animations
PercyJW-2 Oct 7, 2024
381c069
implemented All Animations that are no rain or reactive effects
PercyJW-2 Oct 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
**/production/*.csv
**/production/*.ipc
**/production/
*.elf
*.uf2
.idea
.vscode
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/sago35/tinygo-keyboard
module github.com/percyjw-2/tinygo-keyboard

go 1.19

require (
github.com/itchio/lzma v0.0.0-20190703113020-d3e24e3e3d49
github.com/sago35/tinygo-keyboard v0.0.0-20240718132338-1f0852885672
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b
tinygo.org/x/drivers v0.27.0
tinygo.org/x/tinydraw v0.3.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
github.com/bgould/http v0.0.0-20190627042742-d268792bdee7/go.mod h1:BTqvVegvwifopl4KTEDth6Zezs9eR+lCWhvGKvkxJHE=
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
github.com/frankban/quicktest v1.10.2 h1:19ARM85nVi4xH7xPXuc5eM/udya5ieh7b/Sv+d844Tk=
github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/itchio/lzma v0.0.0-20190703113020-d3e24e3e3d49 h1:+YrBMf3rkLjkT10zIHyVE4S7ma4hqvfjl6XgnzZwS6o=
github.com/itchio/lzma v0.0.0-20190703113020-d3e24e3e3d49/go.mod h1:avNrevQMli1pYPsz1+HIHMvx95pk6O+6otbWqCZPeZI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/sago35/tinygo-keyboard v0.0.0-20240718132338-1f0852885672 h1:mczTPYfh9yjoa6GO967sNW+6LhFjHn2TrTSwW9Wr108=
github.com/sago35/tinygo-keyboard v0.0.0-20240718132338-1f0852885672/go.mod h1:jFhDDtRipnSbvhoqlGrR2M7IInnjcMUzuSWznF6obqM=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
Expand Down
2 changes: 1 addition & 1 deletion kbmatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (d *Device) AddMatrixKeyboard(colPins, rowPins []machine.Pin, keys [][]Keyc
options: o,
callback: func(layer, index int, state State) {},
cycleCounter: cycleCnt,
debounce: 8,
debounce: 2,
}

d.kb = append(d.kb, k)
Expand Down
18 changes: 15 additions & 3 deletions keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Device struct {
flashCh chan bool
flashCnt int

rgbMat *RGBMatrix

kb []KBer

layer int
Expand Down Expand Up @@ -100,7 +102,8 @@ func (d *Device) Init() error {
keys := d.GetMaxKeyCount()

// TODO: refactor
rbuf := make([]byte, 4+layers*keyboards*keys*2+len(device.Macros))
rgbStorageSize := 2 + 1 + 3 // currentEffect + speed + HSV
rbuf := make([]byte, 4+layers*keyboards*keys*2+len(device.Macros)+rgbStorageSize)
_, err := machine.Flash.ReadAt(rbuf, 0)
if err != nil {
return err
Expand All @@ -123,6 +126,12 @@ func (d *Device) Init() error {
}
}

modeID := uint16(rbuf[offset]) | (uint16(rbuf[offset+1]) >> 8)
device.SetCurrentRGBMode(modeID)
device.SetCurrentSpeed(rbuf[offset+2])
device.SetCurrentHSV(rbuf[offset+3], rbuf[offset+4], rbuf[offset+5])
offset += 6

for i, b := range rbuf[offset:] {
if b == 0xFF {
b = 0
Expand All @@ -131,6 +140,9 @@ func (d *Device) Init() error {
}
//copy(device.Macros[:], rbuf[offset:])

// Start RGB task
go d.updateRGBTask()

return nil
}

Expand Down Expand Up @@ -380,7 +392,7 @@ func (d *Device) Key(layer, kbIndex, index int) Keycode {
}

func (d *Device) KeyVia(layer, kbIndex, index int) Keycode {
//fmt.Printf(" KeyVia(%d, %d, %d)\n", layer, kbIndex, index)
//fmt.Printf(" KeyVia(%d, %d, %d)\n", layer, KbIndex, index)
if kbIndex >= len(d.kb) {
return 0
}
Expand Down Expand Up @@ -438,7 +450,7 @@ func (d *Device) SetKeycodeVia(layer, kbIndex, index int, key Keycode) {
if kbIndex >= len(d.kb) {
return
}
//fmt.Printf("SetKeycodeVia(%d, %d, %d, %04X)\n", layer, kbIndex, index, key)
//fmt.Printf("SetKeycodeVia(%d, %d, %d, %04X)\n", layer, KbIndex, index, key)
kc := keycodeViaToTGK(key)

d.kb[kbIndex].SetKeycode(layer, index, kc)
Expand Down
267 changes: 267 additions & 0 deletions rgb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
package keyboard
PercyJW-2 marked this conversation as resolved.
Show resolved Hide resolved

import (
"image/color"
"time"
"tinygo.org/x/drivers/ws2812"
)

type RGBMatrix struct {
maximumBrightness uint8
ledCount uint16
LedMatrixMapping []LedMatrixPosition
implementedEffects []RgbAnimation
currentEffect RgbAnimation
CurrentSpeed uint8
CurrentHue uint8
CurrentSaturation uint8
CurrentValue uint8
LedMatrixDirectVals []LedMatrixDirectModeColor
LedMatrixVals []color.RGBA
ledDriver *ws2812.Device
}

type LedMatrixPosition struct {
PhysicalX uint8
PhysicalY uint8
KbIndex uint8
MatrixIndex uint8
LedFlags uint8
}

type LedMatrixDirectModeColor struct {
H uint8
S uint8
V uint8
}

type RgbAnimationFunc func(matrix *RGBMatrix)

type RgbAnimation struct {
AnimationFunc RgbAnimationFunc
AnimationType uint16
}

const (
// LED Flags
LED_FLAG_NONE uint8 = 0x00 // If this LED has no flags
LED_FLAG_ALL uint8 = 0xFF // if this LED has all flags
LED_FLAG_MODIFIER uint8 = 0x01 // if the Key for this LED is a modifier
LED_FLAG_UNDERGLOW uint8 = 0x02 // if the LED is for underglow
LED_FLAG_KEYLIGHT uint8 = 0x04 // if the LED is for key backlight
)

const (
// RGB Modes
VIALRGB_EFFECT_OFF = iota
VIALRGB_EFFECT_DIRECT
VIALRGB_EFFECT_SOLID_COLOR
VIALRGB_EFFECT_ALPHAS_MODS
VIALRGB_EFFECT_GRADIENT_UP_DOWN
VIALRGB_EFFECT_GRADIENT_LEFT_RIGHT
VIALRGB_EFFECT_BREATHING
VIALRGB_EFFECT_BAND_SAT
VIALRGB_EFFECT_BAND_VAL
VIALRGB_EFFECT_BAND_PINWHEEL_SAT
VIALRGB_EFFECT_BAND_PINWHEEL_VAL
VIALRGB_EFFECT_BAND_SPIRAL_SAT
VIALRGB_EFFECT_BAND_SPIRAL_VAL
VIALRGB_EFFECT_CYCLE_ALL
VIALRGB_EFFECT_CYCLE_LEFT_RIGHT
VIALRGB_EFFECT_CYCLE_UP_DOWN
VIALRGB_EFFECT_RAINBOW_MOVING_CHEVRON
VIALRGB_EFFECT_CYCLE_OUT_IN
VIALRGB_EFFECT_CYCLE_OUT_IN_DUAL
VIALRGB_EFFECT_CYCLE_PINWHEEL
VIALRGB_EFFECT_CYCLE_SPIRAL
VIALRGB_EFFECT_DUAL_BEACON
VIALRGB_EFFECT_RAINBOW_BEACON
VIALRGB_EFFECT_RAINBOW_PINWHEELS
VIALRGB_EFFECT_RAINDROPS
VIALRGB_EFFECT_JELLYBEAN_RAINDROPS
VIALRGB_EFFECT_HUE_BREATHING
VIALRGB_EFFECT_HUE_PENDULUM
VIALRGB_EFFECT_HUE_WAVE
VIALRGB_EFFECT_TYPING_HEATMAP
VIALRGB_EFFECT_DIGITAL_RAIN
VIALRGB_EFFECT_SOLID_REACTIVE_SIMPLE
VIALRGB_EFFECT_SOLID_REACTIVE
VIALRGB_EFFECT_SOLID_REACTIVE_WIDE
VIALRGB_EFFECT_SOLID_REACTIVE_MULTIWIDE
VIALRGB_EFFECT_SOLID_REACTIVE_CROSS
VIALRGB_EFFECT_SOLID_REACTIVE_MULTICROSS
VIALRGB_EFFECT_SOLID_REACTIVE_NEXUS
VIALRGB_EFFECT_SOLID_REACTIVE_MULTINEXUS
VIALRGB_EFFECT_SPLASH
VIALRGB_EFFECT_MULTISPLASH
VIALRGB_EFFECT_SOLID_SPLASH
VIALRGB_EFFECT_SOLID_MULTISPLASH
VIALRGB_EFFECT_PIXEL_RAIN
VIALRGB_EFFECT_PIXEL_FRACTAL
)

func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMapping []LedMatrixPosition, animations []RgbAnimation, ledDriver *ws2812.Device) {
if int(ledCount) != len(ledMatrixMapping) {
panic("LedMatrixMapping must have length equal to number of LedMatrixMapping")
}
effectOffAnimation := RgbAnimation{
AnimationFunc: func(matrix *RGBMatrix) {
matrix.ledDriver.WriteColors(matrix.LedMatrixVals)
},
AnimationType: VIALRGB_EFFECT_OFF,
}
rgbMatrix := RGBMatrix{
maximumBrightness: brightness,
ledCount: ledCount,
LedMatrixMapping: ledMatrixMapping,
implementedEffects: []RgbAnimation{
effectOffAnimation,
},
currentEffect: effectOffAnimation,
CurrentSpeed: 0x00,
CurrentHue: 0xFF,
CurrentSaturation: 0xFF,
CurrentValue: brightness,
LedMatrixVals: make([]color.RGBA, ledCount),
ledDriver: ledDriver,
}
rgbMatrix.implementedEffects = append(rgbMatrix.implementedEffects, animations...)
for _, animation := range animations {
if animation.AnimationType == VIALRGB_EFFECT_DIRECT {
rgbMatrix.LedMatrixDirectVals = make([]LedMatrixDirectModeColor, ledCount)
}
}
d.rgbMat = &rgbMatrix
}

func (d *Device) GetRGBMatrixMaximumBrightness() uint8 {
if !d.IsRGBMatrixEnabled() {
return 0
}
return d.rgbMat.maximumBrightness
}

func (d *Device) GetRGBMatrixLEDCount() uint16 {
if !d.IsRGBMatrixEnabled() {
return 0
}
return d.rgbMat.ledCount
}

func (d *Device) GetRGBMatrixLEDMapping(ledIndex uint16) LedMatrixPosition {
invalidPosition := LedMatrixPosition{
KbIndex: 0xFF,
MatrixIndex: 0xFF,
}
if !d.IsRGBMatrixEnabled() || ledIndex >= d.rgbMat.ledCount {
return invalidPosition
}
return d.rgbMat.LedMatrixMapping[ledIndex]
}

func (d *Device) GetSupportedRGBModes() []RgbAnimation {
if !d.IsRGBMatrixEnabled() {
return []RgbAnimation{}
}
return d.rgbMat.implementedEffects
}

func (d *Device) GetCurrentRGBMode() uint16 {
if !d.IsRGBMatrixEnabled() {
return 0
}
return d.rgbMat.currentEffect.AnimationType
}

func (d *Device) SetCurrentRGBMode(mode uint16) {
if !d.IsRGBMatrixEnabled() {
return
}
for _, e := range d.rgbMat.implementedEffects {
if e.AnimationType == mode {
d.rgbMat.currentEffect = e
}
}
}

func (d *Device) GetCurrentSpeed() uint8 {
if !d.IsRGBMatrixEnabled() {
return 0
}
return d.rgbMat.CurrentSpeed
}

func (d *Device) SetCurrentSpeed(speed uint8) {
if !d.IsRGBMatrixEnabled() {
return
}
d.rgbMat.CurrentSpeed = speed
}

func (d *Device) GetCurrentHue() uint8 {
if !d.IsRGBMatrixEnabled() {
return 0
}
return d.rgbMat.CurrentHue
}

func (d *Device) GetCurrentSaturation() uint8 {
if !d.IsRGBMatrixEnabled() {
return 0
}
return d.rgbMat.CurrentSaturation
}

func (d *Device) GetCurrentValue() uint8 {
if !d.IsRGBMatrixEnabled() {
return 0
}
return d.rgbMat.CurrentValue
}

func (d *Device) SetCurrentHSV(hue uint8, saturation uint8, value uint8) {
if !d.IsRGBMatrixEnabled() {
return
}
d.rgbMat.CurrentHue = hue
d.rgbMat.CurrentSaturation = saturation
d.rgbMat.CurrentValue = value
}

func (d *Device) SetDirectHSV(hue uint8, saturation uint8, value uint8, ledIndex uint16) {
if !d.IsDirectModeEnabled() {
return
}
rgb := d.rgbMat
var actualValue uint8
if value > rgb.maximumBrightness {
actualValue = rgb.maximumBrightness
} else {
actualValue = value
}
rgb.LedMatrixDirectVals[ledIndex] = LedMatrixDirectModeColor{
H: hue,
S: saturation,
V: actualValue,
}
}

func (d *Device) updateRGBTask() {
if !d.IsRGBMatrixEnabled() {
return
}
rgb := d.rgbMat
for {
time.Sleep(time.Millisecond * time.Duration(0x100-uint16(rgb.CurrentSpeed)))
rgb.currentEffect.AnimationFunc(rgb)
_ = rgb.ledDriver.WriteColors(rgb.LedMatrixVals)
}
}

func (d *Device) IsRGBMatrixEnabled() bool {
return d.rgbMat != nil
}

func (d *Device) IsDirectModeEnabled() bool {
return d.IsRGBMatrixEnabled() && d.rgbMat.LedMatrixDirectVals != nil
}
Loading
Loading