Skip to content

Commit

Permalink
Future LED API?
Browse files Browse the repository at this point in the history
  • Loading branch information
Ow1e committed Apr 26, 2024
1 parent 1a37209 commit 54a11c9
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/coralis/commands/BlankCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BlankCommand(private val onStart: () -> Unit, private val commandName : St
* This function checks if the command is finished.
* @return false as the command is never finished until override.
*/
override fun isFinished() = false
override fun isFinished() = true

/**
* This function checks if the command runs when disabled.
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/coralis/led/Animation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package coralis.led

import edu.wpi.first.wpilibj.AddressableLEDBuffer

interface Animation {
fun onStart()
fun onUpdate(buffer: AddressableLEDBuffer, startingIndex : Int, endingIndex : Int) {}
}
35 changes: 35 additions & 0 deletions src/main/kotlin/coralis/led/LightController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package coralis.led

import edu.wpi.first.wpilibj.AddressableLED
import edu.wpi.first.wpilibj.AddressableLEDBuffer
import edu.wpi.first.wpilibj.Notifier

class LightController(
private val ledPin : Int,
private val ledCount : Int,
private val section: Array<LightSection>,
private val updateFreqHz : Int = 10
) {
private val ledChannel = AddressableLED(ledPin)
private val ledBuffer = AddressableLEDBuffer(ledCount)
private val notifier = Notifier(this::update)

init {
// Setup LED stuff
ledChannel.setLength(ledCount)
ledChannel.start()

notifier.setName("LightController")
notifier.startPeriodic(1.0 / updateFreqHz)
}

private fun update() {
for (i in section.indices) {
section[i].update(ledBuffer)
}

syncBuffer()
}

private fun syncBuffer() = ledChannel.setData(ledBuffer)
}
27 changes: 27 additions & 0 deletions src/main/kotlin/coralis/led/LightSection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package coralis.led

import edu.wpi.first.wpilibj.AddressableLEDBuffer

class LightSection(
private var startingIndex: Int,
private var endingIndex: Int,
startingAnimation: Animation
) {
private var currentAnimation: Animation = startingAnimation

var animation
get() = currentAnimation
set(value) {
value.onStart()
currentAnimation = value
}

fun update(buffer: AddressableLEDBuffer) {
currentAnimation.onUpdate(buffer, startingIndex, endingIndex)
}

fun changeSection(startingIndex: Int, endingIndex: Int) {
this.startingIndex = startingIndex
this.endingIndex = endingIndex
}
}
14 changes: 14 additions & 0 deletions src/main/kotlin/coralis/led/animations/Pulse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package coralis.led.animations

import coralis.led.Animation
import edu.wpi.first.wpilibj.AddressableLEDBuffer

class Pulse : Animation {
override fun onStart() {
TODO("Not yet implemented")
}

override fun onUpdate(buffer: AddressableLEDBuffer, startingIndex: Int, endingIndex: Int) {
TODO("Not yet implemented")
}
}
22 changes: 22 additions & 0 deletions src/main/kotlin/coralis/led/animations/Rainbow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package coralis.led.animations

import coralis.led.Animation
import edu.wpi.first.wpilibj.AddressableLEDBuffer

class Rainbow : Animation {
private var hue = 0

override fun onStart() {
hue = 0
}

override fun onUpdate(buffer: AddressableLEDBuffer, startingIndex: Int, endingIndex: Int) {
for (i in startingIndex..endingIndex) {
val hue = (this.hue + (i * 180 / (endingIndex - startingIndex))) % 180
buffer.setHSV(i, hue, 255, 128)
}

hue += 3
hue %= 180
}
}
18 changes: 18 additions & 0 deletions src/main/kotlin/coralis/led/animations/Solid.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package coralis.led.animations

import coralis.led.Animation
import edu.wpi.first.wpilibj.AddressableLEDBuffer

class Solid(
private val red: Int = 255,
private val green: Int = 255,
private val blue: Int = 255
) : Animation {
override fun onStart() {} // Not needed

override fun onUpdate(buffer: AddressableLEDBuffer, startingIndex: Int, endingIndex: Int) {
for (i in startingIndex..endingIndex) {
buffer.setRGB(i, red, green, blue)
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/coralis/utils/PowerLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PowerLogger(
val switchableChannel = pdh.switchableChannel

init {
notifier.setName("RevPowerLogger")
notifier.setName("PowerLogger")
notifier.startPeriodic(0.1)
}

Expand Down

0 comments on commit 54a11c9

Please sign in to comment.