Skip to content

Commit

Permalink
implemented Breathing effect, TODO: rework animation speed
Browse files Browse the repository at this point in the history
  • Loading branch information
PercyJW-2 committed Aug 16, 2024
1 parent 9f797ef commit 02dff8c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
42 changes: 42 additions & 0 deletions rgbanimations/utilFuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 24 additions & 0 deletions rgbanimations/vialrgbBreathing.go
Original file line number Diff line number Diff line change
@@ -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++
}

0 comments on commit 02dff8c

Please sign in to comment.