|
| 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 | +// Build command: |
| 5 | +// GOOS=linux GOARCH=arm64 go build -o rpio-example ./examples/rpio/ |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "image/color" |
| 10 | + "time" |
| 11 | + |
| 12 | + go_rpio "github.com/stianeikeland/go-rpio/v4" |
| 13 | + "tinygo.org/x/drivers" |
| 14 | + "tinygo.org/x/drivers/rpio" |
| 15 | + "tinygo.org/x/drivers/st7735" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + black = color.RGBA{0, 0, 0, 255} |
| 20 | + colors = [...]color.RGBA{ |
| 21 | + {255, 0, 0, 255}, // red |
| 22 | + {0, 255, 0, 255}, // green |
| 23 | + {0, 0, 255, 255}, // blue |
| 24 | + {255, 255, 0, 255}, // yellow |
| 25 | + } |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + if err := go_rpio.Open(); err != nil { |
| 30 | + fmt.Println("Error opening GPIO:", err) |
| 31 | + return |
| 32 | + } |
| 33 | + defer go_rpio.Close() |
| 34 | + |
| 35 | + // Initialize SPI and pins |
| 36 | + spi := rpio.NewSPI() |
| 37 | + resetPin := rpio.NewPin(27) |
| 38 | + dcPin := rpio.NewPin(25) |
| 39 | + csPin := rpio.NewPin(8) |
| 40 | + blPin := rpio.NewPin(24) |
| 41 | + |
| 42 | + rotatePin := rpio.NewPin(20) // middle button on the HAT |
| 43 | + rotatePin.Pin.PullUp() // enable pull-up resistor, our wrapper sets only input mode automatically |
| 44 | + |
| 45 | + // Initialize display |
| 46 | + device := st7735.New(spi, resetPin, dcPin, csPin, blPin) |
| 47 | + device.Configure(st7735.Config{ |
| 48 | + Width: 128, |
| 49 | + Height: 128, |
| 50 | + Model: st7735.GREENTAB, |
| 51 | + RowOffset: 3, |
| 52 | + ColumnOffset: 2, |
| 53 | + }) |
| 54 | + device.InvertColors(false) |
| 55 | + device.EnableBacklight(true) |
| 56 | + device.IsBGR(true) // no effect w/o rotation! |
| 57 | + device.SetRotation(st7735.NO_ROTATION) |
| 58 | + |
| 59 | + width, height := device.Size() |
| 60 | + |
| 61 | + // Clear display |
| 62 | + device.FillScreen(black) |
| 63 | + |
| 64 | + // Draw rectangles in a loop, clockwise rotation of colors |
| 65 | + pos := 0 |
| 66 | + for { |
| 67 | + device.FillRectangle(0, 0, width/2, height/2, colors[(pos+0)%len(colors)]) // top left |
| 68 | + device.FillRectangle(0, height/2, width/2, height/2, colors[(pos+1)%len(colors)]) // bottom left |
| 69 | + device.FillRectangle(width/2, height/2, width/2, height/2, colors[(pos+2)%len(colors)]) // bottom right |
| 70 | + device.FillRectangle(width/2, 0, width/2, height/2, colors[(pos+3)%len(colors)]) // top right |
| 71 | + device.FillRectangle(0, 0, 10, 10, black) // rotation marker |
| 72 | + pos++ |
| 73 | + |
| 74 | + timeout := time.Now().Add(1 * time.Second) |
| 75 | + for time.Now().Before(timeout) { |
| 76 | + // check button pressed |
| 77 | + if !rotatePin.Get() { |
| 78 | + currentRotation := device.Rotation() |
| 79 | + newRotation := (currentRotation + 1) % 4 |
| 80 | + device.SetRotation(drivers.Rotation(newRotation)) |
| 81 | + // wait for button release |
| 82 | + for !rotatePin.Get() { |
| 83 | + time.Sleep(10 * time.Millisecond) |
| 84 | + } |
| 85 | + break |
| 86 | + } |
| 87 | + time.Sleep(50 * time.Millisecond) |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | +} |
0 commit comments