From 125fd0f2aef688703d37ee61d21715527a8af207 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Thu, 22 Aug 2019 23:37:02 +0200
Subject: [PATCH] fix(display): Dim backlight in percent steps

---
 epicardium/epicardium.h         |  2 +-
 lib/card10/display.c            |  2 +-
 lib/gfx/GUI_DEV/DEV_Config.c    | 58 ++++++++++++++++++---------------
 lib/gfx/LCD/LCD_Driver.c        |  2 +-
 pycardium/modules/py/display.py |  2 +-
 5 files changed, 35 insertions(+), 31 deletions(-)

diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index 12acffb8..60f008a3 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -1337,7 +1337,7 @@ API(API_DISP_FRAMEBUFFER, int epic_disp_framebuffer(union disp_framebuffer *fb))
 /**
  * Set the backlight brightness value
  *
- * :param brightness: brightness from 0 - 1000
+ * :param brightness: brightness from 0 - 100
  * :return: ``0`` on success or negative value in case of an error:
  *
  *    - ``-EBUSY``: Display was already locked from another task.
diff --git a/lib/card10/display.c b/lib/card10/display.c
index 6bf37a0d..f5fd4715 100644
--- a/lib/card10/display.c
+++ b/lib/card10/display.c
@@ -37,7 +37,7 @@ void display_init(void)
 
 	GPIO_Config(&DEV_DC_PIN);
 
-	LCD_SetBacklight(200);
+	LCD_SetBacklight(20);
 	LCD_Init();
 
 	display_screen = gfx_screen(LCD_framebuffer());
diff --git a/lib/gfx/GUI_DEV/DEV_Config.c b/lib/gfx/GUI_DEV/DEV_Config.c
index 543604f7..bd2475e4 100644
--- a/lib/gfx/GUI_DEV/DEV_Config.c
+++ b/lib/gfx/GUI_DEV/DEV_Config.c
@@ -76,39 +76,43 @@ void DEV_Set_BL(uint16_t _Value)
 	gpio_cfg_t gpio_pwm;   // to configure GPIO
 	tmr_cfg_t tmr;         // to congigure timer
 	tmr_pwm_cfg_t tmr_pwm; // for configure PWM
-	unsigned int period_ticks = PeripheralClock / FREQ;
-	unsigned int duty_ticks   = period_ticks * _Value / 1000;
-
-	// Congfigure GPIO port and pin for PWM
-	gpio_pwm.func = GPIO_FUNC_ALT4;
-	gpio_pwm.port = PORT_PWM;
-	gpio_pwm.mask = PIN_PWM;
-	gpio_pwm.pad  = GPIO_PAD_PULL_DOWN;
 
-	GPIO_Config(&gpio_pwm);
+	if (_Value > 100) {
+		_Value = 100;
+	}
 
-	/* 
-    Steps for configuring a timer for PWM mode:
-    1. Disable the timer
-    2. Set the prescale value
-    3. Configure the timer for PWM mode
-    4. Set polarity, pwm parameters
-    5. Enable Timer
-    */
+	unsigned int period_ticks = PeripheralClock / FREQ;
+	unsigned int duty_ticks   = period_ticks * _Value / 100;
 
 	TMR_Disable(PWM_TIMER);
 
-	TMR_Init(PWM_TIMER, TMR_PRES_1, 0);
+	if (duty_ticks > 0) {
+		// Congfigure GPIO port and pin for PWM
+		gpio_pwm.func = GPIO_FUNC_ALT4;
+		gpio_pwm.port = PORT_PWM;
+		gpio_pwm.mask = PIN_PWM;
+		gpio_pwm.pad  = GPIO_PAD_PULL_DOWN;
+		GPIO_Config(&gpio_pwm);
+
+		TMR_Init(PWM_TIMER, TMR_PRES_1, 0);
 
-	tmr.mode    = TMR_MODE_PWM;
-	tmr.cmp_cnt = period_ticks;
-	tmr.pol     = 0;
-	TMR_Config(PWM_TIMER, &tmr);
+		tmr.mode    = TMR_MODE_PWM;
+		tmr.cmp_cnt = period_ticks;
+		tmr.pol     = 0;
+		TMR_Config(PWM_TIMER, &tmr);
 
-	tmr_pwm.pol      = 1;
-	tmr_pwm.per_cnt  = period_ticks;
-	tmr_pwm.duty_cnt = duty_ticks;
+		tmr_pwm.pol      = 1;
+		tmr_pwm.per_cnt  = period_ticks;
+		tmr_pwm.duty_cnt = duty_ticks;
 
-	TMR_PWMConfig(PWM_TIMER, &tmr_pwm);
-	TMR_Enable(PWM_TIMER);
+		TMR_PWMConfig(PWM_TIMER, &tmr_pwm);
+		TMR_Enable(PWM_TIMER);
+	} else {
+		gpio_pwm.func = GPIO_FUNC_OUT;
+		gpio_pwm.port = PORT_PWM;
+		gpio_pwm.mask = PIN_PWM;
+		gpio_pwm.pad  = GPIO_PAD_PULL_DOWN;
+		GPIO_Config(&gpio_pwm);
+		GPIO_OutClr(&gpio_pwm);
+	}
 }
diff --git a/lib/gfx/LCD/LCD_Driver.c b/lib/gfx/LCD/LCD_Driver.c
index 481d6929..55c872a8 100644
--- a/lib/gfx/LCD/LCD_Driver.c
+++ b/lib/gfx/LCD/LCD_Driver.c
@@ -50,7 +50,7 @@ static void LCD_Reset(void)
 function:
 	Setting backlight
 parameter	:
-		value : Range 0~1000   Duty cycle is value/1000	
+		value : Range 0~100   Duty cycle is value/100
 *******************************************************************************/
 void LCD_SetBacklight(UWORD Value)
 {
diff --git a/pycardium/modules/py/display.py b/pycardium/modules/py/display.py
index d6b53864..e957977d 100644
--- a/pycardium/modules/py/display.py
+++ b/pycardium/modules/py/display.py
@@ -95,7 +95,7 @@ class Display:
         """
         Set display backlight brightness
 
-        :param brightness: backlight brightness 0 <= brightness <= 1000
+        :param brightness: backlight brightness 0 <= brightness <= 100
         """
 
         sys_display.backlight(brightness)
-- 
GitLab