Skip to content

Instantly share code, notes, and snippets.

View tomaszrykala's full-sized avatar

Tomasz Rykała tomaszrykala

View GitHub Profile
@tomaszrykala
tomaszrykala / LedStripController.kt
Created January 20, 2018 23:58
LedStripController.kt
class LedStripController(private val supplier: LedStripSupplier) : BaseController {
private val leds = Led.values().filterNot { it == Led.NONE || it == Led.ALL }
private val ledStates = HashMap<Led, Boolean>(supplier.getLength())
fun light(led: Led) {
when (led) {
Led.ALL -> putAll(true)
else -> ledStates.put(led, true)
}
@tomaszrykala
tomaszrykala / LedStripSupplierImpl.kt
Created January 20, 2018 23:57
LedStripSupplierImpl.kt
class LedStripSupplierImpl : LedStripSupplier {
private val apa102: Apa102 by lazy {
RainbowHat.openLedStrip().apply {
direction = Apa102.Direction.REVERSED
brightness = 7
}
}
override fun getLength(): Int = RainbowHat.LEDSTRIP_LENGTH
@tomaszrykala
tomaszrykala / LedStripSupplier.kt
Last active January 27, 2018 14:22
LedStripSupplier.kt
const val TURN_OFF_COLOR = -1
interface LedStripSupplier : BaseSupplier {
fun light(colors: IntArray)
fun getLength(): Int
}
@tomaszrykala
tomaszrykala / AbcButtonsController.kt
Last active January 27, 2018 14:21
AbcButtonsController.kt
class AbcButtonsController(private val supplier: AbcButtonsSupplier) : BaseController, AbcButtonsSupplier.Listener {
private lateinit var listener: AbcButton.Listener
private var lastPressed: AbcButton? = null
init {
supplier.setListener(this)
}
fun setListener(listener: AbcButton.Listener) {
@tomaszrykala
tomaszrykala / AbcButtonsSupplierImpl.kt
Created January 20, 2018 23:52
AbcButtonsSupplierImpl.kt
class AbcButtonsSupplierImpl : AbcButtonsSupplier {
private val buttons: Map<Button, AbcButton> = mapOf(
RainbowHat.openButtonA() to AbcButton.A,
RainbowHat.openButtonB() to AbcButton.B,
RainbowHat.openButtonC() to AbcButton.C
)
private lateinit var listener: AbcButtonsSupplier.Listener
@tomaszrykala
tomaszrykala / AbcButtonsSupplier.kt
Created January 20, 2018 23:50
AbcButtonsSupplier.kt
interface AbcButtonsSupplier : BaseSupplier, Button.OnButtonEventListener {
fun setListener(listener: Listener)
interface Listener {
fun onButtonEvent(abcButton: AbcButton, pressed: Boolean)
}
}
@tomaszrykala
tomaszrykala / Timer.kt
Last active January 27, 2018 14:16
Timer.kt
const val MSG = 1
class Timer(private val listener: Timer.Listener) {
interface Listener {
fun onTick(tick: Double)
fun onStart()
fun onStop()
}
@tomaszrykala
tomaszrykala / GameController.kt
Last active January 27, 2018 14:33
GameController.kt
class GameController(private val abcButtons: AbcButtonsController,
private val abcLeds: AbcLedsController,
private val digiDisplay: DigiDisplayController,
private val timer: Timer,
private val game: Game) : AbcButton.Listener {
private val closeables: List<AutoCloseable> = listOf(abcButtons, abcLeds, digiDisplay)
init { abcButtons.setListener(this) }
@tomaszrykala
tomaszrykala / DoReFindMiActivity.kt
Created January 20, 2018 23:39
DoReFindMiActivity.kt
class DoReFindMiActivity : Activity() {
private val abcButtonsController = AbcButtonsController(AbcButtonsSupplierImpl())
private val abcLedsController = AbcLedsController(AbcLedsSupplierImpl())
private val digiDisplayController = DigiDisplayController(DigiDisplaySupplierImpl())
private val ledStripController = LedStripController(LedStripSupplierImpl())
private val buzzerController = BuzzerController(BuzzerSupplierImpl())
private val gameController: GameController by lazy {
GameController(
@tomaszrykala
tomaszrykala / MidiControls.kt
Created December 23, 2017 19:24
MidiControls.kt
class MidiControls(presenter: MidiControllerContract.Presenter) {
private val driver: Driver = Driver()
private val mcpDriverManager: McpDriverManager = McpDriverManager(presenter, driver)
private val midiButtonDrivers: MutableList<ButtonInputDriver> = mutableListOf()
private val midiButtonMapping: Map<String, Int> = mapOf(
driver.getBtn0() to KeyEvent.KEYCODE_0,
driver.getBtn1() to KeyEvent.KEYCODE_1,
driver.getBtn2() to KeyEvent.KEYCODE_2,
driver.getBtn3() to KeyEvent.KEYCODE_3