Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How backlight works on t-deck plus? #61

Open
bettio opened this issue Oct 28, 2024 · 5 comments
Open

How backlight works on t-deck plus? #61

bettio opened this issue Oct 28, 2024 · 5 comments

Comments

@bettio
Copy link

bettio commented Oct 28, 2024

Display backlight (gpio 42) is working on regular t-deck using LEDC PWM , but same code doesn't work with the Plus model.
How backlight can be enabled with esp-idf APIs? Should I use any specific PWM frequency or duty cycle?

@lewisxhe
Copy link
Contributor

void setBrightness(uint8_t value)

@bettio
Copy link
Author

bettio commented Oct 29, 2024

Thanks for the reply, I have 2 more questions: does setBrightness works same way on both Plus and non Plus?
Furthermore, I tried to translate it to regular esp-idf code, with no success:

#define BOARD_BL_PIN 42

void setBrightness(uint8_t value)
{
    gpio_set_direction(BOARD_BL_PIN, GPIO_MODE_OUTPUT);

    static uint8_t level = 0;
    static uint8_t steps = 16;
    if (value == 0) {
        gpio_set_level(BOARD_BL_PIN, 0);
        vTaskDelay(3 / portTICK_PERIOD_MS);
        level = 0;
        return;
    }
    if (level == 0) {
        gpio_set_level(BOARD_BL_PIN, 1);
        level = steps;
        delayMicroseconds(30);
    }
    int from = steps - level;
    int to = steps - value;
    int num = (steps + to - from) % steps;
    for (int i = 0; i < num; i++) {
        gpio_set_level(BOARD_BL_PIN, 0);
        gpio_set_level(BOARD_BL_PIN, 1);
    }
    level = value;
}

#define NOP() asm volatile ("nop")

unsigned long IRAM_ATTR micros()
{
    return (unsigned long) (esp_timer_get_time());
}

void IRAM_ATTR delayMicroseconds(uint32_t us)
{
    uint32_t m = micros();
    if(us){
        uint32_t e = (m + us);
        if(m > e){ //overflow
            while(micros() > e){
                NOP();
            }
        }
        while(micros() < e){
            NOP();
        }
    }
}

am I missing anything?
Furthermore can you provide few words about required timings to make this work?

@lewisxhe
Copy link
Contributor

Plus and non-Plus are the same, only the shell and screen are different

I tested it and there was no problem at all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include <rom/ets_sys.h>
#define BOARD_BL_PIN 42
#define BOARD_POWERON 10

void setBrightness(uint8_t value)
{
    static uint8_t level = 0;
    static uint8_t steps = 16;
    if (value == 0)
    {
        gpio_set_level((gpio_num_t)BOARD_BL_PIN, 0);
        vTaskDelay(pdMS_TO_TICKS(3));
        level = 0;
        return;
    }
    if (level == 0)
    {
        gpio_set_level((gpio_num_t)BOARD_BL_PIN, 1);
        level = steps;
        ets_delay_us(30);
    }
    int from = steps - level;
    int to = steps - value;
    int num = (steps + to - from) % steps;
    for (int i = 0; i < num; i++)
    {
        gpio_set_level((gpio_num_t)BOARD_BL_PIN, 0);
        gpio_set_level((gpio_num_t)BOARD_BL_PIN, 1);
    }
    level = value;
}

void app_main(void)
{

    gpio_config_t config;
    memset(&config, 0, sizeof(config));
    config.pin_bit_mask = 1ULL << BOARD_POWERON;
    config.mode = GPIO_MODE_OUTPUT;
    config.pull_up_en = GPIO_PULLUP_DISABLE;
    config.pull_down_en = GPIO_PULLDOWN_DISABLE;
    config.intr_type = GPIO_INTR_DISABLE;
    ESP_ERROR_CHECK(gpio_config(&config));
    gpio_set_level((gpio_num_t)BOARD_POWERON, 1);

    gpio_config_t bl_pin;
    memset(&bl_pin, 0, sizeof(bl_pin));
    bl_pin.pin_bit_mask = 1ULL << BOARD_BL_PIN;
    bl_pin.mode = GPIO_MODE_OUTPUT;
    bl_pin.pull_up_en = GPIO_PULLUP_DISABLE;
    bl_pin.pull_down_en = GPIO_PULLDOWN_DISABLE;
    bl_pin.intr_type = GPIO_INTR_DISABLE;
    ESP_ERROR_CHECK(gpio_config(&bl_pin));


    uint8_t level = 0;
    while (1)
    {
        setBrightness(level++);
        level %= 16;
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

@bettio
Copy link
Author

bettio commented Oct 30, 2024

Now it works, but there is something that I don't understand yet: how gpio 10 is involved here?
Also it looks required only for the non plus one.

@lewisxhe
Copy link
Contributor

lewisxhe commented Nov 1, 2024

//! The board peripheral power control pin needs to be set to HIGH when using the peripheral

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants