unicornpaint

A web-based painting app for raspberry PI and pimoroni Unicorn Hat HD
Log | Files | Refs | README

Unicorn.go (1501B)


      1 package unicorn
      2 
      3 // Unicorn ...
      4 // Object representing the Unicorn HAT to be controlled
      5 type Unicorn interface {
      6 	// Not all unicorns are the same size
      7 	GetWidth() uint8
      8 	GetHeight() uint8
      9 
     10 	// Array of pixels, indexed x, then y, then color (rgb)
     11 	GetPixels() [][][]uint8
     12 
     13 	// Set an individual pixel
     14 	SetPixel(x, y, r, g, b uint8)
     15 
     16 	// Flip the display buffer
     17 	Show()
     18 
     19 	// Set all pixels back to black
     20 	Clear()
     21 
     22 	// Turns off the LEDs
     23 	Off()
     24 
     25 	// Unicorn needs to be in charge of the main thread
     26 	MainLoop()
     27 }
     28 
     29 // GetUnicorn ...
     30 // Get a unicorn. Get's a real on on ARM hardware,
     31 // get's a fake one on x86
     32 func GetUnicorn() (Unicorn, error) {
     33 	return NewUnicorn()
     34 }
     35 
     36 type BaseUnicorn struct {
     37 	pixels [][][]uint8
     38 }
     39 
     40 func (f *BaseUnicorn) GetWidth() uint8 {
     41 	return uint8(len(f.pixels))
     42 }
     43 
     44 func (f *BaseUnicorn) GetHeight() uint8 {
     45 	if len(f.pixels) > 0 {
     46 		return uint8(len(f.pixels[0]))
     47 	}
     48 	return 0
     49 }
     50 
     51 func (f *BaseUnicorn) GetPixels() [][][]uint8 {
     52 	return f.pixels
     53 }
     54 
     55 func (f *BaseUnicorn) SetPixel(x, y, r, g, b uint8) {
     56 	f.pixels[x][y] = []uint8{r, g, b}
     57 }
     58 
     59 func (f *BaseUnicorn) Clear() {
     60 	f.pixels = makePixels(f.GetWidth(), f.GetHeight())
     61 }
     62 
     63 func makePixels(width, height uint8) [][][]uint8 {
     64 	pixels := make([][][]uint8, width)
     65 	for x := uint8(0); x < width; x++ {
     66 		pixels[x] = make([][]uint8, height)
     67 		for y := uint8(0); y < height; y++ {
     68 			pixels[x][y] = []uint8{0, 0, 0}
     69 		}
     70 	}
     71 	return pixels
     72 }
     73 
     74 func Rgb(pixel []uint8) (uint8, uint8, uint8) {
     75 	return pixel[0], pixel[1], pixel[2]
     76 }