Skip to content

Instantly share code, notes, and snippets.

@mrothNET
Last active April 28, 2019 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrothNET/6b47f731357903eb87615fd206c9ef87 to your computer and use it in GitHub Desktop.
Save mrothNET/6b47f731357903eb87615fd206c9ef87 to your computer and use it in GitHub Desktop.
AWAIT/ASYNC for embedded programming in C.
#include <stdbool.h>
#include <stdint.h>
#define ASYNC(id_t) \
static id_t ASYNC__=0; switch (ASYNC__) case 0: while(1)
#define YIELD() \
do { ASYNC__=(__COUNTER__+2)/2; return; case (__COUNTER__+1)/2: ASYNC__=0; } while(0)
#define AWAIT(test) \
do { while (!(test)) YIELD(); } while(0)
extern bool led;
extern bool pushbutton_on, pushbutton_off, pushbutton_toggle;
void example()
{
ASYNC(uint8_t)
{
AWAIT(!pushbutton_on && !pushbutton_off && !pushbutton_toggle);
AWAIT(pushbutton_on || pushbutton_off || pushbutton_toggle);
if (pushbutton_on && !pushbutton_off && !pushbutton_toggle)
led = true;
else if (pushbutton_off && !pushbutton_on && !pushbutton_toggle)
led = false;
else if (pushbutton_toggle && !pushbutton_on && !pushbutton_off)
led = !led;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment