|
| 1 | +package main |
| 2 | + |
| 3 | +// Example program for the ST7735 display (Waveshare 1.44" LCD HAT) using the rpio driver on a Raspberry Pi. |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "image/color" |
| 8 | + "time" |
| 9 | + |
| 10 | + go_rpio "github.com/stianeikeland/go-rpio/v4" |
| 11 | + "tinygo.org/x/drivers/rpio" |
| 12 | + "tinygo.org/x/drivers/st7735" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + black = color.RGBA{0, 0, 0, 255} |
| 17 | + colors = [...]color.RGBA{ |
| 18 | + {255, 0, 0, 255}, // red |
| 19 | + {0, 255, 0, 255}, // green |
| 20 | + {0, 0, 255, 255}, // blue |
| 21 | + {255, 255, 0, 255}, // yellow |
| 22 | + } |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + if err := go_rpio.Open(); err != nil { |
| 27 | + fmt.Println("Error opening GPIO:", err) |
| 28 | + return |
| 29 | + } |
| 30 | + defer go_rpio.Close() |
| 31 | + |
| 32 | + // Initialize SPI and pins |
| 33 | + spi := rpio.NewSPI() |
| 34 | + resetPin := rpio.NewPin(27) |
| 35 | + dcPin := rpio.NewPin(25) |
| 36 | + csPin := rpio.NewPin(8) |
| 37 | + blPin := rpio.NewPin(24) |
| 38 | + |
| 39 | + // Initialize display |
| 40 | + device := st7735.New(spi, resetPin, dcPin, csPin, blPin) |
| 41 | + device.Configure(st7735.Config{ |
| 42 | + Width: 128, |
| 43 | + Height: 128, |
| 44 | + Model: st7735.GREENTAB, |
| 45 | + RowOffset: 3, |
| 46 | + ColumnOffset: 2, |
| 47 | + }) |
| 48 | + device.InvertColors(false) |
| 49 | + device.EnableBacklight(true) |
| 50 | + device.IsBGR(true) // no effect w/o rotation! |
| 51 | + device.SetRotation(st7735.NO_ROTATION) |
| 52 | + |
| 53 | + width, height := device.Size() |
| 54 | + |
| 55 | + // Clear display |
| 56 | + device.FillScreen(black) |
| 57 | + |
| 58 | + // Draw rectangles in a loop, clockwise rotation of colors |
| 59 | + pos := 0 |
| 60 | + for { |
| 61 | + device.FillRectangle(0, 0, width/2, height/2, colors[(pos+0)%len(colors)]) // top left |
| 62 | + device.FillRectangle(0, height/2, width/2, height/2, colors[(pos+1)%len(colors)]) // bottom left |
| 63 | + device.FillRectangle(width/2, height/2, width/2, height/2, colors[(pos+2)%len(colors)]) // bottom right |
| 64 | + device.FillRectangle(width/2, 0, width/2, height/2, colors[(pos+3)%len(colors)]) // top right |
| 65 | + pos++ |
| 66 | + time.Sleep(1 * time.Second) |
| 67 | + } |
| 68 | +} |
0 commit comments