Skip to content

Instantly share code, notes, and snippets.

@MauroMombelli
Last active August 29, 2015 14:04
Show Gist options
  • Save MauroMombelli/ded418587c389f45cbe4 to your computer and use it in GitHub Desktop.
Save MauroMombelli/ded418587c389f45cbe4 to your computer and use it in GitHub Desktop.
playing with overflow (modular arithmetic) to create a scheduler witch support dinamic event period and unregular execution times
#include <stdio.h>
#include <stdint.h>
int main(int argv, char *args[]){
uint8_t tempo_attuale = 0;
uint8_t tempo_ultima_esecuzione = -1;//force overflow, or if first execution is at 0 it get all screwed up untill next overflow!
uint8_t periodo_in_esame;
/*le seguenti 3 variabili possono essere messe in una strutture a poi "arrayzzate" per avere più eventi indipendenti*/
uint8_t tempo_esecuzione = 0;
uint8_t tempo_passato;
uint8_t periodo = 10;
/* more difficult! il timer è irregolare */
uint8_t ganularita_timer = 3;
for (uint16_t b = 0; b < 200; b++){
tempo_passato = tempo_attuale-tempo_esecuzione;
periodo_in_esame = tempo_attuale-tempo_ultima_esecuzione;
printf("\nt1=%d, t=%d,tempo_passato=%d", tempo_esecuzione, tempo_attuale,tempo_passato);
if (tempo_passato <= periodo_in_esame){
tempo_esecuzione += periodo;
printf(" ESECUZIONE!");
}
tempo_ultima_esecuzione = tempo_attuale;
tempo_attuale+=ganularita_timer;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment