Skip to content

Commit b988152

Browse files
committed
fw/settings: move "ALS Threshold" setting to Debugging
Signed-off-by: Joshua Jun <joshuajun@proton.me>
1 parent 96d7302 commit b988152

2 files changed

Lines changed: 109 additions & 116 deletions

File tree

src/fw/apps/system_apps/settings/settings_display.c

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343

4444
typedef struct SettingsDisplayData {
4545
SettingsCallbacks callbacks;
46-
char als_threshold_buffer[16]; // Buffer for formatted ALS threshold
47-
char als_status_buffer[64]; // Buffer for NumberWindow label with status
48-
bool als_adjustment_active; // Track if ALS adjustment is active
4946
} SettingsDisplayData;
5047

5148
// Intensity Settings
@@ -135,88 +132,6 @@ static void prv_timeout_menu_push(SettingsDisplayData *data) {
135132
true /* icons_enabled */, s_timeout_labels, data);
136133
}
137134

138-
// ALS Threshold Settings
139-
/////////////////////////////
140-
141-
static void prv_update_als_threshold_label(NumberWindow *number_window, SettingsDisplayData *data) {
142-
uint32_t current_reading = ambient_light_get_light_level();
143-
uint32_t current_threshold = (uint32_t)number_window_get_value(number_window);
144-
bool would_backlight_be_on = current_reading <= current_threshold;
145-
146-
snprintf(data->als_status_buffer, sizeof(data->als_status_buffer),
147-
"Backlight: %s",
148-
would_backlight_be_on ? "ON" : "OFF");
149-
150-
number_window_set_label(number_window, data->als_status_buffer);
151-
}
152-
153-
static void prv_als_threshold_incremented(NumberWindow *number_window, void *context) {
154-
SettingsDisplayData *data = (SettingsDisplayData*)context;
155-
prv_update_als_threshold_label(number_window, data);
156-
}
157-
158-
static void prv_als_threshold_decremented(NumberWindow *number_window, void *context) {
159-
SettingsDisplayData *data = (SettingsDisplayData*)context;
160-
prv_update_als_threshold_label(number_window, data);
161-
}
162-
163-
static void prv_als_threshold_selected(NumberWindow *number_window, void *context) {
164-
SettingsDisplayData *data = (SettingsDisplayData*)context;
165-
uint32_t new_threshold = (uint32_t)number_window_get_value(number_window);
166-
backlight_set_ambient_threshold(new_threshold);
167-
data->als_adjustment_active = false;
168-
light_allow(true);
169-
app_window_stack_remove(&number_window->window, true /* animated */);
170-
}
171-
172-
static void prv_als_threshold_menu_push(SettingsDisplayData *data) {
173-
// Disable backlight while adjusting ALS threshold to prevent skewing readings
174-
light_allow(false);
175-
data->als_adjustment_active = true;
176-
177-
// Give time for backlight to turn off
178-
// If we don't do this, the text may say the false result
179-
// until the user changes the value
180-
psleep(200);
181-
182-
// Get current ambient light reading to show backlight status
183-
uint32_t current_reading = ambient_light_get_light_level();
184-
uint32_t current_threshold = backlight_get_ambient_threshold();
185-
bool would_backlight_be_on = current_reading <= current_threshold;
186-
187-
// Create descriptive label with current status
188-
snprintf(data->als_status_buffer, sizeof(data->als_status_buffer),
189-
"Backlight: %s",
190-
would_backlight_be_on ? "ON" : "OFF");
191-
192-
NumberWindow *number_window = number_window_create(
193-
data->als_status_buffer,
194-
(NumberWindowCallbacks) {
195-
.selected = prv_als_threshold_selected,
196-
.incremented = prv_als_threshold_incremented,
197-
.decremented = prv_als_threshold_decremented,
198-
},
199-
data
200-
);
201-
202-
if (!number_window) {
203-
// Re-enable backlight if NumberWindow creation failed
204-
data->als_adjustment_active = false;
205-
light_allow(true);
206-
return;
207-
}
208-
209-
210-
// Set reasonable min/max values for ALS threshold
211-
number_window_set_min(number_window, 0);
212-
number_window_set_max(number_window, AMBIENT_LIGHT_LEVEL_MAX);
213-
number_window_set_step_size(number_window, 1);
214-
number_window_set_value(number_window, (int32_t)current_threshold);
215-
216-
const bool animated = true;
217-
app_window_stack_push(&number_window->window, animated);
218-
}
219-
220135
// Legacy App Mode Settings (Obelix only)
221136
/////////////////////////////
222137
#if PLATFORM_OBELIX
@@ -251,7 +166,6 @@ enum SettingsDisplayItem {
251166
SettingsDisplayBacklightMode,
252167
SettingsDisplayMotionSensor,
253168
SettingsDisplayAmbientSensor,
254-
SettingsDisplayAmbientThreshold,
255169
SettingsDisplayBacklightIntensity,
256170
SettingsDisplayBacklightTimeout,
257171
#if PLATFORM_SPALDING
@@ -271,19 +185,11 @@ static bool prv_should_show_backlight_sub_items() {
271185
return backlight_is_enabled();
272186
}
273187

274-
static bool prv_should_show_als_threshold() {
275-
return backlight_is_enabled() && backlight_is_ambient_sensor_enabled();
276-
}
277-
278188
uint16_t prv_get_item_from_row(uint16_t row) {
279189
if (!prv_should_show_backlight_sub_items() && (row > SettingsDisplayBacklightMode)) {
280190
row += NUM_BACKLIGHT_SUB_ITEMS;
281191
}
282192

283-
if (!prv_should_show_als_threshold() && (row >= SettingsDisplayAmbientThreshold)) {
284-
row++;
285-
}
286-
287193
return row;
288194
}
289195

@@ -302,9 +208,6 @@ static void prv_select_click_cb(SettingsCallbacks *context, uint16_t row) {
302208
case SettingsDisplayAmbientSensor:
303209
light_toggle_ambient_sensor_enabled();
304210
break;
305-
case SettingsDisplayAmbientThreshold:
306-
prv_als_threshold_menu_push(data);
307-
break;
308211
case SettingsDisplayBacklightIntensity:
309212
prv_intensity_menu_push(data);
310213
break;
@@ -332,12 +235,6 @@ static void prv_draw_row_cb(SettingsCallbacks *context, GContext *ctx,
332235
const Layer *cell_layer, uint16_t row, bool selected) {
333236
SettingsDisplayData *data = (SettingsDisplayData*) context;
334237

335-
// Check if user canceled out of ALS adjustment (this gets called when we return to settings menu)
336-
if (data->als_adjustment_active) {
337-
data->als_adjustment_active = false;
338-
light_allow(true);
339-
}
340-
341238
const char *title = NULL;
342239
const char *subtitle = NULL;
343240
switch (prv_get_item_from_row(row)) {
@@ -369,15 +266,6 @@ static void prv_draw_row_cb(SettingsCallbacks *context, GContext *ctx,
369266
subtitle = i18n_noop("Off");
370267
}
371268
break;
372-
case SettingsDisplayAmbientThreshold: {
373-
title = i18n_noop("ALS Threshold");
374-
// Show current threshold value
375-
uint32_t current_threshold = backlight_get_ambient_threshold();
376-
snprintf(data->als_threshold_buffer, sizeof(data->als_threshold_buffer),
377-
"%"PRIu32, current_threshold);
378-
subtitle = data->als_threshold_buffer;
379-
break;
380-
}
381269
case SettingsDisplayBacklightIntensity:
382270
title = i18n_noop("Intensity");
383271
subtitle = s_intensity_labels[prv_intensity_get_selection_index()];
@@ -411,10 +299,6 @@ static uint16_t prv_num_rows_cb(SettingsCallbacks *context) {
411299
rows -= NUM_BACKLIGHT_SUB_ITEMS;
412300
}
413301

414-
if (!prv_should_show_als_threshold()) {
415-
rows--;
416-
}
417-
418302
return rows;
419303
}
420304

src/fw/apps/system_apps/settings/settings_system.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@
2727
#include "applib/ui/dialogs/expandable_dialog.h"
2828
#include "applib/ui/option_menu_window.h"
2929
#include "applib/ui/ui.h"
30+
#include "drivers/ambient_light.h"
3031
#include "kernel/core_dump.h"
3132
#include "kernel/event_loop.h"
3233
#include "kernel/pbl_malloc.h"
3334
#include "kernel/ui/modals/modal_manager.h"
35+
#include "kernel/util/sleep.h"
3436
#include "mfg/mfg_info.h"
3537
#include "mfg/mfg_serials.h"
3638
#include "resource/resource_ids.auto.h"
3739
#include "services/common/bluetooth/local_id.h"
3840
#include "services/common/i18n/i18n.h"
41+
#include "services/common/light.h"
3942
#include "services/common/status_led.h"
4043
#include "services/common/system_task.h"
4144
#include "services/normal/stationary.h"
@@ -71,6 +74,7 @@ enum {
7174
enum {
7275
DebuggingItemCoreDumpNow = 0,
7376
DebuggingItemCoreDumpShortcut,
77+
DebuggingItemALSThreshold,
7478
DebuggingItem_Count,
7579
};
7680

@@ -135,6 +139,11 @@ typedef struct SettingsSystemData {
135139
Window window;
136140
MenuLayer menu_layer;
137141
StatusBarLayer status_layer;
142+
143+
// ALS threshold data
144+
char als_threshold_buffer[16]; // Buffer for formatted ALS threshold
145+
char als_status_buffer[64]; // Buffer for NumberWindow label with status
146+
bool als_adjustment_active; // Track if ALS adjustment is active
138147
} SettingsSystemData;
139148

140149
typedef enum {
@@ -373,12 +382,95 @@ static void prv_maybe_trigger_core_dump() {
373382
app_confirmation_dialog_push(confirmation_dialog);
374383
}
375384

385+
// ALS Threshold Settings
386+
/////////////////////////////
387+
388+
static void prv_update_als_threshold_label(NumberWindow *number_window, SettingsSystemData *data) {
389+
uint32_t current_reading = ambient_light_get_light_level();
390+
uint32_t current_threshold = (uint32_t)number_window_get_value(number_window);
391+
bool would_backlight_be_on = current_reading <= current_threshold;
392+
393+
snprintf(data->als_status_buffer, sizeof(data->als_status_buffer),
394+
"Backlight: %s",
395+
would_backlight_be_on ? "ON" : "OFF");
396+
397+
number_window_set_label(number_window, data->als_status_buffer);
398+
}
399+
400+
static void prv_als_threshold_incremented(NumberWindow *number_window, void *context) {
401+
SettingsSystemData *data = (SettingsSystemData*)context;
402+
prv_update_als_threshold_label(number_window, data);
403+
}
404+
405+
static void prv_als_threshold_decremented(NumberWindow *number_window, void *context) {
406+
SettingsSystemData *data = (SettingsSystemData*)context;
407+
prv_update_als_threshold_label(number_window, data);
408+
}
409+
410+
static void prv_als_threshold_selected(NumberWindow *number_window, void *context) {
411+
SettingsSystemData *data = (SettingsSystemData*)context;
412+
uint32_t new_threshold = (uint32_t)number_window_get_value(number_window);
413+
backlight_set_ambient_threshold(new_threshold);
414+
data->als_adjustment_active = false;
415+
light_allow(true);
416+
app_window_stack_remove(&number_window->window, true /* animated */);
417+
}
418+
419+
static void prv_als_threshold_menu_push(SettingsSystemData *data) {
420+
// Disable backlight while adjusting ALS threshold to prevent skewing readings
421+
light_allow(false);
422+
data->als_adjustment_active = true;
423+
424+
// Give time for backlight to turn off
425+
// If we don't do this, the text may say the false result
426+
// until the user changes the value
427+
psleep(200);
428+
429+
// Get current ambient light reading to show backlight status
430+
uint32_t current_reading = ambient_light_get_light_level();
431+
uint32_t current_threshold = backlight_get_ambient_threshold();
432+
bool would_backlight_be_on = current_reading <= current_threshold;
433+
434+
// Create descriptive label with current status
435+
snprintf(data->als_status_buffer, sizeof(data->als_status_buffer),
436+
"Backlight: %s",
437+
would_backlight_be_on ? "ON" : "OFF");
438+
439+
NumberWindow *number_window = number_window_create(
440+
data->als_status_buffer,
441+
(NumberWindowCallbacks) {
442+
.selected = prv_als_threshold_selected,
443+
.incremented = prv_als_threshold_incremented,
444+
.decremented = prv_als_threshold_decremented,
445+
},
446+
data
447+
);
448+
449+
if (!number_window) {
450+
// Re-enable backlight if NumberWindow creation failed
451+
data->als_adjustment_active = false;
452+
light_allow(true);
453+
return;
454+
}
455+
456+
457+
// Set reasonable min/max values for ALS threshold
458+
number_window_set_min(number_window, 0);
459+
number_window_set_max(number_window, AMBIENT_LIGHT_LEVEL_MAX);
460+
number_window_set_step_size(number_window, 1);
461+
number_window_set_value(number_window, (int32_t)current_threshold);
462+
463+
const bool animated = true;
464+
app_window_stack_push(&number_window->window, animated);
465+
}
466+
376467
// Debug options window
377468
///////////////////////
378469

379470
static const char* s_debugging_titles[DebuggingItem_Count] = {
380471
[DebuggingItemCoreDumpNow] = i18n_noop("Bug report now"),
381472
[DebuggingItemCoreDumpShortcut] = i18n_noop("Bug shortcut"),
473+
[DebuggingItemALSThreshold] = i18n_noop("ALS Threshold"),
382474
};
383475

384476
static void prv_debugging_draw_row_callback(GContext* ctx, const Layer *cell_layer,
@@ -388,10 +480,22 @@ static void prv_debugging_draw_row_callback(GContext* ctx, const Layer *cell_lay
388480

389481
SettingsSystemData *data = (SettingsSystemData *) context;
390482

483+
// Check if user canceled out of ALS adjustment (this gets called when we return to settings menu)
484+
if (data->als_adjustment_active) {
485+
data->als_adjustment_active = false;
486+
light_allow(true);
487+
}
488+
391489
const char *title = i18n_get(s_debugging_titles[cell_index->row], data);
392490
const char *subtitle_text = NULL;
393491
if (cell_index->row == DebuggingItemCoreDumpShortcut) {
394492
subtitle_text = shell_prefs_can_coredump_on_request() ? i18n_get("10 back-button presses", data) : i18n_get("Disabled", data);
493+
} else if (cell_index->row == DebuggingItemALSThreshold) {
494+
// Show current threshold value
495+
uint32_t current_threshold = backlight_get_ambient_threshold();
496+
snprintf(data->als_threshold_buffer, sizeof(data->als_threshold_buffer),
497+
"%"PRIu32, current_threshold);
498+
subtitle_text = data->als_threshold_buffer;
395499
}
396500
menu_cell_basic_draw(ctx, cell_layer, title, subtitle_text, NULL);
397501
}
@@ -412,13 +516,18 @@ static uint16_t prv_debugging_get_num_rows_callback(MenuLayer *menu_layer,
412516
static void prv_debugging_select_callback(MenuLayer *menu_layer,
413517
MenuIndex *cell_index,
414518
void *context) {
519+
SettingsSystemData *data = (SettingsSystemData *) context;
520+
415521
switch (cell_index->row) {
416522
case DebuggingItemCoreDumpNow:
417523
prv_maybe_trigger_core_dump();
418524
break;
419525
case DebuggingItemCoreDumpShortcut:
420526
shell_prefs_set_coredump_on_request(!shell_prefs_can_coredump_on_request());
421527
break;
528+
case DebuggingItemALSThreshold:
529+
prv_als_threshold_menu_push(data);
530+
break;
422531
default:
423532
WTF;
424533
}

0 commit comments

Comments
 (0)