ST7789

package module
v0.0.2-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2022 License: MIT Imports: 4 Imported by: 0

README

ST7789

使用Goland实现的操作ST7789,适用于无CS引脚的240x204 LCD显示屏。 目前仅在Raspberry zero 2w上测试通过。

本库根据Python版 https://github.com/solinnovay/Python_ST7789 移植而来,并在此基础上实现了RGBA转RGB565,支持透明图层。

安装

go get github.com/manx98/go-st7789

使用示例

package main

import (
	"context"
	ST7789 "github.com/manx98/go-st7789"
	"github.com/stianeikeland/go-rpio/v4"
	"image"
	"image/gif"
	"log"
	"os"
	"sync"
	"time"
)

// displayGIF
//
//	@Description: 显示GIF图片
//	@param ctx
//	@param canvas 画布
//	@param filePath GIF路径
func displayGIF(ctx context.Context, canvas *ST7789.Canvas, filePath string) {
	f, err := os.OpenFile(filePath, os.O_RDONLY, os.ModePerm)
	if err != nil {
		log.Fatalf("failed to open: %v", err)
	}
	defer func() {
		if err = f.Close(); err != nil {
			log.Fatalf("failed to close: %v", err)
		}
	}()
	all, err := gif.DecodeAll(f)
	if err != nil {
		log.Fatalf("failed to decode: %v", err)
	}
	showWork := make(chan image.Image, 1)
	waitGroup := sync.WaitGroup{}
	displayCtx, cancelFunc := context.WithCancel(ctx)
	waitGroup.Add(1)
	var totalTime int64
	var total int64
	go func() {
		defer func() {
			waitGroup.Done()
			cancelFunc()
		}()
		for {
			select {
			case <-displayCtx.Done():
				return
			case img := <-showWork:
				start := time.Now()
				canvas.DrawImage(img)
				canvas.Flush()
				totalTime += time.Now().Sub(start).Milliseconds()
				total++
			}
		}
	}()
	defer func() {
		cancelFunc()
		waitGroup.Wait()
		log.Printf("平均速度:%dms/fps\n", totalTime/total)
	}()
	for {
		for i, img := range all.Image {
			select {
			case <-displayCtx.Done():
				return
			case showWork <- img:
				time.Sleep(time.Duration(all.Delay[i]) * time.Millisecond * 10)
			}
		}
		if all.LoopCount < 0 {
			break
		}
		if all.LoopCount != 0 {
			all.LoopCount -= 1
		}
	}
}

type MyPin struct {
	rpio.Pin
}

func (m *MyPin) SetOutput() {
	m.Mode(rpio.Output)
}

type MySpi struct {
}

func (m *MySpi) SpiSpeed(speed uint32) {
	rpio.SpiSpeed(int(speed))
}

func (m *MySpi) SetSpiMode3() {
	rpio.SpiMode(1, 1)
}

func (m *MySpi) SpiTransmit(data []byte) {
	rpio.SpiTransmit(data...)
}

func main() {
	if err := rpio.Open(); err != nil {
		log.Fatalf("failed to open rpio: %v", err)
	}
	defer func() {
		if err := rpio.Close(); err != nil {
			log.Fatalf("failed to close gpio: %v", err)
		}
	}()
	err := rpio.SpiBegin(rpio.Spi0)
	if err != nil {
		log.Fatalf("failed to begin gpio: %v", err)
	}
	device := ST7789.NewST7789(
		&MySpi{},
		&MyPin{rpio.Pin(25)},
		&MyPin{rpio.Pin(27)},
		&MyPin{rpio.Pin(24)},
		ST7789.Screen240X240,
	)
	canvas := device.GetFullScreenCanvas()
	timeout, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	displayGIF(timeout, canvas, "./sample/TeaTime.gif")
	canvas.Clear()
	canvas.Flush()
}

感谢

  1. Python原始实现 https://github.com/solinnovay/Python_ST7789
  2. GPIO库 https://github.com/stianeikeland/go-rpio/

Documentation

Index

Constants

