func (this *LedArray) Store(address, value iris16.Word) error { if address == (this.base + commandCell) { cmd := ledCommand(value) if cmd >= numLedCommands { return fmt.Errorf("Illegal unicornhat opcode %x", cmd) } else { switch cmd { case ledCommandClear: unicornhat.ClearLEDBuffer() case ledCommandSetPixel: if pos, err := unicornhat.PixelPosition(int(this.x), int(this.y)); err != nil { return err } else { unicornhat.SetPixelColor(pos, byte(this.red), byte(this.green), byte(this.blue)) } case ledCommandGetPixel: if pos, err := unicornhat.PixelPosition(int(this.x), int(this.y)); err != nil { return err } else { pix := unicornhat.GetPixelColor(pos) this.red, this.green, this.blue = iris16.Word(pix.R), iris16.Word(pix.G), iris16.Word(pix.B) } case ledCommandSetBrightness: setBrightness(this.brightness) case ledCommandGetBrightness: this.brightness = getBrightness() case ledCommandShow: unicornhat.Show() } return nil } } else if address == (this.base + brightnessCell) { setBrightness(value) return nil } else if address == (this.base + xCell) { this.x = value return nil } else if address == (this.base + yCell) { this.y = value return nil } else if address == (this.base + redChannel) { this.red = value return nil } else if address == (this.base + blueChannel) { this.blue = value return nil } else if address == (this.base + greenChannel) { this.green = value return nil } else { return fmt.Errorf("Illegal address %x provided!", address) } }
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 (um *UnicornManager) init() { unicornhat.SetBrightness(unicornhat.DefaultBrightness()) unicornhat.Init(64) unicornhat.ClearLEDBuffer() }