Version 0.1, +05:30 09:01:36 PM 18-02-2025, Tuesday
- CSE_Touch - Unified touch controller library for Arduino.
The debug output is controlled by the DEBUG_SERIAL macro.
All of the constants are defined in the CSE_CST328_Constants.h file.
CSE_CST328- The main class for wrapping the data and functions of the library.
uint16_t defWidth- Default width of the screen. This is used when rotating the screen.uint16_t defHeight- Default height of the screen. This is used when rotating the screen.uint16_t width- Current width of the screen.uint16_t height- Current height of the screen.uint8_t rotation- Current rotation of the screen.CSE_TouchPoint touchPoints [5]- Array of 5 touch points.
TwoWire *wireInstance- A pointer to the I2C bus.int8_t pinReset- Chip reset pin.int8_t pinInterrupt- Chip interrupt pin.bool inited- Flag to indicate if the library has been initialized.
Constructor for the CSE_CST328 class.
CSE_CST328 (uint16_t width, uint16_t height, TwoWire *i2c = &Wire, int8_t pinRst = -1, int8_t pinIrq = -1);Default width and height are set to the initial width and height. The touchPoints array is initialized with all 0s, but with 0~4 touchIds. The default rotation is set to 0 and the inited flag is set to false.
width: Width of the screen.height: Height of the screen.i2c: Pointer to the I2C bus.pinRst: Chip reset pin. Defaults to-1for no reset pin.pinIrq: Chip interrupt pin. Defaults to-1for no interrupt pin.
CSE_CST328object.
Initializes the library and the touch panel. This must be called first before using any other functions in the library. If the library is already initialized, this function will return immediately. The Wire instance is also initialized by default. If the reset pin is present (not -1), a reset sequence will be performed. If the interrupt pin is present (not -1), the pin is set to INPUT_PULLUP.
The chip is detected by reading the constant 0xCACA value at the REG_CST328_INFO_3 register. If the value is not found, the initialization will fail. After successful reading, the chip is put to normal mode.
bool begin();- None
bool:trueif initialization was successful,falseotherwise.
Reads the entire set of touch information registers from the CST328. The total number of registers is 27 bytes. The read data is saved to the touchPoints array after conversions and rotations. Since this function reads all data at a time, this can be slower. If you want to read the registers faster, try fastReadData().
void readData();- None
- None.
This reads the set of registers for a single finger or touch point. You can pass the id of the finger (0~4) to read the data. This operation is faster than readData(). Data is saved to the touchPoints array after applying conversions and rotations to the read data. Other touch points are unchanged. Since this function only reads one touch point at a time, the data of other touch points in the touchPoints array may be outdated.
void fastReadData (uint8_t id = 0);id: The finger ID of the point as per the data sheet. This is the index of the finger data in thetouchPointsarray. For 5 touch points, the range is 0~4.
- None.
Returns the number of active touches. Even though there is a dedicated register (0xD005) for reporting the number of touches, the register is not updated when the touches are lifted. That means, you can not rely on the register value to determine the number of active touches. To solve this problem, getTouches() reads all touch data from the chip and check the touch state of all points individually. The total count is then returned.
If you do not want to find the number of touches this way, you can try reading the register 0xD005 directly.
uint8_t getTouches();- None
uint8_t: Number of active touches.
Checks if the touch panel is touched or a specific touch point is being touched. There are two overloads.
bool isTouched();Check if the screen is being touched by checking all touch points.
- None
bool:trueif the screen is being touched,falseotherwise.
bool isTouched (uint8_t id = 0);Check if a specific touch point is being touched.
id: The finger ID of the point as per the data sheet. This is the index of the finger data in thetouchPointsarray. For 5 touch points, the range is 0~4.
bool:trueif the touch point is being touched,falseotherwise.
Returns the coordinates of the touch point. You need to send the finger ID to get the data. The data in the touchPoints array is returned. New data is not read from the chip while doing this. You need to manually call readData() or fastReadData() to update the data in the array. This is done so to give you control of when to read the data from the chip.
CSE_TouchPoint getPoint (uint8_t id = 0);id: The finger ID of the point as per the data sheet. This is the index of the finger data in thetouchPointsarray. For 5 touch points, the range is 0~4.
CSE_TouchPointobject.
Sets the rotation of the touch panel. The rotation of the touch panel is relative to the width and height you set during initialization. This function sets the rotation and updates the width and height variables accordingly.
If you think that the touch panel coordinates are rotated from the rotation of your screen, try the other possible rotation values. One of them will match your screen rotation.
void setRotation (uint8_t rotation);rotation: Possible values are 0, 1, 2, 3.
uint8_t: Current rotation. The possible values are 0, 1, 2, 3.
Returns the current rotation of the touch panel.
uint8_t getRotation();- None
uint8_t: Current rotation. The possible values are 0, 1, 2, 3.
Returns the current width of the touch panel. If you apply rotations, the width and height will be different. The default width and height are always stored in the defWidth and defHeight variables.
uint16_t getWidth();- None
uint16_t: Current width.
Returns the current height of the touch panel. If you apply rotations, the width and height will be different. The default width and height are always stored in the defWidth and defHeight variables.
uint16_t getHeight();- None
uint16_t: Current height.
Writes a single byte to a register in the chip. Since CST328 have all 16-bit register addresses, you need to send the 16-bit addresses.
void writeRegister8 (uint16_t reg, uint8_t val);reg: 16-bit address of the register.val: Data to be written to the register.
- None.
Reads a single byte from a register in the chip. Since CST328 have all 16-bit register addresses, you need to send the 16-bit addresses.
uint8_t readRegister8 (uint16_t reg);reg: 16-bit address of the register.
uint8_t: Data read from the register.
Reads a 32-bit value (four bytes) from a register in the chip. Since CST328 have all 16-bit register addresses, you need to send the 16-bit addresses.
uint32_t readRegister32 (uint16_t reg);reg: 16-bit address of the register.
uint32_t: Data read from the registers.
This is a special function that simply writes the register address as specified in the datasheet. It does not accept any values. Since CST328 have all 16-bit register addresses, you need to send the 16-bit addresses.
void write16 (uint16_t reg);reg: 16-bit address of the register.
- None.