Skip to content

Commit c7d18a6

Browse files
committed
Scheduler: port to mbed to demonstrate API compatibility
1 parent 3ff1ae4 commit c7d18a6

File tree

6 files changed

+236
-0
lines changed

6 files changed

+236
-0
lines changed

libraries/Scheduler/README.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
= Scheduler Library for Arduino =
2+
3+
The Scheduler library enables the Arduino Due to run multiple functions at the same time. This allows tasks to happen without interrupting each other.
4+
5+
For more information about this library please visit us at
6+
http://www.arduino.cc/en/Reference/Scheduler
7+
8+
== License ==
9+
10+
Copyright (c) 2012 The Android Open Source Project. All right reserved.
11+
12+
Licensed under the Apache License, Version 2.0 (the "License");
13+
you may not use this file except in compliance with the License.
14+
You may obtain a copy of the License at
15+
16+
http://www.apache.org/licenses/LICENSE-2.0
17+
18+
Unless required by applicable law or agreed to in writing, software
19+
distributed under the License is distributed on an "AS IS" BASIS,
20+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
See the License for the specific language governing permissions and
22+
limitations under the License.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Multiple Blinks
3+
4+
Demonstrates the use of the Scheduler library for the Arduino Nano 33 BLE
5+
6+
Hardware required :
7+
* None (LEDs are already conencted to RGB LED)
8+
9+
ATTENTION: LEDs polarity is reversed (so loop3 will turn the LED off by writing 1)
10+
11+
created 8 Oct 2012
12+
by Cristian Maglie
13+
Modified by
14+
Scott Fitzgerald 19 Oct 2012
15+
16+
This example code is in the public domain
17+
18+
http://www.arduino.cc/en/Tutorial/MultipleBlinks
19+
*/
20+
21+
// Include Scheduler since we want to manage multiple tasks.
22+
#include <Scheduler.h>
23+
24+
int led1 = LEDR;
25+
int led2 = LEDG;
26+
int led3 = LEDB;
27+
28+
void setup() {
29+
Serial.begin(9600);
30+
31+
// Setup the 3 pins as OUTPUT
32+
pinMode(led1, OUTPUT);
33+
pinMode(led2, OUTPUT);
34+
pinMode(led3, OUTPUT);
35+
36+
// Add "loop2" and "loop3" to scheduling.
37+
// "loop" is always started by default.
38+
Scheduler.startLoop(loop2);
39+
Scheduler.startLoop(loop3);
40+
}
41+
42+
// Task no.1: blink LED with 1 second delay.
43+
void loop() {
44+
digitalWrite(led1, HIGH);
45+
46+
// IMPORTANT:
47+
// When multiple tasks are running 'delay' passes control to
48+
// other tasks while waiting and guarantees they get executed.
49+
delay(1000);
50+
51+
digitalWrite(led1, LOW);
52+
delay(1000);
53+
}
54+
55+
// Task no.2: blink LED with 0.1 second delay.
56+
void loop2() {
57+
digitalWrite(led2, HIGH);
58+
delay(100);
59+
digitalWrite(led2, LOW);
60+
delay(100);
61+
}
62+
63+
// Task no.3: accept commands from Serial port
64+
// '0' turns off LED
65+
// '1' turns on LED
66+
void loop3() {
67+
if (Serial.available()) {
68+
char c = Serial.read();
69+
if (c == '0') {
70+
digitalWrite(led3, LOW);
71+
Serial.println("Led turned off!");
72+
}
73+
if (c == '1') {
74+
digitalWrite(led3, HIGH);
75+
Serial.println("Led turned on!");
76+
}
77+
}
78+
79+
// IMPORTANT:
80+
// We must call 'yield' at a regular basis to pass
81+
// control to other tasks.
82+
yield();
83+
}

libraries/Scheduler/keywords.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#######################################
2+
# Syntax Coloring Map For Scheduler
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Scheduler KEYWORD1 Scheduler
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
startLoop KEYWORD2
16+
17+
#######################################
18+
# Constants (LITERAL1)
19+
#######################################
20+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Scheduler
2+
version=0.4.4
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Allows multiple tasks to run at the same time, without interrupting each other. For Arduino sam and samd architectures only (Due, Zero...).
6+
paragraph=The Scheduler library enables the Arduino to run multiple functions at the same time. This allows tasks to happen without interrupting each other.</br>This is a cooperative scheduler in that the CPU switches from one task to another. The library includes methods for passing control between tasks.
7+
category=Other
8+
url=http://www.arduino.cc/en/Reference/Scheduler
9+
architectures=mbed

libraries/Scheduler/src/Scheduler.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Scheduler.h"
18+
19+
SchedulerClass::SchedulerClass() {
20+
21+
}
22+
23+
static void loophelper(SchedulerTask task) {
24+
while (1) {
25+
task();
26+
}
27+
}
28+
29+
void SchedulerClass::startLoop(SchedulerTask task, uint32_t stackSize) {
30+
int i = 0;
31+
while (threads[i] != NULL && i < MAX_THREADS_NUMBER) {
32+
i++;
33+
}
34+
threads[i] = new rtos::Thread(stackSize);
35+
threads[i]->start(mbed::callback(loophelper, task));
36+
}
37+
38+
void SchedulerClass::start(SchedulerTask task, uint32_t stackSize) {
39+
int i = 0;
40+
while (threads[i] != NULL && i < MAX_THREADS_NUMBER) {
41+
i++;
42+
}
43+
threads[i] = new rtos::Thread(stackSize);
44+
threads[i]->start(task);
45+
}
46+
47+
void SchedulerClass::start(SchedulerParametricTask task, void *taskData, uint32_t stackSize) {
48+
int i = 0;
49+
while (threads[i] != NULL && i < MAX_THREADS_NUMBER) {
50+
i++;
51+
}
52+
threads[i] = new rtos::Thread(stackSize);
53+
threads[i]->start(mbed::callback(task, taskData));
54+
}
55+
56+
SchedulerClass Scheduler;
57+

libraries/Scheduler/src/Scheduler.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef _SCHEDULER_H_
18+
#define _SCHEDULER_H_
19+
20+
#include <Arduino.h>
21+
#include "mbed.h"
22+
23+
#define MAX_THREADS_NUMBER 10
24+
25+
extern "C" {
26+
typedef void (*SchedulerTask)(void);
27+
typedef void (*SchedulerParametricTask)(void *);
28+
}
29+
30+
class SchedulerClass {
31+
public:
32+
SchedulerClass();
33+
void startLoop(SchedulerTask task, uint32_t stackSize = 1024);
34+
void start(SchedulerTask task, uint32_t stackSize = 1024);
35+
void start(SchedulerParametricTask task, void *data, uint32_t stackSize = 1024);
36+
37+
void yield() { ::yield(); };
38+
private:
39+
rtos::Thread* threads[MAX_THREADS_NUMBER] = {NULL};
40+
};
41+
42+
extern SchedulerClass Scheduler;
43+
44+
#endif
45+

0 commit comments

Comments
 (0)