View Source
const (
	SPI_CLOCK_HZ = 40000000 // 40 MHz

	ST7789_NOP       = 0x00
	ST7789_SWRESET   = 0x01 // 软复位指令
	ST7789_RDDID     = 0x04
	ST7789_RDDST     = 0x09
	ST7789_RDDPM     = 0x0A
	ST7789_RDDMADCTL = 0x0B
	ST7789_RDDCOLMOD = 0x0C
	ST7789_RDDIM     = 0x0D
	ST7789_RDDSM     = 0x0E
	ST7789_RDDSDR    = 0x0F

	ST7789_SLPIN  = 0x10
	ST7789_SLPOUT = 0x11
	ST7789_PTLON  = 0x12
	ST7789_NORON  = 0x13

	ST7789_INVOFF  = 0x20
	ST7789_INVON   = 0x21
	ST7789_GAMSET  = 0x26
	ST7789_DISPOFF = 0x28
	ST7789_DISPON  = 0x29
	ST7789_CASET   = 0x2A
	ST7789_RASET   = 0x2B
	ST7789_RAMWR   = 0x2C
	ST7789_RAMRD   = 0x2E

	ST7789_PTLAR    = 0x30
	ST7789_VSCRDEF  = 0x33
	ST7789_TEOFF    = 0x34
	ST7789_TEON     = 0x35
	ST7789_MADCTL   = 0x36
	ST7789_VSCRSADD = 0x37
	ST7789_IDMOFF   = 0x38
	ST7789_IDMON    = 0x39
	ST7789_COLMOD   = 0x3A
	ST7789_RAMWRC   = 0x3C
	ST7789_RAMRDC   = 0x3E

	ST7789_TESCAN   = 0x44
	ST7789_RDTESCAN = 0x45

	ST7789_WRDISBV  = 0x51
	ST7789_RDDISBV  = 0x52
	ST7789_WRCTRLD  = 0x53
	ST7789_RDCTRLD  = 0x54
	ST7789_WRCACE   = 0x55
	ST7789_RDCABC   = 0x56
	ST7789_WRCABCMB = 0x5E
	ST7789_RDCABCMB = 0x5F

	ST7789_RDABCSDR = 0x68

	ST7789_RDID1 = 0xDA
	ST7789_RDID2 = 0xDB
	ST7789_RDID3 = 0xDC

	ST7789_RAMCTRL = 0xB0
	ST7789_RGBCTRL = 0xB1
	ST7789_PORCTRL = 0xB2
	ST7789_FRCTRL1 = 0xB3

	ST7789_GCTRL = 0xB7
	ST7789_DGMEN = 0xBA
	ST7789_VCOMS = 0xBB

	ST7789_LCMCTRL  = 0xC0
	ST7789_IDSET    = 0xC1
	ST7789_VDVVRHEN = 0xC2

	ST7789_VRHS     = 0xC3
	ST7789_VDVSET   = 0xC4
	ST7789_VCMOFSET = 0xC5
	ST7789_FRCTR2   = 0xC6
	ST7789_CABCCTRL = 0xC7
	ST7789_REGSEL1  = 0xC8
	ST7789_REGSEL2  = 0xCA
	ST7789_PWMFRSEL = 0xCC

	ST7789_PWCTRL1   = 0xD0
	ST7789_VAPVANEN  = 0xD2
	ST7789_CMD2EN    = 0xDF5A6902
	ST7789_PVGAMCTRL = 0xE0
	ST7789_NVGAMCTRL = 0xE1
	ST7789_DGMLUTR   = 0xE2
	ST7789_DGMLUTB   = 0xE3
	ST7789_GATECTRL  = 0xE4
	ST7789_PWCTRL2   = 0xE8
	ST7789_EQCTRL    = 0xE9
	ST7789_PROMCTRL  = 0xEC
	ST7789_PROMEN    = 0xFA
	ST7789_NVMSET    = 0xFC
	ST7789_PROMACT   = 0xFE

	ST7789_POWSAVE    = 0xbc
	ST7789_DLPOFFSAVE = 0xbd

	// Colours for convenience
	ST7789_BLACK   = 0x0000 // 0b 00000 000000 00000
	ST7789_BLUE    = 0x001F // 0b 00000 000000 11111
	ST7789_GREEN   = 0x07E0 // 0b 00000 111111 00000
	ST7789_RED     = 0xF800 // 0b 11111 000000 00000
	ST7789_CYAN    = 0x07FF // 0b 00000 111111 11111
	ST7789_MAGENTA = 0xF81F // 0b 11111 000000 11111
	ST7789_YELLOW  = 0xFFE0 // 0b 11111 111111 00000
	ST7789_WHITE   = 0xFFFF // 0b 11111 111111 11111

	COLOR_MODE_65K   = ColorMode(0x50)
	COLOR_MODE_262K  = ColorMode(0x60)
	COLOR_MODE_12BIT = ColorMode(0x03)
	COLOR_MODE_16BIT = ColorMode(0x05)
	COLOR_MODE_18BIT = ColorMode(0x06)
	COLOR_MODE_16M   = ColorMode(0x07)

	// Screen320X240 Width 320,Height 240
	Screen320X240 = ScreenType(0)
	// Screen240X240 Width 240,Height 240
	Screen240X240 = ScreenType(1)
	// Screen135X240 Width 135,Height 240
	Screen135X240 = ScreenType(2)
)

