Skip to content
Snippets Groups Projects
Commit f47b91f1 authored by Cyril Danilevski's avatar Cyril Danilevski :scooter:
Browse files

WIP: Configurable power stages

parent d80b9d93
No related branches found
No related tags found
No related merge requests found
Pipeline #163113 passed
......@@ -3,7 +3,9 @@
#include "mpod.hpp"
PowerProcedure::PowerProcedure(const IPAddress& ipAddr)
: ipAddr(ipAddr), currentStage(""), currentChannel(-1) {
: ipAddr(ipAddr), stagesCount(STAGESCOUNT), currentStage(""), currentChannel(-1) {
stages = new Stage[stagesCount];
stages[0].name = "ASICS";
stages[0].size = 4;
stages[0].channels = new int[stages[0].size]{1, 2, 3, 8};
......@@ -14,10 +16,10 @@ PowerProcedure::PowerProcedure(const IPAddress& ipAddr)
}
PowerProcedure::~PowerProcedure() {
size_t stagesCount = sizeof(stages) / sizeof(stages[0]);
for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
delete[] stages[stageIdx].channels;
}
delete[] stages;
}
String PowerProcedure::toJSON() {
......@@ -25,9 +27,9 @@ String PowerProcedure::toJSON() {
json += "\"target\":\"" + ipAddr.toString() + "\",\n";
json += "\"groups\": [\n";
Stage* currentStage = stages;
Stage* lastStage = stages + sizeof(stages) / sizeof(stages[0]);
for (currentStage; currentStage < lastStage; currentStage++) {
for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
Stage* currentStage = &stages[stageIdx];
json += "{\"name\":\"" + currentStage->name + "\",";
json += "\"channels\":[\n";
......@@ -47,17 +49,18 @@ String PowerProcedure::toJSON() {
json += "]\n";
json += "},\n";
}
json.remove(json.lastIndexOf(','), 1); // Remove trailing comma of last entry
json += "]\n"; // groups body
json += "}"; // json body
return json;
}
bool PowerProcedure::powerOn(const String& stage) {
Stage* currentStage = stages;
Stage* lastStage = stages + sizeof(stages) / sizeof(stages[0]);
for (currentStage; currentStage < lastStage; currentStage++) {
for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
Stage* currentStage = &stages[stageIdx];
if (currentStage->name == stage) {
for (size_t chIdx = 0; chIdx < currentStage->size; chIdx++) {
setChannelAndWait(&ipAddr, currentStage->channels[chIdx], 1); // 1 == ON
......@@ -69,9 +72,8 @@ bool PowerProcedure::powerOn(const String& stage) {
}
bool PowerProcedure::powerOff(const String& stage) {
Stage* currentStage = stages;
Stage* lastStage = stages + sizeof(stages) / sizeof(stages[0]);
for (currentStage; currentStage < lastStage; currentStage++) {
for (size_t stageIdx = 0; stageIdx < stagesCount; stageIdx++) {
Stage* currentStage = &stages[stageIdx];
if (currentStage->name == stage) {
for (int chIdx = currentStage->size - 1; chIdx >= 0; chIdx--) {
setChannelAndWait(&ipAddr, currentStage->channels[chIdx], 0); // 0 == OFF
......
#pragma once
#include <Arduino.h>
#define STAGESCOUNT 2
class PowerProcedure {
private:
String currentStage; // Stores the current stage name
int currentChannel; // Stores the current channel
uint8_t stagesCount; // Stores the quantity of power stages
struct Stage {
String name; // Name of the stage (e.g., "ASICS")
int* channels; // Pointer to dynamically allocated channel array
size_t size; // Number of channels in the stage
};
Stage stages[2]; // Array to hold stages
Stage* stages; // Array to hold stages
public:
const IPAddress ipAddr; // Stores the IP address
const IPAddress ipAddr; // Stores the IP address
PowerProcedure(const IPAddress& ipAddr);
~PowerProcedure();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment