Skip to content

Commit

Permalink
Made Arduios able to work as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
J0K0SAN authored Jun 12, 2019
1 parent e5df18a commit 338d36d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
39 changes: 6 additions & 33 deletions Arduios.ino
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
/*
* Arduios
* by John´s Project
*
* Arduios is like a operating system for the arduino,
* it enables you to use a sketch in different use cases,
* without having to upload a new one every time.
*
* You can easily write an app by including "kernel.h",
* extending and implementing the methods of the App class
* and registering it in the apps array here.
* (App class is in "kernel.h")
*
* If you need docs just take a look into the '.h' files.
* ----------------------------------------------------------
* https://github.com/JohnsProject/Arduios
* ----------------------------------------------------------
*
* This is the bootloader of the Arduios, it loads the kernel
* and is the place where your apps should be registered.
*/

#include "Kernel.h"

#include "Shell.h"
//#include "TestApp.h"

// the kernel will call the first app in the array when its loaded
const App *apps[] = {
&shell,
//&testApp // uncomment to enable TestApp
};
#include <Kernel.h>
#include <Shell.h>
#include <TestApp.h>

void setup() {
kernel.setup(apps, sizeof(apps) / sizeof(App));
kernel.addApp(shell);
//kernel.addApp(testApp);
kernel.setup();
}

void loop() {
Expand Down
24 changes: 17 additions & 7 deletions Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@

Kernel kernel;

void Kernel::setup(App *apps[], uint8_t appsCount) {
registry.appsCount = appsCount;
void Kernel::setup() {
registry.currentApp = 0;
registry.apps = apps;
registry.apps[registry.currentApp]->setup();
}

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

void Kernel::addApp(App &app) {
for (uint8_t i = 0; i < registry.MAX_APPS; i++) {
if(registry.apps[i] == NULL){
registry.apps[i] = &app;
return;
}
}
}

void Kernel::loadApp(uint8_t app_index) {
registry.currentApp = app_index;
registry.apps[registry.currentApp]->setup();
}

void Kernel::loadApp(String app_name) {
for (uint8_t i = 0; i < registry.appsCount; i++) {
if (app_name == registry.apps[i]->getName()) {
registry.currentApp = i;
registry.apps[registry.currentApp]->setup();
for (uint8_t i = 0; i < registry.MAX_APPS; i++) {
if(registry.apps[i] != NULL){
if (app_name == registry.apps[i]->getName()) {
registry.currentApp = i;
registry.apps[registry.currentApp]->setup();
return;
}
}
}
}
28 changes: 25 additions & 3 deletions Kernel.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* Arduios
* by John´s Project
*
* Arduios is like a operating system for the arduino,
* it enables you to use a sketch in different use cases,
* without having to upload a new one every time.
*
* You can easily write an app by including "kernel.h",
* extending and implementing the methods of the App class
* and registering it in the apps array here.
* (App class is in "kernel.h")
*
* If you need docs just take a look into the '.h' files.
* ----------------------------------------------------------
* https://github.com/JohnsProject/Arduios
* ----------------------------------------------------------
*
* This is the bootloader of the Arduios, it loads the kernel
* and is the place where your apps should be registered.
*/
#ifndef __KERNEL_H_INCLUDED__
#define __KERNEL_H_INCLUDED__

Expand All @@ -21,17 +42,18 @@ class App {
extern struct Kernel {

struct Registry {
App **apps;
uint8_t appsCount;
const static uint8_t MAX_APPS = 10;
const App *apps[MAX_APPS];
uint8_t currentApp;
} registry;

void addApp(App &app);
// method used to load an app from the registry by its index
void loadApp(uint8_t app_index);
// method used to load an app from the registry by its name
void loadApp(String app_name);
// method called by the bootloader once when the kernel is loaded
void setup(App *apps[], uint8_t appsCount);
void setup();
// method constantly called by the bootloader
void loop();
} kernel;
Expand Down
10 changes: 4 additions & 6 deletions Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ void Shell::loop() {
Serial.println(F("commands: list., load:app_name."));
}
if (content == F("list")) {
if (kernel.registry.appsCount > 1) {
for (uint8_t i = 1; i < kernel.registry.appsCount; i++) {
Serial.println(kernel.registry.apps[i]->getName());
for (uint8_t i = 1; i < kernel.registry.MAX_APPS; i++) {
if(kernel.registry.apps[i] != NULL){
Serial.println(kernel.registry.apps[i]->getName());
}
}
} else {
Serial.println(F("No apps found"));
}
}
if (content.substring(0, 5) == F("load:")) {
content.replace("load:", "");
Expand Down
5 changes: 3 additions & 2 deletions TestApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class TestApp: public App {

void setup() {
// put your setup code here, to run once:
Serial.println("TestApp begin");
Serial.begin(9600);
Serial.println(F("TestApp begin"));
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println("TestApp loop");
Serial.println(F("TestApp loop"));
delay(1000);
}

Expand Down

0 comments on commit 338d36d

Please sign in to comment.