Variables

This section is empty.

Functions

func ColorToRgb565

func ColorToRgb565(c color.Color, backgroundColor uint16) uint16

ColorToRgb565

@Description: 转换 color.Color 为RGB565
@param c 当前颜色
@param backgroundColor 背景颜色(RGB565)
@return uint16 RGB565色值

func Rgb565ToRgb

func Rgb565ToRgb(c uint16) (r, g, b uint16)

Rgb565ToRgb

@Description: 转换RGB565为标准RGB
@param c RGB565色值
@return r RGB(R色值)
@return g RGB(G色值)
@return b RGB(B色值)

Types

type BaseCanvas

type BaseCanvas interface {
	//
	// SetRGB565
	//  @Description: 设置指定坐标RGB565色值
	//  @param x X轴
	//  @param y Y轴
	//  @param c RBG565色值
	//
	SetRGB565(x, y int, c uint16)
	//
	// GetRGB565
	//  @Description: 获取指定坐标RGB565色值
	//  @param x X轴
	//  @param y Y轴
	//  @result RGB565色值
	//
	GetRGB565(x, y int) uint16
}

type Canvas

type Canvas struct {
	X0     int    // X轴画布起始偏移
	Y0     int    // Y轴画布起始偏移
	X1     int    // X轴画布结束偏移
	Y1     int    // Y轴画布结束偏移
	Width  int    // 画布宽度
	Height int    // 画布高度
	Buffer []byte // 缓冲区
	// contains filtered or unexported fields
}

Canvas @Description: 画布

func (*Canvas) Clear

func (d *Canvas) Clear()

Clear

@Description: 清空画布缓冲区数据
@receiver d

func (*Canvas) DrawImage

func (d *Canvas) DrawImage(img image.Image)

DrawImage

@Description: 将图像绘制到画布缓冲区中
@receiver d
@param img 图像

func (*Canvas) Flush

func (d *Canvas) Flush()

Flush

@Description: 将缓冲区内容刷新到屏幕上
@receiver d

func (*Canvas) FlushDirectly

func (d *Canvas) FlushDirectly(buffer []byte)

FlushDirectly

@Description: 直接将buffer内容绘制到画布所对应的显示区域,该方法不会覆盖画布缓冲区
@receiver d
@param buffer

func (*Canvas) GetColor

func (d *Canvas) GetColor(x, y int) color.Color

GetColor

@Description: 获取缓冲区指定坐标RGBA色值(由于该值从RBG565转换而来,故A值始终为1)
@receiver d
@param x X轴坐标
@param y Y轴坐标
@return color.Color

func (*Canvas) GetRGB565

func (d *Canvas) GetRGB565(x, y int) uint16

GetRGB565

@Description: 获取缓存区指定坐标的RGB565色值
@receiver d
@param x X轴坐标
@param y Y轴坐标
@return uint16 RGB565色值

func (*Canvas) SetColor

func (d *Canvas) SetColor(x, y int, c color.Color)

SetColor

@Description: 设置缓冲区指定坐标的色值
@receiver d
@param x X轴坐标
@param y Y轴坐标
@param c 色值

func (*Canvas) SetRGB565

func (d *Canvas) SetRGB565(x, y int, c uint16)

SetRGB565

@Description: 设置缓存区指定坐标的RGB565色值
@receiver d
@param x X轴坐标
@param y Y轴坐标
@param c RGB565色值

type ColorMode

type ColorMode uint8

ColorMode 色彩模式

type PIN

type PIN interface {
	//
	// High
	//  @Description:输出为高电频
	//
	High()
	//
	// Low
	//  @Description:设置为低电频
	//
	Low()
	//
	// SetOutput
	//  @Description:设置为输出模式
	//
	SetOutput()
}

type SPI

type SPI interface {
	//
	// SpiSpeed
	//  @Description: 设置SPI速率
	//  @param speed
	//
	SpiSpeed(speed uint32)
	//
	// SetSpiMode3
	//  @Description:设置为Mode3 CPOL=1, CPHA=1模式
	//
	SetSpiMode3()
	//
	// SpiTransmit
	//  @Description: 发送数据
	//  @param data 需要发送的数据
	//
	SpiTransmit(data []byte)
}

