From 02dff8c8253bca894850bb3b06d8303eb8a96ee0 Mon Sep 17 00:00:00 2001 From: PercyJW-2 Date: Fri, 16 Aug 2024 22:25:29 +0200 Subject: [PATCH] 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++ +}