From 7063b9bd2872d3eea76c9acab8e4380e87352d40 Mon Sep 17 00:00:00 2001 From: Joris Wachsmuth Date: Mon, 17 Jun 2024 18:27:10 +0200 Subject: [PATCH 01/27] implemented kbexpander --- kbexpander.go | 154 +++++++++++++++++++++++++++ targets/popcorne/def.go | 9 ++ targets/popcorne/main.go | 151 +++++++++++++++++++++++++++ targets/popcorne/vial.json | 207 +++++++++++++++++++++++++++++++++++++ 4 files changed, 521 insertions(+) create mode 100644 kbexpander.go create mode 100644 targets/popcorne/def.go create mode 100644 targets/popcorne/main.go create mode 100644 targets/popcorne/vial.json diff --git a/kbexpander.go b/kbexpander.go new file mode 100644 index 0000000..64ac9fd --- /dev/null +++ b/kbexpander.go @@ -0,0 +1,154 @@ +//go:build tinygo + +package keyboard + +import ( + "tinygo.org/x/drivers/mcp23017" +) + +type ExpanderKeyboard struct { + State []State + Keys [][]Keycode + options Options + callback Callback + + Col []mcp23017.Pin + Row []mcp23017.Pin + cycleCounter []uint8 + debounce uint8 + expander *mcp23017.Device +} + +func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, rowPins []mcp23017.Pin, keys [][]Keycode, opt ...Option) *ExpanderKeyboard { + col := len(colPins) + row := len(rowPins) + state := make([]State, row*col) + cycleCnt := make([]uint8, len(state)) + + for c := range colPins { + colPins[c].SetMode(mcp23017.Output) + } + for r := range rowPins { + rowPins[r].SetMode(mcp23017.Output) + } + + o := Options{} + for _, f := range opt { + f(&o) + } + + keydef := make([][]Keycode, LayerCount) + for l := 0; l < len(keydef); l++ { + keydef[l] = make([]Keycode, len(state)) + } + for l := 0; l < len(keys); l++ { + for kc := 0; kc < len(keys[l]); kc++ { + keydef[l][kc] = keys[l][kc] + } + } + + k := &ExpanderKeyboard{ + Col: colPins, + Row: rowPins, + State: state, + Keys: keydef, + options: o, + callback: func(layer, index int, state State) {}, + cycleCounter: cycleCnt, + debounce: 8, + expander: expanderDevice, + } + d.kb = append(d.kb, k) + return k +} + +func (d *ExpanderKeyboard) SetCallback(fn Callback) { + d.callback = fn +} + +func (d *ExpanderKeyboard) Callback(layer, index int, state State) { + if d.callback != nil { + d.callback(layer, index, state) + } +} + +func (d *ExpanderKeyboard) Get() []State { + for c := range d.Col { + for r := range d.Row { + current := false + if !d.options.InvertDiode { + d.Col[c].SetMode(mcp23017.Output) + d.Col[c].High() + current, _ = d.Row[r].Get() + } else { + d.Row[r].SetMode(mcp23017.Output) + d.Row[r].High() + current, _ = d.Col[c].Get() + } + idx := r*len(d.Col) + c + switch d.State[idx] { + case None: + if current { + if d.cycleCounter[idx] >= d.debounce { + d.State[idx] = NoneToPress + d.cycleCounter[idx] = 0 + } else { + d.cycleCounter[idx]++ + } + } else { + d.cycleCounter[idx] = 0 + } + case NoneToPress: + d.State[idx] = Press + case Press: + if current { + d.cycleCounter[idx] = 0 + } else { + if d.cycleCounter[idx] >= d.debounce { + d.State[idx] = PressToRelease + d.cycleCounter[idx] = 0 + } else { + d.cycleCounter[idx]++ + } + } + case PressToRelease: + d.State[idx] = None + } + if !d.options.InvertDiode { + d.Col[c].Low() + } else { + d.Row[r].Low() + } + } + } + + return d.State +} + +func (d *ExpanderKeyboard) Key(layer, index int) Keycode { + if layer >= LayerCount { + return 0 + } + if index >= len(d.Keys[layer]) { + return 0 + } + return d.Keys[layer][index] +} + +func (d *ExpanderKeyboard) SetKeycode(layer, index int, key Keycode) { + if layer >= LayerCount { + return + } + if index >= len(d.Keys[layer]) { + return + } + d.Keys[layer][index] = key +} + +func (d *ExpanderKeyboard) GetKeyCount() int { + return len(d.State) +} + +func (d *ExpanderKeyboard) Init() error { + return nil +} diff --git a/targets/popcorne/def.go b/targets/popcorne/def.go new file mode 100644 index 0000000..1ea58ec --- /dev/null +++ b/targets/popcorne/def.go @@ -0,0 +1,9 @@ +package main + +import keyboard "github.com/sago35/tinygo-keyboard" + +func loadKeyboardDef() { + keyboard.KeyboardDef = []byte{ + 0x5D, 0x00, 0x00, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x3D, 0x82, 0x80, 0x17, 0x1C, 0x2D, 0x8D, 0xC7, 0xC2, 0xB8, 0xA5, 0xA8, 0x07, 0x51, 0x3E, 0xB7, 0x22, 0xE7, 0x32, 0x09, 0xB7, 0x37, 0xD6, 0x00, 0x10, 0xE9, 0xA0, 0x6B, 0x77, 0xA9, 0x02, 0x4D, 0x98, 0x19, 0x84, 0x42, 0xC8, 0xAC, 0x8B, 0xA3, 0x9B, 0xE5, 0x85, 0xD3, 0x03, 0x2D, 0x5E, 0x1E, 0xB4, 0xD1, 0x52, 0x87, 0x7D, 0xED, 0xEE, 0xD0, 0xD4, 0xEE, 0x2F, 0x01, 0x25, 0xE8, 0xD9, 0xDF, 0x18, 0x6C, 0x2D, 0x49, 0x52, 0xF6, 0xA7, 0x27, 0xB1, 0xFC, 0x8C, 0x2F, 0x02, 0x24, 0x42, 0x3F, 0x53, 0x59, 0xB0, 0x9C, 0x7E, 0xEF, 0xAA, 0xE4, 0x4C, 0x95, 0x00, 0x8D, 0x2F, 0x5F, 0xFE, 0xF3, 0x87, 0x2A, 0x9B, 0xE6, 0x29, 0xDE, 0xFE, 0x9C, 0xED, 0x68, 0x44, 0x9D, 0x09, 0x3F, 0x84, 0x4E, 0xBA, 0x78, 0x32, 0xF2, 0x36, 0xF4, 0x19, 0xBE, 0x07, 0xFD, 0xAE, 0x33, 0x72, 0x13, 0x49, 0x69, 0xE5, 0x04, 0xC7, 0x9B, 0x40, 0x4A, 0x55, 0xA7, 0x08, 0xC1, 0xC2, 0xF1, 0x6B, 0xF4, 0x4C, 0xA1, 0xED, 0x3E, 0xCB, 0xDF, 0x85, 0xD7, 0xCB, 0x34, 0xAC, 0x4B, 0x13, 0x78, 0x88, 0x07, 0xA9, 0x6B, 0xAF, 0xEA, 0xF6, 0x28, 0x9F, 0x90, 0x9E, 0xEC, 0x69, 0xCF, 0x7D, 0xAC, 0x97, 0x21, 0xF4, 0x0E, 0x76, 0x9D, 0xAD, 0x56, 0x77, 0x01, 0xF1, 0x55, 0x1E, 0x7D, 0x04, 0x79, 0x8C, 0x94, 0x97, 0xFE, 0x9A, 0x97, 0xA6, 0x23, 0x37, 0x8A, 0x44, 0x52, 0x6E, 0x0E, 0x6B, 0x7B, 0x3D, 0xDE, 0x05, 0x30, 0xC8, 0x9C, 0x3E, 0xB9, 0xB7, 0xDB, 0x01, 0xB9, 0x8F, 0x5A, 0x85, 0x26, 0x94, 0x3C, 0x0B, 0x21, 0x47, 0xAE, 0x6C, 0x21, 0xF8, 0x21, 0x3A, 0x2D, 0x6D, 0xB5, 0x55, 0x84, 0xC6, 0xD0, 0xD1, 0xDF, 0x64, 0xD4, 0x08, 0xEE, 0xC4, 0x32, 0xCF, 0x25, 0xB7, 0x04, 0x57, 0x23, 0xFB, 0xED, 0xBB, 0xC5, 0xD2, 0x7D, 0xDA, 0x00, 0xD0, 0xEC, 0x51, 0x99, 0xE5, 0x2A, 0x01, 0x64, 0xED, 0x11, 0x0B, 0x0E, 0xC5, 0x86, 0x66, 0x7A, 0x8C, 0x62, 0x1D, 0xBE, 0x71, 0x15, 0xED, 0x63, 0xA0, 0x3D, 0xCC, 0xB4, 0x18, 0xFA, 0x46, 0x06, 0xD2, 0x5C, 0x70, 0x45, 0x6B, 0x00, 0x85, 0xD3, 0x03, 0xBF, 0x64, 0x70, 0x98, 0x20, 0x42, 0x6B, 0x00, 0xFF, 0xCF, 0x31, 0x2E, 0xBA, 0xFD, 0x73, 0xBC, 0x48, + } +} diff --git a/targets/popcorne/main.go b/targets/popcorne/main.go new file mode 100644 index 0000000..686ed74 --- /dev/null +++ b/targets/popcorne/main.go @@ -0,0 +1,151 @@ +package main + +import ( + "context" + _ "embed" + "fmt" + "image/color" + "log" + "machine" + "machine/usb" + + "tinygo.org/x/drivers/mcp23017" + + keyboard "github.com/sago35/tinygo-keyboard" + "github.com/sago35/tinygo-keyboard/keycodes/jp" + "tinygo.org/x/drivers/ssd1306" + "tinygo.org/x/tinydraw" +) + +func main() { + usb.Product = "popcorne" + + err := run() + if err != nil { + log.Fatal(err) + } +} + +type RCS struct { + row, col int + state keyboard.State +} + +func run() error { + machine.I2C0.Configure(machine.I2CConfig{ + Frequency: 400 * machine.KHz, + SDA: machine.P1_13, + SCL: machine.P0_29, + }) + + ch := make(chan RCS, 16) + + display := ssd1306.NewI2C(machine.I2C0) + display.Configure(ssd1306.Config{ + Width: 128, + Height: 32, + }) + display.ClearDisplay() + + expander, _ := mcp23017.NewI2C(machine.I2C0, 0) // TODO actual address + + d := keyboard.New() + + colPinsLeft := []machine.Pin{ + machine.P0_13, + machine.P0_14, + machine.P0_15, + machine.P0_16, + machine.P0_24, + machine.P0_25, + } + + rowPinsLeft := []machine.Pin{ + machine.P0_03, + machine.P0_28, + machine.P0_02, + machine.P0_31, + } + + colPinsRight := []mcp23017.Pin{} + + rowPinsRight := []mcp23017.Pin{} + + mkLeft := d.AddMatrixKeyboard(colPinsLeft, rowPinsLeft, [][]keyboard.Keycode{ + { + jp.KeyTab, jp.KeyQ, jp.KeyW, jp.KeyE, jp.KeyR, jp.KeyT, + jp.KeyLeftCtrl, jp.KeyA, jp.KeyS, jp.KeyD, jp.KeyF, jp.KeyG, + jp.KeyLeftShift, jp.KeyY, jp.KeyX, jp.KeyC, jp.KeyV, jp.KeyB, + 0, jp.KeyMod1, jp.KeyMod2, jp.KeyWindows, jp.KeySpace, 0, + }, + { + jp.KeyEsc, jp.Key1, jp.Key2, jp.Key3, jp.Key4, jp.Key5, + 0, 0, jp.KeyUp, 0, 0, 0, + 0, jp.KeyLeft, jp.KeyDown, jp.KeyRight, 0, 0, + 0, 0, 0, 0, 0, 0, + }, + { + jp.KeyF1, jp.KeyF2, jp.KeyF3, jp.KeyF4, jp.KeyF5, jp.KeyF6, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + }, + }, keyboard.InvertDiode(true)) + mkLeft.SetCallback(func(layer, index int, state keyboard.State) { + row := index / len(colPinsLeft) + col := index % len(colPinsLeft) + fmt.Printf("mkLeft: %d %d %d %d\n", layer, row, col, state) + select { + case ch <- RCS{row: row, col: col, state: state}: + } + }) + + mkRight := d.AddExpanderKeyboard(expander, colPinsRight, rowPinsRight, [][]keyboard.Keycode{ + { + jp.KeyZ, jp.KeyU, jp.KeyI, jp.KeyO, jp.KeyP, jp.KeyMinus, + jp.KeyH, jp.KeyJ, jp.KeyK, jp.KeyL, jp.KeyColon, jp.KeySemicolon, + jp.KeyN, jp.KeyM, jp.KeyComma, jp.KeyPeriod, jp.KeySlash, jp.KeyRightShift, + jp.KeyEnter, jp.KeyMod3, jp.KeyBackspace, jp.KeyDelete, 0, 0, + }, + { + jp.Key6, jp.Key7, jp.Key8, jp.Key9, jp.Key0, jp.KeyHat, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + }, + { + jp.KeyF7, jp.KeyF8, jp.KeyF9, jp.KeyF10, jp.KeyF11, jp.KeyF12, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + }, + }, keyboard.InvertDiode(true)) + mkRight.SetCallback(func(layer, index int, state keyboard.State) { + row := index/len(colPinsRight) + len(rowPinsLeft) + col := index%len(colPinsRight) + len(colPinsLeft) + fmt.Printf("mkRight: %d %d %d %d\n", layer, row, col, state) + select { + case ch <- RCS{row: row, col: col, state: state}: + } + }) + + go func() { + for { + select { + case x := <-ch: + c := color.RGBA{R: 255, G: 255, B: 255, A: 255} + if x.state == keyboard.PressToRelease { + c = color.RGBA{A: 255} + } + tinydraw.FilledRectangle(&display, 10+20*int16(x.col), 10+20*int16(x.row), 18, 18, c) + display.Display() + } + } + }() + + // for Vial + loadKeyboardDef() + + d.Debug = true + return d.Loop(context.Background()) +} diff --git a/targets/popcorne/vial.json b/targets/popcorne/vial.json new file mode 100644 index 0000000..14947f1 --- /dev/null +++ b/targets/popcorne/vial.json @@ -0,0 +1,207 @@ +{ + "lighting": "vialrgb", + "matrix": { + "rows": 4, + "cols": 11 + }, + "layouts": { + "keymap": [ + [ + { + "x": 3 + }, + "0,3", + { + "x": 6.5 + }, + "0,8" + ], + [ + { + "y": -0.75, + "x": 2 + }, + "0,2", + { + "x": 1 + }, + "0,4", + { + "x": 4.5 + }, + "0,7", + { + "x": 1 + }, + "0,9" + ], + [ + { + "y": -0.75 + }, + "0,0", + "0,1", + { + "x": 3 + }, + "0,5", + { + "x": 2.5 + }, + "0,6", + { + "x": 3 + }, + "0,10", + "0,11" + ], + [ + { + "y": -0.5, + "x": 3 + }, + "1,3", + { + "x": 6.5 + }, + "1,8" + ], + [ + { + "y": -0.75, + "x": 2 + }, + "1,2", + { + "x": 1 + }, + "1,4", + { + "x": 4.5 + }, + "1,7", + { + "x": 1 + }, + "1,9" + ], + [ + { + "y": -0.75 + }, + "1,0", + "1,1", + { + "x": 3 + }, + "1,5", + { + "x": 2.5 + }, + "1,6", + { + "x": 3 + }, + "1,10", + "1,11" + ], + [ + { + "y": -0.5, + "x": 3 + }, + "2,3", + { + "x": 6.5 + }, + "2,8" + ], + [ + { + "y": -0.7999999999999998, + "x": 6 + }, + "3,5" + ], + [ + { + "y": -0.9500000000000002, + "x": 2 + }, + "2,2", + { + "x": 1 + }, + "2,4", + { + "x": 4.5 + }, + "2,7", + { + "x": 1 + }, + "2,9" + ], + [ + { + "y": -0.75 + }, + "2,0", + "2,1", + { + "x": 3 + }, + "2,5", + { + "x": 2.5 + }, + "2,6", + { + "x": 3 + }, + "2,10", + "2,11" + ], + [ + { + "y": -0.25, + "x": 2.75 + }, + "3,1", + "3,2", + { + "x": 5 + }, + "3,8", + "3,9" + ], + [ + { + "y": -0.75, + "x": 4.75 + }, + "3,3", + { + "x": 3 + }, + "3,7" + ], + [ + { + "r": 15, + "y": -3, + "x": 6.75 + }, + "3,4" + ], + [ + { + "r": -15, + "y": 2.75, + "x": 6.25 + }, + "3,6" + ] + ] + } +} \ No newline at end of file From ff6ae1c1c31a1a35ab5eba8594781c479df1b7d9 Mon Sep 17 00:00:00 2001 From: jwachsmuth Date: Wed, 10 Jul 2024 18:34:21 +0200 Subject: [PATCH 02/27] stuff --- kbexpander.go | 39 ++++++++++++++++++++++++--------------- targets/popcorne/main.go | 4 ++-- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/kbexpander.go b/kbexpander.go index 64ac9fd..71e15ab 100644 --- a/kbexpander.go +++ b/kbexpander.go @@ -25,18 +25,29 @@ func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, r state := make([]State, row*col) cycleCnt := make([]uint8, len(state)) - for c := range colPins { - colPins[c].SetMode(mcp23017.Output) - } - for r := range rowPins { - rowPins[r].SetMode(mcp23017.Output) - } - o := Options{} for _, f := range opt { f(&o) } + for c := range colPins { + if !o.InvertDiode { + colPins[c].SetMode(mcp23017.Output) + colPins[c].Set(true) + } else { + colPins[c].SetMode(mcp23017.Input | mcp23017.Pullup) + } + + } + for r := range rowPins { + if !o.InvertDiode { + rowPins[r].SetMode(mcp23017.Input | mcp23017.Pullup) + } else { + rowPins[r].SetMode(mcp23017.Output) + rowPins[r].Set(true) + } + } + keydef := make([][]Keycode, LayerCount) for l := 0; l < len(keydef); l++ { keydef[l] = make([]Keycode, len(state)) @@ -77,18 +88,16 @@ func (d *ExpanderKeyboard) Get() []State { for r := range d.Row { current := false if !d.options.InvertDiode { - d.Col[c].SetMode(mcp23017.Output) - d.Col[c].High() + d.Col[c].Set(false) current, _ = d.Row[r].Get() } else { - d.Row[r].SetMode(mcp23017.Output) - d.Row[r].High() + d.Row[r].Set(false) current, _ = d.Col[c].Get() } idx := r*len(d.Col) + c switch d.State[idx] { case None: - if current { + if !current { if d.cycleCounter[idx] >= d.debounce { d.State[idx] = NoneToPress d.cycleCounter[idx] = 0 @@ -101,7 +110,7 @@ func (d *ExpanderKeyboard) Get() []State { case NoneToPress: d.State[idx] = Press case Press: - if current { + if !current { d.cycleCounter[idx] = 0 } else { if d.cycleCounter[idx] >= d.debounce { @@ -115,9 +124,9 @@ func (d *ExpanderKeyboard) Get() []State { d.State[idx] = None } if !d.options.InvertDiode { - d.Col[c].Low() + d.Col[c].Set(true) } else { - d.Row[r].Low() + d.Row[r].Set(true) } } } diff --git a/targets/popcorne/main.go b/targets/popcorne/main.go index 686ed74..8a67f72 100644 --- a/targets/popcorne/main.go +++ b/targets/popcorne/main.go @@ -67,9 +67,9 @@ func run() error { machine.P0_31, } - colPinsRight := []mcp23017.Pin{} + var colPinsRight []mcp23017.Pin - rowPinsRight := []mcp23017.Pin{} + var rowPinsRight []mcp23017.Pin mkLeft := d.AddMatrixKeyboard(colPinsLeft, rowPinsLeft, [][]keyboard.Keycode{ { From 1f4c656580f4d104e8067ba56a992674158c2a2f Mon Sep 17 00:00:00 2001 From: Joris Wachsmuth Date: Thu, 11 Jul 2024 13:37:26 +0200 Subject: [PATCH 03/27] updated stuff --- .gitignore | 3 +++ targets/popcorne/main.go | 20 +++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 88cae3c..3a9c2c5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ **/production/*.csv **/production/*.ipc **/production/ +*.elf +*.uf2 +.idea diff --git a/targets/popcorne/main.go b/targets/popcorne/main.go index 8a67f72..7ecc65b 100644 --- a/targets/popcorne/main.go +++ b/targets/popcorne/main.go @@ -17,21 +17,15 @@ import ( "tinygo.org/x/tinydraw" ) -func main() { - usb.Product = "popcorne" - - err := run() - if err != nil { - log.Fatal(err) - } -} - type RCS struct { row, col int state keyboard.State } -func run() error { +func main() { + usb.Product = "popcorne" + + machine.I2C0.Configure(machine.I2CConfig{ Frequency: 400 * machine.KHz, SDA: machine.P1_13, @@ -147,5 +141,9 @@ func run() error { loadKeyboardDef() d.Debug = true - return d.Loop(context.Background()) + err := d.Loop(context.Background()) + + if err != nil { + log.Fatal(err) + } } From 28d016d531bca78b2513f35d6baf4471667571dc Mon Sep 17 00:00:00 2001 From: Joris Wachsmuth Date: Thu, 11 Jul 2024 13:38:04 +0200 Subject: [PATCH 04/27] more stuff --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3a9c2c5..88237ca 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.elf *.uf2 .idea +.vscode From 34cba97d03fb83220275de75472aa0ec2db692be Mon Sep 17 00:00:00 2001 From: jwachsmuth Date: Thu, 25 Jul 2024 16:18:20 +0200 Subject: [PATCH 05/27] updated module --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 36ad461..a9fcbea 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/sago35/tinygo-keyboard +module github.com/percyjw-2/tinygo-keyboard go 1.19 From e195de3e7adf9b8cc59bfc27495fffa04e936388 Mon Sep 17 00:00:00 2001 From: jwachsmuth Date: Sun, 28 Jul 2024 13:26:55 +0200 Subject: [PATCH 06/27] Fixed kbexpander and reduced debounce time --- kbexpander.go | 51 ++++++++++++++++++++++++++++++--------------------- kbmatrix.go | 2 +- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/kbexpander.go b/kbexpander.go index 71e15ab..8c9eba5 100644 --- a/kbexpander.go +++ b/kbexpander.go @@ -3,6 +3,7 @@ package keyboard import ( + "fmt" "tinygo.org/x/drivers/mcp23017" ) @@ -12,41 +13,48 @@ type ExpanderKeyboard struct { options Options callback Callback - Col []mcp23017.Pin - Row []mcp23017.Pin + Col []int + Row []int cycleCounter []uint8 debounce uint8 expander *mcp23017.Device } -func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, rowPins []mcp23017.Pin, keys [][]Keycode, opt ...Option) *ExpanderKeyboard { +func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, rowPins []int, keys [][]Keycode, opt ...Option) *ExpanderKeyboard { + fmt.Printf("Initializing Expander\n") + fmt.Printf("Expander %d\n", expanderDevice) col := len(colPins) row := len(rowPins) state := make([]State, row*col) cycleCnt := make([]uint8, len(state)) + fmt.Printf("Initializing Options\n") o := Options{} for _, f := range opt { f(&o) } + fmt.Printf("Initialized Options\n") + + for _, c := range colPins { + fmt.Printf("Col: %d\n", c) - for c := range colPins { if !o.InvertDiode { - colPins[c].SetMode(mcp23017.Output) - colPins[c].Set(true) + expanderDevice.Pin(c).SetMode(mcp23017.Output) + expanderDevice.Pin(c).Set(true) } else { - colPins[c].SetMode(mcp23017.Input | mcp23017.Pullup) + expanderDevice.Pin(c).SetMode(mcp23017.Input | mcp23017.Pullup) } } - for r := range rowPins { + for _, r := range rowPins { if !o.InvertDiode { - rowPins[r].SetMode(mcp23017.Input | mcp23017.Pullup) + expanderDevice.Pin(r).SetMode(mcp23017.Input | mcp23017.Pullup) } else { - rowPins[r].SetMode(mcp23017.Output) - rowPins[r].Set(true) + expanderDevice.Pin(r).SetMode(mcp23017.Output) + expanderDevice.Pin(r).Set(true) } } + fmt.Printf("Initialized Expander Pins\n") keydef := make([][]Keycode, LayerCount) for l := 0; l < len(keydef); l++ { @@ -66,9 +74,10 @@ func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, r options: o, callback: func(layer, index int, state State) {}, cycleCounter: cycleCnt, - debounce: 8, + debounce: 2, expander: expanderDevice, } + fmt.Printf("Initialized ExpanderKeyboard\n") d.kb = append(d.kb, k) return k } @@ -84,17 +93,17 @@ func (d *ExpanderKeyboard) Callback(layer, index int, state State) { } func (d *ExpanderKeyboard) Get() []State { - for c := range d.Col { - for r := range d.Row { + for cIdx, c := range d.Col { + for rIdx, r := range d.Row { current := false if !d.options.InvertDiode { - d.Col[c].Set(false) - current, _ = d.Row[r].Get() + d.expander.Pin(c).Set(false) + current, _ = d.expander.Pin(r).Get() } else { - d.Row[r].Set(false) - current, _ = d.Col[c].Get() + d.expander.Pin(r).Set(false) + current, _ = d.expander.Pin(c).Get() } - idx := r*len(d.Col) + c + idx := rIdx*len(d.Col) + cIdx switch d.State[idx] { case None: if !current { @@ -124,9 +133,9 @@ func (d *ExpanderKeyboard) Get() []State { d.State[idx] = None } if !d.options.InvertDiode { - d.Col[c].Set(true) + d.expander.Pin(c).Set(true) } else { - d.Row[r].Set(true) + d.expander.Pin(r).Set(true) } } } diff --git a/kbmatrix.go b/kbmatrix.go index a10be33..51f69c2 100644 --- a/kbmatrix.go +++ b/kbmatrix.go @@ -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) From 059d332459faa1c5820a2bd2e77ebd6f5c7c16cb Mon Sep 17 00:00:00 2001 From: jwachsmuth Date: Sun, 28 Jul 2024 13:30:23 +0200 Subject: [PATCH 07/27] Removed own keyboard --- targets/popcorne/def.go | 9 -- targets/popcorne/main.go | 149 -------------------------- targets/popcorne/vial.json | 207 ------------------------------------- 3 files changed, 365 deletions(-) delete mode 100644 targets/popcorne/def.go delete mode 100644 targets/popcorne/main.go delete mode 100644 targets/popcorne/vial.json diff --git a/targets/popcorne/def.go b/targets/popcorne/def.go deleted file mode 100644 index 1ea58ec..0000000 --- a/targets/popcorne/def.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import keyboard "github.com/sago35/tinygo-keyboard" - -func loadKeyboardDef() { - keyboard.KeyboardDef = []byte{ - 0x5D, 0x00, 0x00, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x3D, 0x82, 0x80, 0x17, 0x1C, 0x2D, 0x8D, 0xC7, 0xC2, 0xB8, 0xA5, 0xA8, 0x07, 0x51, 0x3E, 0xB7, 0x22, 0xE7, 0x32, 0x09, 0xB7, 0x37, 0xD6, 0x00, 0x10, 0xE9, 0xA0, 0x6B, 0x77, 0xA9, 0x02, 0x4D, 0x98, 0x19, 0x84, 0x42, 0xC8, 0xAC, 0x8B, 0xA3, 0x9B, 0xE5, 0x85, 0xD3, 0x03, 0x2D, 0x5E, 0x1E, 0xB4, 0xD1, 0x52, 0x87, 0x7D, 0xED, 0xEE, 0xD0, 0xD4, 0xEE, 0x2F, 0x01, 0x25, 0xE8, 0xD9, 0xDF, 0x18, 0x6C, 0x2D, 0x49, 0x52, 0xF6, 0xA7, 0x27, 0xB1, 0xFC, 0x8C, 0x2F, 0x02, 0x24, 0x42, 0x3F, 0x53, 0x59, 0xB0, 0x9C, 0x7E, 0xEF, 0xAA, 0xE4, 0x4C, 0x95, 0x00, 0x8D, 0x2F, 0x5F, 0xFE, 0xF3, 0x87, 0x2A, 0x9B, 0xE6, 0x29, 0xDE, 0xFE, 0x9C, 0xED, 0x68, 0x44, 0x9D, 0x09, 0x3F, 0x84, 0x4E, 0xBA, 0x78, 0x32, 0xF2, 0x36, 0xF4, 0x19, 0xBE, 0x07, 0xFD, 0xAE, 0x33, 0x72, 0x13, 0x49, 0x69, 0xE5, 0x04, 0xC7, 0x9B, 0x40, 0x4A, 0x55, 0xA7, 0x08, 0xC1, 0xC2, 0xF1, 0x6B, 0xF4, 0x4C, 0xA1, 0xED, 0x3E, 0xCB, 0xDF, 0x85, 0xD7, 0xCB, 0x34, 0xAC, 0x4B, 0x13, 0x78, 0x88, 0x07, 0xA9, 0x6B, 0xAF, 0xEA, 0xF6, 0x28, 0x9F, 0x90, 0x9E, 0xEC, 0x69, 0xCF, 0x7D, 0xAC, 0x97, 0x21, 0xF4, 0x0E, 0x76, 0x9D, 0xAD, 0x56, 0x77, 0x01, 0xF1, 0x55, 0x1E, 0x7D, 0x04, 0x79, 0x8C, 0x94, 0x97, 0xFE, 0x9A, 0x97, 0xA6, 0x23, 0x37, 0x8A, 0x44, 0x52, 0x6E, 0x0E, 0x6B, 0x7B, 0x3D, 0xDE, 0x05, 0x30, 0xC8, 0x9C, 0x3E, 0xB9, 0xB7, 0xDB, 0x01, 0xB9, 0x8F, 0x5A, 0x85, 0x26, 0x94, 0x3C, 0x0B, 0x21, 0x47, 0xAE, 0x6C, 0x21, 0xF8, 0x21, 0x3A, 0x2D, 0x6D, 0xB5, 0x55, 0x84, 0xC6, 0xD0, 0xD1, 0xDF, 0x64, 0xD4, 0x08, 0xEE, 0xC4, 0x32, 0xCF, 0x25, 0xB7, 0x04, 0x57, 0x23, 0xFB, 0xED, 0xBB, 0xC5, 0xD2, 0x7D, 0xDA, 0x00, 0xD0, 0xEC, 0x51, 0x99, 0xE5, 0x2A, 0x01, 0x64, 0xED, 0x11, 0x0B, 0x0E, 0xC5, 0x86, 0x66, 0x7A, 0x8C, 0x62, 0x1D, 0xBE, 0x71, 0x15, 0xED, 0x63, 0xA0, 0x3D, 0xCC, 0xB4, 0x18, 0xFA, 0x46, 0x06, 0xD2, 0x5C, 0x70, 0x45, 0x6B, 0x00, 0x85, 0xD3, 0x03, 0xBF, 0x64, 0x70, 0x98, 0x20, 0x42, 0x6B, 0x00, 0xFF, 0xCF, 0x31, 0x2E, 0xBA, 0xFD, 0x73, 0xBC, 0x48, - } -} diff --git a/targets/popcorne/main.go b/targets/popcorne/main.go deleted file mode 100644 index 7ecc65b..0000000 --- a/targets/popcorne/main.go +++ /dev/null @@ -1,149 +0,0 @@ -package main - -import ( - "context" - _ "embed" - "fmt" - "image/color" - "log" - "machine" - "machine/usb" - - "tinygo.org/x/drivers/mcp23017" - - keyboard "github.com/sago35/tinygo-keyboard" - "github.com/sago35/tinygo-keyboard/keycodes/jp" - "tinygo.org/x/drivers/ssd1306" - "tinygo.org/x/tinydraw" -) - -type RCS struct { - row, col int - state keyboard.State -} - -func main() { - usb.Product = "popcorne" - - - machine.I2C0.Configure(machine.I2CConfig{ - Frequency: 400 * machine.KHz, - SDA: machine.P1_13, - SCL: machine.P0_29, - }) - - ch := make(chan RCS, 16) - - display := ssd1306.NewI2C(machine.I2C0) - display.Configure(ssd1306.Config{ - Width: 128, - Height: 32, - }) - display.ClearDisplay() - - expander, _ := mcp23017.NewI2C(machine.I2C0, 0) // TODO actual address - - d := keyboard.New() - - colPinsLeft := []machine.Pin{ - machine.P0_13, - machine.P0_14, - machine.P0_15, - machine.P0_16, - machine.P0_24, - machine.P0_25, - } - - rowPinsLeft := []machine.Pin{ - machine.P0_03, - machine.P0_28, - machine.P0_02, - machine.P0_31, - } - - var colPinsRight []mcp23017.Pin - - var rowPinsRight []mcp23017.Pin - - mkLeft := d.AddMatrixKeyboard(colPinsLeft, rowPinsLeft, [][]keyboard.Keycode{ - { - jp.KeyTab, jp.KeyQ, jp.KeyW, jp.KeyE, jp.KeyR, jp.KeyT, - jp.KeyLeftCtrl, jp.KeyA, jp.KeyS, jp.KeyD, jp.KeyF, jp.KeyG, - jp.KeyLeftShift, jp.KeyY, jp.KeyX, jp.KeyC, jp.KeyV, jp.KeyB, - 0, jp.KeyMod1, jp.KeyMod2, jp.KeyWindows, jp.KeySpace, 0, - }, - { - jp.KeyEsc, jp.Key1, jp.Key2, jp.Key3, jp.Key4, jp.Key5, - 0, 0, jp.KeyUp, 0, 0, 0, - 0, jp.KeyLeft, jp.KeyDown, jp.KeyRight, 0, 0, - 0, 0, 0, 0, 0, 0, - }, - { - jp.KeyF1, jp.KeyF2, jp.KeyF3, jp.KeyF4, jp.KeyF5, jp.KeyF6, - 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, - }, - }, keyboard.InvertDiode(true)) - mkLeft.SetCallback(func(layer, index int, state keyboard.State) { - row := index / len(colPinsLeft) - col := index % len(colPinsLeft) - fmt.Printf("mkLeft: %d %d %d %d\n", layer, row, col, state) - select { - case ch <- RCS{row: row, col: col, state: state}: - } - }) - - mkRight := d.AddExpanderKeyboard(expander, colPinsRight, rowPinsRight, [][]keyboard.Keycode{ - { - jp.KeyZ, jp.KeyU, jp.KeyI, jp.KeyO, jp.KeyP, jp.KeyMinus, - jp.KeyH, jp.KeyJ, jp.KeyK, jp.KeyL, jp.KeyColon, jp.KeySemicolon, - jp.KeyN, jp.KeyM, jp.KeyComma, jp.KeyPeriod, jp.KeySlash, jp.KeyRightShift, - jp.KeyEnter, jp.KeyMod3, jp.KeyBackspace, jp.KeyDelete, 0, 0, - }, - { - jp.Key6, jp.Key7, jp.Key8, jp.Key9, jp.Key0, jp.KeyHat, - 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, - }, - { - jp.KeyF7, jp.KeyF8, jp.KeyF9, jp.KeyF10, jp.KeyF11, jp.KeyF12, - 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, - }, - }, keyboard.InvertDiode(true)) - mkRight.SetCallback(func(layer, index int, state keyboard.State) { - row := index/len(colPinsRight) + len(rowPinsLeft) - col := index%len(colPinsRight) + len(colPinsLeft) - fmt.Printf("mkRight: %d %d %d %d\n", layer, row, col, state) - select { - case ch <- RCS{row: row, col: col, state: state}: - } - }) - - go func() { - for { - select { - case x := <-ch: - c := color.RGBA{R: 255, G: 255, B: 255, A: 255} - if x.state == keyboard.PressToRelease { - c = color.RGBA{A: 255} - } - tinydraw.FilledRectangle(&display, 10+20*int16(x.col), 10+20*int16(x.row), 18, 18, c) - display.Display() - } - } - }() - - // for Vial - loadKeyboardDef() - - d.Debug = true - err := d.Loop(context.Background()) - - if err != nil { - log.Fatal(err) - } -} diff --git a/targets/popcorne/vial.json b/targets/popcorne/vial.json deleted file mode 100644 index 14947f1..0000000 --- a/targets/popcorne/vial.json +++ /dev/null @@ -1,207 +0,0 @@ -{ - "lighting": "vialrgb", - "matrix": { - "rows": 4, - "cols": 11 - }, - "layouts": { - "keymap": [ - [ - { - "x": 3 - }, - "0,3", - { - "x": 6.5 - }, - "0,8" - ], - [ - { - "y": -0.75, - "x": 2 - }, - "0,2", - { - "x": 1 - }, - "0,4", - { - "x": 4.5 - }, - "0,7", - { - "x": 1 - }, - "0,9" - ], - [ - { - "y": -0.75 - }, - "0,0", - "0,1", - { - "x": 3 - }, - "0,5", - { - "x": 2.5 - }, - "0,6", - { - "x": 3 - }, - "0,10", - "0,11" - ], - [ - { - "y": -0.5, - "x": 3 - }, - "1,3", - { - "x": 6.5 - }, - "1,8" - ], - [ - { - "y": -0.75, - "x": 2 - }, - "1,2", - { - "x": 1 - }, - "1,4", - { - "x": 4.5 - }, - "1,7", - { - "x": 1 - }, - "1,9" - ], - [ - { - "y": -0.75 - }, - "1,0", - "1,1", - { - "x": 3 - }, - "1,5", - { - "x": 2.5 - }, - "1,6", - { - "x": 3 - }, - "1,10", - "1,11" - ], - [ - { - "y": -0.5, - "x": 3 - }, - "2,3", - { - "x": 6.5 - }, - "2,8" - ], - [ - { - "y": -0.7999999999999998, - "x": 6 - }, - "3,5" - ], - [ - { - "y": -0.9500000000000002, - "x": 2 - }, - "2,2", - { - "x": 1 - }, - "2,4", - { - "x": 4.5 - }, - "2,7", - { - "x": 1 - }, - "2,9" - ], - [ - { - "y": -0.75 - }, - "2,0", - "2,1", - { - "x": 3 - }, - "2,5", - { - "x": 2.5 - }, - "2,6", - { - "x": 3 - }, - "2,10", - "2,11" - ], - [ - { - "y": -0.25, - "x": 2.75 - }, - "3,1", - "3,2", - { - "x": 5 - }, - "3,8", - "3,9" - ], - [ - { - "y": -0.75, - "x": 4.75 - }, - "3,3", - { - "x": 3 - }, - "3,7" - ], - [ - { - "r": 15, - "y": -3, - "x": 6.75 - }, - "3,4" - ], - [ - { - "r": -15, - "y": 2.75, - "x": 6.25 - }, - "3,6" - ] - ] - } -} \ No newline at end of file From 40757fc8337cdb8825e8f2c776f2e3faf0f72f08 Mon Sep 17 00:00:00 2001 From: jwachsmuth Date: Sun, 28 Jul 2024 14:56:26 +0200 Subject: [PATCH 08/27] Removed debug log prints --- kbexpander.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/kbexpander.go b/kbexpander.go index 8c9eba5..10b6dda 100644 --- a/kbexpander.go +++ b/kbexpander.go @@ -3,7 +3,6 @@ package keyboard import ( - "fmt" "tinygo.org/x/drivers/mcp23017" ) @@ -21,22 +20,17 @@ type ExpanderKeyboard struct { } func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, rowPins []int, keys [][]Keycode, opt ...Option) *ExpanderKeyboard { - fmt.Printf("Initializing Expander\n") - fmt.Printf("Expander %d\n", expanderDevice) col := len(colPins) row := len(rowPins) state := make([]State, row*col) cycleCnt := make([]uint8, len(state)) - fmt.Printf("Initializing Options\n") o := Options{} for _, f := range opt { f(&o) } - fmt.Printf("Initialized Options\n") for _, c := range colPins { - fmt.Printf("Col: %d\n", c) if !o.InvertDiode { expanderDevice.Pin(c).SetMode(mcp23017.Output) @@ -54,7 +48,6 @@ func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, r expanderDevice.Pin(r).Set(true) } } - fmt.Printf("Initialized Expander Pins\n") keydef := make([][]Keycode, LayerCount) for l := 0; l < len(keydef); l++ { @@ -77,7 +70,6 @@ func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, r debounce: 2, expander: expanderDevice, } - fmt.Printf("Initialized ExpanderKeyboard\n") d.kb = append(d.kb, k) return k } From e2228426d2cc1590921909518adcc34086501702 Mon Sep 17 00:00:00 2001 From: jwachsmuth Date: Sun, 28 Jul 2024 14:58:06 +0200 Subject: [PATCH 09/27] Added explicitly ignoring possible errors --- kbexpander.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/kbexpander.go b/kbexpander.go index 10b6dda..da854a1 100644 --- a/kbexpander.go +++ b/kbexpander.go @@ -33,19 +33,19 @@ func (d *Device) AddExpanderKeyboard(expanderDevice *mcp23017.Device, colPins, r for _, c := range colPins { if !o.InvertDiode { - expanderDevice.Pin(c).SetMode(mcp23017.Output) - expanderDevice.Pin(c).Set(true) + _ = expanderDevice.Pin(c).SetMode(mcp23017.Output) + _ = expanderDevice.Pin(c).Set(true) } else { - expanderDevice.Pin(c).SetMode(mcp23017.Input | mcp23017.Pullup) + _ = expanderDevice.Pin(c).SetMode(mcp23017.Input | mcp23017.Pullup) } } for _, r := range rowPins { if !o.InvertDiode { - expanderDevice.Pin(r).SetMode(mcp23017.Input | mcp23017.Pullup) + _ = expanderDevice.Pin(r).SetMode(mcp23017.Input | mcp23017.Pullup) } else { - expanderDevice.Pin(r).SetMode(mcp23017.Output) - expanderDevice.Pin(r).Set(true) + _ = expanderDevice.Pin(r).SetMode(mcp23017.Output) + _ = expanderDevice.Pin(r).Set(true) } } @@ -89,10 +89,10 @@ func (d *ExpanderKeyboard) Get() []State { for rIdx, r := range d.Row { current := false if !d.options.InvertDiode { - d.expander.Pin(c).Set(false) + _ = d.expander.Pin(c).Set(false) current, _ = d.expander.Pin(r).Get() } else { - d.expander.Pin(r).Set(false) + _ = d.expander.Pin(r).Set(false) current, _ = d.expander.Pin(c).Get() } idx := rIdx*len(d.Col) + cIdx @@ -125,9 +125,9 @@ func (d *ExpanderKeyboard) Get() []State { d.State[idx] = None } if !d.options.InvertDiode { - d.expander.Pin(c).Set(true) + _ = d.expander.Pin(c).Set(true) } else { - d.expander.Pin(r).Set(true) + _ = d.expander.Pin(r).Set(true) } } } From 2020ea478bf8cd0b73a640a27d6e7843d35fea8a Mon Sep 17 00:00:00 2001 From: jwachsmuth Date: Mon, 29 Jul 2024 17:48:07 +0200 Subject: [PATCH 10/27] Started to implement vialrgb currently working on usb-get part --- go.mod | 1 + go.sum | 6 +++++ keyboard.go | 2 ++ rgb.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ via.go | 43 +++++++++++++++++++++++++++++++++-- 5 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 rgb.go diff --git a/go.mod b/go.mod index a9fcbea..b84058a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ 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 diff --git a/go.sum b/go.sum index dace614..61dbb1c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/keyboard.go b/keyboard.go index 93c85b2..f8158b6 100644 --- a/keyboard.go +++ b/keyboard.go @@ -24,6 +24,8 @@ type Device struct { flashCh chan bool flashCnt int + rgbMat []RGBMatrix + kb []KBer layer int diff --git a/rgb.go b/rgb.go new file mode 100644 index 0000000..ff8400b --- /dev/null +++ b/rgb.go @@ -0,0 +1,65 @@ +package keyboard + +type RGBMatrix struct { + maximumBrightness uint8 + ledCount uint16 + ledMatrixMapping []LedMatrixPosition +} + +type LedMatrixPosition struct { + physicalX uint8 + physicalY uint8 + kbIndex uint8 + matrixIndex uint8 + ledFlags uint8 +} + +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 +) + +func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMapping []LedMatrixPosition) { + if int(ledCount) != len(ledMatrixMapping) { + panic("ledMatrixMapping must have length equal to number of ledMatrixMapping") + } + rgbMatrix := RGBMatrix{ + maximumBrightness: brightness, + ledCount: ledCount, + ledMatrixMapping: ledMatrixMapping, + } + d.rgbMat = append(d.rgbMat, rgbMatrix) +} + +func (d *Device) GetRGBMatrixMaximumBrightness() uint8 { + if !d.IsRGBMatrixEnabled() { + return 0 + } + return d.rgbMat[0].maximumBrightness +} + +func (d *Device) GetRGBMatrixLEDCount() uint16 { + if !d.IsRGBMatrixEnabled() { + return 0 + } + return d.rgbMat[1].ledCount +} + +func (d *Device) GetRGBMatrixLEDMapping(ledIndex uint16) LedMatrixPosition { + invalidPosition := LedMatrixPosition{ + kbIndex: 0xFF, + matrixIndex: 0xFF, + } + if !d.IsRGBMatrixEnabled() || ledIndex >= d.rgbMat[0].ledCount { + return invalidPosition + } + return d.rgbMat[0].ledMatrixMapping[ledIndex] +} + +func (d *Device) IsRGBMatrixEnabled() bool { + return len(d.rgbMat) > 0 +} diff --git a/via.go b/via.go index 85ffbdc..bba02cd 100644 --- a/via.go +++ b/via.go @@ -195,10 +195,46 @@ func rxHandler2(b []byte) bool { device.SetKeycodeVia(int(b[1]), int(b[2]), int(b[3]), Keycode((uint16(b[4])<<8)+uint16(b[5]))) device.flashCh <- true //Changed = true + case 0x07: + // id_lighting_set_value + if device.IsRGBMatrixEnabled() { + + } case 0x08: // id_lighting_get_value - txb[1] = 0x00 - txb[2] = 0x00 + if device.IsRGBMatrixEnabled() { + switch b[1] { + case 0x40: + // vialrgb_get_info + const vialRgbProtocolVersion = 1 + txb[0] = vialRgbProtocolVersion & 0xFF + txb[1] = vialRgbProtocolVersion >> 8 + txb[2] = device.GetRGBMatrixMaximumBrightness() + case 0x41: + // vialrgb_get_mode + case 0x42: + // vialrgb_get_supported + case 0x43: + // vialrgb_get_number_leds + txb[0] = byte(device.GetRGBMatrixLEDCount() & 0xFF) + txb[1] = byte(device.GetRGBMatrixLEDCount() >> 8) + case 0x44: + //vialrgb_get_led_info + ledPos := uint16(b[2]&0xFF) | (uint16(b[3]) >> 8) + position := device.GetRGBMatrixLEDMapping(ledPos) + // physical position + txb[0] = position.physicalX + txb[1] = position.physicalY + // flags + txb[2] = position.ledFlags + // matrix position + txb[3] = position.kbIndex + txb[4] = position.matrixIndex + } + } else { + txb[1] = 0x00 + txb[2] = 0x00 + } case 0xFE: // vial switch b[1] { case 0x00: @@ -216,6 +252,9 @@ func rxHandler2(b []byte) bool { txb[9] = 0xF3 txb[10] = 0x54 txb[11] = 0xE2 + if device.IsRGBMatrixEnabled() { + txb[12] = 1 + } case 0x01: // Retrieve keyboard definition size size := len(KeyboardDef) From 60417d32569629e99ac9bf6b3e023754acb9ed40 Mon Sep 17 00:00:00 2001 From: "github@mail.joris-wachsmuth.de" Date: Tue, 30 Jul 2024 22:11:28 +0200 Subject: [PATCH 11/27] Continued implementation, need to finish writing part --- rgb.go | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- via.go | 30 ++++++++++++- 2 files changed, 160 insertions(+), 4 deletions(-) diff --git a/rgb.go b/rgb.go index ff8400b..bbf9fa0 100644 --- a/rgb.go +++ b/rgb.go @@ -1,9 +1,15 @@ package keyboard type RGBMatrix struct { - maximumBrightness uint8 - ledCount uint16 - ledMatrixMapping []LedMatrixPosition + maximumBrightness uint8 + ledCount uint16 + ledMatrixMapping []LedMatrixPosition + implementedEffects []uint16 + currentEffect uint16 + currentSpeed uint8 + currentHue uint8 + currentSaturation uint8 + currentValue uint8 } type LedMatrixPosition struct { @@ -23,6 +29,55 @@ const ( 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) { if int(ledCount) != len(ledMatrixMapping) { panic("ledMatrixMapping must have length equal to number of ledMatrixMapping") @@ -31,6 +86,14 @@ func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMappin maximumBrightness: brightness, ledCount: ledCount, ledMatrixMapping: ledMatrixMapping, + implementedEffects: []uint16{ + VIALRGB_EFFECT_OFF, + }, + currentEffect: VIALRGB_EFFECT_OFF, + currentSpeed: 0, + currentHue: 0, + currentSaturation: 0, + currentValue: 0, } d.rgbMat = append(d.rgbMat, rgbMatrix) } @@ -60,6 +123,71 @@ func (d *Device) GetRGBMatrixLEDMapping(ledIndex uint16) LedMatrixPosition { return d.rgbMat[0].ledMatrixMapping[ledIndex] } +func (d *Device) GetSupportedRGBModes() []uint16 { + if !d.IsRGBMatrixEnabled() { + return []uint16{} + } + return d.rgbMat[0].implementedEffects +} + +func (d *Device) GetCurrentRGBMode() uint16 { + if !d.IsRGBMatrixEnabled() { + return 0 + } + return d.rgbMat[0].currentEffect +} + +func (d *Device) SetCurrentRGBMode(mode uint16) { + if !d.IsRGBMatrixEnabled() { + return + } + d.rgbMat[0].currentEffect = mode +} + +func (d *Device) GetCurrentSpeed() uint8 { + if !d.IsRGBMatrixEnabled() { + return 0 + } + return d.rgbMat[0].currentSpeed +} + +func (d *Device) SetCurrentSpeed(speed uint8) { + if !d.IsRGBMatrixEnabled() { + return + } + d.rgbMat[0].currentSpeed = speed +} + +func (d *Device) GetCurrentHue() uint8 { + if !d.IsRGBMatrixEnabled() { + return 0 + } + return d.rgbMat[0].currentHue +} + +func (d *Device) GetCurrentSaturation() uint8 { + if !d.IsRGBMatrixEnabled() { + return 0 + } + return d.rgbMat[0].currentSaturation +} + +func (d *Device) GetCurrentValue() uint8 { + if !d.IsRGBMatrixEnabled() { + return 0 + } + return d.rgbMat[0].currentValue +} + +func (d *Device) SetCurrentHSV(hue uint8, saturation uint8, value uint8) { + if !d.IsRGBMatrixEnabled() { + return + } + d.rgbMat[0].currentHue = hue + d.rgbMat[0].currentSaturation = saturation + d.rgbMat[0].currentValue = value +} + func (d *Device) IsRGBMatrixEnabled() bool { return len(d.rgbMat) > 0 } diff --git a/via.go b/via.go index bba02cd..4043237 100644 --- a/via.go +++ b/via.go @@ -198,7 +198,16 @@ func rxHandler2(b []byte) bool { case 0x07: // id_lighting_set_value if device.IsRGBMatrixEnabled() { - + switch b[1] { + case 0x41: + // VIALRGB_SET_MODE + rgbId := uint16(b[2]) | (uint16(b[3]) << 8) + device.SetCurrentRGBMode(rgbId) + device.SetCurrentSpeed(b[4]) + device.SetCurrentHSV(b[5], b[6], b[7]) + case 0x42: + // VIALRGB_DIRECT_FASTSET + } } case 0x08: // id_lighting_get_value @@ -212,8 +221,27 @@ func rxHandler2(b []byte) bool { txb[2] = device.GetRGBMatrixMaximumBrightness() case 0x41: // vialrgb_get_mode + currentEffect := device.GetCurrentRGBMode() + txb[0] = byte(currentEffect & 0xFF) + txb[1] = byte(currentEffect >> 8) + txb[2] = device.GetCurrentSpeed() + txb[3] = device.GetCurrentHue() + txb[4] = device.GetCurrentSaturation() + txb[5] = device.GetCurrentValue() case 0x42: // vialrgb_get_supported + implementedEffects := device.GetSupportedRGBModes() + var length int + if len(implementedEffects) > 16 { + length = 16 + } else { + length = len(implementedEffects) + } + // TODO test if this format is okay + for i := 0; i < length; i++ { + txb[i*2] = byte(implementedEffects[i] & 0xFF) + txb[i*2+1] = byte(implementedEffects[i] >> 8) + } case 0x43: // vialrgb_get_number_leds txb[0] = byte(device.GetRGBMatrixLEDCount() & 0xFF) From a8bdcd2b8c6bca2836ee04d744bd30b820b988dc Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Thu, 1 Aug 2024 14:28:09 +0200 Subject: [PATCH 12/27] Finished untested vialrgb communication --- rgb.go | 52 ++++++++++++++++++++++++++++++++++++++++++---------- via.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/rgb.go b/rgb.go index bbf9fa0..9aed606 100644 --- a/rgb.go +++ b/rgb.go @@ -1,15 +1,16 @@ package keyboard type RGBMatrix struct { - maximumBrightness uint8 - ledCount uint16 - ledMatrixMapping []LedMatrixPosition - implementedEffects []uint16 - currentEffect uint16 - currentSpeed uint8 - currentHue uint8 - currentSaturation uint8 - currentValue uint8 + maximumBrightness uint8 + ledCount uint16 + ledMatrixMapping []LedMatrixPosition + implementedEffects []uint16 + currentEffect uint16 + currentSpeed uint8 + currentHue uint8 + currentSaturation uint8 + currentValue uint8 + ledMatrixDirectVals []LedMatrixDirectModeColor } type LedMatrixPosition struct { @@ -20,6 +21,12 @@ type LedMatrixPosition struct { ledFlags uint8 } +type LedMatrixDirectModeColor struct { + h uint8 + s uint8 + v uint8 +} + const ( // LED Flags LED_FLAG_NONE uint8 = 0x00 // If this LED has no flags @@ -78,7 +85,7 @@ const ( VIALRGB_EFFECT_PIXEL_FRACTAL ) -func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMapping []LedMatrixPosition) { +func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMapping []LedMatrixPosition, directMode bool) { if int(ledCount) != len(ledMatrixMapping) { panic("ledMatrixMapping must have length equal to number of ledMatrixMapping") } @@ -95,6 +102,9 @@ func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMappin currentSaturation: 0, currentValue: 0, } + if directMode { + rgbMatrix.ledMatrixDirectVals = make([]LedMatrixDirectModeColor, ledCount) + } d.rgbMat = append(d.rgbMat, rgbMatrix) } @@ -188,6 +198,28 @@ func (d *Device) SetCurrentHSV(hue uint8, saturation uint8, value uint8) { d.rgbMat[0].currentValue = value } +func (d *Device) SetDirectHSV(hue uint8, saturation uint8, value uint8, ledIndex uint16) { + if !d.IsDirectModeEnabled() { + return + } + rgb := d.rgbMat[0] + 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) IsRGBMatrixEnabled() bool { return len(d.rgbMat) > 0 } + +func (d *Device) IsDirectModeEnabled() bool { + return d.IsRGBMatrixEnabled() && d.rgbMat[0].ledMatrixDirectVals != nil +} diff --git a/via.go b/via.go index 4043237..ae41776 100644 --- a/via.go +++ b/via.go @@ -207,6 +207,29 @@ func rxHandler2(b []byte) bool { device.SetCurrentHSV(b[5], b[6], b[7]) case 0x42: // VIALRGB_DIRECT_FASTSET + if !device.IsDirectModeEnabled() { + break + } + if len(b) < 4 { + break + } + firstIndex := uint16(b[2]) | (uint16(b[3]) << 8) + numLeds := uint16(b[4]) + if int(numLeds)*3 > len(b)-4 { + break + } + var i uint16 + for i = 0; i < numLeds; i++ { + if i+firstIndex >= device.GetRGBMatrixLEDCount() { + break + } + device.SetDirectHSV( + b[i*3+5], + b[i*3+6], + b[i*3+7], + i+firstIndex, + ) + } } } case 0x08: @@ -244,10 +267,16 @@ func rxHandler2(b []byte) bool { } case 0x43: // vialrgb_get_number_leds + if !device.IsDirectModeEnabled() { + break + } txb[0] = byte(device.GetRGBMatrixLEDCount() & 0xFF) txb[1] = byte(device.GetRGBMatrixLEDCount() >> 8) case 0x44: //vialrgb_get_led_info + if !device.IsDirectModeEnabled() { + break + } ledPos := uint16(b[2]&0xFF) | (uint16(b[3]) >> 8) position := device.GetRGBMatrixLEDMapping(ledPos) // physical position From e3941c82daa98e5a6b9619c325626f6eed341687 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Fri, 2 Aug 2024 12:25:04 +0200 Subject: [PATCH 13/27] Finished untested vialrgb implementation with a few animations --- keyboard.go | 5 +- rgb.go | 138 ++++++++++++++++--------- rgbanimations/hsvtorgb.go | 34 ++++++ rgbanimations/vialrgbAlphasModsAnim.go | 28 +++++ rgbanimations/vialrgbDirectAnim.go | 20 ++++ rgbanimations/vialrgbSolidColorAnim.go | 20 ++++ via.go | 6 +- 7 files changed, 198 insertions(+), 53 deletions(-) create mode 100644 rgbanimations/hsvtorgb.go create mode 100644 rgbanimations/vialrgbAlphasModsAnim.go create mode 100644 rgbanimations/vialrgbDirectAnim.go create mode 100644 rgbanimations/vialrgbSolidColorAnim.go diff --git a/keyboard.go b/keyboard.go index f8158b6..883b1af 100644 --- a/keyboard.go +++ b/keyboard.go @@ -24,7 +24,7 @@ type Device struct { flashCh chan bool flashCnt int - rgbMat []RGBMatrix + rgbMat *RGBMatrix kb []KBer @@ -133,6 +133,9 @@ func (d *Device) Init() error { } //copy(device.Macros[:], rbuf[offset:]) + // Start RGB task + go d.updateRGBTask() + return nil } diff --git a/rgb.go b/rgb.go index 9aed606..de819cf 100644 --- a/rgb.go +++ b/rgb.go @@ -1,16 +1,24 @@ package keyboard +import ( + "image/color" + "time" + "tinygo.org/x/drivers/ws2812" +) + type RGBMatrix struct { maximumBrightness uint8 ledCount uint16 - ledMatrixMapping []LedMatrixPosition - implementedEffects []uint16 - currentEffect uint16 - currentSpeed uint8 - currentHue uint8 - currentSaturation uint8 - currentValue uint8 - ledMatrixDirectVals []LedMatrixDirectModeColor + 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 { @@ -18,13 +26,20 @@ type LedMatrixPosition struct { physicalY uint8 kbIndex uint8 matrixIndex uint8 - ledFlags uint8 + LedFlags uint8 } type LedMatrixDirectModeColor struct { - h uint8 - s uint8 - v uint8 + H uint8 + S uint8 + V uint8 +} + +type RgbAnimationFunc func(matrix *RGBMatrix) + +type RgbAnimation struct { + AnimationFunc RgbAnimationFunc + AnimationType uint16 } const ( @@ -85,41 +100,50 @@ const ( VIALRGB_EFFECT_PIXEL_FRACTAL ) -func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMapping []LedMatrixPosition, directMode bool) { +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") + panic("LedMatrixMapping must have length equal to number of LedMatrixMapping") + } + effectOffAnimation := RgbAnimation{ + AnimationFunc: func(matrix *RGBMatrix) {}, + AnimationType: VIALRGB_EFFECT_OFF, } rgbMatrix := RGBMatrix{ maximumBrightness: brightness, ledCount: ledCount, - ledMatrixMapping: ledMatrixMapping, - implementedEffects: []uint16{ - VIALRGB_EFFECT_OFF, + LedMatrixMapping: ledMatrixMapping, + implementedEffects: []RgbAnimation{ + effectOffAnimation, }, - currentEffect: VIALRGB_EFFECT_OFF, - currentSpeed: 0, - currentHue: 0, - currentSaturation: 0, - currentValue: 0, + currentEffect: effectOffAnimation, + CurrentSpeed: 0, + CurrentHue: 0, + CurrentSaturation: 0, + CurrentValue: 0, + LedMatrixVals: make([]color.RGBA, ledCount), + ledDriver: ledDriver, } - if directMode { - rgbMatrix.ledMatrixDirectVals = make([]LedMatrixDirectModeColor, ledCount) + for _, animation := range animations { + rgbMatrix.implementedEffects = append(rgbMatrix.implementedEffects, animation) + if animation.AnimationType == VIALRGB_EFFECT_DIRECT { + rgbMatrix.LedMatrixDirectVals = make([]LedMatrixDirectModeColor, ledCount) + } } - d.rgbMat = append(d.rgbMat, rgbMatrix) + d.rgbMat = &rgbMatrix } func (d *Device) GetRGBMatrixMaximumBrightness() uint8 { if !d.IsRGBMatrixEnabled() { return 0 } - return d.rgbMat[0].maximumBrightness + return d.rgbMat.maximumBrightness } func (d *Device) GetRGBMatrixLEDCount() uint16 { if !d.IsRGBMatrixEnabled() { return 0 } - return d.rgbMat[1].ledCount + return d.rgbMat.ledCount } func (d *Device) GetRGBMatrixLEDMapping(ledIndex uint16) LedMatrixPosition { @@ -127,99 +151,115 @@ func (d *Device) GetRGBMatrixLEDMapping(ledIndex uint16) LedMatrixPosition { kbIndex: 0xFF, matrixIndex: 0xFF, } - if !d.IsRGBMatrixEnabled() || ledIndex >= d.rgbMat[0].ledCount { + if !d.IsRGBMatrixEnabled() || ledIndex >= d.rgbMat.ledCount { return invalidPosition } - return d.rgbMat[0].ledMatrixMapping[ledIndex] + return d.rgbMat.LedMatrixMapping[ledIndex] } -func (d *Device) GetSupportedRGBModes() []uint16 { +func (d *Device) GetSupportedRGBModes() []RgbAnimation { if !d.IsRGBMatrixEnabled() { - return []uint16{} + return []RgbAnimation{} } - return d.rgbMat[0].implementedEffects + return d.rgbMat.implementedEffects } func (d *Device) GetCurrentRGBMode() uint16 { if !d.IsRGBMatrixEnabled() { return 0 } - return d.rgbMat[0].currentEffect + return d.rgbMat.currentEffect.AnimationType } func (d *Device) SetCurrentRGBMode(mode uint16) { if !d.IsRGBMatrixEnabled() { return } - d.rgbMat[0].currentEffect = mode + 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[0].currentSpeed + return d.rgbMat.CurrentSpeed } func (d *Device) SetCurrentSpeed(speed uint8) { if !d.IsRGBMatrixEnabled() { return } - d.rgbMat[0].currentSpeed = speed + d.rgbMat.CurrentSpeed = speed } func (d *Device) GetCurrentHue() uint8 { if !d.IsRGBMatrixEnabled() { return 0 } - return d.rgbMat[0].currentHue + return d.rgbMat.CurrentHue } func (d *Device) GetCurrentSaturation() uint8 { if !d.IsRGBMatrixEnabled() { return 0 } - return d.rgbMat[0].currentSaturation + return d.rgbMat.CurrentSaturation } func (d *Device) GetCurrentValue() uint8 { if !d.IsRGBMatrixEnabled() { return 0 } - return d.rgbMat[0].currentValue + return d.rgbMat.CurrentValue } func (d *Device) SetCurrentHSV(hue uint8, saturation uint8, value uint8) { if !d.IsRGBMatrixEnabled() { return } - d.rgbMat[0].currentHue = hue - d.rgbMat[0].currentSaturation = saturation - d.rgbMat[0].currentValue = value + 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[0] + 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, + 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(0xFF-rgb.CurrentSpeed)) + rgb.currentEffect.AnimationFunc(rgb) + _ = rgb.ledDriver.WriteColors(rgb.LedMatrixVals) } } func (d *Device) IsRGBMatrixEnabled() bool { - return len(d.rgbMat) > 0 + return d.rgbMat != nil } func (d *Device) IsDirectModeEnabled() bool { - return d.IsRGBMatrixEnabled() && d.rgbMat[0].ledMatrixDirectVals != nil + return d.IsRGBMatrixEnabled() && d.rgbMat.LedMatrixDirectVals != nil } diff --git a/rgbanimations/hsvtorgb.go b/rgbanimations/hsvtorgb.go new file mode 100644 index 0000000..add4263 --- /dev/null +++ b/rgbanimations/hsvtorgb.go @@ -0,0 +1,34 @@ +package rgbanimations + +// Source: https://stackoverflow.com/a/14733008 + +func HSVToRGB(h, s, v uint8) (r, g, b, a uint8) { + if s == 0 { + return v, v, v, v + } + region := h / 43 + remainder := (h - (region * 43)) * 6 + + var s16, v16 uint16 + s16 = uint16(s) + v16 = uint16(v) + + p := uint8((v16 * (255 - s16)) >> 8) + q := uint8((v16 * (255 - ((s16 * uint16(remainder)) >> 8))) >> 8) + t := uint8((v16 * (255 - ((s16 * uint16(255-remainder)) >> 8))) >> 8) + + switch region { + case 0: + return v, t, p, v + case 1: + return q, v, p, v + case 2: + return p, v, t, v + case 3: + return p, q, v, v + case 4: + return t, p, v, v + default: + return v, p, q, v + } +} diff --git a/rgbanimations/vialrgbAlphasModsAnim.go b/rgbanimations/vialrgbAlphasModsAnim.go new file mode 100644 index 0000000..327117c --- /dev/null +++ b/rgbanimations/vialrgbAlphasModsAnim.go @@ -0,0 +1,28 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetAlphasModsAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBAlphasModsAnim, + AnimationType: keyboard.VIALRGB_EFFECT_ALPHAS_MODS, + } +} + +func vialRGBAlphasModsAnim(matrix *keyboard.RGBMatrix) { + r1, g1, b1, a1 := HSVToRGB(matrix.CurrentHue, matrix.CurrentSaturation, matrix.CurrentValue) + r2, g2, b2, a2 := HSVToRGB((matrix.CurrentHue+matrix.CurrentSpeed)%0xFF, matrix.CurrentSaturation, matrix.CurrentValue) + for i := range matrix.LedMatrixVals { + if matrix.LedMatrixMapping[i].LedFlags&keyboard.LED_FLAG_MODIFIER == keyboard.LED_FLAG_MODIFIER { + matrix.LedMatrixVals[i].R = r2 + matrix.LedMatrixVals[i].G = g2 + matrix.LedMatrixVals[i].B = b2 + matrix.LedMatrixVals[i].A = a2 + } else { + matrix.LedMatrixVals[i].R = r1 + matrix.LedMatrixVals[i].G = g1 + matrix.LedMatrixVals[i].B = b1 + matrix.LedMatrixVals[i].A = a1 + } + } +} diff --git a/rgbanimations/vialrgbDirectAnim.go b/rgbanimations/vialrgbDirectAnim.go new file mode 100644 index 0000000..d80b23c --- /dev/null +++ b/rgbanimations/vialrgbDirectAnim.go @@ -0,0 +1,20 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetDirectAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBDirectAnim, + AnimationType: keyboard.VIALRGB_EFFECT_DIRECT, + } +} + +func vialRGBDirectAnim(matrix *keyboard.RGBMatrix) { + for i, hsv := range matrix.LedMatrixDirectVals { + r, g, b, a := HSVToRGB(hsv.H, hsv.S, hsv.V) + matrix.LedMatrixVals[i].R = r + matrix.LedMatrixVals[i].G = g + matrix.LedMatrixVals[i].B = b + matrix.LedMatrixVals[i].A = a + } +} diff --git a/rgbanimations/vialrgbSolidColorAnim.go b/rgbanimations/vialrgbSolidColorAnim.go new file mode 100644 index 0000000..7ef4e59 --- /dev/null +++ b/rgbanimations/vialrgbSolidColorAnim.go @@ -0,0 +1,20 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetSolidColorAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialrgbSolidColorAnim, + AnimationType: keyboard.VIALRGB_EFFECT_SOLID_COLOR, + } +} + +func vialrgbSolidColorAnim(matrix *keyboard.RGBMatrix) { + r, g, b, a := HSVToRGB(matrix.CurrentHue, matrix.CurrentSaturation, matrix.CurrentValue) + for i := range matrix.LedMatrixVals { + matrix.LedMatrixVals[i].R = r + matrix.LedMatrixVals[i].G = g + matrix.LedMatrixVals[i].B = b + matrix.LedMatrixVals[i].A = a + } +} diff --git a/via.go b/via.go index ae41776..8cdb22b 100644 --- a/via.go +++ b/via.go @@ -262,8 +262,8 @@ func rxHandler2(b []byte) bool { } // TODO test if this format is okay for i := 0; i < length; i++ { - txb[i*2] = byte(implementedEffects[i] & 0xFF) - txb[i*2+1] = byte(implementedEffects[i] >> 8) + txb[i*2] = byte(implementedEffects[i].AnimationType & 0xFF) + txb[i*2+1] = byte(implementedEffects[i].AnimationType >> 8) } case 0x43: // vialrgb_get_number_leds @@ -283,7 +283,7 @@ func rxHandler2(b []byte) bool { txb[0] = position.physicalX txb[1] = position.physicalY // flags - txb[2] = position.ledFlags + txb[2] = position.LedFlags // matrix position txb[3] = position.kbIndex txb[4] = position.matrixIndex From 49a8c32bf0b9f89c747a680529ad753cd0b1dd45 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Fri, 2 Aug 2024 12:41:24 +0200 Subject: [PATCH 14/27] Made struct elements public --- keyboard.go | 4 ++-- rgb.go | 12 ++++++------ via.go | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/keyboard.go b/keyboard.go index 883b1af..31ff579 100644 --- a/keyboard.go +++ b/keyboard.go @@ -385,7 +385,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 } @@ -439,7 +439,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) diff --git a/rgb.go b/rgb.go index de819cf..a751258 100644 --- a/rgb.go +++ b/rgb.go @@ -22,10 +22,10 @@ type RGBMatrix struct { } type LedMatrixPosition struct { - physicalX uint8 - physicalY uint8 - kbIndex uint8 - matrixIndex uint8 + PhysicalX uint8 + PhysicalY uint8 + KbIndex uint8 + MatrixIndex uint8 LedFlags uint8 } @@ -148,8 +148,8 @@ func (d *Device) GetRGBMatrixLEDCount() uint16 { func (d *Device) GetRGBMatrixLEDMapping(ledIndex uint16) LedMatrixPosition { invalidPosition := LedMatrixPosition{ - kbIndex: 0xFF, - matrixIndex: 0xFF, + KbIndex: 0xFF, + MatrixIndex: 0xFF, } if !d.IsRGBMatrixEnabled() || ledIndex >= d.rgbMat.ledCount { return invalidPosition diff --git a/via.go b/via.go index 8cdb22b..375730a 100644 --- a/via.go +++ b/via.go @@ -280,13 +280,13 @@ func rxHandler2(b []byte) bool { ledPos := uint16(b[2]&0xFF) | (uint16(b[3]) >> 8) position := device.GetRGBMatrixLEDMapping(ledPos) // physical position - txb[0] = position.physicalX - txb[1] = position.physicalY + txb[0] = position.PhysicalX + txb[1] = position.PhysicalY // flags txb[2] = position.LedFlags // matrix position - txb[3] = position.kbIndex - txb[4] = position.matrixIndex + txb[3] = position.KbIndex + txb[4] = position.MatrixIndex } } else { txb[1] = 0x00 From 1bf0544ff3924c83b5339962cb5d46a928e16db0 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Wed, 7 Aug 2024 17:01:45 +0200 Subject: [PATCH 15/27] Fixed RGB communication --- rgb.go | 10 +++++----- via.go | 50 +++++++++++++++++++++++--------------------------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/rgb.go b/rgb.go index a751258..c027803 100644 --- a/rgb.go +++ b/rgb.go @@ -116,15 +116,15 @@ func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMappin effectOffAnimation, }, currentEffect: effectOffAnimation, - CurrentSpeed: 0, - CurrentHue: 0, - CurrentSaturation: 0, - CurrentValue: 0, + CurrentSpeed: 0xFF, + CurrentHue: 0xFF, + CurrentSaturation: 0xFF, + CurrentValue: brightness, LedMatrixVals: make([]color.RGBA, ledCount), ledDriver: ledDriver, } + rgbMatrix.implementedEffects = append(rgbMatrix.implementedEffects, animations...) for _, animation := range animations { - rgbMatrix.implementedEffects = append(rgbMatrix.implementedEffects, animation) if animation.AnimationType == VIALRGB_EFFECT_DIRECT { rgbMatrix.LedMatrixDirectVals = make([]LedMatrixDirectModeColor, ledCount) } diff --git a/via.go b/via.go index 375730a..3162deb 100644 --- a/via.go +++ b/via.go @@ -121,12 +121,6 @@ func rxHandler(b []byte) { } func rxHandler2(b []byte) bool { - switch b[0] { - //case 0x12, 0x0E: - default: - //fmt.Printf("RxHandler % X\n", b) - } - copy(txb[:32], b) switch b[0] { case 0x01: @@ -238,40 +232,43 @@ func rxHandler2(b []byte) bool { switch b[1] { case 0x40: // vialrgb_get_info - const vialRgbProtocolVersion = 1 - txb[0] = vialRgbProtocolVersion & 0xFF - txb[1] = vialRgbProtocolVersion >> 8 - txb[2] = device.GetRGBMatrixMaximumBrightness() + const vialRgbProtocolVersion = 0x0001 + txb[2] = vialRgbProtocolVersion & 0xFF + txb[3] = vialRgbProtocolVersion >> 8 + txb[4] = device.GetRGBMatrixMaximumBrightness() case 0x41: // vialrgb_get_mode currentEffect := device.GetCurrentRGBMode() - txb[0] = byte(currentEffect & 0xFF) - txb[1] = byte(currentEffect >> 8) - txb[2] = device.GetCurrentSpeed() - txb[3] = device.GetCurrentHue() - txb[4] = device.GetCurrentSaturation() - txb[5] = device.GetCurrentValue() + txb[2] = byte(currentEffect & 0xFF) + txb[3] = byte(currentEffect >> 8) + txb[4] = device.GetCurrentSpeed() + txb[5] = device.GetCurrentHue() + txb[6] = device.GetCurrentSaturation() + txb[7] = device.GetCurrentValue() case 0x42: // vialrgb_get_supported implementedEffects := device.GetSupportedRGBModes() var length int - if len(implementedEffects) > 16 { - length = 16 + if len(implementedEffects) > 15 { + length = 15 } else { length = len(implementedEffects) } - // TODO test if this format is okay for i := 0; i < length; i++ { txb[i*2] = byte(implementedEffects[i].AnimationType & 0xFF) txb[i*2+1] = byte(implementedEffects[i].AnimationType >> 8) } + if length < 16 { + txb[length*2] = 0xFF + txb[length*2+1] = 0xFF + } case 0x43: // vialrgb_get_number_leds if !device.IsDirectModeEnabled() { break } - txb[0] = byte(device.GetRGBMatrixLEDCount() & 0xFF) - txb[1] = byte(device.GetRGBMatrixLEDCount() >> 8) + txb[2] = byte(device.GetRGBMatrixLEDCount() & 0xFF) + txb[3] = byte(device.GetRGBMatrixLEDCount() >> 8) case 0x44: //vialrgb_get_led_info if !device.IsDirectModeEnabled() { @@ -280,13 +277,13 @@ func rxHandler2(b []byte) bool { ledPos := uint16(b[2]&0xFF) | (uint16(b[3]) >> 8) position := device.GetRGBMatrixLEDMapping(ledPos) // physical position - txb[0] = position.PhysicalX - txb[1] = position.PhysicalY + txb[2] = position.PhysicalX + txb[3] = position.PhysicalY // flags - txb[2] = position.LedFlags + txb[4] = position.LedFlags // matrix position - txb[3] = position.KbIndex - txb[4] = position.MatrixIndex + txb[5] = position.KbIndex + txb[6] = position.MatrixIndex } } else { txb[1] = 0x00 @@ -354,7 +351,6 @@ func rxHandler2(b []byte) bool { return false } machine.SendUSBInPacket(6, txb[:32]) - //fmt.Printf("Tx % X\n", txb[:32]) return true } From 30bfea493698401b5d430eddeb3b8680e50ff43d Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Tue, 13 Aug 2024 18:06:00 +0200 Subject: [PATCH 16/27] Finished RGB implementation TODO: test remembering vial settings --- keyboard.go | 4 ++++ rgb.go | 8 +++++--- via.go | 11 ++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/keyboard.go b/keyboard.go index 31ff579..fb0eb7a 100644 --- a/keyboard.go +++ b/keyboard.go @@ -125,6 +125,10 @@ func (d *Device) Init() error { } } + device.SetCurrentRGBMode(uint16(rbuf[offset]) & (uint16(rbuf[offset+1]) >> 8)) + device.SetCurrentSpeed(rbuf[offset+2]) + device.SetCurrentHSV(rbuf[offset+3], rbuf[offset+4], rbuf[offset+5]) + for i, b := range rbuf[offset:] { if b == 0xFF { b = 0 diff --git a/rgb.go b/rgb.go index c027803..3ef4192 100644 --- a/rgb.go +++ b/rgb.go @@ -105,7 +105,9 @@ func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMappin panic("LedMatrixMapping must have length equal to number of LedMatrixMapping") } effectOffAnimation := RgbAnimation{ - AnimationFunc: func(matrix *RGBMatrix) {}, + AnimationFunc: func(matrix *RGBMatrix) { + matrix.ledDriver.WriteColors(matrix.LedMatrixVals) + }, AnimationType: VIALRGB_EFFECT_OFF, } rgbMatrix := RGBMatrix{ @@ -116,7 +118,7 @@ func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMappin effectOffAnimation, }, currentEffect: effectOffAnimation, - CurrentSpeed: 0xFF, + CurrentSpeed: 0x00, CurrentHue: 0xFF, CurrentSaturation: 0xFF, CurrentValue: brightness, @@ -250,7 +252,7 @@ func (d *Device) updateRGBTask() { } rgb := d.rgbMat for { - time.Sleep(time.Millisecond * time.Duration(0xFF-rgb.CurrentSpeed)) + time.Sleep(time.Millisecond * time.Duration(0x100-uint16(rgb.CurrentSpeed))) rgb.currentEffect.AnimationFunc(rgb) _ = rgb.ledDriver.WriteColors(rgb.LedMatrixVals) } diff --git a/via.go b/via.go index 3162deb..7e64b9d 100644 --- a/via.go +++ b/via.go @@ -360,7 +360,8 @@ func Save() error { keyboards := device.GetKeyboardCount() cnt := device.GetMaxKeyCount() - wbuf := make([]byte, 4+layers*keyboards*cnt*2+len(device.Macros)) + rgbStorageSize := 2 + 1 + 3 // currentEffect + speed + HSV + wbuf := make([]byte, 4+layers*keyboards*cnt*2+len(device.Macros)+rgbStorageSize) needed := int64(len(wbuf)) / machine.Flash.EraseBlockSize() if needed == 0 { needed = 1 @@ -389,6 +390,14 @@ func Save() error { } } + wbuf[offset] = byte(device.GetCurrentRGBMode()) + wbuf[offset+1] = byte(device.GetCurrentRGBMode() >> 8) + wbuf[offset+2] = device.GetCurrentSpeed() + wbuf[offset+3] = device.GetCurrentHue() + wbuf[offset+4] = device.GetCurrentSaturation() + wbuf[offset+5] = device.GetCurrentValue() + offset += 5 + copy(wbuf[offset:], device.Macros[:]) _, err = machine.Flash.WriteAt(wbuf[:], 0) From 6704554438765cca53c0aec46c9abf41947f6fd6 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Thu, 15 Aug 2024 16:38:21 +0200 Subject: [PATCH 17/27] Fixed saving and loading rgb settings --- keyboard.go | 7 +++++-- via.go | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/keyboard.go b/keyboard.go index fb0eb7a..e9cef34 100644 --- a/keyboard.go +++ b/keyboard.go @@ -102,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 @@ -125,9 +126,11 @@ func (d *Device) Init() error { } } - device.SetCurrentRGBMode(uint16(rbuf[offset]) & (uint16(rbuf[offset+1]) >> 8)) + 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 { diff --git a/via.go b/via.go index 7e64b9d..83f699b 100644 --- a/via.go +++ b/via.go @@ -199,6 +199,7 @@ func rxHandler2(b []byte) bool { device.SetCurrentRGBMode(rgbId) device.SetCurrentSpeed(b[4]) device.SetCurrentHSV(b[5], b[6], b[7]) + device.flashCh <- true case 0x42: // VIALRGB_DIRECT_FASTSET if !device.IsDirectModeEnabled() { @@ -396,7 +397,7 @@ func Save() error { wbuf[offset+3] = device.GetCurrentHue() wbuf[offset+4] = device.GetCurrentSaturation() wbuf[offset+5] = device.GetCurrentValue() - offset += 5 + offset += 6 copy(wbuf[offset:], device.Macros[:]) From 40d471d4fc35cd8f72acde285386959a6ddc6f6f Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Thu, 15 Aug 2024 17:07:18 +0200 Subject: [PATCH 18/27] Implemented new rgb animation --- rgbanimations/{hsvtorgb.go => utilFuncs.go} | 4 ++++ rgbanimations/vialrgbGradientUpDown.go | 22 +++++++++++++++++++++ 2 files changed, 26 insertions(+) rename rgbanimations/{hsvtorgb.go => utilFuncs.go} (87%) create mode 100644 rgbanimations/vialrgbGradientUpDown.go diff --git a/rgbanimations/hsvtorgb.go b/rgbanimations/utilFuncs.go similarity index 87% rename from rgbanimations/hsvtorgb.go rename to rgbanimations/utilFuncs.go index add4263..7087809 100644 --- a/rgbanimations/hsvtorgb.go +++ b/rgbanimations/utilFuncs.go @@ -32,3 +32,7 @@ func HSVToRGB(h, s, v uint8) (r, g, b, a uint8) { return v, p, q, v } } + +func Scale8(i uint8, scale uint8) uint8 { + return uint8((uint16(i) * uint16(scale)) >> 8) +} diff --git a/rgbanimations/vialrgbGradientUpDown.go b/rgbanimations/vialrgbGradientUpDown.go new file mode 100644 index 0000000..eb55fa7 --- /dev/null +++ b/rgbanimations/vialrgbGradientUpDown.go @@ -0,0 +1,22 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetGradientUpDownAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBGradientUpDown, + AnimationType: keyboard.VIALRGB_EFFECT_GRADIENT_UP_DOWN, + } +} + +func vialRGBGradientUpDown(matrix *keyboard.RGBMatrix) { + scale := Scale8(64, matrix.CurrentSpeed) + for i, position := range matrix.LedMatrixMapping { + h := uint16(matrix.CurrentHue) + uint16(scale*(position.PhysicalY>>4)) + r, g, b, a := HSVToRGB(uint8(h&0xFF), matrix.CurrentSaturation, matrix.CurrentValue) + matrix.LedMatrixVals[i].R = r + matrix.LedMatrixVals[i].G = g + matrix.LedMatrixVals[i].B = b + matrix.LedMatrixVals[i].A = a + } +} From 9f797ef7c085d0f371830b3dab886f289e5b900c Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Thu, 15 Aug 2024 17:58:01 +0200 Subject: [PATCH 19/27] Implemented gradient rgb effect --- rgbanimations/vialrgbGradientLeftRight.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 rgbanimations/vialrgbGradientLeftRight.go diff --git a/rgbanimations/vialrgbGradientLeftRight.go b/rgbanimations/vialrgbGradientLeftRight.go new file mode 100644 index 0000000..00a08b7 --- /dev/null +++ b/rgbanimations/vialrgbGradientLeftRight.go @@ -0,0 +1,22 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetGradientLeftRight() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBGradientLeftRight, + AnimationType: keyboard.VIALRGB_EFFECT_GRADIENT_LEFT_RIGHT, + } +} + +func vialRGBGradientLeftRight(matrix *keyboard.RGBMatrix) { + scale := Scale8(64, matrix.CurrentSpeed) + for i, position := range matrix.LedMatrixMapping { + h := uint16(matrix.CurrentHue) + uint16(scale*position.PhysicalX>>5) + r, g, b, a := HSVToRGB(uint8(h&0xFF), matrix.CurrentSaturation, matrix.CurrentValue) + matrix.LedMatrixVals[i].R = r + matrix.LedMatrixVals[i].G = g + matrix.LedMatrixVals[i].B = b + matrix.LedMatrixVals[i].A = a + } +} From 02dff8c8253bca894850bb3b06d8303eb8a96ee0 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Fri, 16 Aug 2024 22:25:29 +0200 Subject: [PATCH 20/27] implemented Breathing effect, TODO: rework animation speed --- rgbanimations/utilFuncs.go | 42 +++++++++++++++++++++++++++++++ rgbanimations/vialrgbBreathing.go | 24 ++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 rgbanimations/vialrgbBreathing.go diff --git a/rgbanimations/utilFuncs.go b/rgbanimations/utilFuncs.go index 7087809..e71cf51 100644 --- a/rgbanimations/utilFuncs.go +++ b/rgbanimations/utilFuncs.go @@ -36,3 +36,45 @@ func HSVToRGB(h, s, v uint8) (r, g, b, a uint8) { func Scale8(i uint8, scale uint8) uint8 { return uint8((uint16(i) * uint16(scale)) >> 8) } + +func Scale16by8(i uint16, scale uint8) uint16 { + return (i * (1 + uint16(scale))) >> 8 +} + +func Abs8(i int8) uint8 { + if i < 0 { + return uint8(-i) + } + return uint8(i) +} + +func Sin8(theta uint8) uint8 { + offset := theta + if theta&0x40 != 0 { + offset = 0xFF - offset + } + offset &= 0x3F + + secoffset := offset & 0x0F + if theta&0x40 != 0 { + secoffset++ + } + + section := offset >> 4 + s2 := section * 2 + var p = []uint8{0, 49, 49, 41, 90, 27, 117, 10} + b := p[s2] + m16 := p[s2+1] + + mx := (m16 * secoffset) >> 4 + + y := int8(mx + b) + if theta&0x80 != 0 { + y = -y + } + + y += 127 + y++ + + return uint8(y) +} diff --git a/rgbanimations/vialrgbBreathing.go b/rgbanimations/vialrgbBreathing.go new file mode 100644 index 0000000..f78ace2 --- /dev/null +++ b/rgbanimations/vialrgbBreathing.go @@ -0,0 +1,24 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetBreathingAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBBreathing, + AnimationType: keyboard.VIALRGB_EFFECT_BREATHING, + } +} + +var time = uint16(0) + +func vialRGBBreathing(matrix *keyboard.RGBMatrix) { + v := Scale8(Abs8(int8(Sin8(uint8(time))-128))*8, matrix.CurrentValue) + r, g, b, a := HSVToRGB(matrix.CurrentHue, matrix.CurrentSaturation, v) + for _, val := range matrix.LedMatrixVals { + val.R = r + val.G = g + val.B = b + val.A = a + } + time++ +} From d920ff467a98e04fc8e0ab3e2ed56341d76ea507 Mon Sep 17 00:00:00 2001 From: Joris Wachsmuth <52176899+PercyJW-2@users.noreply.github.com> Date: Sun, 18 Aug 2024 02:46:59 +0200 Subject: [PATCH 21/27] adding comment to rgb.go to pass ci Co-authored-by: sago35 --- rgb.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rgb.go b/rgb.go index 3ef4192..8fba01e 100644 --- a/rgb.go +++ b/rgb.go @@ -1,3 +1,5 @@ +//go:build tinygo + package keyboard import ( From 094577e3c36b82ce95a917e7e067c210901f6612 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Mon, 7 Oct 2024 16:17:16 +0200 Subject: [PATCH 22/27] reworkted animation speed for breathing animation --- go.mod | 14 ++++++++------ go.sum | 10 ++++++++++ keyboard.go | 8 +++++--- rgb.go | 18 ++++++++++++++---- rgbanimations/vialrgbBreathing.go | 3 ++- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index b84058a..9fd0753 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,16 @@ module github.com/percyjw-2/tinygo-keyboard -go 1.19 +go 1.22.0 + +toolchain go1.23.1 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 - tinygo.org/x/tinyfont v0.3.0 + github.com/sago35/tinygo-keyboard v0.0.0-20240801000429-80e67ab2d444 + golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 + tinygo.org/x/drivers v0.28.0 + tinygo.org/x/tinydraw v0.4.0 + tinygo.org/x/tinyfont v0.4.0 ) require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect diff --git a/go.sum b/go.sum index 61dbb1c..6daac54 100644 --- a/go.sum +++ b/go.sum @@ -15,9 +15,13 @@ 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/sago35/tinygo-keyboard v0.0.0-20240801000429-80e67ab2d444 h1:upA/EBMxms0PV6Lr8Rz27nBSlAvFJkMU9q8AUceya2g= +github.com/sago35/tinygo-keyboard v0.0.0-20240801000429-80e67ab2d444/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= +golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk= +golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY= golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -31,10 +35,16 @@ tinygo.org/x/drivers v0.16.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2 tinygo.org/x/drivers v0.19.0/go.mod h1:uJD/l1qWzxzLx+vcxaW0eY464N5RAgFi1zTVzASFdqI= tinygo.org/x/drivers v0.27.0 h1:TEGk1lQvEhXxfvpEhUu+pwmCnhtldPI+hpHlO9VYixI= tinygo.org/x/drivers v0.27.0/go.mod h1:q/mU8G/wz821p8xXqbkBACOlmZFDHXd//DnYnCW+dDQ= +tinygo.org/x/drivers v0.28.0 h1:ROVrGGXddmpn2+oV/Bu3LceYbtPCJckmgIqvPcN/L0k= +tinygo.org/x/drivers v0.28.0/go.mod h1:T6snsUqS0RAxOANxiV81fQwLxDDNmprxTAYzmxoA7J0= tinygo.org/x/tinydraw v0.3.0 h1:OjsdMcES5+7IIs/4diFpq/pWFsa0VKtbi1mURuj2q64= tinygo.org/x/tinydraw v0.3.0/go.mod h1:Yz0vLSP2rHsIKpLYkEmLnE+2zyhhITu2LxiVtLRiW6I= +tinygo.org/x/tinydraw v0.4.0 h1:U9V0mHz8/jPShKjlh199vCfq1ARFyUOD1b+FfqIwV8c= +tinygo.org/x/tinydraw v0.4.0/go.mod h1:WCV/EMljTv8w04iAxjv+fRD6/4ffx0afATYeJlN90Yo= tinygo.org/x/tinyfont v0.2.1/go.mod h1:eLqnYSrFRjt5STxWaMeOWJTzrKhXqpWw7nU3bPfKOAM= tinygo.org/x/tinyfont v0.3.0 h1:HIRLQoI3oc+2CMhPcfv+Ig88EcTImE/5npjqOnMD4lM= tinygo.org/x/tinyfont v0.3.0/go.mod h1:+TV5q0KpwSGRWnN+ITijsIhrWYJkoUCp9MYELjKpAXk= +tinygo.org/x/tinyfont v0.4.0 h1:XexPKEKiHInf6p4CMCJwsIheVPY0T46HUs6ictYyZfE= +tinygo.org/x/tinyfont v0.4.0/go.mod h1:7nVj3j3geqBoPDzpFukAhF1C8AP9YocMsZy0HSAcGCA= tinygo.org/x/tinyfs v0.1.0/go.mod h1:ysc8Y92iHfhTXeyEM9+c7zviUQ4fN9UCFgSOFfMWv20= tinygo.org/x/tinyterm v0.1.0/go.mod h1:/DDhNnGwNF2/tNgHywvyZuCGnbH3ov49Z/6e8LPLRR4= diff --git a/keyboard.go b/keyboard.go index feb963d..7d0fed4 100644 --- a/keyboard.go +++ b/keyboard.go @@ -86,7 +86,7 @@ func (d *Device) OverrideCtrlH() { } } -func (d *Device) Init() error { +func (d *Device) Init(ctx context.Context) error { for _, k := range d.kb { err := k.Init() if err != nil { @@ -141,7 +141,9 @@ func (d *Device) Init() error { //copy(device.Macros[:], rbuf[offset:]) // Start RGB task - go d.updateRGBTask() + go func() { + _ = d.updateRGBTask(ctx) + }() return nil } @@ -359,7 +361,7 @@ func decKey(k uint32) (int, int, int) { } func (d *Device) Loop(ctx context.Context) error { - err := d.Init() + err := d.Init(ctx) if err != nil { return err } diff --git a/rgb.go b/rgb.go index 3ef4192..b0b3d06 100644 --- a/rgb.go +++ b/rgb.go @@ -1,6 +1,7 @@ package keyboard import ( + "context" "image/color" "time" "tinygo.org/x/drivers/ws2812" @@ -246,16 +247,25 @@ func (d *Device) SetDirectHSV(hue uint8, saturation uint8, value uint8, ledIndex } } -func (d *Device) updateRGBTask() { +func (d *Device) updateRGBTask(ctx context.Context) error { if !d.IsRGBMatrixEnabled() { - return + return nil } rgb := d.rgbMat - for { - time.Sleep(time.Millisecond * time.Duration(0x100-uint16(rgb.CurrentSpeed))) + ticker := time.Tick(10 * time.Millisecond) + cont := true + for cont { + select { + case <-ctx.Done(): + cont = false + continue + case <-ticker: + default: + } rgb.currentEffect.AnimationFunc(rgb) _ = rgb.ledDriver.WriteColors(rgb.LedMatrixVals) } + return nil } func (d *Device) IsRGBMatrixEnabled() bool { diff --git a/rgbanimations/vialrgbBreathing.go b/rgbanimations/vialrgbBreathing.go index f78ace2..ebe1d0d 100644 --- a/rgbanimations/vialrgbBreathing.go +++ b/rgbanimations/vialrgbBreathing.go @@ -12,7 +12,8 @@ func GetBreathingAnim() keyboard.RgbAnimation { var time = uint16(0) func vialRGBBreathing(matrix *keyboard.RGBMatrix) { - v := Scale8(Abs8(int8(Sin8(uint8(time))-128))*8, matrix.CurrentValue) + timeScaled := Scale16by8(time, matrix.CurrentSpeed) + v := Scale8(Abs8(int8(Sin8(uint8(timeScaled))-128))*8, matrix.CurrentValue) r, g, b, a := HSVToRGB(matrix.CurrentHue, matrix.CurrentSaturation, v) for _, val := range matrix.LedMatrixVals { val.R = r From 5c16bf808eede54e98b8c59a43df765f16fbb79c Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Mon, 7 Oct 2024 18:54:29 +0200 Subject: [PATCH 23/27] implemented 4/6 Band Animations --- rgb.go | 16 +++++++++ .../{utilFuncs.go => utilFuncsMath.go} | 34 +++++++++++++++++++ rgbanimations/utilFuncsRunners.go | 28 +++++++++++++++ rgbanimations/vialrgbBandPinwheelSat.go | 19 +++++++++++ rgbanimations/vialrgbBandPinwheelVal.go | 19 +++++++++++ rgbanimations/vialrgbBandSat.go | 26 ++++++++++++++ rgbanimations/vialrgbBandVal.go | 26 ++++++++++++++ rgbanimations/vialrgbBreathing.go | 2 -- 8 files changed, 168 insertions(+), 2 deletions(-) rename rgbanimations/{utilFuncs.go => utilFuncsMath.go} (74%) create mode 100644 rgbanimations/utilFuncsRunners.go create mode 100644 rgbanimations/vialrgbBandPinwheelSat.go create mode 100644 rgbanimations/vialrgbBandPinwheelVal.go create mode 100644 rgbanimations/vialrgbBandSat.go create mode 100644 rgbanimations/vialrgbBandVal.go diff --git a/rgb.go b/rgb.go index 4e8d660..cf523e7 100644 --- a/rgb.go +++ b/rgb.go @@ -13,6 +13,8 @@ type RGBMatrix struct { maximumBrightness uint8 ledCount uint16 LedMatrixMapping []LedMatrixPosition + CenterXPhysical uint8 + CenterYPhysical uint8 implementedEffects []RgbAnimation currentEffect RgbAnimation CurrentSpeed uint8 @@ -113,10 +115,24 @@ func (d *Device) AddRGBMatrix(brightness uint8, ledCount uint16, ledMatrixMappin }, AnimationType: VIALRGB_EFFECT_OFF, } + + maxX := uint8(0) + maxY := uint8(0) + for _, position := range ledMatrixMapping { + if maxX < position.PhysicalX { + maxX = position.PhysicalX + } + if maxY < position.PhysicalY { + maxY = position.PhysicalY + } + } + rgbMatrix := RGBMatrix{ maximumBrightness: brightness, ledCount: ledCount, LedMatrixMapping: ledMatrixMapping, + CenterXPhysical: maxX / 2, + CenterYPhysical: maxY / 2, implementedEffects: []RgbAnimation{ effectOffAnimation, }, diff --git a/rgbanimations/utilFuncs.go b/rgbanimations/utilFuncsMath.go similarity index 74% rename from rgbanimations/utilFuncs.go rename to rgbanimations/utilFuncsMath.go index e71cf51..e272e32 100644 --- a/rgbanimations/utilFuncs.go +++ b/rgbanimations/utilFuncsMath.go @@ -48,6 +48,13 @@ func Abs8(i int8) uint8 { return uint8(i) } +func Abs16(i int16) int16 { + if i < 0 { + return int16(-i) + } + return int16(i) +} + func Sin8(theta uint8) uint8 { offset := theta if theta&0x40 != 0 { @@ -78,3 +85,30 @@ func Sin8(theta uint8) uint8 { return uint8(y) } + +func Atan28(dy int16, dx int16) uint8 { + if dy == 0 { + if dx >= 0 { + return 0 + } else { + return 128 + } + } + + var absY int16 + if dy < 0 { + absY = -dy + } else { + absY = dy + } + var a int8 + if dx >= 0 { + a = int8(32 - (32 * (dx - absY) / (dx + absY))) + } else { + a = int8(96 - (32 * (dx + absY) / (dx - absY))) + } + if dy < 0 { + return uint8(-a) + } + return uint8(a) +} diff --git a/rgbanimations/utilFuncsRunners.go b/rgbanimations/utilFuncsRunners.go new file mode 100644 index 0000000..2307732 --- /dev/null +++ b/rgbanimations/utilFuncsRunners.go @@ -0,0 +1,28 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +var time = uint16(0) + +type effectRunnerIFunc func(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) +type effectRunnerDXDYFunc func(matrix *keyboard.RGBMatrix, dx int16, dy int16, time uint16) (uint8, uint8, uint8) + +func effectRunnerI(matrix *keyboard.RGBMatrix, mathFunc effectRunnerIFunc) { + timeScaled := Scale16by8(time, matrix.CurrentSpeed) + for i, val := range matrix.LedMatrixVals { + h, s, v := mathFunc(matrix, i, timeScaled) + val.R, val.G, val.B, val.A = HSVToRGB(h, s, v) + } + time++ +} + +func effectRunnerDXDY(matrix *keyboard.RGBMatrix, mathFunc effectRunnerDXDYFunc) { + timeScaled := Scale16by8(time, matrix.CurrentSpeed) + for i, val := range matrix.LedMatrixVals { + dx := int16(matrix.LedMatrixMapping[i].PhysicalX) - int16(matrix.CenterXPhysical) + dy := int16(matrix.LedMatrixMapping[i].PhysicalY) - int16(matrix.CenterYPhysical) + h, s, v := mathFunc(matrix, dx, dy, timeScaled) + val.R, val.G, val.B, val.A = HSVToRGB(h, s, v) + } + time++ +} diff --git a/rgbanimations/vialrgbBandPinwheelSat.go b/rgbanimations/vialrgbBandPinwheelSat.go new file mode 100644 index 0000000..d15b35d --- /dev/null +++ b/rgbanimations/vialrgbBandPinwheelSat.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetBandPinwheelSatAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBBandPinwheelSat, + AnimationType: keyboard.VIALRGB_EFFECT_BAND_PINWHEEL_SAT, + } +} + +func bandPinwheelSatMath(matrix *keyboard.RGBMatrix, dx int16, dy int16, time uint16) (uint8, uint8, uint8) { + s := Scale8(matrix.CurrentSaturation-uint8(time)-Atan28(dy, dx), matrix.CurrentSaturation) + return matrix.CurrentHue, s, matrix.CurrentValue +} + +func vialRGBBandPinwheelSat(matrix *keyboard.RGBMatrix) { + effectRunnerDXDY(matrix, bandPinwheelSatMath) +} diff --git a/rgbanimations/vialrgbBandPinwheelVal.go b/rgbanimations/vialrgbBandPinwheelVal.go new file mode 100644 index 0000000..c84d355 --- /dev/null +++ b/rgbanimations/vialrgbBandPinwheelVal.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetBandPinwheelValAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBBandPinwheelVal, + AnimationType: keyboard.VIALRGB_EFFECT_BAND_PINWHEEL_VAL, + } +} + +func bandPinwheelValMath(matrix *keyboard.RGBMatrix, dx int16, dy int16, time uint16) (uint8, uint8, uint8) { + v := Scale8(matrix.CurrentValue-uint8(time)-Atan28(dy, dx), matrix.CurrentValue) + return matrix.CurrentHue, matrix.CurrentSaturation, v +} + +func vialRGBBandPinwheelVal(matrix *keyboard.RGBMatrix) { + effectRunnerDXDY(matrix, bandPinwheelValMath) +} diff --git a/rgbanimations/vialrgbBandSat.go b/rgbanimations/vialrgbBandSat.go new file mode 100644 index 0000000..a5dc64f --- /dev/null +++ b/rgbanimations/vialrgbBandSat.go @@ -0,0 +1,26 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetBandSatAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBBandSat, + AnimationType: keyboard.VIALRGB_EFFECT_BAND_SAT, + } +} + +func bandSatMath(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) { + s16 := int16(matrix.CurrentSaturation) - + Abs16(int16(Scale8(matrix.LedMatrixMapping[i].PhysicalX, 228))+28-int16(time))*8 + var s8 uint8 + if s16 < 0 { + s8 = 0 + } else { + s8 = Scale8(uint8(s16), matrix.CurrentSaturation) + } + return matrix.CurrentHue, s8, matrix.CurrentValue +} + +func vialRGBBandSat(matrix *keyboard.RGBMatrix) { + effectRunnerI(matrix, bandSatMath) +} diff --git a/rgbanimations/vialrgbBandVal.go b/rgbanimations/vialrgbBandVal.go new file mode 100644 index 0000000..7dedc01 --- /dev/null +++ b/rgbanimations/vialrgbBandVal.go @@ -0,0 +1,26 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetBandValAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBBandVal, + AnimationType: keyboard.VIALRGB_EFFECT_BAND_VAL, + } +} + +func bandValMath(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) { + v16 := int16(matrix.CurrentValue) - + Abs16(int16(Scale8(matrix.LedMatrixMapping[i].PhysicalX, 228))+28-int16(time))*8 + var v8 uint8 + if v16 < 0 { + v8 = 0 + } else { + v8 = Scale8(uint8(v16), matrix.CurrentValue) + } + return matrix.CurrentHue, matrix.CurrentSaturation, v8 +} + +func vialRGBBandVal(matrix *keyboard.RGBMatrix) { + effectRunnerI(matrix, bandValMath) +} diff --git a/rgbanimations/vialrgbBreathing.go b/rgbanimations/vialrgbBreathing.go index ebe1d0d..b929f26 100644 --- a/rgbanimations/vialrgbBreathing.go +++ b/rgbanimations/vialrgbBreathing.go @@ -9,8 +9,6 @@ func GetBreathingAnim() keyboard.RgbAnimation { } } -var time = uint16(0) - func vialRGBBreathing(matrix *keyboard.RGBMatrix) { timeScaled := Scale16by8(time, matrix.CurrentSpeed) v := Scale8(Abs8(int8(Sin8(uint8(timeScaled))-128))*8, matrix.CurrentValue) From f874931da28a9a056fd9f5eec9680886e0d0bfa9 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Mon, 7 Oct 2024 20:02:50 +0200 Subject: [PATCH 24/27] implemented 6/6 Band Animations --- rgbanimations/utilFuncsMath.go | 30 +++++++++++++++++++++++++++ rgbanimations/utilFuncsRunners.go | 12 +++++++++++ rgbanimations/vialrgbBandSpiralSat.go | 19 +++++++++++++++++ rgbanimations/vialrgbBandSpiralVal.go | 19 +++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 rgbanimations/vialrgbBandSpiralSat.go create mode 100644 rgbanimations/vialrgbBandSpiralVal.go diff --git a/rgbanimations/utilFuncsMath.go b/rgbanimations/utilFuncsMath.go index e272e32..02e891a 100644 --- a/rgbanimations/utilFuncsMath.go +++ b/rgbanimations/utilFuncsMath.go @@ -33,6 +33,8 @@ func HSVToRGB(h, s, v uint8) (r, g, b, a uint8) { } } +// Math Functions based on lib8tion math8.h + func Scale8(i uint8, scale uint8) uint8 { return uint8((uint16(i) * uint16(scale)) >> 8) } @@ -112,3 +114,31 @@ func Atan28(dy int16, dx int16) uint8 { } return uint8(a) } + +func Sqrt16(x uint16) uint8 { + if x <= 1 { + return uint8(x) + } + + low := uint8(1) // lower bound + var hi, mid uint8 + + if x > 7904 { + hi = 255 + } else { + hi = uint8((x >> 5) + 5) // initial estimate for upper bound + } + + for ok := true; ok; ok = hi >= mid { // emulation of do-while loop + mid = (low + hi) >> 1 + if uint16(mid)*uint16(mid) > x { + hi = mid - 1 + } else { + if mid == 255 { + return 255 + } + low = mid + 1 + } + } + return low - 1 +} diff --git a/rgbanimations/utilFuncsRunners.go b/rgbanimations/utilFuncsRunners.go index 2307732..dfd6b42 100644 --- a/rgbanimations/utilFuncsRunners.go +++ b/rgbanimations/utilFuncsRunners.go @@ -6,6 +6,7 @@ var time = uint16(0) type effectRunnerIFunc func(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) type effectRunnerDXDYFunc func(matrix *keyboard.RGBMatrix, dx int16, dy int16, time uint16) (uint8, uint8, uint8) +type effectRunnerDXDYDistFunc func(matrix *keyboard.RGBMatrix, dx int16, dy int16, dist uint8, time uint16) (uint8, uint8, uint8) func effectRunnerI(matrix *keyboard.RGBMatrix, mathFunc effectRunnerIFunc) { timeScaled := Scale16by8(time, matrix.CurrentSpeed) @@ -26,3 +27,14 @@ func effectRunnerDXDY(matrix *keyboard.RGBMatrix, mathFunc effectRunnerDXDYFunc) } time++ } + +func effectRunnerDXDYDist(matrix *keyboard.RGBMatrix, mathFunc effectRunnerDXDYDistFunc) { + timeScaled := Scale16by8(time, matrix.CurrentSpeed) + for i, val := range matrix.LedMatrixVals { + dx := int16(matrix.LedMatrixMapping[i].PhysicalX) - int16(matrix.CenterXPhysical) + dy := int16(matrix.LedMatrixMapping[i].PhysicalY) - int16(matrix.CenterYPhysical) + dist := Sqrt16(uint16(dx*dx + dy*dy)) + h, s, v := mathFunc(matrix, dx, dy, dist, timeScaled) + val.R, val.G, val.B, val.A = HSVToRGB(h, s, v) + } +} diff --git a/rgbanimations/vialrgbBandSpiralSat.go b/rgbanimations/vialrgbBandSpiralSat.go new file mode 100644 index 0000000..f33993d --- /dev/null +++ b/rgbanimations/vialrgbBandSpiralSat.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetBansSpiralSatAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBBandSpiralSat, + AnimationType: keyboard.VIALRGB_EFFECT_BAND_SPIRAL_SAT, + } +} + +func bandSpiralSatMath(matrix *keyboard.RGBMatrix, dx int16, dy int16, dist uint8, time uint16) (uint8, uint8, uint8) { + s := Scale8(matrix.CurrentSaturation+dist-uint8(time)-Atan28(dy, dx), matrix.CurrentSaturation) + return matrix.CurrentHue, s, matrix.CurrentValue +} + +func vialRGBBandSpiralSat(matrix *keyboard.RGBMatrix) { + effectRunnerDXDYDist(matrix, bandSpiralSatMath) +} diff --git a/rgbanimations/vialrgbBandSpiralVal.go b/rgbanimations/vialrgbBandSpiralVal.go new file mode 100644 index 0000000..63d06e3 --- /dev/null +++ b/rgbanimations/vialrgbBandSpiralVal.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetBansSpiralValAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBBandSpiralVal, + AnimationType: keyboard.VIALRGB_EFFECT_BAND_SPIRAL_VAL, + } +} + +func bandSpiralValMath(matrix *keyboard.RGBMatrix, dx int16, dy int16, dist uint8, time uint16) (uint8, uint8, uint8) { + v := Scale8(matrix.CurrentValue+dist-uint8(time)-Atan28(dy, dx), matrix.CurrentValue) + return matrix.CurrentHue, matrix.CurrentSaturation, v +} + +func vialRGBBandSpiralVal(matrix *keyboard.RGBMatrix) { + effectRunnerDXDYDist(matrix, bandSpiralValMath) +} From ef7aafbc4141e57403abf102f59e2909ebda8b9e Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Mon, 7 Oct 2024 21:01:01 +0200 Subject: [PATCH 25/27] implemented All Cycle Animations --- rgbanimations/vialrgbCycleAll.go | 18 +++++++++++++++++ rgbanimations/vialrgbCycleLeftRight.go | 19 ++++++++++++++++++ rgbanimations/vialrgbCycleOutIn.go | 19 ++++++++++++++++++ rgbanimations/vialrgbCycleOutInDual.go | 21 ++++++++++++++++++++ rgbanimations/vialrgbCyclePinwheel.go | 19 ++++++++++++++++++ rgbanimations/vialrgbCycleSpiral.go | 19 ++++++++++++++++++ rgbanimations/vialrgbCycleUpDown.go | 19 ++++++++++++++++++ rgbanimations/vialrgbRainbowMovingChevron.go | 19 ++++++++++++++++++ 8 files changed, 153 insertions(+) create mode 100644 rgbanimations/vialrgbCycleAll.go create mode 100644 rgbanimations/vialrgbCycleLeftRight.go create mode 100644 rgbanimations/vialrgbCycleOutIn.go create mode 100644 rgbanimations/vialrgbCycleOutInDual.go create mode 100644 rgbanimations/vialrgbCyclePinwheel.go create mode 100644 rgbanimations/vialrgbCycleSpiral.go create mode 100644 rgbanimations/vialrgbCycleUpDown.go create mode 100644 rgbanimations/vialrgbRainbowMovingChevron.go diff --git a/rgbanimations/vialrgbCycleAll.go b/rgbanimations/vialrgbCycleAll.go new file mode 100644 index 0000000..f5cce2b --- /dev/null +++ b/rgbanimations/vialrgbCycleAll.go @@ -0,0 +1,18 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetCycleAllAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBCycleAll, + AnimationType: keyboard.VIALRGB_EFFECT_CYCLE_ALL, + } +} + +func cycleAllMath(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) { + return uint8(time), matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBCycleAll(matrix *keyboard.RGBMatrix) { + effectRunnerI(matrix, cycleAllMath) +} diff --git a/rgbanimations/vialrgbCycleLeftRight.go b/rgbanimations/vialrgbCycleLeftRight.go new file mode 100644 index 0000000..05359e5 --- /dev/null +++ b/rgbanimations/vialrgbCycleLeftRight.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetCycleLeftRightAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBCycleLeftRight, + AnimationType: keyboard.VIALRGB_EFFECT_CYCLE_LEFT_RIGHT, + } +} + +func cycleLeftRightMath(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) { + h := matrix.LedMatrixMapping[i].PhysicalX - uint8(time) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBCycleLeftRight(matrix *keyboard.RGBMatrix) { + effectRunnerI(matrix, cycleLeftRightMath) +} diff --git a/rgbanimations/vialrgbCycleOutIn.go b/rgbanimations/vialrgbCycleOutIn.go new file mode 100644 index 0000000..e52bf3e --- /dev/null +++ b/rgbanimations/vialrgbCycleOutIn.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetCycleOutInAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBCycleOutIn, + AnimationType: keyboard.VIALRGB_EFFECT_CYCLE_OUT_IN, + } +} + +func cycleOutInMath(matrix *keyboard.RGBMatrix, _ int16, _ int16, dist uint8, time uint16) (uint8, uint8, uint8) { + h := 3*dist/2 + uint8(time) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBCycleOutIn(matrix *keyboard.RGBMatrix) { + effectRunnerDXDYDist(matrix, cycleOutInMath) +} diff --git a/rgbanimations/vialrgbCycleOutInDual.go b/rgbanimations/vialrgbCycleOutInDual.go new file mode 100644 index 0000000..63be74d --- /dev/null +++ b/rgbanimations/vialrgbCycleOutInDual.go @@ -0,0 +1,21 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetCycleOutInDualAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBCycleOutInDual, + AnimationType: keyboard.VIALRGB_EFFECT_CYCLE_OUT_IN_DUAL, + } +} + +func cycleOutInDualMath(matrix *keyboard.RGBMatrix, dx int16, dy int16, time uint16) (uint8, uint8, uint8) { + dx = int16(matrix.CenterXPhysical/2) - Abs16(dx) + dist := Sqrt16(uint16(dx*dx) + uint16(dy*dy)) + h := 3*dist + uint8(time) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBCycleOutInDual(matrix *keyboard.RGBMatrix) { + effectRunnerDXDY(matrix, cycleOutInDualMath) +} diff --git a/rgbanimations/vialrgbCyclePinwheel.go b/rgbanimations/vialrgbCyclePinwheel.go new file mode 100644 index 0000000..f46f4f4 --- /dev/null +++ b/rgbanimations/vialrgbCyclePinwheel.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetCyclePinwheelAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBCyclePinwheel, + AnimationType: keyboard.VIALRGB_EFFECT_CYCLE_PINWHEEL, + } +} + +func cyclePinwheelMath(matrix *keyboard.RGBMatrix, dx int16, dy int16, time uint16) (uint8, uint8, uint8) { + h := Atan28(dy, dx) + uint8(time) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBCyclePinwheel(matrix *keyboard.RGBMatrix) { + effectRunnerDXDY(matrix, cyclePinwheelMath) +} diff --git a/rgbanimations/vialrgbCycleSpiral.go b/rgbanimations/vialrgbCycleSpiral.go new file mode 100644 index 0000000..3fd0294 --- /dev/null +++ b/rgbanimations/vialrgbCycleSpiral.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetCycleSpiralAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBCycleSpiral, + AnimationType: keyboard.VIALRGB_EFFECT_CYCLE_SPIRAL, + } +} + +func cycleSpiralMath(matrix *keyboard.RGBMatrix, dx int16, dy int16, dist uint8, time uint16) (uint8, uint8, uint8) { + h := dist - uint8(time) - Atan28(dy, dx) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBCycleSpiral(matrix *keyboard.RGBMatrix) { + effectRunnerDXDYDist(matrix, cycleSpiralMath) +} diff --git a/rgbanimations/vialrgbCycleUpDown.go b/rgbanimations/vialrgbCycleUpDown.go new file mode 100644 index 0000000..7bf24de --- /dev/null +++ b/rgbanimations/vialrgbCycleUpDown.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetCycleUpDownAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBCycleUpDown, + AnimationType: keyboard.VIALRGB_EFFECT_CYCLE_UP_DOWN, + } +} + +func cycleUpDownMath(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) { + h := matrix.LedMatrixMapping[i].PhysicalY - uint8(time) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBCycleUpDown(matrix *keyboard.RGBMatrix) { + effectRunnerI(matrix, cycleUpDownMath) +} diff --git a/rgbanimations/vialrgbRainbowMovingChevron.go b/rgbanimations/vialrgbRainbowMovingChevron.go new file mode 100644 index 0000000..2845737 --- /dev/null +++ b/rgbanimations/vialrgbRainbowMovingChevron.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetRainbowMovingChevronAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBRainbowMovingChevron, + AnimationType: keyboard.VIALRGB_EFFECT_RAINBOW_MOVING_CHEVRON, + } +} + +func rainbowMovingChevronMath(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) { + h := matrix.CurrentHue + Abs8(int8(matrix.LedMatrixMapping[i].PhysicalY-matrix.CenterYPhysical)+int8(matrix.CenterXPhysical-uint8(time))) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBRainbowMovingChevron(matrix *keyboard.RGBMatrix) { + effectRunnerI(matrix, rainbowMovingChevronMath) +} From 39bb1884ce3a5b4c57cfa3c8836887ceb3ef27f4 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Mon, 7 Oct 2024 21:37:42 +0200 Subject: [PATCH 26/27] implemented All Beacon Animations --- rgbanimations/utilFuncsMath.go | 4 ++++ rgbanimations/utilFuncsRunners.go | 13 +++++++++++++ rgbanimations/vialrgbDualBeacon.go | 19 +++++++++++++++++++ rgbanimations/vialrgbRainbowBeacon.go | 19 +++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 rgbanimations/vialrgbDualBeacon.go create mode 100644 rgbanimations/vialrgbRainbowBeacon.go diff --git a/rgbanimations/utilFuncsMath.go b/rgbanimations/utilFuncsMath.go index 02e891a..203b702 100644 --- a/rgbanimations/utilFuncsMath.go +++ b/rgbanimations/utilFuncsMath.go @@ -88,6 +88,10 @@ func Sin8(theta uint8) uint8 { return uint8(y) } +func Cos8(theta uint8) uint8 { + return Sin8(theta + 64) +} + func Atan28(dy int16, dx int16) uint8 { if dy == 0 { if dx >= 0 { diff --git a/rgbanimations/utilFuncsRunners.go b/rgbanimations/utilFuncsRunners.go index dfd6b42..24c66ee 100644 --- a/rgbanimations/utilFuncsRunners.go +++ b/rgbanimations/utilFuncsRunners.go @@ -7,6 +7,7 @@ var time = uint16(0) type effectRunnerIFunc func(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) type effectRunnerDXDYFunc func(matrix *keyboard.RGBMatrix, dx int16, dy int16, time uint16) (uint8, uint8, uint8) type effectRunnerDXDYDistFunc func(matrix *keyboard.RGBMatrix, dx int16, dy int16, dist uint8, time uint16) (uint8, uint8, uint8) +type effectRunnerSinCosIFunc func(matrix *keyboard.RGBMatrix, sin int8, cos int8, i int, time uint16) (uint8, uint8, uint8) func effectRunnerI(matrix *keyboard.RGBMatrix, mathFunc effectRunnerIFunc) { timeScaled := Scale16by8(time, matrix.CurrentSpeed) @@ -37,4 +38,16 @@ func effectRunnerDXDYDist(matrix *keyboard.RGBMatrix, mathFunc effectRunnerDXDYD h, s, v := mathFunc(matrix, dx, dy, dist, timeScaled) val.R, val.G, val.B, val.A = HSVToRGB(h, s, v) } + time++ +} + +func effectRunnerSinCosI(matrix *keyboard.RGBMatrix, mathFunc effectRunnerSinCosIFunc) { + timeScaled := Scale16by8(time, matrix.CurrentSpeed) + cosValue := int8(Cos8(uint8(time)) - 128) + sinValue := int8(Sin8(uint8(time)) - 128) + for i, val := range matrix.LedMatrixVals { + h, s, v := mathFunc(matrix, sinValue, cosValue, i, timeScaled) + val.R, val.G, val.B, val.A = HSVToRGB(h, s, v) + } + time++ } diff --git a/rgbanimations/vialrgbDualBeacon.go b/rgbanimations/vialrgbDualBeacon.go new file mode 100644 index 0000000..b9c3b3a --- /dev/null +++ b/rgbanimations/vialrgbDualBeacon.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetDualBeaconAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBDualBeacon, + AnimationType: keyboard.VIALRGB_EFFECT_DUAL_BEACON, + } +} + +func dualBeaconMath(matrix *keyboard.RGBMatrix, sin int8, cos int8, i int, _ uint16) (uint8, uint8, uint8) { + h := matrix.CurrentHue + uint8(int8(matrix.LedMatrixMapping[i].PhysicalY-matrix.CenterYPhysical)*cos+int8(matrix.LedMatrixMapping[i].PhysicalX-matrix.CenterXPhysical)*sin)/128 + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBDualBeacon(matrix *keyboard.RGBMatrix) { + effectRunnerSinCosI(matrix, dualBeaconMath) +} diff --git a/rgbanimations/vialrgbRainbowBeacon.go b/rgbanimations/vialrgbRainbowBeacon.go new file mode 100644 index 0000000..7e71478 --- /dev/null +++ b/rgbanimations/vialrgbRainbowBeacon.go @@ -0,0 +1,19 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetRainbowBeaconAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBRainbowBeacon, + AnimationType: keyboard.VIALRGB_EFFECT_RAINBOW_BEACON, + } +} + +func rainbowBeaconMath(matrix *keyboard.RGBMatrix, sin int8, cos int8, i int, _ uint16) (uint8, uint8, uint8) { + h := matrix.CurrentHue + uint8(int8(matrix.LedMatrixMapping[i].PhysicalY-matrix.CenterYPhysical)*2*cos+int8(matrix.LedMatrixMapping[i].PhysicalX-matrix.CenterXPhysical)*2*sin)/128 + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBRainbowBeacon(matrix *keyboard.RGBMatrix) { + effectRunnerSinCosI(matrix, rainbowBeaconMath) +} From 381c069a35d127ec20caa218f8e58b01c17bfa85 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Mon, 7 Oct 2024 23:23:30 +0200 Subject: [PATCH 27/27] implemented All Animations that are no rain or reactive effects --- ...AlphasModsAnim.go => vialrgbAlphasMods.go} | 0 ...{vialrgbDirectAnim.go => vialrgbDirect.go} | 0 rgbanimations/vialrgbHueBreathing.go | 25 +++++++++++++++++++ rgbanimations/vialrgbHuePendulum.go | 20 +++++++++++++++ rgbanimations/vialrgbHueWave.go | 20 +++++++++++++++ rgbanimations/vialrgbRainbowPinwheels.go | 23 +++++++++++++++++ ...SolidColorAnim.go => vialrgbSolidColor.go} | 0 7 files changed, 88 insertions(+) rename rgbanimations/{vialrgbAlphasModsAnim.go => vialrgbAlphasMods.go} (100%) rename rgbanimations/{vialrgbDirectAnim.go => vialrgbDirect.go} (100%) create mode 100644 rgbanimations/vialrgbHueBreathing.go create mode 100644 rgbanimations/vialrgbHuePendulum.go create mode 100644 rgbanimations/vialrgbHueWave.go create mode 100644 rgbanimations/vialrgbRainbowPinwheels.go rename rgbanimations/{vialrgbSolidColorAnim.go => vialrgbSolidColor.go} (100%) diff --git a/rgbanimations/vialrgbAlphasModsAnim.go b/rgbanimations/vialrgbAlphasMods.go similarity index 100% rename from rgbanimations/vialrgbAlphasModsAnim.go rename to rgbanimations/vialrgbAlphasMods.go diff --git a/rgbanimations/vialrgbDirectAnim.go b/rgbanimations/vialrgbDirect.go similarity index 100% rename from rgbanimations/vialrgbDirectAnim.go rename to rgbanimations/vialrgbDirect.go diff --git a/rgbanimations/vialrgbHueBreathing.go b/rgbanimations/vialrgbHueBreathing.go new file mode 100644 index 0000000..f28e0fb --- /dev/null +++ b/rgbanimations/vialrgbHueBreathing.go @@ -0,0 +1,25 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetHueBreathingAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBHueBreathing, + AnimationType: keyboard.VIALRGB_EFFECT_HUE_BREATHING, + } +} + +func vialRGBHueBreathing(matrix *keyboard.RGBMatrix) { + huedelta := uint8(12) + h, s, v := matrix.CurrentHue, matrix.CurrentSaturation, matrix.CurrentValue + timeScaled := Scale16by8(time, matrix.CurrentSpeed/8) + h = h + Scale8(Abs8(int8(Sin8(uint8(timeScaled)))-127)*2, huedelta) + r, g, b, a := HSVToRGB(h, s, v) + for _, val := range matrix.LedMatrixVals { + val.R = r + val.G = g + val.B = b + val.A = a + } + time++ +} diff --git a/rgbanimations/vialrgbHuePendulum.go b/rgbanimations/vialrgbHuePendulum.go new file mode 100644 index 0000000..4525243 --- /dev/null +++ b/rgbanimations/vialrgbHuePendulum.go @@ -0,0 +1,20 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetHuePendulumAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBHuePendulum, + AnimationType: keyboard.VIALRGB_EFFECT_HUE_PENDULUM, + } +} + +func huePendulumMath(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) { + huedelta := uint8(12) + h := matrix.CurrentHue + Scale8(Abs8(int8(Sin8(uint8(time))+(matrix.LedMatrixMapping[i].PhysicalX)-128))*2, huedelta) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBHuePendulum(matrix *keyboard.RGBMatrix) { + effectRunnerI(matrix, huePendulumMath) +} diff --git a/rgbanimations/vialrgbHueWave.go b/rgbanimations/vialrgbHueWave.go new file mode 100644 index 0000000..3608ad9 --- /dev/null +++ b/rgbanimations/vialrgbHueWave.go @@ -0,0 +1,20 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetHueWave() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBHueWave, + AnimationType: keyboard.VIALRGB_EFFECT_HUE_WAVE, + } +} + +func hueWaveMath(matrix *keyboard.RGBMatrix, i int, time uint16) (uint8, uint8, uint8) { + huedelta := uint8(24) + h := matrix.CurrentHue + Scale8(Abs8(int8(matrix.LedMatrixMapping[i].PhysicalX-uint8(time))), huedelta) + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBHueWave(matrix *keyboard.RGBMatrix) { + effectRunnerI(matrix, hueWaveMath) +} diff --git a/rgbanimations/vialrgbRainbowPinwheels.go b/rgbanimations/vialrgbRainbowPinwheels.go new file mode 100644 index 0000000..9163626 --- /dev/null +++ b/rgbanimations/vialrgbRainbowPinwheels.go @@ -0,0 +1,23 @@ +package rgbanimations + +import keyboard "github.com/percyjw-2/tinygo-keyboard" + +func GetRainbowPinwheelsAnim() keyboard.RgbAnimation { + return keyboard.RgbAnimation{ + AnimationFunc: vialRGBRainbowPinwheels, + AnimationType: keyboard.VIALRGB_EFFECT_RAINBOW_PINWHEELS, + } +} + +func rainbowPinwheelsMath(matrix *keyboard.RGBMatrix, sin int8, cos int8, i int, time uint16) (uint8, uint8, uint8) { + h := matrix.CurrentHue + + uint8( + int8(matrix.LedMatrixMapping[i].PhysicalY-matrix.CenterYPhysical)*3*cos+ + int8(56-Abs8(int8(matrix.LedMatrixMapping[i].PhysicalX-matrix.CenterXPhysical)))*2*sin, + )/128 + return h, matrix.CurrentSaturation, matrix.CurrentValue +} + +func vialRGBRainbowPinwheels(matrix *keyboard.RGBMatrix) { + effectRunnerSinCosI(matrix, rainbowPinwheelsMath) +} diff --git a/rgbanimations/vialrgbSolidColorAnim.go b/rgbanimations/vialrgbSolidColor.go similarity index 100% rename from rgbanimations/vialrgbSolidColorAnim.go rename to rgbanimations/vialrgbSolidColor.go