void settings_load(void) { if (persist_exists(PERSIST_SETTINGS)) { if (! persist_exists(PERSIST_SETTINGS_VERSION)) { DEBUG("Migrating settings from 2.X to 3.X"); OldSettings old_settings; int res = persist_read_data(PERSIST_SETTINGS, &old_settings, sizeof(old_settings)); if (res >= 0) { migrate_settings_01(old_settings); return; } } else if (persist_read_int(PERSIST_SETTINGS_VERSION) == SETTINGS_VERSION_TINY) { SettingsTiny settings_tiny; int res = persist_read_data(PERSIST_SETTINGS, &settings_tiny, sizeof(settings_tiny)); if (res >= 0) { migrate_settings_02(settings_tiny); return; } } int res = persist_read_data(PERSIST_SETTINGS, &_settings, sizeof(_settings)); if (res < 0) { LOG("Settings load failed: %d", res); } } }
static void show_charge_log() { if (!persist_exists(PERSIST_KEY_LOG_COUNT) || !persist_exists(PERSIST_KEY_LOG_INDEX)) { return; } int log_count = persist_read_int(PERSIST_KEY_LOG_COUNT); int log_index = persist_read_int(PERSIST_KEY_LOG_INDEX); if (log_count == 0) { return; } time_t now = time(NULL); for (int i = 0; i < log_count; ++i) { uint32_t key_log = PERSIST_KEY_LOG_BASE + (log_index + i) % MAX_LOG_COUNT; ChargeLog charge_log; persist_read_data(key_log, &charge_log, sizeof(charge_log)); static char buff[] = "999 4294967296 %100"; snprintf(buff, sizeof(buff), "%d %u %d%%", i, (unsigned)difftime(now, charge_log.time), charge_log.charge_state.charge_percent); APP_LOG(APP_LOG_LEVEL_DEBUG, buff); } }
/* * A place for everything and everything it its place. This is * mainly here because main() would be messy otherwise. */ static void init() { int theme; /* * Read the stored configuration keys or write defaults if they * don't exist so that the config is properly loaded. */ if(persist_exists(KEY_CONFIG_TEMP_UNIT)) { persist_read_string(KEY_CONFIG_TEMP_UNIT, s_temp_unit, sizeof(s_temp_unit)); } else { strncpy(s_temp_unit, DEFAULT_TEMP_UNIT, sizeof(s_temp_unit)); persist_write_string(KEY_CONFIG_TEMP_UNIT, s_temp_unit); } if(persist_exists(KEY_CONFIG_THEME)) { theme = persist_read_int(KEY_CONFIG_THEME); s_theme = (theme >= 0 && theme < THEME_COUNT) ? theme : DEFAULT_THEME; } else { s_theme = DEFAULT_THEME; persist_write_int(KEY_CONFIG_THEME, s_theme); } // Build the main window and register callbacks so that the UI gets // drawn. // s_main_window = window_create(); window_set_window_handlers(s_main_window, (WindowHandlers) { .load = load_cb, .unload = unload_cb });
static void pause_click_handler(ClickRecognizerRef recognizer, void *context) { int calib_stored = persist_exists(CALIB_PKEY) ? persist_read_int(CALIB_PKEY) : CALIB_DEFAULT; if (calib_stored != 0 && calibrating == 0) { pause++; pause = pause % 2; if (pause == 0) { end_time = time(NULL); double elapsed = difftime(end_time, start_time); result = (int)floor(sum_x/elapsed); display(text_layer_3, "Score: %d", result); action_bar_layer_set_icon(s_action_bar, BUTTON_ID_SELECT, s_icon_play); int calib_stored = persist_exists(CALIB_PKEY) ? persist_read_int(CALIB_PKEY) : CALIB_DEFAULT; display(text_layer_4, "Calib: %d", calib_stored); text_layer_set_text(text_layer_2, ""); if (result > 2*calib_stored) { send(0, 1); text_layer_set_text(text_layer_1, "YOU'RE DRUNK"); dialog_message_window_push(); } else { send(0, 0); text_layer_set_text(text_layer_1, "YOU'RE FINE"); } } else { sum_x = 0; text_layer_set_text(text_layer_2, "RUNNING"); text_layer_set_text(text_layer_3, ""); action_bar_layer_set_icon(s_action_bar, BUTTON_ID_SELECT, s_icon_pause); start_time = time(NULL); } } }
void save_charge_log(ChargeLog* charge_log) { int32_t log_count = 0; int32_t log_index = 0; if (persist_exists(PERSIST_KEY_LOG_COUNT)) { log_count = persist_read_int(PERSIST_KEY_LOG_COUNT); } if (persist_exists(PERSIST_KEY_LOG_INDEX)) { log_index = persist_read_int(PERSIST_KEY_LOG_INDEX); } log_count++; uint32_t key_log = PERSIST_KEY_LOG_BASE + (log_index + log_count - 1) % MAX_LOG_COUNT; if (log_count > MAX_LOG_COUNT) { log_count--; log_index++; } persist_write_int(PERSIST_KEY_LOG_COUNT, log_count); persist_write_int(PERSIST_KEY_LOG_INDEX, log_index); persist_write_data(key_log, charge_log, sizeof(*charge_log)); if (launch_reason() != APP_LAUNCH_WAKEUP) { layer_mark_dirty(s_graph_layer); } }
static void loadPersistentValues() { backgroundBlack = persist_exists(PERSIST_KEY_BACKGROUND) ? persist_read_int(PERSIST_KEY_BACKGROUND) : DEFAULT_BACKGROUND; showDay = persist_exists(PERSIST_KEY_SHOW_DAY) ? persist_read_int(PERSIST_KEY_SHOW_DAY) : DEFAULT_SHOWDAY; APP_LOG(APP_LOG_LEVEL_DEBUG, "Loaded values from storage - background=%d, showDay=%d", persist_exists(PERSIST_KEY_BACKGROUND) ? backgroundBlack : -1, persist_exists(PERSIST_KEY_SHOW_DAY)? showDay : -1); }
static void history_load() { if (persist_exists(MESSAGE_KEY_batches)) { current_history_batch = persist_read_int(MESSAGE_KEY_batches); } if (current_history_batch < 0) { // APP_LOG(APP_LOG_LEVEL_DEBUG, "No history in persistent storage: %d", current_history_batch); return; } // APP_LOG(APP_LOG_LEVEL_DEBUG, "Reading %d history batches from persistent storage", current_history_batch+1); int total_bytes_read = 0; for (int i=0; i<=current_history_batch; i++) { if (persist_exists(FIRST_HISTORY_BATCH+i)) { // int result = persist_read_data(FIRST_HISTORY_BATCH+i, &history[i], sizeof(history[i])); // APP_LOG(APP_LOG_LEVEL_DEBUG, "Loaded history batch %d, %d bytes, %d events, result %d", i, (int) sizeof(history[i]), history[i].last_event+1, result); persist_read_data(FIRST_HISTORY_BATCH+i, &history[i], sizeof(history[i])); total_bytes_read += sizeof(history[i]); } else { APP_LOG(APP_LOG_LEVEL_WARNING, "No history batch %d although current_history_batch %d indicates its existence!", i, current_history_batch); } } start_time = (int) history[0].event_time[0]; selected = (Selected) {current_history_batch, history[current_history_batch].last_event}; events = current_history_batch * HISTORY_BATCH_SIZE + history[current_history_batch].last_event + 1; if (persist_exists(MESSAGE_KEY_avgMood)) { average_mood = persist_read_int(MESSAGE_KEY_avgMood); } APP_LOG(APP_LOG_LEVEL_DEBUG, "Total history: %d batches, %d events, %d bytes", current_history_batch+1, events, total_bytes_read); }
// omain init - registers callback functions, reads persistent data, and requests most recent data from // companion app. Sets default timer length to 10 seconds. void main_init(void) { s_reset_app(NULL); // initialize app message app_message_register_inbox_received(inbox_received_callback); app_message_register_inbox_dropped(inbox_dropped_callback); app_message_register_outbox_failed(outbox_failed_callback); app_message_register_outbox_sent(outbox_sent_callback); app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum()); // send request for most recent data DictionaryIterator *iter; app_message_outbox_begin(&iter); dict_write_uint8(iter, 0, 42); app_message_outbox_send(); // gather persistent data for timer length and passcode if (persist_exists(TIMERLEN_PERSIST_KEY)) { s_timer_len = persist_read_int(TIMERLEN_PERSIST_KEY); } else { s_timer_len = 10*1000; } if (persist_exists(PASSCODE_PERSIST_KEY)) { s_passcode_defined = true; persist_read_string(PASSCODE_PERSIST_KEY, s_passcode, PASSCODE_LEN + 1); } else { s_passcode_defined = false; } }
void readConfig() { if (persist_exists(CONFIG_KEY_DATEORDER)) { USDate = persist_read_int(CONFIG_KEY_DATEORDER); } else { USDate = 1; persist_write_int(CONFIG_KEY_DATEORDER, USDate); } if (persist_exists(CONFIG_KEY_WEEKDAY)) { showWeekday = persist_read_int(CONFIG_KEY_WEEKDAY); } else { showWeekday = 0; persist_write_int(CONFIG_KEY_WEEKDAY, showWeekday); } if (persist_exists(CONFIG_KEY_LANG)) { curLang = persist_read_int(CONFIG_KEY_LANG); } else { curLang = LANG_ENGLISH; persist_write_int(CONFIG_KEY_LANG, curLang); } if (persist_exists(CONFIG_KEY_STRIPES)) { stripedDigits = persist_read_int(CONFIG_KEY_STRIPES); } else { stripedDigits = 1; persist_write_int(CONFIG_KEY_STRIPES, stripedDigits); } APP_LOG(APP_LOG_LEVEL_DEBUG, "Stored config (dateorder=%d, weekday=%d, lang=%d, stripedDigits=%d)", USDate, showWeekday, curLang, stripedDigits); }
static void window_load(Window *window) { Layer *root_layer = window_get_root_layer(window); if (persist_exists(PERSIST_KEY_ID_POMODORO)) { persist_read_data(PERSIST_KEY_ID_POMODORO, &pomodoro, sizeof(pomodoro)); } if (persist_exists(PERSIST_KEY_ID_POMODORO_CYCLE)) { persist_read_data(PERSIST_KEY_ID_POMODORO_CYCLE, &pomodoro.cycle, sizeof(pomodoro.cycle)); } if (persist_exists(PERSIST_KEY_ID_POMODORO_CYCLE_NOW)) { persist_read_data(PERSIST_KEY_ID_POMODORO_CYCLE_NOW, &pomodoro.timer, sizeof(pomodoro.timer)); if (pomodoro.cycle <= pomodoro.timer) pomodoro.timer = -1; } tick_timer_service_subscribe(HOUR_UNIT | MINUTE_UNIT, tick_handler); // LOAD RESOURCE uint8_t resource_id = (uint8_t)RESOURCE_ID_HANNA_B; for (uint8_t i = 0; i < bitmaps_length; ++i) { bitmaps[i] = gbitmap_create_with_resource((uint8_t)(resource_id + i)); } load_layers(root_layer); }
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { static char s_uptime_buffer[32]; vibes_short_pulse(); alert_time--; if(alert_time <= 0 ){ //TODO: write notification code tick_timer_service_unsubscribe(); vibes_cancel(); text_layer_set_text(s_alert_text_layer, "Timout Reached\n:("); // Prepare dictionary DictionaryIterator *iterator; app_message_outbox_begin(&iterator); // Write data char buff[100]; if(persist_exists(PERSIST_KEY_PHONE_NUMBER)){ persist_read_string(PERSIST_KEY_PHONE_NUMBER, buff, 100); dict_write_cstring(iterator, PERSIST_KEY_PHONE_NUMBER, buff); } if(persist_exists(PERSIST_KEY_NAME)){ persist_read_string(PERSIST_KEY_NAME, buff, 100); dict_write_cstring(iterator, PERSIST_KEY_NAME, buff); } // Send the data! app_message_outbox_send(); } else { snprintf(s_uptime_buffer, sizeof(s_uptime_buffer), BANNER_TEXT "\n" CLOCK_FORMAT_STRING, alert_time/60, alert_time%60); text_layer_set_text(s_alert_text_layer, s_uptime_buffer); } }
void options_init() { if(persist_exists(OPTION_LARGE_FONT)) large_font = persist_read_bool(OPTION_LARGE_FONT); if(persist_exists(OPTION_TASK_ACTIONS_POSITION)) task_actions_position = persist_read_int(OPTION_TASK_ACTIONS_POSITION); LOG("lg font? %d", large_font); LOG("act pos? %d", task_actions_position); }
// Load existing config from persistent storage void load_config() { if (persist_exists(MESSAGE_KEY_INVERT)) { config_invert = persist_read_bool(MESSAGE_KEY_INVERT); } if (persist_exists(MESSAGE_KEY_DATE_FORMAT)) { config_date_format = persist_read_int(MESSAGE_KEY_DATE_FORMAT); } }
void read_settings_from_memory() { if (persist_exists(KEY_SCALE_CHOICE)) scale = persist_read_int(KEY_SCALE_CHOICE); if (persist_exists(KEY_CLOCK_FORMAT)) clock_format = persist_read_int(KEY_CLOCK_FORMAT); if (persist_exists(KEY_DATE_FORMAT)) date_format = persist_read_int(KEY_DATE_FORMAT); }
static void load_persistent_data(){ uint32_t data_from = persist_exists(DATE_SAVE) ? (uint32_t)persist_read_int(DATE_SAVE) : get_today(); if (data_from == get_today()){ rounds_done = persist_exists(COMPLETED_COUNT) ? persist_read_int(COMPLETED_COUNT) : 0; rounds_canceled = persist_exists(CANCELED_COUNT) ? persist_read_int(CANCELED_COUNT) : 0; } }
static void load_items(void){ int i, key; for (i=0; i<NUM_FIRST_MENU_ITEMS; i++){ key = 2*i; if (persist_exists(key)) persist_read_string(key, book_list[i].name, sizeof(book_list[i].name)); key++; if (persist_exists(key)) book_list[i].page_number = persist_read_int(key); } }
bool LoadPersistedData(void) { CharacterData *characterData; int floor = 0; bool useWorkerApp = false; if(!persist_exists(PERSISTED_IS_DATA_SAVED) || !persist_read_bool(PERSISTED_IS_DATA_SAVED)) return false; if(!IsPersistedDataCurrent()) { WARNING_LOG("Persisted data does not match current version, clearing."); ClearPersistedData(); return false; } ProfileLogStart("LoadPersistedData"); INFO_LOG("Loading persisted data."); characterData = GetCharacter(); persist_read_data(PERSISTED_CHARACTER_DATA, characterData, sizeof(CharacterData)); floor = persist_read_int(PERSISTED_CURRENT_FLOOR); SetCurrentFloor(floor); persist_read_data(PERSISTED_ITEM_DATA, GetItemsOwned(), GetSizeOfItemsOwned()); SetStatPointsPurchased(persist_read_int(PERSISTED_STAT_POINTS_PURCHASED)); SetVibration(persist_read_bool(PERSISTED_VIBRATION)); SetFastMode(persist_read_bool(PERSISTED_FAST_MODE)); if(persist_exists(PERSISTED_USE_OLD_ASSETS)) SetUseOldAssets(persist_read_bool(PERSISTED_USE_OLD_ASSETS)); useWorkerApp = persist_read_bool(PERSISTED_WORKER_APP); if(useWorkerApp) { AttemptToLaunchWorkerApp(); } else { // If the user has launched the worker app outside of MiniDungeon, // they want it on. if(WorkerIsRunning()) SetWorkerApp(true); } SetWorkerCanLaunch(persist_read_bool(PERSISTED_WORKER_CAN_LAUNCH)); if(persist_read_bool(PERSISTED_IN_COMBAT)) { int currentMonster = persist_read_int(PERSISTED_MONSTER_TYPE); int currentMonsterHealth = persist_read_int(PERSISTED_MONSTER_HEALTH); ResumeBattle(currentMonster, currentMonsterHealth); } ProfileLogStop("LoadPersistedData"); if(characterData->level == 0) { // Something bad happened to the data, possible due to a watch crash ERROR_LOG("Persisted data was broken somehow, clearing"); ClearPersistedData(); return false; } return true; }
void handle_init() { keyIndex = (persist_exists(KEY_INDEX_KEY) ? persist_read_int(KEY_INDEX_KEY) : 0); unsigned char offset = '0'; unsigned char rawDST = 'N'; unsigned char rawCount = '1'; resource_load_byte_range(resource_get_handle(RESOURCE_ID_GMT_OFFSET), 0, &offset, 1); timeZoneIndex = (persist_exists(TIME_ZONE_KEY) ? persist_read_int(TIME_ZONE_KEY) : (offset - '0')); resource_load_byte_range(resource_get_handle(RESOURCE_ID_IS_DST), 0, &rawDST, 1); isDST = (persist_exists(IS_DST_KEY) ? persist_read_bool(IS_DST_KEY) : (rawDST == 'Y')); resource_load_byte_range(resource_get_handle(RESOURCE_ID_SECRET_COUNT), 0, &rawCount, 1); keyCount = rawCount - '0'; window = window_create(); window_stack_push(window, true /* Animated */); //Great for debugging the layout. //window_set_background_color(&window, GColorBlack); Layer* rootLayer = window_get_root_layer(window); GRect rootLayerRect = layer_get_bounds(rootLayer); barLayer = layer_create(GRect(0,70,rootLayerRect.size.w,5)); layer_set_update_proc(barLayer, bar_layer_update); layer_add_child(rootLayer, barLayer); currentKey = text_layer_create(GRect(0,0,rootLayerRect.size.w,22)); text_layer_set_font(currentKey, fonts_get_system_font(FONT_KEY_GOTHIC_18)); text_layer_set_text_alignment(currentKey, GTextAlignmentCenter); layer_add_child(rootLayer, text_layer_get_layer(currentKey)); currentCode = text_layer_create(GRect(0,32,rootLayerRect.size.w,36)); text_layer_set_font(currentCode, fonts_get_system_font(FONT_KEY_BITHAM_34_MEDIUM_NUMBERS)); text_layer_set_text_alignment(currentCode, GTextAlignmentCenter); layer_add_child(rootLayer, text_layer_get_layer(currentCode)); currentTime = text_layer_create(GRect(0,rootLayerRect.size.h-(15+22),(rootLayerRect.size.w/3)*2,22)); text_layer_set_font(currentTime, fonts_get_system_font(FONT_KEY_GOTHIC_18)); text_layer_set_text_alignment(currentTime, GTextAlignmentLeft); layer_add_child(rootLayer, text_layer_get_layer(currentTime)); currentOffset = text_layer_create(GRect((rootLayerRect.size.w/3)*2,rootLayerRect.size.h-(15+22),rootLayerRect.size.w/3,22)); text_layer_set_font(currentOffset, fonts_get_system_font(FONT_KEY_GOTHIC_18)); text_layer_set_text_alignment(currentOffset, GTextAlignmentRight); layer_add_child(rootLayer, text_layer_get_layer(currentOffset)); // Attach our desired button functionality window_set_click_config_provider(window, (ClickConfigProvider) click_config_provider); reload(); }
static void init() { s_icon_plus = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ACTION_ICON_PLUS); s_icon_minus = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ACTION_ICON_MINUS); // Get the count from persistent storage for use if it exists, otherwise use the default s_player1_score = persist_exists(PLAYER1_SCORE_PKEY) ? persist_read_int(PLAYER1_SCORE_PKEY) : PLAYER_SCORE_DEFAULT; s_player2_score = persist_exists(PLAYER2_SCORE_PKEY) ? persist_read_int(PLAYER2_SCORE_PKEY) : PLAYER_SCORE_DEFAULT; s_main_window = window_create(); window_set_window_handlers(s_main_window, (WindowHandlers) { .load = main_window_load, .unload = main_window_unload, });
static uint16_t get_events_count_callback(struct MenuLayer *menu_layer, uint16_t section_index, void *callback_context) { int i = 0; int past_events = 0; time_t now = time(0); while (persist_exists(i*PERSIST_EVENT_FIELDCOUNT + PERSIST_EVENT_TITLE)) { if (persist_exists(i*PERSIST_EVENT_FIELDCOUNT + PERSIST_EVENT_END_DATE)) { int end_date = persist_read_int(i * PERSIST_EVENT_FIELDCOUNT + PERSIST_EVENT_END_DATE); if (end_date < now) past_events++; } i++; } return i - past_events; }
static int get_event_no_from_row_index(int index) { int i = 0; int past_events = 0; time_t now = time(0); while ((i - past_events) <= index && persist_exists(i*PERSIST_EVENT_FIELDCOUNT + PERSIST_EVENT_TITLE)) { if (persist_exists(i*PERSIST_EVENT_FIELDCOUNT + PERSIST_EVENT_END_DATE)) { int end_date = persist_read_int(i * PERSIST_EVENT_FIELDCOUNT + PERSIST_EVENT_END_DATE); if (end_date < now) past_events++; } i++; } return i - 1; }
static void loadData() { if(persist_exists(STORAGE_DAY)) { persist_read_string(STORAGE_DAY, g_day, sizeof(g_day)); } if(persist_exists(STORAGE_GOAL_ML)) { g_goal = persist_read_int(STORAGE_GOAL_ML); } if(persist_exists(STORAGE_DRINKS)) { persist_read_data(STORAGE_DRINKS, g_drinks, sizeof(g_drinks)); } if(persist_exists(STORAGE_ACTIVE_DRINK_TYPE)) { g_activeDrinkType = persist_read_int(STORAGE_ACTIVE_DRINK_TYPE); } }
static void init(void) { //(void)ctx; app_message_register_inbox_received(in_received_handler); app_message_register_inbox_dropped(in_dropped_handler); app_message_open(64, 0); if(persist_exists(KEY_24H)) SHOW_24H = persist_read_bool(KEY_24H); if(persist_exists(KEY_DATE)) SHOW_TEXT_DATE = persist_read_bool(KEY_DATE); if(persist_exists(KEY_VIBE)) BT_VIBE = persist_read_bool(KEY_VIBE); if(persist_exists(KEY_INVERT)) INVERT = persist_read_bool(KEY_INVERT); if(INVERT) { BACKGROUND_COLOR = GColorWhite; FOREGROUND_COLOR = GColorBlack; } else { BACKGROUND_COLOR = GColorBlack; FOREGROUND_COLOR = GColorWhite; } window = window_create(); window_set_background_color(window, BACKGROUND_COLOR); window_stack_push(window, true); Layer *window_layer = window_get_root_layer(window); GRect bounds = layer_get_bounds(window_layer); // Init the layer for the minute display minute_display_layer = layer_create(bounds); layer_set_update_proc(minute_display_layer, minute_display_layer_update_callback); layer_add_child(window_layer, minute_display_layer); // Init the layer for the hour display hour_display_layer = layer_create(bounds); layer_set_update_proc(hour_display_layer, hour_display_layer_update_callback); layer_add_child(window_layer, hour_display_layer); // Init the layer for the battery display battery_display_layer = layer_create(bounds); layer_set_update_proc(battery_display_layer, battery_display_layer_update_callback); layer_add_child(window_layer, battery_display_layer); setup_time_date_layers(); tick_timer_service_subscribe(MINUTE_UNIT, handle_minute_tick); bluetooth_connection_service_subscribe(handle_bluetooth_event); }
static void init_sliding_row(SlidingTextData *data, SlidingRow *row, GRect pos, GFont font, int delay) { row->label = text_layer_create(pos); text_layer_set_background_color(row->label, GColorClear); if (persist_exists(KEY_TEXT_COLOR)) { int color = persist_read_int(KEY_TEXT_COLOR); GColor text_color = GColorFromHEX(color); text_layer_set_text_color(row->label, text_color); } else { text_layer_set_text_color(row->label, GColorWhite); } if (font) { text_layer_set_font(row->label, font); row->unchanged_font = true; } else { row->unchanged_font = false; } row->state = IN_FRAME; row->next_string = NULL; row->left_pos = -pos.size.w; row->right_pos = pos.size.w; row->still_pos = pos.origin.x; row->movement_delay = delay; row->delay_count = 0; data->last_hour = -1; data->last_minute = -1; }
static void window_load(Window *window) { // Check for saved settings mInvertColors = persist_exists(KEY_INVERT) ? persist_read_bool(KEY_INVERT) : false; mBackColor = (mInvertColors) ? GColorWhite : GColorBlack; mForeColor = (mInvertColors) ? GColorBlack : GColorWhite; mStarSpeed = STAR_SPEED_SLOW;//persist_exists(KEY_STAR_SPEED) ? persist_read_int(KEY_STAR_SPEED) : STAR_SPEED_FAST; mFramerate = FRAMERATE_SMOOTH;//persist_exists(KEY_FRAMERATE) ? persist_read_int(KEY_FRAMERATE) : FRAMERATE_POWER; window_set_background_color(window, mBackColor); canvas = layer_create(GRect(0, 0, SCREEN_WIDTH , SCREEN_HEIGHT)); layer_set_update_proc(canvas, (LayerUpdateProc) render); layer_add_child(window_get_root_layer(window), canvas); mFont = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_TETRIS_24)); // Set initial time so display isn't blank mTimeText = NULL; struct tm *t; time_t temp; temp = time(NULL); t = localtime(&temp); set_time_display(t); start(); }
static void endtrial_callback() { pause++; vibes_short_pulse(); end_time = time(NULL); double elapsed = difftime(end_time, start_time); result = (int)floor(sum_x/elapsed); display(text_layer_3, "Final: %d", result); action_bar_layer_set_icon(s_action_bar, BUTTON_ID_SELECT, s_icon_play); int calib_stored = persist_exists(CALIB_PKEY) ? persist_read_int(CALIB_PKEY) : CALIB_DEFAULT; if (result > 2*calib_stored) { send(0,1); text_layer_set_text(text_layer_1, "YOU'RE DRUNK"); text_layer_set_text(text_layer_4, ""); dialog_message_window_push(); } else { send(0,0); text_layer_set_text(text_layer_1, "YOU'RE FINE"); text_layer_set_text(text_layer_2, ""); text_layer_set_text(text_layer_4, ""); } }
static void init(void) { if(!persist_exists(CONFIG_WORK)){ persist_write_int(CONFIG_WORK,25); persist_write_int(CONFIG_REST,5); } show_mainwindow(); }
void Settings_init() { // first, check if we have any saved settings if (persist_exists(SETTINGS_PERSIST_KEY)) { // if we do, what version of the settings was saved? uint32_t savedVersion = persist_read_int(SETTINGS_VERSION_KEY); if(savedVersion == 2) { // in this case, we can direclty load the current version persist_read_data(SETTINGS_PERSIST_KEY, &globalSettings, sizeof(Settings)); } else { // in this case, we're upgrading from the old version // first, read in the settings using the original format Settings_v0 oldSettings; persist_read_data(SETTINGS_PERSIST_KEY, &oldSettings, sizeof(Settings_v0)); // now upgrade to the new format globalSettings.timeColor = oldSettings.timeColor; globalSettings.timeBgColor = oldSettings.timeBgColor; globalSettings.sidebarColor = oldSettings.sidebarColor; globalSettings.useMetric = oldSettings.useMetric; globalSettings.sidebarOnRight = oldSettings.sidebarOnRight; // fill the new settings, use the defaults Settings_loadV2Defaults(); } } else { // if there weren't any saved settings, load the defaults Settings_loadAllDefaults(); } // if it doesn't exist, we get "0", which is the default anyway Settings_showLeadingZero = persist_read_int(SETTING_LEADING_ZERO_KEY); }
/** * Function to restore the presets saved in persistent storage into the list. */ void presets_restore(void){ presets_clear(); if (!persist_exists(STORAGE_PRESET_START)) return; int block = 0; PresetBlock* presetBlock = malloc(sizeof(PresetBlock)); persist_read_data(STORAGE_PRESET_START, presetBlock, sizeof(PresetBlock)); uint8_t preset_count = presetBlock->count; int save_time = presetBlock->time; int now = time(NULL); int seconds_elapsed = now-save_time; int minutes_elapse = (int)(floorl(seconds_elapsed / 60)); for (int i = 0; i < preset_count; i++){ if (i > 0 && i % PRESET_BLOCK_SIZE == 0){ block += 1; free(presetBlock); presetBlock = malloc(sizeof(PresetBlock)); persist_read_data(STORAGE_PRESET_START + block, presetBlock, sizeof(PresetBlock)); } Preset *preset = preset_clone(&presetBlock->presets[i % PRESET_BLOCK_SIZE]); preset->eta -= minutes_elapse; if (preset->eta <= 0) preset->eta = PRESET_REFRESHING_ETA; presets_add(preset); } free(presetBlock); send_all_eta_req(); return; }
/** * Function that saves the presets into persistent storage. */ void presets_save(void){ int block = 0; uint8_t num_presets = presets_get_count(); if (num_presets == 0){ while (persist_exists(STORAGE_PRESET_START + block)){ persist_delete(STORAGE_PRESET_START + block); block++; } return; } for (int i = 0; i < num_presets; i+= PRESET_BLOCK_SIZE){ PresetBlock *presetBlock = malloc(sizeof(PresetBlock)); presetBlock->count = num_presets; presetBlock->time = time(NULL); for (int j = 0; j < PRESET_BLOCK_SIZE; j++){ if (i+j >= num_presets) break; presetBlock->presets[j] = *presets_get(i+j); } persist_write_data(STORAGE_PRESET_START+block, presetBlock, sizeof(PresetBlock)); free(presetBlock); block++; } return; }