func (this *LedArray) Startup() error { if this.initialized { return fmt.Errorf("Attempted to startup the led array a second time!") } else { if !unicornhatInitialized { unicornhat.SetBrightness(unicornhat.DefaultBrightness / 2) if err := unicornhat.Initialize(); err != nil { return err } unicornhat.ClearLEDBuffer() unicornhat.Show() unicornhatInitialized = true } this.initialized = true return nil } }
func main() { flag.Parse() delay := time.Duration(*msecdelay) var cpus []*µcore var memory Memory runtime.GOMAXPROCS(runtime.NumCPU()) defer unicornhat.Shutdown() unicornhat.SetBrightness(unicornhat.DefaultBrightness / 2) if err := unicornhat.Initialize(); err != nil { fmt.Println(err) return } unicornhat.ClearLEDBuffer() unicornhat.Show() cpus = make([]*µcore, NumCpus) memory = make(Memory, MemSize) logPrint("Randomizing memory") for i, j := 0, 0; i < len(memory); i, j = i+4, j+1 { memory[i+red] = 0 memory[i+blue] = 0 memory[i+green] = 0 if *xmas { offset := red if rand.Int()%2 == 0 { offset = red } else { offset = green } memory[i+offset] = byte(j + rand.Int()) } else if *gscale { intensity := byte(j * rand.Int()) memory[i+red] = intensity memory[i+green] = intensity memory[i+blue] = intensity } else if *purple { intensity := byte(j + rand.Int()) memory[i+red] = intensity memory[i+blue] = intensity } else if *blueOnly { memory[i+blue] = byte(j + rand.Int()) } else { memory[i+red] = byte(j + rand.Int()) memory[i+green] = byte(j + rand.Int()) memory[i+blue] = byte(j + rand.Int()) } if *drainfill { if val := byte(j * rand.Int()); val%2 == 0 { memory[i+control] = OpFill } else { memory[i+control] = OpDrain } } else { memory[i+control] = byte(j + rand.Int()) } //logPrint(fmt.Sprintf("@%d = {%d, %d, %d}", i, memory[i+red], memory[i+green], memory[i+blue])) } logPrint("Done randomizing memory") upperHalf := memory for i := 0; i < 64; i++ { targetMem := upperHalf[:µcoreSize] cpus[i] = New(i, targetMem) go cpus[i].Execute() upperHalf = upperHalf[µcoreSize:] } count := 0 for { if count >= 64 { break } for i := 0; i < 64; i++ { if c := cpus[i]; c == nil { continue } else { select { case value := <-c.result: unicornhat.SetPixelColor(c.index, value.R, value.G, value.B) case <-c.done: count++ cpus[i] = nil default: } } } unicornhat.Show() MillisecondDelay(delay) } }
func setBrightness(brightness iris16.Word) { unicornhat.SetBrightness(byte(brightness)) }
func (um *UnicornManager) init() { unicornhat.SetBrightness(unicornhat.DefaultBrightness()) unicornhat.Init(64) unicornhat.ClearLEDBuffer() }