void timers_save(void) { if (timers_count() == 0) { persist_delete(PERSIST_TIMER_START); return; } TimerBlock* block = NULL; uint8_t block_count = 0; for (uint8_t b = 0; b < timers_count(); b += 1) { if (NULL == block) { block = malloc(sizeof(TimerBlock)); block->total_timers = timers_count(); block->save_time = time(NULL); } uint8_t timer_block_pos = b % TIMER_BLOCK_SIZE; block->timers[timer_block_pos] = *timers_get(b); bool is_last_timer_in_block = timer_block_pos == (TIMER_BLOCK_SIZE - 1); if (is_last_timer_in_block) { persist_write_data(PERSIST_TIMER_START + block_count, block, sizeof(TimerBlock)); block_count += 1; free(block); block = NULL; } } if (block) { persist_write_data(PERSIST_TIMER_START + block_count, block, sizeof(TimerBlock)); } persist_write_int(PERSIST_TIMERS_VERSION, TIMERS_VERSION_CURRENT); }
void storage_store(){ APP_LOG(APP_LOG_LEVEL_INFO,"storage_store()"); APP_LOG(APP_LOG_LEVEL_INFO,"max_cell_size: %d timer_size: %d laps_size: %d clock_size: %d",PERSIST_DATA_MAX_LENGTH,sizeof(Timer),sizeof(Laps),sizeof(Clock)); int result; uint8_t c=timers_count(); result=persist_write_int(STORAGE_KEY_VERSION,STORAGE_VERSION); if(result<0){ APP_LOG(APP_LOG_LEVEL_ERROR,"write error count %d in storage %d #%d",c,STORAGE_KEY_COUNT,result); return; } result=persist_write_int(STORAGE_KEY_COUNT,c); if(result<0){ APP_LOG(APP_LOG_LEVEL_ERROR,"write error count %d in storage %d #%d",count_total(count),STORAGE_KEY_COUNT,result); return; } APP_LOG(APP_LOG_LEVEL_INFO,"timer count %d",count_total(count)); Timer* timer; for(uint8_t i=0;i<c;i++){ timer=timers_get(i); result=persist_write_data(STORAGE_KEY_DATA+i,timer,sizeof(Timer)); if(result<0){ APP_LOG(APP_LOG_LEVEL_ERROR,"write error timer %d in storage %d #%d",i,STORAGE_KEY_DATA+i,result); continue; } APP_LOG(APP_LOG_LEVEL_INFO,"stored timer %d",i); } APP_LOG(APP_LOG_LEVEL_INFO,"stored"); }
void handleAlarm(void* data){ APP_LOG(APP_LOG_LEVEL_INFO,"timer finished"); for(uint8_t i=timers_stopwatch_count();i<timers_count();i++){ timer_checkEnd(timers_get(i)); } window_alarm_show(); timers_updateNearest(); }
int16_t timers_index_of(uint16_t id) { uint8_t count = timers_count(); for (uint8_t c = 0; c < count; c += 1) { Timer* timer = timers_get(c); if (timer->id == id) { return c; } } return -1; }
Timer* timers_find(uint16_t id) { uint8_t count = timers_count(); for (uint8_t c = 0; c < count; c += 1) { Timer* timer = timers_get(c); if (timer->id == id) { return timer; } } return NULL; }
bool timers_remove(uint8_t position) { Timer* timer = timers_get(position); if (NULL == timer) { return false; } timer_pause(timer); linked_list_remove(timers, position); free(timer); timers_mark_updated(); return true; }
Timer* timers_find_last_wakeup(void) { Timer* last = NULL; uint16_t last_wakeup_time = 0; uint8_t count = timers_count(); for (uint8_t c = 0; c < count; c += 1) { Timer* timer = timers_get(c); if (timer->wakeup_id < 0) { continue; } if (timer->current_time > last_wakeup_time) { last = timer; last_wakeup_time = timer->current_time; } } return last; }
Timer* timers_find_wakeup_collision(Timer* timer) { time_t wakeup_time; wakeup_query(timer->wakeup_id, &wakeup_time); uint8_t count = timers_count(); for (uint8_t c = 0; c < count; c += 1) { Timer* timer_to_check = timers_get(c); if (timer_to_check->wakeup_id < 0) { continue; } if (timer_to_check->id == timer->id) { continue; } time_t check_time; wakeup_query(timer_to_check->wakeup_id, &check_time); if (abs(check_time - wakeup_time) <= 60) { return timer_to_check; } } return NULL; }
void timers_updateNearest(){ uint8_t min=0; Clock* minc=NULL; for(uint8_t i=timers_stopwatch_count();i<timers_count();i++){ Timer* t=timers_get(i); Clock* c=timer_getNextFinish(t); if(c==NULL){ continue; } if(minc==NULL||clock_compare(minc,c)>0){ clock_destroy(minc); minc=c; min=i; }else{ clock_destroy(c); } } nearest=min; scheduler_update(minc); }
status_t timers_save(void) { int block = 0; uint8_t num_timers = timers_get_count(); if (num_timers == 0) { persist_delete(STORAGE_TIMER_START); return 0; } for (int t = 0; t < num_timers; t += TIMER_BLOCK_SIZE) { TimerBlock* timerBlock = malloc(sizeof(TimerBlock)); timerBlock->count = num_timers; timerBlock->time = time(NULL); for (int u = 0; u < TIMER_BLOCK_SIZE; u += 1) { if (t + u >= num_timers) { break; } timerBlock->timers[u] = *timers_get(t + u); } persist_write_data(STORAGE_TIMER_START + block, timerBlock, sizeof(TimerBlock)); free(timerBlock); block += 1; } return 0; }
void timers_remove(int pos) { Timer* tmr = timers_get(pos); free_safe(tmr); linked_list_remove(timers, pos); }
Timer* timers_get_selected() { return timers_get(selected); }