aboutsummaryrefslogtreecommitdiff
path: root/sem4/embedded/m1/router/router.ino
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-06-07 23:40:14 +0200
committerJulian T <julian@jtle.dk>2020-06-07 23:40:14 +0200
commit6075c41624efa8260344dfd4b0c7fa563f4cfe5e (patch)
tree0ee0942543935c70ddd108f71562612cdd72daee /sem4/embedded/m1/router/router.ino
parent6cb9bec1e0241fe64afdad9a520f026a9c25ea91 (diff)
Added solutions for many of the first exercises
Diffstat (limited to 'sem4/embedded/m1/router/router.ino')
-rw-r--r--sem4/embedded/m1/router/router.ino47
1 files changed, 47 insertions, 0 deletions
diff --git a/sem4/embedded/m1/router/router.ino b/sem4/embedded/m1/router/router.ino
new file mode 100644
index 0000000..ba0b375
--- /dev/null
+++ b/sem4/embedded/m1/router/router.ino
@@ -0,0 +1,47 @@
+#define LEDPIN 13
+
+int task1(void) {
+ static bool state = 0;
+
+ digitalWrite(LEDPIN, state);
+ state = !state;
+ return 0;
+}
+
+int task2(void) {
+ Serial.print("l");
+ return 0;
+}
+int task3(void) {
+ Serial.print("h");
+ return 0;
+}
+
+int runevery(int (*f)(), unsigned long *last, unsigned long period) {
+ unsigned long now = millis();
+ if (*last + period > now) {
+ return -1;
+ }
+
+ *last = now;
+ return (*f)();
+}
+
+
+#define TASKS 3
+unsigned long lasts[TASKS];
+
+void setup() {
+ Serial.begin(115200);
+ pinMode(LEDPIN, OUTPUT);
+ for (int i = 0; i < TASKS; i++) {
+ lasts[i] = 0;
+ }
+}
+
+void loop() {
+ unsigned i = 0;
+ runevery(task1, &lasts[i++], 1000);
+ runevery(task2, &lasts[i++], 100);
+ runevery(task3, &lasts[i++], 10);
+}