-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters