diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index af34996..aa08903 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -1,7 +1,6 @@ #include "HardwareRevX.hpp" #include "display.hpp" #include "wifihandler.hpp" -#include "driver/ledc.h" std::shared_ptr HardwareRevX::mInstance = nullptr; @@ -70,19 +69,17 @@ HardwareRevX::WakeReason getWakeReason() { void HardwareRevX::init() { // Make sure ESP32 is running at full speed setCpuFrequencyMhz(240); + wakeup_reason = getWakeReason(); + initIO(); + Serial.begin(115200); mDisplay = Display::getInstance(); mBattery = std::make_shared(ADC_BAT,CRG_STAT); mWifiHandler = wifiHandler::getInstance(); + restorePreferences(); mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint){ standbyTimer = SLEEP_TIMEOUT;}); - wakeup_reason = getWakeReason(); - initIO(); - setupBacklight(); - Serial.begin(115200); - restorePreferences(); - slowDisplayWakeup(); setupIMU(); setupIR(); @@ -254,29 +251,6 @@ void HardwareRevX::configIMUInterrupts() { IMU.writeRegister(LIS3DH_CTRL_REG3, dataToWrite); } -// TODO move to display -void HardwareRevX::setupBacklight() { - // Configure the backlight PWM - // Manual setup because ledcSetup() briefly turns on the backlight - ledc_channel_config_t ledc_channel_left; - ledc_channel_left.gpio_num = (gpio_num_t)LCD_BL; - ledc_channel_left.speed_mode = LEDC_HIGH_SPEED_MODE; - ledc_channel_left.channel = LEDC_CHANNEL_5; - ledc_channel_left.intr_type = LEDC_INTR_DISABLE; - ledc_channel_left.timer_sel = LEDC_TIMER_1; - ledc_channel_left.flags.output_invert = 1; // Can't do this with ledcSetup() - ledc_channel_left.duty = 0; - - ledc_timer_config_t ledc_timer; - ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE; - ledc_timer.duty_resolution = LEDC_TIMER_8_BIT; - ledc_timer.timer_num = LEDC_TIMER_1; - ledc_timer.freq_hz = 640; - - ledc_channel_config(&ledc_channel_left); - ledc_timer_config(&ledc_timer); -} - void HardwareRevX::restorePreferences() { // Restore settings from internal flash memory int backlight_brightness = 255; @@ -304,19 +278,6 @@ void HardwareRevX::setupIMU() { IMU.readRegister(&intDataRead, LIS3DH_INT1_SRC); // clear interrupt } -// TODO move to display -void HardwareRevX::slowDisplayWakeup() { - // Slowly charge the VSW voltage to prevent a brownout - // Workaround for hardware rev 1! - for (int i = 0; i < 100; i++) { - digitalWrite(LCD_EN, HIGH); // LCD Logic off - delayMicroseconds(1); - digitalWrite(LCD_EN, LOW); // LCD Logic on - } - - delay(100); // Wait for the LCD driver to power on -} - void HardwareRevX::setupIR() { // Setup IR IrSender.begin(); diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index e34b140..249f8ad 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -43,7 +43,6 @@ public: protected: // Init Functions to setup hardware void initIO(); - void setupBacklight(); void restorePreferences(); void slowDisplayWakeup(); void setupIMU(); diff --git a/Platformio/HAL/Targets/ESP32/display/display.cpp b/Platformio/HAL/Targets/ESP32/display/display.cpp index 854acdd..403ee98 100644 --- a/Platformio/HAL/Targets/ESP32/display/display.cpp +++ b/Platformio/HAL/Targets/ESP32/display/display.cpp @@ -2,6 +2,7 @@ #include "display.hpp" #include "omoteconfig.h" #include "Wire.h" +#include "driver/ledc.h" std::shared_ptr Display::getInstance() { @@ -23,11 +24,12 @@ Display::Display(int backlight_pin, int enable_pin): DisplayAbstract(), pinMode(mBacklightPin, OUTPUT); digitalWrite(mBacklightPin, HIGH); - ledcSetup(LCD_BACKLIGHT_LEDC_CHANNEL, LCD_BACKLIGHT_LEDC_FREQUENCY, LCD_BACKLIGHT_LEDC_BIT_RESOLUTION); - ledcAttachPin(mBacklightPin, LCD_BACKLIGHT_LEDC_CHANNEL); - ledcWrite(LCD_BACKLIGHT_LEDC_CHANNEL, 0); + setupBacklight(); // This eliminates the flash of the backlight - setupTFT(); + // This backlight init causes the backlight to come on full during startup + // ledcSetup(LCD_BACKLIGHT_LEDC_CHANNEL, LCD_BACKLIGHT_LEDC_FREQUENCY, LCD_BACKLIGHT_LEDC_BIT_RESOLUTION); + // ledcAttachPin(mBacklightPin, LCD_BACKLIGHT_LEDC_CHANNEL); + // ledcWrite(LCD_BACKLIGHT_LEDC_CHANNEL, 0); // Slowly charge the VSW voltage to prevent a brownout // Workaround for hardware rev 1! @@ -37,16 +39,40 @@ Display::Display(int backlight_pin, int enable_pin): DisplayAbstract(), digitalWrite(this->mEnablePin, LOW); // LCD Logic on } + setupTFT(); setupTouchScreen(); mFadeTaskMutex = xSemaphoreCreateBinary(); xSemaphoreGive(mFadeTaskMutex); } +void Display::setupBacklight() { + // Configure the backlight PWM + // Manual setup because ledcSetup() briefly turns on the backlight + ledc_channel_config_t ledc_channel_left; + ledc_channel_left.gpio_num = (gpio_num_t)mBacklightPin; + ledc_channel_left.speed_mode = LEDC_HIGH_SPEED_MODE; + ledc_channel_left.channel = LEDC_CHANNEL_5; + ledc_channel_left.intr_type = LEDC_INTR_DISABLE; + ledc_channel_left.timer_sel = LEDC_TIMER_1; + ledc_channel_left.flags.output_invert = 1; // Can't do this with ledcSetup() + ledc_channel_left.duty = 0; + ledc_channel_left.hpoint = 0; + ledc_timer_config_t ledc_timer; + ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE; + ledc_timer.duty_resolution = LEDC_TIMER_8_BIT; + ledc_timer.timer_num = LEDC_TIMER_1; + ledc_timer.clk_cfg = LEDC_AUTO_CLK; + ledc_timer.freq_hz = 640; + ledc_channel_config(&ledc_channel_left); + ledc_timer_config(&ledc_timer); +} + void Display::onTouch(Notification::HandlerTy aTouchHandler){ mTouchEvent.onNotify(std::move(aTouchHandler)); } void Display::setupTFT() { + delay(100); tft.init(); tft.initDMA(); tft.setRotation(0); @@ -74,10 +100,11 @@ uint8_t Display::getBrightness(){ void Display::setCurrentBrightness(uint8_t brightness){ mBrightness = brightness; - auto duty = abs(255 - static_cast(mBrightness)); + // auto duty = abs(255 - static_cast(mBrightness)); + auto duty = abs(static_cast(mBrightness)); ledcWrite(LCD_BACKLIGHT_LEDC_CHANNEL, duty); - Serial.print("Current Brightness:"); - Serial.println(mBrightness); + // Serial.print("Current Brightness:"); + // Serial.println(mBrightness); } void Display::turnOff() diff --git a/Platformio/HAL/Targets/ESP32/display/display.hpp b/Platformio/HAL/Targets/ESP32/display/display.hpp index 51b29d7..214c8b8 100644 --- a/Platformio/HAL/Targets/ESP32/display/display.hpp +++ b/Platformio/HAL/Targets/ESP32/display/display.hpp @@ -52,6 +52,7 @@ class Display: public DisplayAbstract Display(int backlight_pin, int enable_pin); void setupTFT(); void setupTouchScreen(); + void setupBacklight(); int mEnablePin; int mBacklightPin;