2020-11-14 23:04:22 -05:00
|
|
|
#include "Cst816s.h"
|
2020-01-03 10:32:31 -05:00
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <legacy/nrf_drv_gpiote.h>
|
2020-11-14 23:04:22 -05:00
|
|
|
#include <nrfx_log.h>
|
|
|
|
#include <task.h>
|
2021-08-03 14:32:23 -04:00
|
|
|
#include "drivers/PinMap.h"
|
2020-11-14 23:04:22 -05:00
|
|
|
|
2020-01-03 10:32:31 -05:00
|
|
|
using namespace Pinetime::Drivers;
|
|
|
|
|
|
|
|
/* References :
|
2021-04-18 13:28:14 -04:00
|
|
|
* This implementation is based on this article :
|
|
|
|
* https://medium.com/@ly.lee/building-a-rust-driver-for-pinetimes-touch-controller-cbc1a5d5d3e9 Touch panel datasheet (weird chinese
|
|
|
|
* translation) : https://wiki.pine64.org/images/5/51/CST816S%E6%95%B0%E6%8D%AE%E6%89%8B%E5%86%8CV1.1.en.pdf
|
2020-01-03 10:32:31 -05:00
|
|
|
*
|
|
|
|
* TODO : we need a complete datasheet and protocol reference!
|
|
|
|
* */
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
Cst816S::Cst816S(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, twiAddress {twiAddress} {
|
2020-07-19 14:30:44 -04:00
|
|
|
}
|
|
|
|
|
2021-08-18 08:23:30 -04:00
|
|
|
bool Cst816S::Init() {
|
2021-08-03 14:32:23 -04:00
|
|
|
nrf_gpio_cfg_output(PinMap::Cst816sReset);
|
|
|
|
nrf_gpio_pin_clear(PinMap::Cst816sReset);
|
2020-07-19 14:30:44 -04:00
|
|
|
vTaskDelay(5);
|
2021-08-03 14:32:23 -04:00
|
|
|
nrf_gpio_pin_set(PinMap::Cst816sReset);
|
2020-07-19 14:30:44 -04:00
|
|
|
vTaskDelay(50);
|
2020-01-03 10:32:31 -05:00
|
|
|
|
2020-07-19 14:30:44 -04:00
|
|
|
// Wake the touchpanel up
|
|
|
|
uint8_t dummy;
|
|
|
|
twiMaster.Read(twiAddress, 0x15, &dummy, 1);
|
|
|
|
vTaskDelay(5);
|
|
|
|
twiMaster.Read(twiAddress, 0xa7, &dummy, 1);
|
2021-04-10 14:09:33 -04:00
|
|
|
vTaskDelay(5);
|
2021-04-18 13:28:14 -04:00
|
|
|
|
2021-11-06 14:01:19 -04:00
|
|
|
static constexpr uint8_t maxRetries = 3;
|
2021-11-07 10:19:06 -05:00
|
|
|
bool isDeviceOk;
|
2021-11-06 14:01:19 -04:00
|
|
|
uint8_t retries = 0;
|
|
|
|
do {
|
|
|
|
isDeviceOk = CheckDeviceIds();
|
|
|
|
retries++;
|
2021-11-07 10:19:06 -05:00
|
|
|
} while (!isDeviceOk && retries < maxRetries);
|
2021-11-06 14:01:19 -04:00
|
|
|
|
2021-11-07 10:19:06 -05:00
|
|
|
if (!isDeviceOk) {
|
2021-11-06 14:01:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-03 22:08:51 -04:00
|
|
|
/*
|
|
|
|
[2] EnConLR - Continuous operation can slide around
|
|
|
|
[1] EnConUD - Slide up and down to enable continuous operation
|
|
|
|
[0] EnDClick - Enable Double-click action
|
|
|
|
*/
|
|
|
|
static constexpr uint8_t motionMask = 0b00000101;
|
|
|
|
twiMaster.Write(twiAddress, 0xEC, &motionMask, 1);
|
2021-07-14 10:11:16 -04:00
|
|
|
|
2021-07-15 07:11:27 -04:00
|
|
|
/*
|
|
|
|
[7] EnTest - Interrupt pin to test, enable automatic periodic issued after a low pulse.
|
|
|
|
[6] EnTouch - When a touch is detected, a periodic pulsed Low.
|
|
|
|
[5] EnChange - Upon detecting a touch state changes, pulsed Low.
|
|
|
|
[4] EnMotion - When the detected gesture is pulsed Low.
|
|
|
|
[0] OnceWLP - Press gesture only issue a pulse signal is low.
|
|
|
|
*/
|
|
|
|
static constexpr uint8_t irqCtl = 0b01110000;
|
|
|
|
twiMaster.Write(twiAddress, 0xFA, &irqCtl, 1);
|
2021-08-28 10:19:49 -04:00
|
|
|
|
2021-11-06 14:01:19 -04:00
|
|
|
return true;
|
2020-01-03 10:32:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Cst816S::TouchInfos Cst816S::GetTouchInfo() {
|
|
|
|
Cst816S::TouchInfos info;
|
2021-09-15 05:25:41 -04:00
|
|
|
uint8_t touchData[7];
|
2020-01-03 10:32:31 -05:00
|
|
|
|
2021-04-03 22:08:51 -04:00
|
|
|
auto ret = twiMaster.Read(twiAddress, 0, touchData, sizeof(touchData));
|
2021-07-14 10:11:16 -04:00
|
|
|
if (ret != TwiMaster::ErrorCodes::NoError) {
|
|
|
|
info.isValid = false;
|
2021-04-18 13:28:14 -04:00
|
|
|
return info;
|
2021-07-14 10:11:16 -04:00
|
|
|
}
|
2021-04-03 22:08:51 -04:00
|
|
|
|
2021-07-14 10:11:16 -04:00
|
|
|
// This can only be 0 or 1
|
2021-09-15 05:25:41 -04:00
|
|
|
uint8_t nbTouchPoints = touchData[touchPointNumIndex] & 0x0f;
|
|
|
|
uint8_t xHigh = touchData[touchXHighIndex] & 0x0f;
|
|
|
|
uint8_t xLow = touchData[touchXLowIndex];
|
2021-11-06 14:01:19 -04:00
|
|
|
uint16_t x = (xHigh << 8) | xLow;
|
2021-09-15 05:25:41 -04:00
|
|
|
uint8_t yHigh = touchData[touchYHighIndex] & 0x0f;
|
|
|
|
uint8_t yLow = touchData[touchYLowIndex];
|
2021-11-06 14:01:19 -04:00
|
|
|
uint16_t y = (yHigh << 8) | yLow;
|
|
|
|
Gestures gesture = static_cast<Gestures>(touchData[gestureIndex]);
|
|
|
|
|
|
|
|
// Validity check
|
|
|
|
if(x >= maxX || y >= maxY ||
|
|
|
|
(gesture != Gestures::None &&
|
|
|
|
gesture != Gestures::SlideDown &&
|
|
|
|
gesture != Gestures::SlideUp &&
|
|
|
|
gesture != Gestures::SlideLeft &&
|
|
|
|
gesture != Gestures::SlideRight &&
|
|
|
|
gesture != Gestures::SingleTap &&
|
|
|
|
gesture != Gestures::DoubleTap &&
|
|
|
|
gesture != Gestures::LongPress)) {
|
|
|
|
info.isValid = false;
|
|
|
|
return info;
|
|
|
|
}
|
2021-04-03 22:08:51 -04:00
|
|
|
|
2021-11-06 14:01:19 -04:00
|
|
|
info.x = x;
|
|
|
|
info.y = y;
|
2021-07-14 10:11:16 -04:00
|
|
|
info.touching = (nbTouchPoints > 0);
|
2021-11-06 14:01:19 -04:00
|
|
|
info.gesture = gesture;
|
|
|
|
info.isValid = true;
|
2020-01-03 10:32:31 -05:00
|
|
|
return info;
|
|
|
|
}
|
2020-01-17 16:16:45 -05:00
|
|
|
|
|
|
|
void Cst816S::Sleep() {
|
2021-08-03 14:32:23 -04:00
|
|
|
nrf_gpio_pin_clear(PinMap::Cst816sReset);
|
2020-08-22 11:59:59 -04:00
|
|
|
vTaskDelay(5);
|
2021-08-03 14:32:23 -04:00
|
|
|
nrf_gpio_pin_set(PinMap::Cst816sReset);
|
2020-08-22 11:59:59 -04:00
|
|
|
vTaskDelay(50);
|
|
|
|
static constexpr uint8_t sleepValue = 0x03;
|
|
|
|
twiMaster.Write(twiAddress, 0xA5, &sleepValue, 1);
|
|
|
|
NRF_LOG_INFO("[TOUCHPANEL] Sleep");
|
2020-01-17 16:16:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Cst816S::Wakeup() {
|
|
|
|
Init();
|
2020-08-22 11:59:59 -04:00
|
|
|
NRF_LOG_INFO("[TOUCHPANEL] Wakeup");
|
2021-07-14 10:11:16 -04:00
|
|
|
}
|
2021-11-06 14:01:19 -04:00
|
|
|
|
|
|
|
bool Cst816S::CheckDeviceIds() {
|
|
|
|
// There's mixed information about which register contains which information
|
|
|
|
if (twiMaster.Read(twiAddress, 0xA7, &chipId, 1) == TwiMaster::ErrorCodes::TransactionFailed) {
|
|
|
|
chipId = 0xFF;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (twiMaster.Read(twiAddress, 0xA8, &vendorId, 1) == TwiMaster::ErrorCodes::TransactionFailed) {
|
|
|
|
vendorId = 0xFF;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (twiMaster.Read(twiAddress, 0xA9, &fwVersion, 1) == TwiMaster::ErrorCodes::TransactionFailed) {
|
|
|
|
fwVersion = 0xFF;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return chipId == 0xb4 && vendorId == 0 && fwVersion == 1;
|
|
|
|
}
|