Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Created December 15, 2019 12:35
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 ma2shita/c4fef35fc12cc9afb73cbb1b61436d06 to your computer and use it in GitHub Desktop.
Save ma2shita/c4fef35fc12cc9afb73cbb1b61436d06 to your computer and use it in GitHub Desktop.
#include <WioCellLibforArduino.h> /* for Wio3G */
#define CONSOLE SerialUSB
#include <pt.h>
/* PT_WAIT is very useful macro. delay() similar using. https://qiita.com/narumi18wa/items/070d3e5b2a18c4976304 */
#define PT_WAIT(pt, timestamp, usec) PT_WAIT_UNTIL(pt, millis() - *timestamp > usec);*timestamp = millis();
static struct pt pt1, pt2, pt3;
static int thread1(struct pt *pt) {
static unsigned long timestamp = 0; /* unit=ms; timestamp used as a ref in thread */
PT_BEGIN(pt);
while (true) { /* Same as loop() */
CONSOLE.println("thread1");
PT_WAIT(pt, &timestamp, 1000); /* Same as delay(1000) */
}
PT_END(pt);
}
static int thread2(struct pt *pt) {
static unsigned long timestamp = 0;
PT_BEGIN(pt);
while (true) {
/* no wait loop */
CONSOLE.println("thread2 - 1");
PT_WAIT_THREAD(pt, thread3(&pt3, 1)); /* Invoke and waiting for `thread3` */
CONSOLE.println("thread2 - 2");
}
PT_END(pt);
}
static int thread3(struct pt *pt, int val) {
static unsigned long timestamp = 0;
PT_BEGIN(pt);
/* one time callee */
CONSOLE.println(val);
CONSOLE.println(" thread3 - 1");
PT_WAIT(pt, &timestamp, 3000);
CONSOLE.println(" thread3 - 2");
PT_WAIT(pt, &timestamp, 4000);
PT_END(pt);
}
void setup() {
delay(200);
CONSOLE.begin(115200);
CONSOLE.println("--- START");
/* Initialize threads */
PT_INIT(&pt1);
PT_INIT(&pt2);
PT_INIT(&pt3);
}
void loop() {
/* Starting each thread */
thread1(&pt1);
thread2(&pt2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment