Skip to content

Commit 338d36d

Browse files
authored
Made Arduios able to work as a library
1 parent e5df18a commit 338d36d

File tree

5 files changed

+55
-51
lines changed

5 files changed

+55
-51
lines changed

Arduios.ino

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
1-
/*
2-
* Arduios
3-
* by John´s Project
4-
*
5-
* Arduios is like a operating system for the arduino,
6-
* it enables you to use a sketch in different use cases,
7-
* without having to upload a new one every time.
8-
*
9-
* You can easily write an app by including "kernel.h",
10-
* extending and implementing the methods of the App class
11-
* and registering it in the apps array here.
12-
* (App class is in "kernel.h")
13-
*
14-
* If you need docs just take a look into the '.h' files.
15-
* ----------------------------------------------------------
16-
* https://github.com/JohnsProject/Arduios
17-
* ----------------------------------------------------------
18-
*
19-
* This is the bootloader of the Arduios, it loads the kernel
20-
* and is the place where your apps should be registered.
21-
*/
22-
23-
#include "Kernel.h"
24-
25-
#include "Shell.h"
26-
//#include "TestApp.h"
27-
28-
// the kernel will call the first app in the array when its loaded
29-
const App *apps[] = {
30-
&shell,
31-
//&testApp // uncomment to enable TestApp
32-
};
1+
#include <Kernel.h>
2+
#include <Shell.h>
3+
#include <TestApp.h>
334

345
void setup() {
35-
kernel.setup(apps, sizeof(apps) / sizeof(App));
6+
kernel.addApp(shell);
7+
//kernel.addApp(testApp);
8+
kernel.setup();
369
}
3710

3811
void loop() {

Kernel.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,37 @@
22

33
Kernel kernel;
44

5-
void Kernel::setup(App *apps[], uint8_t appsCount) {
6-
registry.appsCount = appsCount;
5+
void Kernel::setup() {
76
registry.currentApp = 0;
8-
registry.apps = apps;
97
registry.apps[registry.currentApp]->setup();
108
}
119

1210
void Kernel::loop() {
1311
registry.apps[registry.currentApp]->loop();
1412
}
1513

14+
void Kernel::addApp(App &app) {
15+
for (uint8_t i = 0; i < registry.MAX_APPS; i++) {
16+
if(registry.apps[i] == NULL){
17+
registry.apps[i] = &app;
18+
return;
19+
}
20+
}
21+
}
22+
1623
void Kernel::loadApp(uint8_t app_index) {
1724
registry.currentApp = app_index;
1825
registry.apps[registry.currentApp]->setup();
1926
}
2027

2128
void Kernel::loadApp(String app_name) {
22-
for (uint8_t i = 0; i < registry.appsCount; i++) {
23-
if (app_name == registry.apps[i]->getName()) {
24-
registry.currentApp = i;
25-
registry.apps[registry.currentApp]->setup();
29+
for (uint8_t i = 0; i < registry.MAX_APPS; i++) {
30+
if(registry.apps[i] != NULL){
31+
if (app_name == registry.apps[i]->getName()) {
32+
registry.currentApp = i;
33+
registry.apps[registry.currentApp]->setup();
34+
return;
35+
}
2636
}
2737
}
2838
}

Kernel.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/*
2+
* Arduios
3+
* by John´s Project
4+
*
5+
* Arduios is like a operating system for the arduino,
6+
* it enables you to use a sketch in different use cases,
7+
* without having to upload a new one every time.
8+
*
9+
* You can easily write an app by including "kernel.h",
10+
* extending and implementing the methods of the App class
11+
* and registering it in the apps array here.
12+
* (App class is in "kernel.h")
13+
*
14+
* If you need docs just take a look into the '.h' files.
15+
* ----------------------------------------------------------
16+
* https://github.com/JohnsProject/Arduios
17+
* ----------------------------------------------------------
18+
*
19+
* This is the bootloader of the Arduios, it loads the kernel
20+
* and is the place where your apps should be registered.
21+
*/
122
#ifndef __KERNEL_H_INCLUDED__
223
#define __KERNEL_H_INCLUDED__
324

@@ -21,17 +42,18 @@ class App {
2142
extern struct Kernel {
2243

2344
struct Registry {
24-
App **apps;
25-
uint8_t appsCount;
45+
const static uint8_t MAX_APPS = 10;
46+
const App *apps[MAX_APPS];
2647
uint8_t currentApp;
2748
} registry;
2849

50+
void addApp(App &app);
2951
// method used to load an app from the registry by its index
3052
void loadApp(uint8_t app_index);
3153
// method used to load an app from the registry by its name
3254
void loadApp(String app_name);
3355
// method called by the bootloader once when the kernel is loaded
34-
void setup(App *apps[], uint8_t appsCount);
56+
void setup();
3557
// method constantly called by the bootloader
3658
void loop();
3759
} kernel;

Shell.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ void Shell::loop() {
2424
Serial.println(F("commands: list., load:app_name."));
2525
}
2626
if (content == F("list")) {
27-
if (kernel.registry.appsCount > 1) {
28-
for (uint8_t i = 1; i < kernel.registry.appsCount; i++) {
29-
Serial.println(kernel.registry.apps[i]->getName());
27+
for (uint8_t i = 1; i < kernel.registry.MAX_APPS; i++) {
28+
if(kernel.registry.apps[i] != NULL){
29+
Serial.println(kernel.registry.apps[i]->getName());
30+
}
3031
}
31-
} else {
32-
Serial.println(F("No apps found"));
33-
}
3432
}
3533
if (content.substring(0, 5) == F("load:")) {
3634
content.replace("load:", "");

TestApp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ class TestApp: public App {
1616

1717
void setup() {
1818
// put your setup code here, to run once:
19-
Serial.println("TestApp begin");
19+
Serial.begin(9600);
20+
Serial.println(F("TestApp begin"));
2021
}
2122

2223
void loop() {
2324
// put your main code here, to run repeatedly:
24-
Serial.println("TestApp loop");
25+
Serial.println(F("TestApp loop"));
2526
delay(1000);
2627
}
2728

0 commit comments

Comments
 (0)