Created
July 4, 2021 18:33
-
-
Save frankkienl/ce3524d47e48d3d264eb28231b554b38 to your computer and use it in GitHub Desktop.
Led Matrix 8x8 to Base64
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 java.awt.Color | |
import java.io.File | |
import java.util.* | |
import javax.imageio.ImageIO | |
import kotlin.system.exitProcess | |
fun main(args: Array<String>) { | |
if (args.isEmpty()) { | |
printHelp() | |
exitProcess(0) | |
} | |
val filename = args[0] | |
val file = File(filename) | |
if (!file.exists()) { | |
System.err.println("File does not exist") | |
exitProcess(-1) | |
} | |
if (!file.canRead()) { | |
System.err.println("Can't read file") | |
exitProcess(-1) | |
} | |
if (file.isDirectory) { | |
file.listFiles().forEach { someFile -> | |
val leds = parseFile(someFile) | |
leds?.let { | |
val base64 = ledsToBase64(leds) | |
println(someFile.name) | |
println(base64) | |
} | |
} | |
} else { | |
val leds = parseFile(File(filename)) | |
leds?.let { | |
val sourceCode = ledsToSourcecode(leds) | |
//println(sourceCode) | |
val base64 = ledsToBase64(leds) | |
println(base64) | |
} | |
} | |
} | |
fun printHelp() { | |
print( | |
""" | |
usage: matrix <FILE> | |
Transform png image to bytes for arduino | |
Needs to be 8x8 rgb png no transparency | |
""".trimIndent() | |
) | |
} | |
fun ledsToSourcecode(leds: List<CRGB>): String { | |
val sb = StringBuilder() | |
for (i in 0..63) { | |
val led = leds[i] | |
sb.append(" leds[$i] = CRGB(${led.r},${led.g},${led.b});\n") | |
} | |
return sb.toString() | |
} | |
fun ledsToBase64(leds: List<CRGB>): String { | |
val bytes = mutableListOf<Byte>() | |
for (i in 0..63) { | |
val led = leds[i] | |
bytes.add(led.r.toByte()) | |
bytes.add(led.g.toByte()) | |
bytes.add(led.b.toByte()) | |
} | |
val byteArray = bytes.toByteArray() | |
val base64String = Base64.getEncoder().encodeToString(byteArray) | |
return base64String | |
} | |
fun parseFile(file: File): List<CRGB>? { | |
val bufferedImage = ImageIO.read(file) ?: return null | |
val leds = mutableListOf<CRGB>() | |
//fill with black | |
for (ii in 1..64) { | |
leds.add(CRGB(0, 0, 0)) | |
} | |
for (y in 0..7) { | |
for (x in 0..7) { | |
val rgbInt = bufferedImage.getRGB(x, y) | |
val color = Color(rgbInt) | |
leds[xy(x, y)].r = color.red | |
leds[xy(x, y)].g = color.green | |
leds[xy(x, y)].b = color.blue | |
} | |
} | |
return leds | |
} | |
fun xy(x: Int, y: Int): Int { | |
val MATRIX_WIDTH = 8 | |
val i = if ((y and 0x01 == 1)) { | |
// Odd rows run backwards | |
val reverseX: Int = MATRIX_WIDTH - 1 - x | |
y * MATRIX_WIDTH + reverseX | |
} else { | |
// Even rows run forwards | |
y * MATRIX_WIDTH + x | |
} | |
return i | |
} | |
data class CRGB(var r: Int, var g: Int, var b: Int); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment