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

     1  // Package ft6336 provides a driver for the FT6336 I2C Self-Capacitive touch
     2  // panel controller.
     3  //
     4  // Datasheet: https://focuslcds.com/content/FT6236.pdf
     5  package ft6336
     6  
     7  import (
     8  	"machine"
     9  
    10  	"tinygo.org/x/drivers"
    11  	"tinygo.org/x/drivers/internal/legacy"
    12  	"tinygo.org/x/drivers/touch"
    13  )
    14  
    15  // Device wraps FT6336 I2C Self-Capacitive touch
    16  type Device struct {
    17  	bus     drivers.I2C
    18  	buf     []byte
    19  	Address uint8
    20  	intPin  machine.Pin
    21  }
    22  
    23  // New returns FT6336 device for the provided I2C bus using default address.
    24  func New(i2c drivers.I2C, intPin machine.Pin) *Device {
    25  	return &Device{
    26  		bus:     i2c,
    27  		buf:     make([]byte, 11),
    28  		Address: Address,
    29  		intPin:  intPin,
    30  	}
    31  }
    32  
    33  // Config contains settings for FT6636.
    34  type Config struct {
    35  }
    36  
    37  // Configure the FT6336 device.
    38  func (d *Device) Configure(config Config) error {
    39  	d.write1Byte(0xA4, 0x00)
    40  	d.intPin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
    41  	return nil
    42  }
    43  
    44  // SetGMode sets interrupt mode.
    45  //
    46  //	0x00 : Interrupt Polling mode
    47  //	0x01 : Interrupt Trigger mode (default)
    48  func (d *Device) SetGMode(v uint8) {
    49  	d.write1Byte(RegGMode, v)
    50  }
    51  
    52  // GetGMode gets interrupt mode.
    53  func (d *Device) GetGMode() uint8 {
    54  	return d.read8bit(RegGMode)
    55  }
    56  
    57  // SetPeriodActive sets report rate in Active mode.
    58  func (d *Device) SetPeriodActive(v uint8) {
    59  	d.write1Byte(RegPeriodActive, v)
    60  }
    61  
    62  // GetPeriodActive gets report rate in Active mode.
    63  func (d *Device) GetPeriodActive() uint8 {
    64  	return d.read8bit(RegPeriodActive)
    65  }
    66  
    67  // GetFirmwareID gets firmware version.
    68  func (d *Device) GetFirmwareID() uint8 {
    69  	return d.read8bit(RegFirmid)
    70  }
    71  
    72  // Read reads the registers.
    73  func (d *Device) Read() []byte {
    74  	d.bus.Tx(uint16(d.Address), []byte{0x02}, d.buf[:])
    75  	return d.buf[:]
    76  }
    77  
    78  // ReadTouchPoint reads a single touch.Point from the device. The maximum value
    79  // for each touch.Point is 0xFFFF.
    80  func (d *Device) ReadTouchPoint() touch.Point {
    81  	d.Read()
    82  	z := 0xFFFFF
    83  	switch d.buf[0] {
    84  	case 0, 255:
    85  		z = 0
    86  	}
    87  
    88  	//Scale X&Y to 16 bit for consistency across touch drivers
    89  	return touch.Point{
    90  		X: (int(d.buf[1]&0x0F)<<8 + int(d.buf[2])) * ((1 << 16) / 320),
    91  		Y: (int(d.buf[3]&0x0F)<<8 + int(d.buf[4])) * ((1 << 16) / 270),
    92  		Z: z,
    93  	}
    94  }
    95  
    96  // Touched returns if touched or not.
    97  func (d *Device) Touched() bool {
    98  	p := d.ReadTouchPoint()
    99  	return p.Z > 0
   100  }
   101  
   102  func (d *Device) write1Byte(reg, data uint8) {
   103  	legacy.WriteRegister(d.bus, d.Address, reg, []byte{data})
   104  }
   105  
   106  func (d *Device) read8bit(reg uint8) uint8 {
   107  	legacy.ReadRegister(d.bus, d.Address, reg, d.buf[:1])
   108  	return d.buf[0]
   109  }