From ab5326bea26eaa6ad6de6ae73fc480209cc6d84f Mon Sep 17 00:00:00 2001 From: DiSlord Date: Tue, 11 May 2021 11:00:17 +0300 Subject: [PATCH] Add brightness support --- ili9341.c | 30 +++++++++++++++++++++++++++++- main.c | 8 ++++++++ nanovna.h | 8 +++++++- ui_sa.c | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/ili9341.c b/ili9341.c index 327dd47..3602bcc 100644 --- a/ili9341.c +++ b/ili9341.c @@ -501,9 +501,37 @@ static const uint8_t ili9341_init_seq[] = { #define LCD_INIT ili9341_init_seq #endif +#ifdef __LCD_BRIGHTNESS__ +#if HAL_USE_DAC == FALSE +#error "Need set HAL_USE_DAC in halconf.h for use __LCD_BRIGHTNESS__" +#endif + +static const DACConfig dac1cfg1 = { + init: 0, + datamode: DAC_DHRM_12BIT_RIGHT +}; + +static void lcd_initBrightness(void){ + dacStart(&DACD2, &dac1cfg1); +} + +#define BRIGHTNESS_MIN_LEVEL 0 +#define BRIGHTNESS_MAX_LEVEL 3300 +// Brightness control range 0 - 100 +void lcd_setBrightness(uint16_t b){ + b = BRIGHTNESS_MIN_LEVEL + b*((BRIGHTNESS_MAX_LEVEL-BRIGHTNESS_MIN_LEVEL)/100); + dacPutChannelX(&DACD2, 0, b); +} +#else +#define lcd_initBrightness() +#endif + void ili9341_init(void) { spi_init(); + // Init Brightness if LCD support + lcd_initBrightness(); + LCD_DC_DATA; LCD_RESET_ASSERT; chThdSleepMilliseconds(10); @@ -514,7 +542,7 @@ void ili9341_init(void) p += 2 + p[1]; chThdSleepMilliseconds(5); } -// ili9341_clear_screen(); + ili9341_clear_screen(); LCD_CS_HIGH; } diff --git a/main.c b/main.c index 905b979..ecd22da 100644 --- a/main.c +++ b/main.c @@ -885,6 +885,7 @@ config_t config = { .ext_zero_level = 128, #endif #ifdef TINYSA4 + ._brightness = DEFAULT_BRIGHTNESS, .vbat_offset = 220, .frequency_IF1 = DEFAULT_IF, .frequency_IF2 = 0, @@ -2265,6 +2266,13 @@ int main(void) // ui_mode_menu(); // Show menu when autostarting mode ui_mode_normal(); + /* + * Set LCD display brightness + */ + #ifdef __LCD_BRIGHTNESS__ + lcd_setBrightness(config._brightness); + #endif + chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO-1, Thread1, NULL); diff --git a/nanovna.h b/nanovna.h index a078e98..dc6fed0 100644 --- a/nanovna.h +++ b/nanovna.h @@ -80,6 +80,7 @@ #ifdef TINYSA4 #define __USE_RTC__ // Enable RTC clock #define __USE_SD_CARD__ // Enable SD card support +#define __LCD_BRIGHTNESS__ // LCD or hardware allow change brightness, add menu item for this #define __HARMONIC__ #define __VBW__ #define __SWEEP_RESTART__ @@ -696,6 +697,7 @@ typedef struct config { int8_t cor_am; int8_t cor_wfm; int8_t cor_nfm; + uint8_t _brightness; uint8_t high_out_adf4350; float sweep_voltage; float switch_offset; @@ -858,6 +860,9 @@ typedef uint16_t pixel_t; #define LCD_HEIGHT 240 #endif +// Default LCD brightness if display support it +#define DEFAULT_BRIGHTNESS 70 + #define LCD_BG_COLOR 0 #define LCD_FG_COLOR 1 #define LCD_GRID_COLOR 2 @@ -967,6 +972,7 @@ void ili9341_drawfont(uint8_t ch, int x, int y); void ili9341_read_memory(int x, int y, int w, int h, uint16_t* out); void ili9341_line(int x0, int y0, int x1, int y1); void show_version(void); +void lcd_setBrightness(uint16_t b); /* * flash.c @@ -1186,7 +1192,7 @@ typedef struct properties { //sizeof(properties_t) == 0x1200 -#define CONFIG_MAGIC 0x434f4f4f /* 'CONF' */ +#define CONFIG_MAGIC 0x434f4e50 /* 'CONF' */ extern int16_t lastsaveid; //extern properties_t *active_props; diff --git a/ui_sa.c b/ui_sa.c index 0a15f56..54f6bc2 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1702,6 +1702,39 @@ static UI_FUNCTION_ADV_CALLBACK(menu_settings_bpf_acb){ toggle_tracking(); } +#ifdef __LCD_BRIGHTNESS__ +static UI_FUNCTION_CALLBACK(menu_brightness_cb) +{ + (void)item; + (void)data; + int16_t value = config._brightness; + ili9341_set_foreground(LCD_MENU_TEXT_COLOR); + ili9341_set_background(LCD_MENU_COLOR); + ili9341_fill(LCD_WIDTH/2-80, LCD_HEIGHT/2-20, 160, 40); + ili9341_drawstring_7x13("BRIGHTNESS", LCD_WIDTH/2-35, LCD_HEIGHT/2-13); + ili9341_drawstring_7x13(S_LARROW" USE LEVELER BUTTON "S_RARROW, LCD_WIDTH/2-72, LCD_HEIGHT/2+2); + while (TRUE) { + int status = btn_check(); + if (status & (EVT_UP|EVT_DOWN)) { + do { + if (status & EVT_UP ) value+=5; + if (status & EVT_DOWN) value-=5; + if (value < 0) value = 0; + if (value > 100) value = 100; + lcd_setBrightness(value); + status = btn_wait_release(); + } while (status != 0); + } + if (status == EVT_BUTTON_SINGLE_CLICK) + break; + } + config._brightness = (uint8_t)value; + lcd_setBrightness(value); + redraw_request|= REDRAW_AREA; + ui_mode_normal(); +} +#endif + static UI_FUNCTION_ADV_CALLBACK(menu_settings_pulse_acb){ (void)item; (void)data; @@ -2561,7 +2594,9 @@ static const menuitem_t menu_config[] = { #ifndef TINYSA4 { MT_SUBMENU, 0, S_RARROW" DFU", menu_dfu}, #endif - +#ifdef __LCD_BRIGHTNESS__ + { MT_CALLBACK, 0, "BRIGHTHESS", menu_brightness_cb}, +#endif { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel };