aboutsummaryrefslogtreecommitdiff
path: root/sem4/embedded/m1
diff options
context:
space:
mode:
Diffstat (limited to 'sem4/embedded/m1')
-rw-r--r--sem4/embedded/m1/int/int.ino22
-rw-r--r--sem4/embedded/m1/router/router.ino47
2 files changed, 59 insertions, 10 deletions
diff --git a/sem4/embedded/m1/int/int.ino b/sem4/embedded/m1/int/int.ino
index 8978be7..c4a2c31 100644
--- a/sem4/embedded/m1/int/int.ino
+++ b/sem4/embedded/m1/int/int.ino
@@ -4,19 +4,21 @@
#define DEBOUNCE 1
ISR(INT0_vect) {
- static bool state = 0;
+ static unsigned long last = 0;
- // Toggle LEDPIN
- digitalWrite(LEDPIN, state);
- state = !state;
+ unsigned long now = millis();
+ if (now - last < 100) {
+ return;
+ }
+ last = now;
+
+ static unsigned char state = 0;
- /*
- for(long i = 0; i < 1000; i++) {
- Serial.print("Hej ");
- Serial.println(i);
+ // Toggle LEDPIN
+ digitalWrite(LEDPIN, state % 2 == 0);
+ state++;
+ Serial.print("Hej "); Serial.println(state);
- }
- */
}
void setup() {
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);
+}