Begin v2 conversion

This commit is contained in:
RandyTheSilly committed 2026-10-21 18:31:17 -04:00
1 parent 98f13a941d
commit ce67fc3081
9 files changed
+95 -275

No files matched your search

+47 -157
View File
@@ -61,12 +61,8 @@ static bool s_skip_current_day = false;
static WakeupReason s_alarm_reason;
static uint8_t s_solution_indices[3];
static uint8_t s_selected_indices[3] = {9, 9, 9};
static uint8_t s_main_countdown_till_zap;
static uint8_t s_awareness_countdown_int = 0;
static char s_awareness_countdown_buf[4];
static uint8_t s_alarm_zap_repeat_countdown = 0;
static uint8_t s_awareness_zap_intensity = 0;
static bool s_hide_countdown = true;
static uint32_t s_vibe_segments[5];
static VibePattern s_vibe_pattern;
@@ -94,16 +90,9 @@ static char *s_options_under[2];
////////////////////// STATUS //////////////////////
static void click_handler_disable_tutorial(void *recognizer, void *ctx) {
settings.TutorialEnabled = false;
persist_write_data(STORAGE_KEY_SETTINGS, &settings, sizeof(settings));
window_stack_pop_all(true);
}
static void click_handler_push_setter(void *recognizer, void *ctx) {
s_highlight_pos = 0;
s_skip_current_day = true; // setting the next alarm should assume the user meant tomorrow
s_alarm_reason = WAKE_MAIN_ALARM; // going back to setter means alarm was disarmed or failed to set; do not punish user
s_skip_current_day = true; // setting the next alarm should assume the user meant tomorrow
window_stack_pop_all(true);
push_setter_window();
}
@@ -116,11 +105,6 @@ static void click_handler_retry(void *recognizer, void *ctx) {
window_stack_pop(true);
}
static void status_main_tutorial_click_config_provider(void *ctx) {
window_single_click_subscribe(BUTTON_ID_SELECT, click_handler_exit);
window_single_click_subscribe(BUTTON_ID_BACK, click_handler_disable_tutorial);
}
static void status_confirmation_click_config_provider(void *ctx) {
window_single_click_subscribe(BUTTON_ID_BACK, click_handler_push_setter);
window_single_click_subscribe(BUTTON_ID_SELECT, click_handler_exit);
@@ -143,11 +127,6 @@ static void status_window_load(StatusWindowPurpose purpose) {
text_layer_set_font(s_status_txt_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24));
switch (purpose) {
case STATUS_MAIN_TUTORIAL:
// don't disable ticks to ensure vibrations remain till the tutorial is closed
text_layer_set_text(s_status_txt_layer, "Correct! An awareness alarm is now being set. Solve it within the time limit to disarm Trigalarm.\n\nPress SELECT to exit or BACK to disable this tutorial in the future (and exit).");
window_set_click_config_provider(s_status_window, status_main_tutorial_click_config_provider);
break;
case STATUS_CONFIRMATION: {
static char s_status_confirmed_buffer[106];
snprintf(s_status_confirmed_buffer, sizeof(s_status_confirmed_buffer), "Alarm disarmed and re-set (%02u:%02u) successfully.\n\nPress SELECT to exit or BACK to change the alarm time.", the_time.HourSet, the_time.MinuteSet);
@@ -209,40 +188,26 @@ static bool schedule_alarm(WakeupReason schedule_reason) {
WakeupId status;
switch (schedule_reason) {
case WAKE_MAIN_ALARM: {
case WAKE_ALARM: {
target = compute_alarm_time(the_time.HourWIP, the_time.MinuteWIP);
wakeup_cancel_all();
status = wakeup_schedule_simple_robust(target, SECONDS_PER_MINUTE, schedule_reason);
if (settings.PavlokEnabled) {
persist_write_int(STORAGE_KEY_PAVLOK_INTENSITY, settings.PavlokMin);
}
break;
}
case WAKE_AWARENESS_ALARM:
target = time(NULL) + settings.AwarenessTimeGapMinutes * SECONDS_PER_MINUTE;
wakeup_cancel_all();
status = wakeup_schedule_simple_robust(target, SECONDS_PER_MINUTE, schedule_reason);
if (settings.PavlokEnabled && (s_alarm_reason == WAKE_AWARENESS_ALARM || s_alarm_reason == WAKE_RECOVERY_AWARENESS_ALARM)) {
uint8_t next_intensity = s_awareness_zap_intensity + settings.PavlokStep;
if (next_intensity <= settings.PavlokMax && next_intensity <= 100) {
// only boost next intensity if it is within the allowed bounds
persist_write_int(STORAGE_KEY_PAVLOK_INTENSITY, s_awareness_zap_intensity + settings.PavlokStep);
}
}
break;
case WAKE_RECOVERY_MAIN_ALARM:
case WAKE_RECOVERY_AWARENESS_ALARM:
case WAKE_RECOVERY_ALARM:
target = time(NULL) + 15;
// no need to cancel alarms, as recovery alarms short-lived and is expired by the time another is being scheduled
// no need to cancel alarms, as recovery alarms are short-lived and are expired by the time another is being scheduled
status = wakeup_schedule_simple_robust(target, 15, schedule_reason);
}
if (status < 0) {
push_status_window(STATUS_SET_FAILURE);
persist_write_int(STORAGE_KEY_ALARM_REASON, WAKE_MAIN_ALARM);
persist_write_int(STORAGE_KEY_ALARM_REASON, WAKE_ALARM);
s_alarm_reason = WAKE_ALARM;
return false;
}
if (persist_read_int(STORAGE_KEY_ALARM_REASON) != schedule_reason) {
persist_write_int(STORAGE_KEY_ALARM_REASON, schedule_reason);
s_alarm_reason = schedule_reason;
}
return true;
}
@@ -289,25 +254,15 @@ static void inbox_received_handler(DictionaryIterator *iter, void *context) {
if (dict_find(iter, MESSAGE_KEY_PKJS_ALARM_RESET)) {
the_time.HourWIP = 0;
the_time.MinuteWIP = 0;
s_alarm_reason = WAKE_MAIN_ALARM;
schedule_alarm(WAKE_MAIN_ALARM);
s_alarm_reason = WAKE_ALARM;
schedule_alarm(WAKE_ALARM);
return;
}
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_PAVLOK_ENABLE, &settings.PavlokEnabled);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_PAVLOK_ZAP_INTERVAL, &settings.PavlokIntervalSeconds);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_PAVLOK_ZAP_MIN, &settings.PavlokMin);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_PAVLOK_ZAP_MAX, &settings.PavlokMax);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_PAVLOK_ZAP_STEP, &settings.PavlokStep);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_PAVLOK_ZAP_MAIN_DELAY, &settings.PavlokMainDelay);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_AWARENESS_TIME_GAP, &settings.AwarenessTimeGapMinutes);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_AWARENESS_TIME_LIMIT, &settings.AwarenessTimeLimitSeconds);
if (settings.PavlokMax <= settings.PavlokMin) {
// disable step mode if max is not higher than min
settings.PavlokStep = 0;
// ensure force quit/overwrite punishments aren't weaker than base
settings.PavlokMax = settings.PavlokMin;
}
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_PAVLOK_ZAP_INTENSITY, &settings.PavlokIntensity);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_TRIANGLE_COUNT, &settings.TriangleCount);
apply_uint8_from_tuple(iter, MESSAGE_KEY_PKJS_TRIANGLE_MIN_SECS, &settings.TriangeMinSecs);
persist_write_data(STORAGE_KEY_SETTINGS, &settings, sizeof(settings));
}
@@ -330,43 +285,13 @@ static void send_zap(uint8_t intensity) {
////////////////////// ALARM //////////////////////
static void main_alarm_seconds_handler(struct tm *tick_time, TimeUnits units_changed) {
if (s_main_countdown_till_zap > 0) {
s_main_countdown_till_zap--;
if (s_main_countdown_till_zap == 0) {
send_zap(settings.PavlokMin);
s_alarm_zap_repeat_countdown = settings.PavlokIntervalSeconds;
}
} else if (s_alarm_zap_repeat_countdown > 0) {
s_alarm_zap_repeat_countdown--;
if (s_alarm_zap_repeat_countdown == 0) {
send_zap(settings.PavlokMin);
s_alarm_zap_repeat_countdown = settings.PavlokIntervalSeconds;
}
}
vibes_enqueue_custom_pattern(s_vibe_pattern);
}
static void awareness_alarm_seconds_handler(struct tm *tick_time, TimeUnits units_changed) {
if (s_awareness_countdown_int > 0) {
s_awareness_countdown_int--;
snprintf(s_awareness_countdown_buf, sizeof(s_awareness_countdown_buf), "%03u", s_awareness_countdown_int);
text_layer_set_text(s_countdown_txt_layer, s_awareness_countdown_buf);
if (s_awareness_countdown_int == 0) {
send_zap(s_awareness_zap_intensity);
s_alarm_zap_repeat_countdown = settings.PavlokIntervalSeconds;
}
} else {
if (s_alarm_zap_repeat_countdown > 0) {
s_alarm_zap_repeat_countdown--;
if (s_alarm_zap_repeat_countdown == 0) {
send_zap(s_awareness_zap_intensity);
s_alarm_zap_repeat_countdown = settings.PavlokIntervalSeconds;
}
}
layer_set_hidden(text_layer_get_layer(s_countdown_txt_layer), s_hide_countdown);
s_hide_countdown = !s_hide_countdown;
vibes_enqueue_custom_pattern(s_vibe_pattern);
s_awareness_countdown_int--;
snprintf(s_awareness_countdown_buf, sizeof(s_awareness_countdown_buf), "%03u", s_awareness_countdown_int);
text_layer_set_text(s_countdown_txt_layer, s_awareness_countdown_buf);
if (s_awareness_countdown_int == 0) {
send_zap(settings.PavlokIntensity);
// TODO demote and reset countdown int
}
}
@@ -447,20 +372,15 @@ static void alarm_select_hold_handler(void *recognizer, void *ctx) {
wakeup_service_subscribe(NULL);
if (s_awareness_countdown_int > 0) {
// set main alarm if the awareness alarm was solved within time limit
// TODO promote OR disarm
// if (s_current_triangle == settings.TriangleCount) {
tick_timer_service_unsubscribe(); // alarm is fully disarmed - prevent stimulus in confirmation window
if (schedule_alarm(WAKE_MAIN_ALARM)) {
if (schedule_alarm(WAKE_ALARM)) {
push_status_window(STATUS_CONFIRMATION);
}
} else {
// otherwise, set awareness alarm and exit
if (schedule_alarm(WAKE_AWARENESS_ALARM)) {
if (settings.TutorialEnabled && (s_alarm_reason == WAKE_MAIN_ALARM || s_alarm_reason == WAKE_RECOVERY_MAIN_ALARM)) {
push_status_window(STATUS_MAIN_TUTORIAL);
} else {
window_stack_pop_all(true);
}
}
// } else {
// GENERATE NEW TRIANLGE AND SET TIME LIMIT
// }
}
}
@@ -529,7 +449,7 @@ static void drop_menu_window_unload(Window *window) {
}
static void live_wakeup_handler_for_alarm(WakeupId id, int32_t wakeup_reason) {
if (wakeup_reason == WAKE_RECOVERY_MAIN_ALARM || wakeup_reason == WAKE_RECOVERY_AWARENESS_ALARM) {
if (wakeup_reason == WAKE_RECOVERY_ALARM) {
// if recovery alarm, re-schedule it
schedule_alarm(wakeup_reason);
}
@@ -771,26 +691,13 @@ static void alarm_window_load() {
text_layer_set_background_color(s_countdown_txt_layer, GColorClear);
text_layer_set_text_color(s_countdown_txt_layer, GColorWhite);
text_layer_set_text_alignment(s_countdown_txt_layer, GTextAlignmentCenter);
s_alarm_zap_repeat_countdown = 0;
if (s_alarm_reason == WAKE_AWARENESS_ALARM || s_alarm_reason == WAKE_RECOVERY_AWARENESS_ALARM) {
s_awareness_countdown_int = settings.AwarenessTimeLimitSeconds;
text_layer_set_font(s_countdown_txt_layer, s_font_lecoish_small);
snprintf(s_awareness_countdown_buf, sizeof(s_awareness_countdown_buf), "%03u", s_awareness_countdown_int);
text_layer_set_text(s_countdown_txt_layer, s_awareness_countdown_buf);
layer_add_child(window_layer, text_layer_get_layer(s_countdown_txt_layer));
schedule_alarm(WAKE_RECOVERY_AWARENESS_ALARM);
tick_timer_service_subscribe(SECOND_UNIT, awareness_alarm_seconds_handler);
} else {
s_awareness_countdown_int = 0;
s_main_countdown_till_zap = settings.PavlokMainDelay;
text_layer_set_font(s_countdown_txt_layer, s_font_mansalva_small);
text_layer_set_text(s_countdown_txt_layer, "🏁 hold select 🏁");
layer_add_child(window_layer, text_layer_get_layer(s_countdown_txt_layer));
schedule_alarm(WAKE_RECOVERY_MAIN_ALARM);
tick_timer_service_subscribe(SECOND_UNIT, main_alarm_seconds_handler);
}
s_awareness_countdown_int = settings.TriangeMinSecs; // TODO calculate max based on min and triangle count
text_layer_set_font(s_countdown_txt_layer, s_font_lecoish_small);
snprintf(s_awareness_countdown_buf, sizeof(s_awareness_countdown_buf), "%03u", s_awareness_countdown_int);
text_layer_set_text(s_countdown_txt_layer, s_awareness_countdown_buf);
layer_add_child(window_layer, text_layer_get_layer(s_countdown_txt_layer));
schedule_alarm(WAKE_RECOVERY_ALARM);
tick_timer_service_subscribe(SECOND_UNIT, awareness_alarm_seconds_handler);
}
static void alarm_window_unload() {
@@ -820,21 +727,8 @@ static void alarm_window_unload() {
}
static void push_alarm_window() {
s_alarm_reason = persist_read_int(STORAGE_KEY_ALARM_REASON);
switch (s_alarm_reason) {
case WAKE_RECOVERY_MAIN_ALARM:
send_zap(settings.PavlokMax);
break;
case WAKE_RECOVERY_AWARENESS_ALARM:
send_zap(settings.PavlokMax);
[[fallthrough]];
case WAKE_AWARENESS_ALARM:
s_awareness_zap_intensity = persist_read_int(STORAGE_KEY_PAVLOK_INTENSITY);
vibes_long_pulse();
break;
default:
break;
if (s_alarm_reason == WAKE_RECOVERY_ALARM) {
send_zap(settings.PavlokIntensity);
}
window_set_window_handlers(
@@ -935,12 +829,7 @@ static void setter_down_click_handler(void *recognizer, void *ctx) {
static void setter_select_click_handler(void *recognizer, void *ctx) {
if (s_highlight_pos == 2) {
if (s_alarm_reason != WAKE_MAIN_ALARM) {
send_zap(settings.PavlokMax);
vibes_double_pulse();
return; // early return to avoid overwriting recovery/awareness alarm
}
bool success = schedule_alarm(WAKE_MAIN_ALARM);
bool success = schedule_alarm(WAKE_ALARM);
if (!success) {
return; // early return to avoid indicating alarm was set when it was not
}
@@ -1057,8 +946,6 @@ static void setter_window_unload() {
}
static void push_setter_window() {
s_alarm_reason = persist_read_int(STORAGE_KEY_ALARM_REASON);
window_set_window_handlers(
s_window,
(WindowHandlers){.load = setter_window_load, .unload = setter_window_unload});
@@ -1076,7 +963,7 @@ static void init() {
// initialize pkjs
app_message_register_inbox_received(inbox_received_handler);
app_message_open(128, 128); // TODO tighten sizes
app_message_open(128, 64);
// load settings from persist (or set defaults if not present)
if (persist_exists(STORAGE_KEY_SETTINGS)) {
@@ -1084,21 +971,24 @@ static void init() {
} else {
settings.TutorialEnabled = true;
settings.PavlokEnabled = false;
settings.PavlokIntervalSeconds = 10;
settings.PavlokMin = 30;
settings.PavlokMax = 60;
settings.PavlokStep = 10;
settings.PavlokMainDelay = 60;
settings.AwarenessTimeGapMinutes = 9;
settings.AwarenessTimeLimitSeconds = 30;
settings.PavlokIntensity = 50;
settings.TriangleCount = 5;
settings.TriangeMinSecs = 25;
}
// read even when opening setter to ensure a recovery alarm is not able to be overwritten
s_alarm_reason = persist_read_int(STORAGE_KEY_ALARM_REASON);
switch (launch_reason()) {
case APP_LAUNCH_WAKEUP:
push_alarm_window();
break;
default:
push_setter_window();
if (s_alarm_reason == WAKE_RECOVERY_ALARM) {
push_alarm_window();
} else {
push_setter_window();
}
};
}