From bd74ad00d8832ac36a2d398e8dbbc7b7b817fde9 Mon Sep 17 00:00:00 2001 From: Wolfvak Date: Fri, 7 Jun 2019 17:13:42 -0300 Subject: [PATCH] add initial brightness config dialog, currently inaccessible --- arm11/source/main.c | 3 +- arm9/source/common/power.c | 12 +++++ arm9/source/common/power.h | 5 ++ arm9/source/common/ui.c | 105 +++++++++++++++++++++++++++++++++++++ arm9/source/common/ui.h | 2 + common/fixp.h | 5 ++ 6 files changed, 131 insertions(+), 1 deletion(-) diff --git a/arm11/source/main.c b/arm11/source/main.c index e28e4fd..c0d22f4 100644 --- a/arm11/source/main.c +++ b/arm11/source/main.c @@ -148,10 +148,11 @@ void PXI_RX_Handler(u32 __attribute__((unused)) irqn) case PXI_BRIGHTNESS: { ret = LCD_GetBrightness(); - if (args[0] && (args[0] < 0x100)) { + if ((args[0] > 0) && (args[0] < 0x100)) { LCD_SetBrightness(args[0]); auto_brightness = false; } else { + prev_bright_lvl = -1; auto_brightness = true; } break; diff --git a/arm9/source/common/power.c b/arm9/source/common/power.c index 2260486..62ffbfa 100644 --- a/arm9/source/common/power.c +++ b/arm9/source/common/power.c @@ -3,6 +3,18 @@ #include "i2c.h" #include "pxi.h" +u32 SetScreenBrightness(int level) { + u32 arg; + + if (level != BRIGHTNESS_AUTOMATIC) { + arg = clamp(level, BRIGHTNESS_MIN, BRIGHTNESS_MAX); + } else { + arg = 0; + } + + return PXI_DoCMD(PXI_BRIGHTNESS, &arg, 1); +} + u32 GetBatteryPercent() { u8 battery = 0; I2C_readRegBuf(I2C_DEV_MCU, 0x0B, &battery, 1); diff --git a/arm9/source/common/power.h b/arm9/source/common/power.h index 082d837..bcc565f 100644 --- a/arm9/source/common/power.h +++ b/arm9/source/common/power.h @@ -2,6 +2,11 @@ #include "common.h" +#define BRIGHTNESS_AUTOMATIC (-1) +#define BRIGHTNESS_MIN (10) +#define BRIGHTNESS_MAX (210) + +u32 SetScreenBrightness(int level); u32 GetBatteryPercent(); bool IsCharging(); void Reboot(); diff --git a/arm9/source/common/ui.c b/arm9/source/common/ui.c index 9f4d308..0129b1f 100644 --- a/arm9/source/common/ui.c +++ b/arm9/source/common/ui.c @@ -14,6 +14,7 @@ #include "timer.h" #include "power.h" #include "hid.h" +#include "fixp.h" #define STRBUF_SIZE 512 // maximum size of the string buffer #define FONT_MAX_WIDTH 8 @@ -1060,3 +1061,107 @@ bool ShowProgress(u64 current, u64 total, const char* opstr) return !CheckButton(BUTTON_B); } + +int ShowBrightnessConfig(int set_brightness) +{ + u32 btn_input, bar_count; + int bar_x_pos, bar_y_pos, bar_width, bar_height; + + const char *brightness_str = + "[<] Decrease brightness\n" + "[>] Increase brightness\n" + "[X] Use the volume slider as control\n" + "\n" + "[A] Set screen brightness\n" + "[B] Exit"; + static const u16 brightness_slider_colmasks[] = { + COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE + }; + + ClearScreen(MAIN_SCREEN, COLOR_STD_BG); + + bar_count = countof(brightness_slider_colmasks); + bar_width = (SCREEN_WIDTH_MAIN / 8) * 6; + bar_x_pos = SCREEN_WIDTH_MAIN / 8; + + bar_height = 10; + bar_y_pos = SCREEN_HEIGHT / 4; + + // default to average brightness if invalid / automatic + if (set_brightness < BRIGHTNESS_MIN || set_brightness > BRIGHTNESS_MAX) + set_brightness = (BRIGHTNESS_MAX + BRIGHTNESS_MIN) / 2; + + // draw initial UI stuff + DrawStringF(MAIN_SCREEN, + (SCREEN_WIDTH_MAIN - GetDrawStringWidth(brightness_str)) / 2, + (SCREEN_HEIGHT / 4) * 3, COLOR_STD_FONT, COLOR_STD_BG, brightness_str); + + // draw all color gradient bars + for (int x = 0; x < bar_width; x++) { + u32 intensity; + u16 intensity_mask; + + intensity = FIXP_TO_INT(fixp_changespace( + INT_TO_FIXP(x), + INT_TO_FIXP(0), INT_TO_FIXP(bar_width), + INT_TO_FIXP(0), INT_TO_FIXP(256) + )); + + intensity_mask = RGB(intensity, intensity, intensity); + + for (u32 b = 0; b < bar_count; b++) { + u16 *screen_base = &MAIN_SCREEN[PIXEL_OFFSET(bar_x_pos + x, (b * bar_height) + bar_y_pos)]; + for (int y = 0; y < bar_height; y++) + *(screen_base++) = brightness_slider_colmasks[b] & intensity_mask; + } + } + + while(1) { + int old_br, slider_x_pos, slider_y_pos; + + old_br = set_brightness; + slider_y_pos = bar_y_pos + (bar_height * 3) + font_height; + + if (set_brightness != BRIGHTNESS_AUTOMATIC) { + slider_x_pos = bar_x_pos + font_width + FIXP_TO_INT(fixp_changespace( + INT_TO_FIXP(set_brightness), + INT_TO_FIXP(BRIGHTNESS_MIN), INT_TO_FIXP(BRIGHTNESS_MAX), + INT_TO_FIXP(0), INT_TO_FIXP(bar_width) + )); + + // redraw the slider position character (if necessary) + DrawCharacter(MAIN_SCREEN, '^', slider_x_pos, + slider_y_pos, COLOR_STD_FONT, COLOR_STD_BG); + } + + btn_input = InputWait(0); + + // draw a small rectangle to clear the character + if (set_brightness != BRIGHTNESS_AUTOMATIC) { + DrawRectangle(MAIN_SCREEN, slider_x_pos, + slider_y_pos, font_width, font_height, COLOR_STD_BG); + } + + if (btn_input & BUTTON_LEFT) { + set_brightness -= 10; + } else if (btn_input & BUTTON_RIGHT) { + set_brightness += 10; + } else if (btn_input & BUTTON_X) { + set_brightness = BRIGHTNESS_AUTOMATIC; + } else if (btn_input & BUTTON_B) { + set_brightness = 0; + break; + } else if (btn_input & BUTTON_A) { + break; + } + + if (set_brightness != BRIGHTNESS_AUTOMATIC) + set_brightness = clamp(set_brightness, BRIGHTNESS_MIN, BRIGHTNESS_MAX); + + if (set_brightness != old_br) + SetScreenBrightness(set_brightness); + } + + ClearScreen(MAIN_SCREEN, COLOR_STD_BG); + return set_brightness; +} diff --git a/arm9/source/common/ui.h b/arm9/source/common/ui.h index 15f5b83..d7fdfae 100644 --- a/arm9/source/common/ui.h +++ b/arm9/source/common/ui.h @@ -85,3 +85,5 @@ u64 ShowNumberPrompt(u64 start_val, const char *format, ...); bool ShowDataPrompt(u8* data, u32* size, const char *format, ...); bool ShowRtcSetterPrompt(void* time, const char *format, ...); bool ShowProgress(u64 current, u64 total, const char* opstr); + +int ShowBrightnessConfig(int set_brightness); diff --git a/common/fixp.h b/common/fixp.h index c11797c..c9cebe3 100755 --- a/common/fixp.h +++ b/common/fixp.h @@ -53,3 +53,8 @@ static inline fixp_t fixp_round(fixp_t n) { return (n + FIXP_HALF_UNIT) & FIXP_UNIT_MASK; } + +static inline fixp_t fixp_changespace(fixp_t n, fixp_t lower_s, fixp_t upper_s, fixp_t lower_d, fixp_t upper_d) +{ + return fixp_product(n - lower_s, fixp_quotient(upper_d, upper_s)) + lower_d; +}