type ST7789

type ST7789 struct {
	// contains filtered or unexported fields
}

func NewST7789

func NewST7789(spi SPI, dc, rst, led PIN, screen ScreenType) *ST7789

NewST7789

@Description: ST7789显示驱动
@param spi SPI通信端口
@param dc 引脚DC
@param rst 引脚RES
@param led 引脚BLK
@param screen 显示器类型
@return *ST7789
@return error 创建失败

func (*ST7789) ColorMode

func (s *ST7789) ColorMode(mode ColorMode)

ColorMode

@Description: 设置颜色模式
@receiver s
@param mode
	COLOR_MODE_65K, COLOR_MODE_262K, COLOR_MODE_12BIT,
	COLOR_MODE_16BIT, COLOR_MODE_18BIT, COLOR_MODE_16M

func (*ST7789) Command

func (s *ST7789) Command(data byte)

Command

@Description: 写入显示命令(非线程安全,请使用 Tx 包裹执行)
@receiver s
@param data 数据

func (*ST7789) ExchangeData

func (s *ST7789) ExchangeData(isData bool, data []byte)

ExchangeData

@Description: 将数据写入SPI,isData为true表示写入的是数据,反之则是命令(非线程安全,请使用 Tx 包裹执行)
@receiver s
@param data 需要发送的数据
@param isData 是否是数据类型

func (*ST7789) FlushBitBuffer

func (s *ST7789) FlushBitBuffer(x0, y0, x1, y1 int, buffer []byte)

FlushBitBuffer

@Description: 将画布上的图像绘制到屏幕上
@receiver s
@param X0 区域开始X轴位置(包含)
@param Y0 区域开始Y轴位置(包含)
@param X1 区域结束X轴位置(包含)
@param Y1 区域结束Y轴位置(包含)
@param Buffer RGB565图像

func (*ST7789) GetCanvas

func (s *ST7789) GetCanvas(x0, y0, x1, y1 int) *Canvas

GetCanvas

@Description: 获取画布
@receiver s
@param X0 区域X轴起始(包含)
@param Y0 区域Y轴起始(包含)
@param X1 区域X轴截止(包含)
@param Y1 区域X轴截止(包含)
@return *Canvas

func (*ST7789) GetFullScreenCanvas

func (s *ST7789) GetFullScreenCanvas() *Canvas

GetFullScreenCanvas

@Description: 获取全屏画布
@receiver s
@return *Canvas 画布

func (*ST7789) HardReset

func (s *ST7789) HardReset()

HardReset

@Description: 硬重启设备
@receiver s

func (*ST7789) InversionMode

func (s *ST7789) InversionMode(enable bool)

InversionMode

@Description: 是否启用显示反转模式
@receiver s
@param enable 是否启用

func (*ST7789) PowerSave

func (s *ST7789) PowerSave(mode uint8)

PowerSave

@Description:
@receiver s
@param mode
	0 - off
	1 - idle
	2 - normal
	4 - display off

func (*ST7789) Rotation

func (s *ST7789) Rotation(rotation uint8)

Rotation

@Description: 设置显示旋转
@receiver s
@param rotation
  	0-Portrait
  	1-Landscape
  	2-Inverted Portrait
  	3-Inverted Landscape

func (*ST7789) SendData

func (s *ST7789) SendData(data ...byte)

SendData

@Description: 写入显示数据
@receiver s
@param data 数据

func (*ST7789) SetWindow

func (s *ST7789) SetWindow(x0, y0, x1, y1 int)

SetWindow

@Description: Set the pixel address window for proceeding drawing commands. X0 and
   X1 should define the minimum and maximum x pixel bounds.  Y0 and Y1
   should define the minimum and maximum y pixel bound.
@receiver s
@param X0 区域开始X轴位置(包含)
@param Y0 区域开始Y轴位置(包含)
@param X1 区域结束X轴位置(包含)
@param Y1 区域结束Y轴位置(包含)

func (*ST7789) Size

func (s *ST7789) Size() *image.Point

Size

@Description: 获取显示器尺寸
@receiver s
@return *image.Point 尺寸

func (*ST7789) SleepMode

func (s *ST7789) SleepMode(enable bool)

SleepMode

@Description: 是否启用显示休眠模式
@receiver s
@param enable 是否开启

func (*ST7789) SoftReset

func (s *ST7789) SoftReset()

SoftReset

@Description: 软复位
@receiver s

type ScreenType

type ScreenType uint8

ScreenType 显示屏类型

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL