all repos — visor @ 4332225c9c1a67916a542fa634ec193e5143ab25

Unnamed repository; edit this file 'description' to name the repository.

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
package main

import (
	"image/color"
	"machine"
	"math"

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

	for i, value := range matrix {
		out[lookup[i]] = value
	}
	return out
}

func eyes(canvas []color.RGBA, config visorConfig) []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 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 {
		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)
	}
	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},
	}
	device.WriteColors(eyes(leds, config))
}