diff options
| author | Jean-François Milants <jf@codingfield.com> | 2021-11-11 09:08:20 (GMT) |
|---|---|---|
| committer | Jean-François Milants <jf@codingfield.com> | 2021-11-11 09:08:20 (GMT) |
| commit | f6d0ec49e6f2669c64729b081eb3342f02151f4a (patch) | |
| tree | 7e21c83726bd3e0d18ec4ebe871274b1dbe2eb38 /src/drivers | |
| parent | f41aaad6836ae348d1b5b084b4533b636f516b93 (diff) | |
| parent | a57fda6ba4a29866083a1254ffdf92939d00e182 (diff) | |
Merge branch 'develop'
# Conflicts:
# doc/buildAndProgram.md
Diffstat (limited to 'src/drivers')
| -rw-r--r-- | src/drivers/Cst816s.cpp | 69 | ||||
| -rw-r--r-- | src/drivers/Cst816s.h | 23 | ||||
| -rw-r--r-- | src/drivers/St7789.cpp | 9 |
3 files changed, 77 insertions, 24 deletions
diff --git a/src/drivers/Cst816s.cpp b/src/drivers/Cst816s.cpp index 1ff163b..4aac19f 100644 --- a/src/drivers/Cst816s.cpp +++ b/src/drivers/Cst816s.cpp @@ -18,10 +18,8 @@ using namespace Pinetime::Drivers; Cst816S::Cst816S(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, twiAddress {twiAddress} { } -void Cst816S::Init() { +bool Cst816S::Init() { nrf_gpio_cfg_output(PinMap::Cst816sReset); - nrf_gpio_pin_set(PinMap::Cst816sReset); - vTaskDelay(50); nrf_gpio_pin_clear(PinMap::Cst816sReset); vTaskDelay(5); nrf_gpio_pin_set(PinMap::Cst816sReset); @@ -34,6 +32,18 @@ void Cst816S::Init() { twiMaster.Read(twiAddress, 0xa7, &dummy, 1); vTaskDelay(5); + static constexpr uint8_t maxRetries = 3; + bool isDeviceOk; + uint8_t retries = 0; + do { + isDeviceOk = CheckDeviceIds(); + retries++; + } while (!isDeviceOk && retries < maxRetries); + + if (!isDeviceOk) { + return false; + } + /* [2] EnConLR - Continuous operation can slide around [1] EnConUD - Slide up and down to enable continuous operation @@ -51,10 +61,13 @@ void Cst816S::Init() { */ static constexpr uint8_t irqCtl = 0b01110000; twiMaster.Write(twiAddress, 0xFA, &irqCtl, 1); + + return true; } Cst816S::TouchInfos Cst816S::GetTouchInfo() { Cst816S::TouchInfos info; + uint8_t touchData[7]; auto ret = twiMaster.Read(twiAddress, 0, touchData, sizeof(touchData)); if (ret != TwiMaster::ErrorCodes::NoError) { @@ -62,21 +75,35 @@ Cst816S::TouchInfos Cst816S::GetTouchInfo() { return info; } - auto nbTouchPoints = touchData[2] & 0x0f; - - auto xHigh = touchData[touchXHighIndex] & 0x0f; - auto xLow = touchData[touchXLowIndex]; + // This can only be 0 or 1 + uint8_t nbTouchPoints = touchData[touchPointNumIndex] & 0x0f; + uint8_t xHigh = touchData[touchXHighIndex] & 0x0f; + uint8_t xLow = touchData[touchXLowIndex]; uint16_t x = (xHigh << 8) | xLow; - - auto yHigh = touchData[touchYHighIndex] & 0x0f; - auto yLow = touchData[touchYLowIndex]; + uint8_t yHigh = touchData[touchYHighIndex] & 0x0f; + uint8_t yLow = touchData[touchYLowIndex]; 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; + } info.x = x; info.y = y; info.touching = (nbTouchPoints > 0); - info.gesture = static_cast<Gestures>(touchData[gestureIndex]); - + info.gesture = gesture; + info.isValid = true; return info; } @@ -94,3 +121,21 @@ void Cst816S::Wakeup() { Init(); NRF_LOG_INFO("[TOUCHPANEL] Wakeup"); } + +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; +} diff --git a/src/drivers/Cst816s.h b/src/drivers/Cst816s.h index 7b46c5d..507dd4f 100644 --- a/src/drivers/Cst816s.h +++ b/src/drivers/Cst816s.h @@ -21,7 +21,7 @@ namespace Pinetime { uint16_t y = 0; Gestures gesture = Gestures::None; bool touching = false; - bool isValid = true; + bool isValid = false; }; Cst816S(TwiMaster& twiMaster, uint8_t twiAddress); @@ -30,12 +30,23 @@ namespace Pinetime { Cst816S(Cst816S&&) = delete; Cst816S& operator=(Cst816S&&) = delete; - void Init(); + bool Init(); TouchInfos GetTouchInfo(); void Sleep(); void Wakeup(); + uint8_t GetChipId() const { + return chipId; + } + uint8_t GetVendorId() const { + return vendorId; + } + uint8_t GetFwVersion() const { + return fwVersion; + } private: + bool CheckDeviceIds(); + // Unused/Unavailable commented out static constexpr uint8_t gestureIndex = 1; static constexpr uint8_t touchPointNumIndex = 2; @@ -49,9 +60,15 @@ namespace Pinetime { //static constexpr uint8_t touchXYIndex = 7; //static constexpr uint8_t touchMiscIndex = 8; - uint8_t touchData[7]; + static constexpr uint8_t maxX = 240; + static constexpr uint8_t maxY = 240; + TwiMaster& twiMaster; uint8_t twiAddress; + + uint8_t chipId; + uint8_t vendorId; + uint8_t fwVersion; }; } diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index 0f1dc02..4d81cf2 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -170,16 +170,7 @@ void St7789::Sleep() { void St7789::Wakeup() { nrf_gpio_cfg_output(pinDataCommand); - // TODO why do we need to reset the controller? - HardwareReset(); - SoftwareReset(); SleepOut(); - ColMod(); - MemoryDataAccessControl(); - ColumnAddressSet(); - RowAddressSet(); - DisplayInversionOn(); - NormalModeOn(); VerticalScrollStartAddress(verticalScrollingStartAddress); DisplayOn(); NRF_LOG_INFO("[LCD] Wakeup") |
