-
Notifications
You must be signed in to change notification settings - Fork 1
/
mu_minimal.cpp
54 lines (43 loc) · 1.43 KB
/
mu_minimal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#define USE_SERIAL_DBG 1
// Make sure to use a platform define before following includes
#include "ustd_platform.h"
#include "scheduler.h"
#include "heartbeat.h"
ustd::Scheduler sched;
void appLoop();
void task0(String topic, String msg, String originator) {
// when receiving a message published to subscribed topic "led", do:
if (msg == "on")
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on
if (msg == "off")
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off
}
void task1() { // scheduled every 50ms
static int s1 = 0;
static ustd::heartbeat intervall = 500L; // 500 msec
if (intervall.beat()) {
if (s1 == 0) {
s1 = 1;
sched.publish("led", "on"); // MQTT-style publish to topic "led"
// If munet with mqtt is used, those messages are transparently send
// to an external MQTT-server, by default, muwerk only dispatches
// them between local tasks.
} else {
s1 = 0;
sched.publish("led", "off");
}
}
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
int tID = sched.add(appLoop, "main");
sched.subscribe(tID, "led", task0); // subscribe to MQTT-like top "led"
sched.add(task1, "task1", 50000L); // execute task1 every 50ms (50000us)
}
void appLoop() {
// your code goes here.
}
// Never add code to this loop, use appLoop() instead.
void loop() {
sched.loop();
}