#include <Preferences.h>
Preferences prefs;
// ===== PARANCSOK =====
enum CmdType {
CMD_SETPIN,
CMD_WAIT,
CMD_PRINT
};
struct Command {
CmdType type;
int arg1;
int arg2;
char text[64]; // saját szöveg
};
// ===== TASK =====
struct Task {
uint8_t id;
uint8_t enabled;
uint32_t interval;
uint32_t lastRun;
Command cmds[10];
uint8_t cmdCount;
};
Task tasks[10];
int taskCount = 0;
// ===== TASK FUTTATÓ =====
void runTask(Task &t) {
uint32_t now = millis();
if (now - t.lastRun < t.interval) return;
t.lastRun = now;
for (int i = 0; i < t.cmdCount; i++) {
Command &c = t.cmds[i];
switch (c.type) {
case CMD_SETPIN:
digitalWrite(c.arg1, c.arg2);
break;
case CMD_WAIT:
delay(c.arg1);
break;
case CMD_PRINT:
Serial.println(c.text);
break;
}
}
}
// ===== TASKOK MENTÉSE =====
void saveTasks() {
prefs.begin("taskdb", false);
prefs.putBytes("tasks", tasks, sizeof(tasks));
prefs.putInt("count", taskCount);
prefs.end();
}
// ===== TASKOK BETÖLTÉSE =====
void loadTasks() {
prefs.begin("taskdb", true);
taskCount = prefs.getInt("count", 0);
prefs.getBytes("tasks", tasks, sizeof(tasks));
prefs.end();
}
// ===== ÚJ TASK =====
void addTask(uint32_t interval) {
if (taskCount >= 10) return;
Task &t = tasks[taskCount];
t.id = taskCount;
t.enabled = 1;
t.interval = interval;
t.lastRun = 0;
t.cmdCount = 0;
taskCount++;
saveTasks();
}
// ===== PARANCS HOZZÁADÁSA =====
void addCommand(int id, CmdType type, const char* text, int arg1 = 0, int arg2 = 0) {
if (id >= taskCount) return;
Task &t = tasks[id];
if (t.cmdCount >= 10) return;
Command &c = t.cmds[t.cmdCount++];
c.type = type;
c.arg1 = arg1;
c.arg2 = arg2;
strncpy(c.text, text, 63);
saveTasks();
}
// ===== TASK LISTA =====
void listTasks() {
Serial.println("=== TASK LIST ===");
for (int i = 0; i < taskCount; i++) {
Serial.printf("ID %d | interval %u ms | cmds %d\n",
tasks[i].id, tasks[i].interval, tasks[i].cmdCount);
}
}
// ===== PARANCSOK LISTÁZÁSA =====
void listCommands(int id) {
if (id >= taskCount) return;
Serial.printf("=== Commands in task %d ===\n", id);
Task &t = tasks[id];
for (int i = 0; i < t.cmdCount; i++) {
Command &c = t.cmds[i];
if (c.type == CMD_PRINT)
Serial.printf("%d: PRINT \"%s\"\n", i, c.text);
if (c.type == CMD_SETPIN)
Serial.printf("%d: SETPIN pin=%d val=%d\n", i, c.arg1, c.arg2);
if (c.type == CMD_WAIT)
Serial.printf("%d: WAIT %d ms\n", i, c.arg1);
}
}
// ===== CLI PARANCS ÉRTELMEZŐ =====
void parseCommand(String cmd) {
cmd.trim();
// Új task
if (cmd.startsWith("addtask")) {
uint32_t interval = cmd.substring(8).toInt();
addTask(interval);
Serial.println("Task added.");
}
// PRINT parancs hozzáadása
else if (cmd.startsWith("addcmd")) {
int id = cmd.substring(7, cmd.indexOf(" ", 7)).toInt();
String rest = cmd.substring(cmd.indexOf(" ", 7) + 1);
if (rest.startsWith("PRINT")) {
String text = rest.substring(6);
addCommand(id, CMD_PRINT, text.c_str());
Serial.println("PRINT command added.");
}
}
// Taskok listázása
else if (cmd.startsWith("list")) {
listTasks();
}
// Parancsok listázása
else if (cmd.startsWith("listcmd")) {
int id = cmd.substring(8).toInt();
listCommands(id);
}
else {
Serial.println("Ismeretlen parancs.");
}
}
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
loadTasks();
Serial.println("CLI ready.");
}
void loop() {
// CLI olvasás
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
parseCommand(cmd);
}
// Task futtatás
for (int i = 0; i < taskCount; i++) {
if (tasks[i].enabled) {
runTask(tasks[i]);
}
}
}