main.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
package main
import (
"image/color"
"machine"
"math"
"math/rand"
"time"
"tinygo.org/x/drivers/ws2812"
)
const ledCount = 512
type visorConfig struct {
screenWidth int
screenHeight int
spacingOuter int
spacingTop int
eyeWidth int
eyeHeight int
borderWidth int // 0 = full infil
eyeColor color.RGBA
}
func unfuckMatrix(matrix []color.RGBA) []color.RGBA {
lookup := []int{
0, 31, 32, 63, 64, 95, 96, 127, 128, 159, 160, 191, 192, 223, 224, 255, 256, 287, 288, 319, 320, 351, 352, 383, 384, 415, 416, 447, 448, 479, 480, 511,
1, 30, 33, 62, 65, 94, 97, 126, 129, 158, 161, 190, 193, 222, 225, 254, 257, 286, 289, 318, 321, 350, 353, 382, 385, 414, 417, 446, 449, 478, 481, 510,
2, 29, 34, 61, 66, 93, 98, 125, 130, 157, 162, 189, 194, 221, 226, 253, 258, 285, 290, 317, 322, 349, 354, 381, 386, 413, 418, 445, 450, 477, 482, 509,
3, 28, 35, 60, 67, 92, 99, 124, 131, 156, 163, 188, 195, 220, 227, 252, 259, 284, 291, 316, 323, 348, 355, 380, 387, 412, 419, 444, 451, 476, 483, 508,
4, 27, 36, 59, 68, 91, 100, 123, 132, 155, 164, 187, 196, 219, 228, 251, 260, 283, 292, 315, 324, 347, 356, 379, 388, 411, 420, 443, 452, 475, 484, 507,
5, 26, 37, 58, 69, 90, 101, 122, 133, 154, 165, 186, 197, 218, 229, 250, 261, 282, 293, 314, 325, 346, 357, 378, 389, 410, 421, 442, 453, 474, 485, 506,
6, 25, 38, 57, 70, 89, 102, 121, 134, 153, 166, 185, 198, 217, 230, 249, 262, 281, 294, 313, 326, 345, 358, 377, 390, 409, 422, 441, 454, 473, 486, 505,
7, 24, 39, 56, 71, 88, 103, 120, 135, 152, 167, 184, 199, 216, 231, 248, 263, 280, 295, 312, 327, 344, 359, 376, 391, 408, 423, 440, 455, 472, 487, 504,
8, 23, 40, 55, 72, 87, 104, 119, 136, 151, 168, 183, 200, 215, 232, 247, 264, 279, 296, 311, 328, 343, 360, 375, 392, 407, 424, 439, 456, 471, 488, 503,
9, 22, 41, 54, 73, 86, 105, 118, 137, 150, 169, 182, 201, 214, 233, 246, 265, 278, 297, 310, 329, 342, 361, 374, 393, 406, 425, 438, 457, 470, 489, 502,
10, 21, 42, 53, 74, 85, 106, 117, 138, 149, 170, 181, 202, 213, 234, 245, 266, 277, 298, 309, 330, 341, 362, 373, 394, 405, 426, 437, 458, 469, 490, 501,
11, 20, 43, 52, 75, 84, 107, 116, 139, 148, 171, 180, 203, 212, 235, 244, 267, 276, 299, 308, 331, 340, 363, 372, 395, 404, 427, 436, 459, 468, 491, 500,
12, 19, 44, 51, 76, 83, 108, 115, 140, 147, 172, 179, 204, 211, 236, 243, 268, 275, 300, 307, 332, 339, 364, 371, 396, 403, 428, 435, 460, 467, 492, 499,
13, 18, 45, 50, 77, 82, 109, 114, 141, 146, 173, 178, 205, 210, 237, 242, 269, 274, 301, 306, 333, 338, 365, 370, 397, 402, 429, 434, 461, 466, 493, 498,
14, 17, 46, 49, 78, 81, 110, 113, 142, 145, 174, 177, 206, 209, 238, 241, 270, 273, 302, 305, 334, 337, 366, 369, 398, 401, 430, 433, 462, 465, 494, 497,
15, 16, 47, 48, 79, 80, 111, 112, 143, 144, 175, 176, 207, 208, 239, 240, 271, 272, 303, 304, 335, 336, 367, 368, 399, 400, 431, 432, 463, 464, 495, 496,
}
out := make([]color.RGBA, len(matrix))
for i, value := range matrix {
out[lookup[i]] = value
}
return out
}
func eyes(canvas []color.RGBA, config visorConfig, closed bool) []color.RGBA {
for i := range canvas {
x := i % 32
y := int(math.Floor(float64(i) / 32))
if y > config.spacingTop && y < config.spacingTop+config.eyeHeight {
if !closed || (y > config.spacingTop+(config.eyeHeight/2) && y <= config.spacingTop+(config.eyeHeight/2)+2) {
if x > config.spacingOuter && x < config.spacingOuter+config.eyeWidth {
canvas[i] = config.eyeColor
}
if x > config.screenWidth-(config.spacingOuter+config.eyeWidth) && x < config.screenWidth-config.spacingOuter {
canvas[i] = config.eyeColor
}
}
}
}
if config.borderWidth > 0 && !closed {
infilConfig := config
infilConfig.eyeHeight -= config.borderWidth * 2
infilConfig.eyeWidth -= config.borderWidth * 2
infilConfig.spacingOuter += config.borderWidth
infilConfig.spacingTop += config.borderWidth
infilConfig.borderWidth = 0
infilConfig.eyeColor = color.RGBA{R: 0, G: 0, B: 0}
return eyes(canvas, infilConfig, false)
}
return unfuckMatrix(canvas)
}
func main() {
var pin machine.Pin
pin = 13
pin.Configure(machine.PinConfig{Mode: machine.PinOutput})
device := ws2812.NewWS2812(pin)
leds := make([]color.RGBA, ledCount)
config := visorConfig{
screenWidth: 32,
screenHeight: 16,
spacingOuter: 6,
spacingTop: 2,
eyeWidth: 6,
eyeHeight: 10,
borderWidth: 1,
eyeColor: color.RGBA{R: 2, G: 0, B: 2},
}
open := eyes(leds, config, false)
closedConfig := config
closedConfig.eyeColor = color.RGBA{R: 0, G: 0, B: 0}
closed := eyes(eyes(leds, closedConfig, false), config, true)
for {
if rand.Float32() > 0.1 {
device.WriteColors(open)
} else {
device.WriteColors(closed)
}
time.Sleep(time.Millisecond * 200)
}
}
|