0
mirror of https://github.com/OneOfEleven/uv-k5-firmware-custom.git synced 2025-04-28 14:21:25 +03:00

96 lines
2.7 KiB
C
Raw Permalink Normal View History

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;
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;
2023-11-10 16:45:07 +00:00
PWM_PLUS0_CLKSRC |= (CPU_CLOCK_HZ / 1024 / PWM_FREQUENCY_HZ) << 16;
2023-11-10 13:23:32 +00:00
PWM_PLUS0_PERIOD = 1023;
PORTCON_PORTB_SEL0 &= ~(PORTCON_PORTB_SEL0_B6_MASK);
PORTCON_PORTB_SEL0 |= PORTCON_PORTB_SEL0_B6_BITS_PWMP0_CH0;
2023-11-10 17:02:04 +00:00
PWM_PLUS0_GEN = PWMPLUS_GEN_CH0_OE_BITS_ENABLE | PWMPLUS_GEN_CH0_OUTINV_BITS_ENABLE;
2023-11-10 13:23:32 +00:00
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-10 16:45:07 +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 = 120; break; // 2 min
case 6: ticks = 240; break; // 4 min
2023-10-24 18:16:16 +01:00
}
2023-11-10 13:23:32 +00:00
return ticks * 100;
}
2023-11-10 16:45:07 +00:00
bool BACKLIGHT_is_on(void)
{
return (PWM_PLUS0_CH0_COMP > 0) ? true : false;
}
2023-11-10 13:23:32 +00:00
void BACKLIGHT_set_brightness(unsigned int brightness)
{
2023-11-10 16:45:07 +00:00
if (brightness > BACKLIGHT_MAX_BRIGHTNESS)
brightness = BACKLIGHT_MAX_BRIGHTNESS;
2023-11-10 13:23:32 +00:00
2023-11-25 08:52:23 +00:00
// 0 ~ 1023 logify it though
2023-11-10 16:45:07 +00:00
PWM_PLUS0_CH0_COMP = (1023ul * brightness * brightness * brightness) / (BACKLIGHT_MAX_BRIGHTNESS * BACKLIGHT_MAX_BRIGHTNESS * BACKLIGHT_MAX_BRIGHTNESS);
2023-11-10 13:23:32 +00:00
//PWM_PLUS0_SWLOAD = 1;
2023-10-24 18:16:16 +01:00
}
2023-11-10 16:45:07 +00:00
void BACKLIGHT_turn_on(const unsigned int min_secs)
2023-09-09 08:03:56 +01:00
{
2023-11-10 17:02:04 +00:00
const uint16_t min_ticks = 100u * min_secs;
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
g_backlight_tick_10ms = BACKLIGHT_ticks();
2023-11-10 17:02:04 +00:00
BACKLIGHT_set_brightness(BACKLIGHT_MAX_BRIGHTNESS);
2023-09-09 08:03:56 +01:00
}
}
2023-11-10 13:23:32 +00:00
void BACKLIGHT_turn_off(void)
{
2023-11-10 17:02:04 +00:00
if (g_backlight_tick_10ms > BACKLIGHT_MAX_BRIGHTNESS)
g_backlight_tick_10ms = BACKLIGHT_MAX_BRIGHTNESS;
// g_backlight_tick_10ms = 0;
BACKLIGHT_set_brightness(g_backlight_tick_10ms);
2023-11-10 13:23:32 +00:00
}