Last active
June 27, 2018 20:47
-
-
Save tyczj/44849ae6e3c0f9f7c8015bc053e1c101 to your computer and use it in GitHub Desktop.
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
import android.graphics.Color | |
import android.support.annotation.WorkerThread | |
import android.util.Log | |
import fr.bmartel.opc.OpcClient | |
import fr.bmartel.opc.OpcDevice | |
import fr.bmartel.opc.PixelStrip | |
import java.util.ArrayList | |
/** | |
* Class creates a light pattern that scrolls white lines across red lights | |
* 4 Strips of 20 white led lines scroll across while the rest of the led's are red | |
* The class takes an OPC Client (Open Pixel Control), OPC Device (Device that displays the lights) | |
* and an ArrayList of PixelStrip objects that hold the number of light strips (max 64 lights/strip) for a FadeCandy device | |
*/ | |
class RedWhiteScroll(client: OpcClient?, device: OpcDevice?, strips: ArrayList<PixelStrip>) { | |
private var pixelStrips: ArrayList<PixelStrip> = ArrayList<PixelStrip>() | |
private var opcClient: OpcClient? = null | |
private var opcDevice: OpcDevice? = null | |
private var whitePixels: ArrayList<ArrayList<Int>> = ArrayList<ArrayList<Int>>() | |
//Brightness of the led's | |
var brightness:Int = 100 | |
set(value){ | |
field = value | |
updateBrightness(value) | |
} | |
//Boolean to keep track if animation is running and used to stop the pattern display | |
private var running = false | |
init{ | |
this.opcClient = client | |
this.opcDevice = device | |
this.pixelStrips = strips | |
} | |
/** | |
* Updates the brightness of the led's | |
*/ | |
private fun updateBrightness(brightness:Int){ | |
opcDevice?.setColorCorrection(2.5f,brightness / 100f,brightness / 100f,brightness / 100f) | |
show() | |
} | |
/** | |
* Sends the data to the lights to be displayed | |
* This is a blocking call and should be called from a non-UI thread | |
*/ | |
@WorkerThread | |
private fun show(){ | |
opcClient!!.show() | |
} | |
/** | |
* Starts the pattern sequence in a new Thread | |
* New Thread is used so that the pattern can run indefinitely and not block other things from happening | |
*/ | |
fun run(){ | |
Thread{ | |
//Set all the led's to red at start | |
running = true | |
for (strip in pixelStrips){ | |
for (j in 0 until strip.pixelCount){ | |
strip.setPixelColor(j, Color.RED) | |
} | |
} | |
show() | |
//start the scrolling pattern | |
while(running){ | |
updateBrightness(brightness) | |
var isWhite = true //used for alternating white and red | |
//even though there are 164 lights we use 184 so that the white lights can scroll off the "end" | |
for(i in 0 until 184){ | |
if((i%20) == 0 && isWhite){ | |
//Creates 4 strips of 20 led's that will be white | |
if(whitePixels.size < 4){ | |
whitePixels.add(createStrip(i)) | |
} | |
isWhite = !isWhite | |
}else if((i%20) == 0){ | |
isWhite = !isWhite | |
} | |
//Set all led's red again | |
for (strip in pixelStrips){ | |
for (j in 0 until strip.pixelCount){ | |
strip.setPixelColor(j, Color.RED) | |
} | |
} | |
showPixels() | |
show() | |
movePixels() | |
// slow down the pattern speed | |
Thread.sleep(25) | |
} | |
} | |
}.start() | |
} | |
/** | |
* Creates an ArrayList of positions for the white led's | |
*/ | |
private fun createStrip(index:Int): ArrayList<Int>{ | |
//Start at -19 so that the pattern appears to be scrolling in from the side | |
return (-19 until 1).mapTo(ArrayList<Int>()) { it } | |
} | |
/** | |
* Sets specific led's to white | |
* Since fadecandy can only do 64 led's per connection we have to do some index shifting based on the led number | |
*/ | |
private fun showPixels(){ | |
whitePixels.forEach { | |
it.forEach { | |
if(it in 0..163){ | |
when{ | |
it < 64 -> pixelStrips[0].setPixelColor(it, Color.WHITE) | |
it < 128 -> pixelStrips[1].setPixelColor(it-64, Color.WHITE) | |
else -> pixelStrips[2].setPixelColor(it-128, Color.WHITE) | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Shifts the white indexes by one so that scrolling happens | |
*/ | |
private fun movePixels(){ | |
for(i in 0 until whitePixels.size){ | |
for(j in 0 until whitePixels[i].size){ | |
var newIndex:Int = whitePixels[i][j]+1 | |
//Since there are only 164 led's reset the index back to zero | |
if(newIndex > 163){ | |
newIndex = 0 | |
} | |
whitePixels[i][j] = newIndex | |
} | |
} | |
} | |
/** | |
* Stops the patter from running | |
*/ | |
fun stop(){ | |
running = false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment