tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/ili9341/spi.go (about)

     1  //go:build !atsamd51 && !atsame5x && !atsamd21
     2  
     3  package ili9341
     4  
     5  import (
     6  	"machine"
     7  
     8  	"tinygo.org/x/drivers"
     9  )
    10  
    11  var buf [64]byte
    12  
    13  type spiDriver struct {
    14  	bus drivers.SPI
    15  }
    16  
    17  func NewSPI(bus drivers.SPI, dc, cs, rst machine.Pin) *Device {
    18  	return &Device{
    19  		dc:  dc,
    20  		cs:  cs,
    21  		rst: rst,
    22  		rd:  machine.NoPin,
    23  		driver: &spiDriver{
    24  			bus: bus,
    25  		},
    26  	}
    27  }
    28  
    29  func (pd *spiDriver) configure(config *Config) {
    30  }
    31  
    32  func (pd *spiDriver) write8(b byte) {
    33  	buf[0] = b
    34  	pd.bus.Tx(buf[:1], nil)
    35  }
    36  
    37  func (pd *spiDriver) write8n(b byte, n int) {
    38  	buf[0] = b
    39  	for i := 0; i < n; i++ {
    40  		pd.bus.Tx(buf[:1], nil)
    41  	}
    42  }
    43  
    44  func (pd *spiDriver) write8sl(b []byte) {
    45  	pd.bus.Tx(b, nil)
    46  }
    47  
    48  func (pd *spiDriver) write16(data uint16) {
    49  	buf[0] = uint8(data >> 8)
    50  	buf[1] = uint8(data)
    51  	pd.bus.Tx(buf[:2], nil)
    52  }
    53  
    54  func (pd *spiDriver) write16n(data uint16, n int) {
    55  	for i := 0; i < len(buf); i += 2 {
    56  		buf[i] = uint8(data >> 8)
    57  		buf[i+1] = uint8(data)
    58  	}
    59  
    60  	for i := 0; i < (n >> 5); i++ {
    61  		pd.bus.Tx(buf[:], nil)
    62  	}
    63  
    64  	pd.bus.Tx(buf[:n%64], nil)
    65  }
    66  
    67  func (pd *spiDriver) write16sl(data []uint16) {
    68  	for i, c := 0, len(data); i < c; i++ {
    69  		buf[0] = uint8(data[i] >> 8)
    70  		buf[1] = uint8(data[i])
    71  		pd.bus.Tx(buf[:2], nil)
    72  	}
    73  }