Last active
August 29, 2015 14:03
-
-
Save TJC/16ff3752b8c08c6bbe52 to your computer and use it in GitHub Desktop.
Quick knock-up of scrolling chevron effect
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
package main | |
import "fmt" | |
import "time" | |
import "github.com/mgutz/ansi" | |
const ledsAcross = 12 // rows of display | |
const ledsDown = 16 // columns of display (was 8) | |
const ledTotal = 12*16 | |
type Colour struct { | |
r, g, b int | |
} | |
var LEDs[ledTotal] Colour | |
// define a few colour shortcuts: | |
var black = Colour{0,0,0} | |
var red = Colour{255,0,0} | |
var blue = Colour{0,0,255} | |
var green = Colour{0,255,0} | |
var yellow = Colour{200,200,0} | |
func main() { | |
fmt.Println("total LEDs:", ledTotal) | |
blankLEDs() | |
setLED(0, 0, red) | |
setLED(ledsAcross-1, 0, green) | |
setLED(0, ledsDown-1, blue) | |
setLED(ledsAcross-1, ledsDown-1, yellow) | |
printLEDs() | |
time.Sleep(1000 * time.Millisecond) | |
chevronDemo(1) | |
chevronDemo(-1) | |
} | |
// Note that this clips coordinates to allowed region | |
func setLED(x,y int, c Colour) { | |
if x >= 0 && x < ledsAcross && y >= 0 && y < ledsDown { | |
LEDs[x + y*ledsAcross] = c | |
} | |
} | |
func blankLEDs() { | |
for i := range LEDs { | |
LEDs[i] = black | |
} | |
} | |
func chevronDemo(dir int) { | |
var ystart, yend int | |
if (dir > 0) { | |
ystart = ledsDown+10 | |
yend = -10 | |
} else { | |
ystart = 1 | |
yend = ledsDown+14 | |
} | |
for i:=ystart; i != yend; i+=-1*dir { | |
time.Sleep(150 * time.Millisecond) | |
blankLEDs() | |
chevron(i, dir, red) | |
chevron(i - 3, dir, yellow) | |
chevron(i - 4, dir, yellow) | |
chevron(i - 7, dir, green) | |
printLEDs() | |
} | |
} | |
// direction should be -1 or 1 | |
func chevron(y, direction int, c Colour) { | |
var middleX int = ledsAcross/2 | |
offset := 0 | |
for y >= -5 && y < ledsDown+5 && offset < middleX { | |
setLED(middleX-offset, y, c) | |
setLED(middleX+offset, y, c) | |
y += direction | |
offset ++ | |
} | |
} | |
// A hacky way to display output to terminal while testing: | |
var ansiMap = map[Colour]string { | |
red: ansi.ColorCode("red+h:red+h"), | |
green: ansi.ColorCode("green+h:green+h"), | |
blue: ansi.ColorCode("blue+h:blue+h"), | |
yellow: ansi.ColorCode("yellow+h:yellow+h"), | |
black: ansi.ColorCode("gray:black"), | |
} | |
var ansiReset = ansi.ColorCode("reset") | |
func printLEDs() { | |
// clear screen: | |
fmt.Print("\033[2J") | |
for y := 0; y < ledsDown; y++ { | |
for x := 0; x < ledsAcross; x++ { | |
idx := x + y*ledsAcross | |
fmt.Print(" ") | |
fmt.Print(ansiMap[LEDs[idx]], "*", ansiReset) | |
} | |
fmt.Println() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment