2023-09-09 08:03:56 +01:00
|
|
|
/* Copyright 2023 Dual Tachyon
|
|
|
|
* https://github.com/DualTachyon
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-11-10 13:23:32 +00:00
|
|
|
#include "backlight.h"
|
2023-09-09 08:03:56 +01:00
|
|
|
#include "bsp/dp32g030/gpio.h"
|
2023-11-10 13:23:32 +00:00
|
|
|
#include "bsp/dp32g030/pwmplus.h"
|
|
|
|
#include "bsp/dp32g030/portcon.h"
|
2023-09-09 08:03:56 +01:00
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
2023-11-10 13:23:32 +00:00
|
|
|
uint16_t g_backlight_tick_10ms;
|
|
|
|
bool g_backlight_on;
|
2023-09-09 08:03:56 +01:00
|
|
|
|
2023-11-10 13:23:32 +00:00
|
|
|
void BACKLIGHT_init(void)
|
|
|
|
{
|
|
|
|
// 48MHz / 94 / 1024 ~ 500Hz
|
|
|
|
const uint32_t PWM_FREQUENCY_HZ = 1000;
|
|
|
|
PWM_PLUS0_CLKSRC |= ((48000000 / 1024 / PWM_FREQUENCY_HZ) << 16);
|
|
|
|
PWM_PLUS0_PERIOD = 1023;
|
|
|
|
|
|
|
|
PORTCON_PORTB_SEL0 &= ~(PORTCON_PORTB_SEL0_B6_MASK);
|
|
|
|
PORTCON_PORTB_SEL0 |= PORTCON_PORTB_SEL0_B6_BITS_PWMP0_CH0;
|
|
|
|
|
|
|
|
PWM_PLUS0_GEN = PWMPLUS_GEN_CH0_OE_BITS_ENABLE | PWMPLUS_GEN_CH0_OUTINV_BITS_ENABLE;
|
|
|
|
|
|
|
|
PWM_PLUS0_CFG = PWMPLUS_CFG_CNT_REP_BITS_ENABLE | PWMPLUS_CFG_COUNTER_EN_BITS_ENABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t BACKLIGHT_ticks(void)
|
2023-10-24 18:16:16 +01:00
|
|
|
{
|
2023-11-04 12:53:36 +00:00
|
|
|
uint16_t ticks = 0;
|
2023-11-02 10:00:51 +00:00
|
|
|
switch (g_eeprom.config.setting.backlight_time)
|
2023-10-24 18:16:16 +01:00
|
|
|
{
|
2023-11-04 12:53:36 +00:00
|
|
|
case 1: ticks = 5; break; // 5 sec
|
|
|
|
case 2: ticks = 10; break; // 10 sec
|
|
|
|
case 3: ticks = 20; break; // 20 sec
|
|
|
|
case 4: ticks = 60; break; // 1 min
|
|
|
|
case 5: ticks = 60 * 2; break; // 2 min
|
|
|
|
case 6: ticks = 60 * 4; break; // 4 min
|
2023-10-24 18:16:16 +01:00
|
|
|
}
|
2023-11-10 13:23:32 +00:00
|
|
|
return ticks * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BACKLIGHT_set_brightness(unsigned int brightness)
|
|
|
|
{
|
|
|
|
brightness = (brightness > BACKLIGHT_MAX_BRIGHTNESS) ? BACKLIGHT_MAX_BRIGHTNESS : brightness;
|
|
|
|
|
|
|
|
// non-linear
|
|
|
|
PWM_PLUS0_CH0_COMP = (1023ul * brightness * brightness) / (BACKLIGHT_MAX_BRIGHTNESS * BACKLIGHT_MAX_BRIGHTNESS);
|
|
|
|
//PWM_PLUS0_SWLOAD = 1;
|
|
|
|
|
|
|
|
g_backlight_on = (brightness > 0) ? true : false;
|
2023-10-24 18:16:16 +01:00
|
|
|
}
|
|
|
|
|
2023-11-10 13:23:32 +00:00
|
|
|
void BACKLIGHT_turn_on(const uint16_t min_ticks)
|
2023-09-09 08:03:56 +01:00
|
|
|
{
|
2023-10-16 18:19:28 +01:00
|
|
|
if (min_ticks > 0)
|
|
|
|
{
|
2023-11-10 13:23:32 +00:00
|
|
|
if (g_backlight_tick_10ms < min_ticks)
|
|
|
|
g_backlight_tick_10ms = min_ticks;
|
|
|
|
BACKLIGHT_set_brightness(BACKLIGHT_MAX_BRIGHTNESS);
|
2023-10-16 18:19:28 +01:00
|
|
|
}
|
|
|
|
else
|
2023-11-02 10:00:51 +00:00
|
|
|
if (g_eeprom.config.setting.backlight_time > 0)
|
2023-09-09 08:03:56 +01:00
|
|
|
{
|
2023-11-10 13:23:32 +00:00
|
|
|
BACKLIGHT_set_brightness(BACKLIGHT_MAX_BRIGHTNESS);
|
|
|
|
g_backlight_tick_10ms = BACKLIGHT_ticks();
|
2023-09-09 08:03:56 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 13:23:32 +00:00
|
|
|
|
|
|
|
void BACKLIGHT_turn_off(void)
|
|
|
|
{
|
|
|
|
g_backlight_tick_10ms = 0;
|
|
|
|
BACKLIGHT_set_brightness(0);
|
|
|
|
}
|