Skip to content

Instantly share code, notes, and snippets.

@pilgrim2go
Created October 23, 2014 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pilgrim2go/8384af636dabd980c253 to your computer and use it in GitHub Desktop.
Save pilgrim2go/8384af636dabd980c253 to your computer and use it in GitHub Desktop.
lcd number ( ported from http://rubyquiz.com/quiz14.html)
package main
import "os"
import "fmt"
// import "sort"
import "strings"
import "time"
var LCD_STATES =[]string {
"HORIZONTAL",
"VERTICAL",
"HORIZONTAL",
"VERTICAL",
"HORIZONTAL",
"DONE",
}
var LCD_DISPLAY_DATA = map[string][]int {
"0" : {1,3,0,3,1},
"1" : {0,1,0,1,0},
"2" : {1,1,1,2,1},
"3" : {1,1,1,1,1},
"4" : {0,3,1,1,0},
"5" : {1,2,1,1,1},
"6" : {1,2,1,3,1},
"7" : {1,1,0,1,0},
"8" : {1,3,1,3,1},
"9" : {1,3,1,1,1},
}
type LCD struct {
size, spacing int
}
func (lcd LCD) horizontal_segment(type_ int) string{
switch type_ {
case 1:
return " " + strings.Repeat("-", lcd.size) + " " + strings.Repeat(" ", lcd.spacing)
default:
return " " + strings.Repeat(" ", lcd.size) + " " + strings.Repeat(" ", lcd.spacing)
}
}
func (lcd LCD) vertical_segment(type_ int) string{
switch type_ {
case 1:
return " " + strings.Repeat(" ", lcd.size) + "|" + strings.Repeat(" ", lcd.spacing)
case 2:
return "|" + strings.Repeat(" ", lcd.size) + "" + strings.Repeat(" ", lcd.spacing)
case 3:
return "|" + strings.Repeat(" ", lcd.size) + "|" + strings.Repeat(" ", lcd.spacing)
default:
return " " + strings.Repeat(" ", lcd.size) + " " + strings.Repeat(" ", lcd.spacing)
}
}
func (lcd LCD) display(digits string) {
for i, state := range LCD_STATES {
switch state {
case "HORIZONTAL":
line := ""
for _, b := range digits {
var type_ = LCD_DISPLAY_DATA[string(b)][i]
line += lcd.horizontal_segment(type_)
}
fmt.Println(line+"\n")
case "VERTICAL":
for j:=0;j< lcd.size;j++ {
line := ""
for _, b := range digits {
var type_ = LCD_DISPLAY_DATA[string(b)][i]
line += lcd.vertical_segment(type_)
}
fmt.Println(line+"\n")
}
case "DONE":
break
}
// sleep here to see line is rendered one by one
time.Sleep(1000 * time.Millisecond)
}
}
func main(){
digits := os.Args[1:]
fmt.Println(digits)
lcd := LCD{size:2,spacing:2}
lcd.display(digits[0])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment