Skip to content

Instantly share code, notes, and snippets.

@pbabics
Last active December 24, 2018 10:53
Show Gist options
  • Save pbabics/1020698fd1c9e52858b5b737956d5af5 to your computer and use it in GitHub Desktop.
Save pbabics/1020698fd1c9e52858b5b737956d5af5 to your computer and use it in GitHub Desktop.
typedef int (*CALLABLE) (void);
CALLABLE async_functions[] = {
xyz,
yzx,
zxy,
};
int async_delays[] = {0, 0, 0};
void loop() {
int min_time = 1 << 31,
min_index = -1;
for (int i = 0; i < sizeof(async_functions) / sizeof(*async_functions); ++i) {
if (min_time > async_delays[i]) {
min_time = async_delays[i];
min_index = i;
}
if (min_time == 0)
break;
}
delay(min_time);
if (min_time > 0)
for (int i = 0; i < sizeof(async_functions) / sizeof(*async_functions); ++i)
async_delays[i] = max(0, async_delays[i] - min_time);
async_delays[min_index] = async_functions[min_index]();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment