Skip to content

Instantly share code, notes, and snippets.

@riyaz-ali
Created March 11, 2020 06:56
Show Gist options
  • Save riyaz-ali/b35d6c48062bb6083792441eccc457cc to your computer and use it in GitHub Desktop.
Save riyaz-ali/b35d6c48062bb6083792441eccc457cc to your computer and use it in GitHub Desktop.
Waveshare E-Paper 2.9inch Driver in Golang
package main
import (
"github.com/stianeikeland/go-rpio/v4"
"log"
"time"
)
const (
RST = rpio.Pin(17)
DC = rpio.Pin(25)
CS = rpio.Pin(8)
BUSY = rpio.Pin(24)
HEIGHT = 296
WIDTH = 128
)
type LUT []byte
var lut_full_update = LUT{
0x50, 0xAA, 0x55, 0xAA, 0x11, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF, 0x1F, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
//noinspection GoUnusedGlobalVariable
var lut_partial_update = LUT{
0x10, 0x18, 0x18, 0x08, 0x18, 0x18,
0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
func reset() {
RST.Write(rpio.High)
time.Sleep(200 * time.Millisecond)
RST.Write(rpio.Low)
time.Sleep(10 * time.Millisecond)
RST.Write(rpio.High)
time.Sleep(200 * time.Millisecond)
}
func command(c byte) {
DC.Write(rpio.Low)
CS.Write(rpio.Low)
rpio.SpiTransmit(c)
CS.Write(rpio.High)
}
func data(d byte) {
DC.Write(rpio.High)
CS.Write(rpio.Low)
rpio.SpiTransmit(d)
CS.Write(rpio.High)
}
func idle() {
for BUSY.Read() == rpio.High {
time.Sleep(200 * time.Millisecond)
}
}
func initialize(lut LUT) {
reset()
// DRIVER_OUTPUT_CONTROL
command(0x01)
data((HEIGHT - 1) & 0xFF)
data(((HEIGHT - 1) >> 8) & 0xFF)
data(0x00)
// BOOSTER_SOFT_START_CONTROL
command(0x0C)
data(0xD7)
data(0xD6)
data(0x9D)
// WRITE_VCOM_REGISTER
command(0x2C)
data(0xA8)
// SET_DUMMY_LINE_PERIOD
command(0x3A)
data(0x1A)
// SET_GATE_TIME
command(0x3B)
data(0x08)
// DATA_ENTRY_MODE_SETTING
command(0x11)
data(0x03)
// WRITE_LUT_REGISTER
command(0x32)
for _, b := range lut {
data(b)
}
}
func sleep() {
command(0x10)
data(0x01)
}
func turnOnDisplay() {
command(0x22)
data(0xC4)
command(0x20)
command(0xFF)
idle()
}
func window(x0, x1 byte, y0, y1 uint16) {
command(0x44)
data((x0 >> 3) & 0xFF)
data((x1 >> 3) & 0xFF)
command(0x45)
data(byte(y0 & 0xFF))
data(byte((y0 >> 8) & 0xFF))
data(byte(y1 & 0xFF))
data(byte((y1 >> 8) & 0xFF))
}
func cursor(x uint8, y uint16) {
command(0x4E)
data((x >> 3) & 0xFF)
command(0x4F)
data(byte(y & 0xFF))
data(byte((y >> 8) & 0xFF))
idle()
}
func clear(color byte) {
window(0, WIDTH-1,0, HEIGHT-1)
for i := 0; i < HEIGHT; i++ {
cursor(0, uint16(i))
command(0x24)
for j := 0; j < (WIDTH / 8); j++ {
data(color)
}
}
turnOnDisplay()
}
func draw(image []byte) {
window(0, WIDTH-1, 0, HEIGHT-1)
for i := 0; i < HEIGHT; i++ {
cursor(0, uint16(i))
for j := 0; j < (WIDTH / 8); j++ {
data(image[i+j*(WIDTH/8)])
}
}
}
func main() {
// start the GPIO controller
if err := rpio.Open(); err != nil {
log.Fatalf("failed to start gpio: %v", err)
}
defer rpio.Close()
// Enable SPI on SPI0
if err := rpio.SpiBegin(rpio.Spi0); err != nil {
log.Fatalf("failed to enable SPI: %v", err)
}
// set pin modes
RST.Mode(rpio.Output)
DC.Mode(rpio.Output)
CS.Mode(rpio.Output)
BUSY.Mode(rpio.Input)
// configure SPI settings
rpio.SpiSpeed(4_000_000)
rpio.SpiMode(0, 0)
// initialize the display
initialize(lut_full_update)
clear(0xFF)
sleep()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment