Skip to content

Commit

Permalink
switch action component
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Aug 3, 2021
1 parent f7a934e commit 5d018ef
Show file tree
Hide file tree
Showing 18 changed files with 404 additions and 95 deletions.
14 changes: 8 additions & 6 deletions src/eez/flow/components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

#define FLOW_DEBUG 0

using namespace eez::gui;

namespace eez {
Expand All @@ -39,6 +37,8 @@ void executeEndComponent(Assets *assets, FlowState *flowState, Component *compon
void executeDelayComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState);
void executeConstantComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState);
void executeSetVariableComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState);
void executeSwitchComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState);
void executeLogComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState);
void executeScpiComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState);

void executeComponent(Assets *assets, FlowState *flowState, unsigned componentIndex, ComponenentExecutionState *componentExecutionState) {
Expand All @@ -56,12 +56,14 @@ void executeComponent(Assets *assets, FlowState *flowState, unsigned componentIn
executeConstantComponent(assets, flowState, component, componentExecutionState);
} else if (component->type == defs_v3::COMPONENT_TYPE_SET_VARIABLE_ACTION) {
executeSetVariableComponent(assets, flowState, component, componentExecutionState);
} else if (component->type == defs_v3::COMPONENT_TYPE_SCPI_ACTION) {
} else if (component->type == defs_v3::COMPONENT_TYPE_SWITCH_ACTION) {
executeSwitchComponent(assets, flowState, component, componentExecutionState);
} else if (component->type == defs_v3::COMPONENT_TYPE_LOG_ACTION) {
executeLogComponent(assets, flowState, component, componentExecutionState);
} else if (component->type == defs_v3::COMPONENT_TYPE_SCPI_ACTION) {
executeScpiComponent(assets, flowState, component, componentExecutionState);
} else {
#if FLOW_DEBUG
printf("Unknown component at index = %d, type = %d\n", componentIndex, component->type);
#endif
DebugTrace("Unknown component at index = %d, type = %d\n", componentIndex, component->type);
}

if (componentExecutionState) {
Expand Down
5 changes: 0 additions & 5 deletions src/eez/flow/components/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

#define FLOW_DEBUG 0

using namespace eez::gui;

namespace eez {
namespace flow {

void executeConstantComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState) {
#if FLOW_DEBUG
printf("Execute CONSTANT component at index = %d\n", componentIndex);
#endif
auto flowDefinition = assets->flowDefinition.ptr(assets);

struct ConstantActionComponent : public Component {
Expand Down
10 changes: 2 additions & 8 deletions src/eez/flow/components/delay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

#define FLOW_DEBUG 0

using namespace eez::gui;

namespace eez {
namespace flow {

void executeDelayComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState) {
#if FLOW_DEBUG
printf("Execute DELAY component at index = %d\n", componentIndex);
#endif

struct DelayComponenentExecutionState : public ComponenentExecutionState {
uint32_t waitUntil;
};
Expand All @@ -48,7 +42,7 @@ void executeDelayComponent(Assets *assets, FlowState *flowState, Component *comp

Value value;
if (!evalExpression(assets, flowState, propertyValue->evalInstructions, value)) {
throwError("delay component milliseconds eval error");
throwError("delay component milliseconds eval error\n");
return;
}

Expand All @@ -58,7 +52,7 @@ void executeDelayComponent(Assets *assets, FlowState *flowState, Component *comp
delayComponentExecutionState->waitUntil = millis() + (uint32_t)floor(milliseconds);
componentExecutionState = delayComponentExecutionState;
} else {
throwError("delay component milliseconds invalid value");
throwError("delay component milliseconds invalid value\n");
return;
}
} else {
Expand Down
6 changes: 0 additions & 6 deletions src/eez/flow/components/end.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

#define FLOW_DEBUG 0

using namespace eez::gui;

namespace eez {
namespace flow {

void executeEndComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState) {
#if FLOW_DEBUG
printf("Execute END component at index = %d\n", componentIndex);
#endif

scripting::stopScript();
}

Expand Down
54 changes: 54 additions & 0 deletions src/eez/flow/components/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* EEZ Modular Firmware
* Copyright (C) 2021-present, Envox d.o.o.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <math.h>

#include <eez/alloc.h>
#include <eez/system.h>
#include <eez/scripting/scripting.h>

#include <eez/flow/components.h>
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

using namespace eez::gui;

namespace eez {
namespace flow {

void executeLogComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState) {
auto propertyValue = component->propertyValues.item(assets, defs_v3::LOG_ACTION_COMPONENT_PROPERTY_VALUE);

Value value;
if (!evalExpression(assets, flowState, propertyValue->evalInstructions, value)) {
throwError("log component value eval error\n");
return;
}

const char *valueStr = value.toString(assets);
if (valueStr && *valueStr) {
DebugTrace(valueStr);
if (valueStr[strlen(valueStr) - 1] != '\n') {
DebugTrace("\n");
}
}
}

} // namespace flow
} // namespace eez
31 changes: 3 additions & 28 deletions src/eez/flow/components/scpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

#define FLOW_DEBUG 0

using namespace eez::gui;

namespace eez {
Expand Down Expand Up @@ -76,10 +74,6 @@ void scpiResultIsReady() {
}

void executeScpiComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState) {
#if FLOW_DEBUG
printf("Execute SCPI component at index = %d\n", componentIndex);
#endif

struct ScpiActionComponent : public Component {
uint8_t instructions[1];
};
Expand All @@ -106,9 +100,6 @@ void executeScpiComponent(Assets *assets, FlowState *flowState, Component *compo

while (true) {
if (scpiComponentExecutionState->op == SCPI_PART_STRING) {
#if FLOW_DEBUG
printf("SCPI_PART_STRING\n");
#endif
uint16_t sizeLowByte = instructions[scpiComponentExecutionState->instructionIndex++];
uint16_t sizeHighByte = instructions[scpiComponentExecutionState->instructionIndex++];
uint16_t stringLength = sizeLowByte | (sizeHighByte << 8);
Expand All @@ -120,14 +111,10 @@ void executeScpiComponent(Assets *assets, FlowState *flowState, Component *compo
);
scpiComponentExecutionState->instructionIndex += stringLength;
} else if (scpiComponentExecutionState->op == SCPI_PART_EXPR) {
#if FLOW_DEBUG
printf("SCPI_PART_EXPR\n");
#endif

Value value;
int numInstructionBytes;
if (!evalExpression(assets, flowState, instructions + scpiComponentExecutionState->instructionIndex, value, &numInstructionBytes)) {
throwError("scpi component eval assignable expression");
throwError("scpi component eval assignable expression\n");
return;
}
scpiComponentExecutionState->instructionIndex += numInstructionBytes;
Expand All @@ -141,9 +128,6 @@ void executeScpiComponent(Assets *assets, FlowState *flowState, Component *compo
valueStr
);
} else if (scpiComponentExecutionState->op == SCPI_PART_QUERY_WITH_ASSIGNMENT) {
#if FLOW_DEBUG
printf("SCPI_PART_QUERY_WITH_ASSIGNMENT\n");
#endif
if (!scpiComponentExecutionState->scpi()) {
break;
}
Expand All @@ -153,15 +137,15 @@ void executeScpiComponent(Assets *assets, FlowState *flowState, Component *compo
int err;
if (!scripting::getLatestScpiResult(&resultText, &resultTextLen, &err)) {
char errorMessage[256];
snprintf(errorMessage, sizeof(errorMessage), "scpi component error: '%s', %s", scpiComponentExecutionState->commandOrQueryText, SCPI_ErrorTranslate(err));
snprintf(errorMessage, sizeof(errorMessage), "scpi component error: '%s', %s\n", scpiComponentExecutionState->commandOrQueryText, SCPI_ErrorTranslate(err));
throwError(errorMessage);
return;
}

Value dstValue;
int numInstructionBytes;
if (!evalAssignableExpression(assets, flowState, instructions + scpiComponentExecutionState->instructionIndex, dstValue, &numInstructionBytes)) {
throwError("scpi component eval assignable expression");
throwError("scpi component eval assignable expression\n");
return;
}
scpiComponentExecutionState->instructionIndex += numInstructionBytes;
Expand All @@ -172,26 +156,17 @@ void executeScpiComponent(Assets *assets, FlowState *flowState, Component *compo

assignValue(assets, flowState, component, dstValue, srcValue);
} else if (scpiComponentExecutionState->op == SCPI_PART_QUERY) {
#if FLOW_DEBUG
printf("SCPI_PART_QUERY\n");
#endif
if (!scpiComponentExecutionState->scpi()) {
break;
}
scpiComponentExecutionState->commandOrQueryText[0] = 0;
} else if (scpiComponentExecutionState->op == SCPI_PART_COMMAND) {
#if FLOW_DEBUG
printf("SCPI_PART_COMMAND\n");
#endif
if (!scpiComponentExecutionState->scpi()) {
break;
}
scpiComponentExecutionState->commandOrQueryText[0] = 0;

} else if (scpiComponentExecutionState->op == SCPI_PART_END) {
#if FLOW_DEBUG
printf("SCPI_PART_END\n");
#endif
ObjectAllocator<ScpiComponentExecutionState>::deallocate(scpiComponentExecutionState);
componentExecutionState = nullptr;

Expand Down
10 changes: 2 additions & 8 deletions src/eez/flow/components/set_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,27 @@
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

#define FLOW_DEBUG 0

using namespace eez::gui;

namespace eez {
namespace flow {

void executeSetVariableComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState) {
#if FLOW_DEBUG
printf("Execute SetVariable component at index = %d\n", componentIndex);
#endif

struct SetVariableActionComponent : public Component {
uint8_t assignableExpressionEvalInstructions[1];
};
auto setVariableActionComponent = (SetVariableActionComponent *)component;

Value dstValue;
if (!evalAssignableExpression(assets, flowState, setVariableActionComponent->assignableExpressionEvalInstructions, dstValue)) {
throwError("setvariable component eval dest assignable expression");
throwError("setvariable component eval dest assignable expression\n");
return;
}

auto propertyValue = component->propertyValues.item(assets, defs_v3::SET_VARIABLE_ACTION_COMPONENT_PROPERTY_VALUE);
Value srcValue;
if (!evalExpression(assets, flowState, propertyValue->evalInstructions, srcValue)) {
throwError("setvariable component eval src expression");
throwError("setvariable component eval src expression\n");
return;
}

Expand Down
5 changes: 0 additions & 5 deletions src/eez/flow/components/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

#define FLOW_DEBUG 0

using namespace eez::gui;

namespace eez {
namespace flow {

void executeStartComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState) {
#if FLOW_DEBUG
printf("Execute START component at index = %d\n", componentIndex);
#endif
}

} // namespace flow
Expand Down
67 changes: 67 additions & 0 deletions src/eez/flow/components/switch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* EEZ Modular Firmware
* Copyright (C) 2021-present, Envox d.o.o.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <math.h>

#include <eez/alloc.h>
#include <eez/system.h>
#include <eez/scripting/scripting.h>

#include <eez/flow/components.h>
#include <eez/flow/flow_defs_v3.h>
#include <eez/flow/queue.h>

using namespace eez::gui;

namespace eez {
namespace flow {

void executeSwitchComponent(Assets *assets, FlowState *flowState, Component *component, ComponenentExecutionState *&componentExecutionState) {
struct SwitchTest {
uint16_t outputIndex;
uint8_t conditionInstructions[1];
};

struct SwitchActionComponent : public Component {
ListOfAssetsPtr<SwitchTest> tests;
};

auto switchActionComponent = (SwitchActionComponent *)component;

for (uint32_t testIndex = 0; testIndex < switchActionComponent->tests.count; testIndex++) {
auto test = switchActionComponent->tests.item(assets, testIndex);

Value conditionValue;
if (!evalExpression(assets, flowState, test->conditionInstructions, conditionValue)) {
throwError("switch component eval src expression\n");
return;
}

if (conditionValue.getBoolean()) {
auto flowDefinition = assets->flowDefinition.ptr(assets);
auto &nullValue = *flowDefinition->constants.item(assets, NULL_VALUE_INDEX);
auto &componentOutput = *component->outputs.item(assets, test->outputIndex);
propagateValue(assets, flowState, componentOutput, nullValue);
break;
}
}
}

} // namespace flow
} // namespace eez
Loading

0 comments on commit 5d018ef

Please sign in to comment.