Last active
April 28, 2019 21:54
-
-
Save mrothNET/6b47f731357903eb87615fd206c9ef87 to your computer and use it in GitHub Desktop.
AWAIT/ASYNC for embedded programming in C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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