#include <Preferences.h>
Preferences prefs;
// ===== PLC I/O =====
const int X_PINS[] = {34, 35, 32, 33}; // bemenetek
const int Y_PINS[] = {2, 4, 5, 18}; // kimenetek
// ===== PARANCSOK =====
enum CmdType {
CMD_SETPIN, // Y kimenet állítása
CMD_PRINT, // szöveg kiírás
CMD_IF // feltételes parancs
};
struct Command {
CmdType type;
int arg1; // pin vagy X index
int arg2; // érték
char text[64]; // PRINT szöveg
int thenType; // IF után végrehajtandó parancs típusa
int thenArg1;
int thenArg2;
};
// ===== 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) {
// ===== Y kimenet állítása =====
case CMD_SETPIN:
digitalWrite(Y_PINS[c.arg1], c.arg2);
break;
// ===== PRINT =====
case CMD_PRINT:
Serial.println(c.text);
break;
// ===== IF X feltétel =====
case CMD_IF:
if (digitalRead(X_PINS[c.arg1]) == c.arg2) {
if (c.thenType == CMD_SETPIN) {
digitalWrite(Y_PINS[c.thenArg1], c.thenArg2);
}
if (c.thenType == 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, Command c) {
if (id >= taskCount) return;
Task &t = tasks[id];
if (t.cmdCount >= 10) return;
t.cmds[t.cmdCount++] = c;
saveTasks();
}
// ===== 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
else if (cmd.startsWith("addcmd")) {
int id = cmd.substring(7, cmd.indexOf(" ", 7)).toInt();
String rest = cmd.substring(cmd.indexOf(" ", 7) + 1);
// PRINT
if (rest.startsWith("PRINT")) {
String text = rest.substring(6);
Command c;
c.type = CMD_PRINT;
strncpy(c.text, text.c_str(), 63);
addCommand(id, c);
Serial.println("PRINT added.");
}
// SETPIN Y0 1
if (rest.startsWith("Y")) {
int y = rest.substring(1, rest.indexOf(" ")).toInt();
int val = rest.substring(rest.indexOf(" ") + 1).toInt();
Command c;
c.type = CMD_SETPIN;
c.arg1 = y;
c.arg2 = val;
addCommand(id, c);
Serial.println("SETPIN added.");
}
// IF X0 == 1 THEN Y1 = 1
if (rest.startsWith("IF")) {
int x = rest.substring(3, rest.indexOf(" ")).toInt();
int val = rest.substring(rest.indexOf("==") + 3, rest.indexOf(" ", rest.indexOf("==") + 3)).toInt();
int y = rest.substring(rest.indexOf("Y") + 1, rest.indexOf("=", rest.indexOf("Y"))).toInt();
int yval = rest.substring(rest.indexOf("=", rest.indexOf("Y")) + 1).toInt();
Command c;
c.type = CMD_IF;
c.arg1 = x;
c.arg2 = val;
c.thenType = CMD_SETPIN;
c.thenArg1 = y;
c.thenArg2 = yval;
addCommand(id, c);
Serial.println("IF added.");
}
}
else {
Serial.println("Ismeretlen parancs.");
}
}
void setup() {
Serial.begin(115200);
// I/O beállítás
for (int i = 0; i < 4; i++) pinMode(X_PINS[i], INPUT);
for (int i = 0; i < 4; i++) pinMode(Y_PINS[i], OUTPUT);
loadTasks();
Serial.println("PLC CLI ready.");
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
parseCommand(cmd);
}
for (int i = 0; i < taskCount; i++) {
if (tasks[i].enabled) runTask(tasks[i]);
}
}
/*
addtask 200
addcmd 0 Y0 1
addcmd 0 PRINT Hello PLC világ!
addcmd 0 IF X0 == 1 THEN Y1 = 1
LEDblink
addtask 500
addcmd 0 Y0 1
addcmd 0 Y0 0
addcmd 0 PRINT LED villog!
LED task1 be
addtask 200
addcmd 0 Y0 1
addcmd 0 Y0 0
Print taks2 be
addtask 1000
addcmd 1 PRINT Task 1 fut!
Feltétel task3 ba
addtask 100
addcmd 2 IF X0 == 1 THEN Y1 = 1
addcmd 2 IF X0 == 0 THEN Y1 = 0
*/