libdo is an embeddable library for predicate-driven programming in C and C++.
- C89 compatible
- Simple API
- Priority based dispatch
- Expirable handlers
- No restrictions on adding/removing handlers from within handlers
- Test suites
git clone https://github.com/Sufi-Al-Hussaini/libdo.git
Copy libdo.{h,c}
and vector.h
to your source code tree and add libdo.c
to your build system source files list.
Run make
to compile C & C++ tests and ./tests
or ./testscpp
to run them.
- Create a directory named
libdo
in your~/Arduino/libraries
directory. - Copy
libdo.{h,c}
andvector.h
into it. - Import the
libdo
library as usual. Typically,Sketch -> Include Library -> libdo
in the Arduino IDE.
Note: Error-checking removed for brevity.
/* Create a doer */
struct do_doer *doer = do_init();
/* Create a work with a work function and predicate */
bool run_work = true;
struct do_work *work = do_work_if(work_function, NULL, &run_work);
/* Ask the doer to do it */
/* This also gives up ownership of the work instance to the doer */
do_so(doer, work);
/* Call the loop() method */
/* This calls all work functions whose associated predicate evaluates to true */
while (do_loop(doer)) {
sleep(1);
}
/* Cleanup */
do_destroy(doer);
Documentation and possible applications can be found on this blog post.
- Don't sleep in your predicate or work functions. Instead, add a
work
with a time predicate, if you want to do something after a delay. - Add a delay between subsequent
loop()
calls, to keep the processor happy. Ideally, have a blocking function call before theloop()
call. - Make sure you remove
works
that you don't need.
Work for these features is planned and under way:
- Code documentation
- C++11 header-only wrapper class
- Examples
- Submit pull requests for bug-fixes directly.
- For everything else (feature request, feature addition, discussions, etc.) open an issue first, so that it is easy for everyone to track.
- Follow libdo's code style and write appropriate test.
The MIT License (MIT)