Skip to content

Commit

Permalink
implemented 6/6 Band Animations
Browse files Browse the repository at this point in the history
  • Loading branch information
PercyJW-2 committed Oct 7, 2024
1 parent 5c16bf8 commit f874931
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
30 changes: 30 additions & 0 deletions rgbanimations/utilFuncsMath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions rgbanimations/utilFuncsRunners.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
19 changes: 19 additions & 0 deletions rgbanimations/vialrgbBandSpiralSat.go
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 19 additions & 0 deletions rgbanimations/vialrgbBandSpiralVal.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit f874931

Please sign in to comment.