Skip to content

Commit

Permalink
firmware as flow support
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Dec 9, 2021
1 parent 8fe8215 commit 60b29ce
Show file tree
Hide file tree
Showing 36 changed files with 8,372 additions and 8,297 deletions.
Binary file modified scripts/DC Power App/DC Power.app
Binary file not shown.
7,925 changes: 3,962 additions & 3,963 deletions src/bb3/gui/document_simulator.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/bb3/gui/document_simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2860,4 +2860,4 @@ enum PagesEnum {
PAGE_ID_DIB_MUX14D_RELAY_CYCLES = 243
};

extern const uint8_t assets[490104];
extern const uint8_t assets[490093];
8,219 changes: 4,109 additions & 4,110 deletions src/bb3/gui/document_stm32.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/bb3/gui/document_stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -2786,4 +2786,4 @@ enum PagesEnum {
PAGE_ID_DIB_MUX14D_RELAY_CYCLES = 231
};

extern const uint8_t assets[311126];
extern const uint8_t assets[311115];
1 change: 1 addition & 0 deletions src/bb3/gui/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ EEZ_MESSAGE_QUEUE_DECLARE(gui, {
void startThread() {
loadMainAssets(assets, sizeof(assets));
display::onThemeChanged();
guiInit();
mouse::init();
EEZ_MESSAGE_QUEUE_CREATE(gui, 10);
EEZ_THREAD_CREATE(gui, mainLoop);
Expand Down
77 changes: 49 additions & 28 deletions src/eez/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

#include <eez/alloc.h>
#include <eez/os.h>
Expand Down Expand Up @@ -63,13 +64,14 @@ void *alloc(size_t size, uint32_t id) {
return nullptr;
}

size = ((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
if (EEZ_MUTEX_WAIT(alloc, 0)) {
AllocBlock *firstBlock = (AllocBlock *)g_heap;

AllocBlock *first = (AllocBlock *)g_heap;
AllocBlock *block = firstBlock;
size = ((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;

AllocBlock *block = first;

if (EEZ_MUTEX_WAIT(alloc, 0)) {
// find free block with enough size
// TODO merge multiple free consecutive blocks into one that has enough size
while (block) {
if (block->free && block->size >= size) {
break;
Expand All @@ -82,63 +84,82 @@ void *alloc(size_t size, uint32_t id) {
return nullptr;
}

int nextBlockSize = block->size - size - sizeof(AllocBlock);
if (nextBlockSize >= (int)MIN_BLOCK_SIZE) {
auto nextBlock = (AllocBlock *)((uint8_t *)block + sizeof(AllocBlock) + size);
nextBlock->next = block->next;
nextBlock->free = 1;
nextBlock->size = nextBlockSize;
block->next = nextBlock;
int remainingSize = block->size - size - sizeof(AllocBlock);
if (remainingSize >= (int)MIN_BLOCK_SIZE) {
// remainingSize is enough to create a new block
auto newBlock = (AllocBlock *)((uint8_t *)block + sizeof(AllocBlock) + size);
newBlock->free = 1;

auto nextBlock = block->next;
if (nextBlock && nextBlock->free) {
// nextBlock is free, merge newBlock with nextBlock
newBlock->next = nextBlock->next;
newBlock->size = remainingSize + sizeof(AllocBlock) + nextBlock->size;
} else {
newBlock->next = nextBlock;
newBlock->size = remainingSize;
}

block->next = newBlock;
block->size = size;
}

block->free = 0;
block->id = id;

EEZ_MUTEX_RELEASE(alloc);

return block + 1;
}
return block + 1;

return nullptr;
}

void free(void *ptr) {
if (ptr == 0) {
return;
}

AllocBlock *first = (AllocBlock *)g_heap;

AllocBlock *block = first;

if (EEZ_MUTEX_WAIT(alloc, 0)) {
AllocBlock *firstBlock = (AllocBlock *)g_heap;

AllocBlock *prevBlock = nullptr;
AllocBlock *block = firstBlock;

while (block && block + 1 < ptr) {
prevBlock = block;
block = block->next;
}

if (!block || block + 1 != ptr) {
// assert(0);
if (!block || block + 1 != ptr || block->free) {
assert(false);
EEZ_MUTEX_RELEASE(alloc);
return;
}

if (block->next && block->next->free) {
// reset memory to catch errors when memory is used after free is called
memset(ptr, 0xCC, block->size);

auto nextBlock = block->next;
if (nextBlock && nextBlock->free) {
if (prevBlock && prevBlock->free) {
prevBlock->next = block->next->next;
prevBlock->size += sizeof(AllocBlock) + block->size + sizeof(AllocBlock) + block->next->size;
// both next and prev blocks are free, merge 3 blocks into one
prevBlock->next = nextBlock->next;
prevBlock->size += sizeof(AllocBlock) + block->size + sizeof(AllocBlock) + nextBlock->size;
} else {
block->size += sizeof(AllocBlock) + block->next->size;
block->next = block->next->next;
// next block is free, merge 2 blocks into one
block->next = nextBlock->next;
block->size += sizeof(AllocBlock) + nextBlock->size;
block->free = 1;
}
} else if (prevBlock && prevBlock->free) {
prevBlock->next = block->next;
// prev block is free, merge 2 blocks into one
prevBlock->next = nextBlock;
prevBlock->size += sizeof(AllocBlock) + block->size;
} else {
// just free
block->free = 1;
}
block->free = 1;

EEZ_MUTEX_RELEASE(alloc);
}
Expand All @@ -164,7 +185,7 @@ void dumpAlloc(scpi_t *context) {
}
}

void getAllocInfo(int &free, int &alloc) {
void getAllocInfo(uint32_t &free, uint32_t &alloc) {
free = 0;
alloc = 0;
if (EEZ_MUTEX_WAIT(alloc, 0)) {
Expand Down
2 changes: 1 addition & 1 deletion src/eez/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ template<class T> struct ObjectAllocator {

void dumpAlloc(scpi_t *context);

void getAllocInfo(int &free, int &alloc);
void getAllocInfo(uint32_t &free, uint32_t &alloc);

} // eez
13 changes: 11 additions & 2 deletions src/eez/flow/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <eez/flow/private.h>
#include <eez/flow/operations.h>

#include <eez/gui/gui.h>

using namespace eez::gui;

namespace eez {
Expand Down Expand Up @@ -47,8 +49,15 @@ bool evalExpression(FlowState *flowState, int componentIndex, const uint8_t *ins
return false;
}
} else if (instructionType == EXPR_EVAL_INSTRUCTION_TYPE_PUSH_GLOBAL_VAR) {
if (!stack.push(flowDefinition->globalVariables.item(assets, instructionArg))) {
return false;
if ((uint32_t)instructionArg < flowDefinition->globalVariables.count) {
if (!stack.push(flowDefinition->globalVariables.item(assets, instructionArg))) {
return false;
}
} else {
// native variable
if (!stack.push(get(g_widgetCursor, instructionArg + 1))) {
return false;
}
}
} else if (instructionType == EXPR_EVAL_INSTRUCTION_TYPE_PUSH_OUTPUT) {
if (!stack.push(Value((uint16_t)instructionArg, VALUE_TYPE_FLOW_OUTPUT))) {
Expand Down
4 changes: 1 addition & 3 deletions src/eez/flow/flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ void tick() {
if (--flowState->numActiveComponents == 0 && flowState->isAction) {
auto componentExecutionState = flowState->parentFlowState->componenentExecutionStates[flowState->parentComponentIndex];
if (componentExecutionState) {
ObjectAllocator<ComponenentExecutionState>::deallocate(componentExecutionState);
flowState->parentFlowState->componenentExecutionStates[flowState->parentComponentIndex] = nullptr;
ObjectAllocator<ComponenentExecutionState>::deallocate(componentExecutionState);
} else {
throwError(flowState, componentIndex, "Unexpected: no CallAction component state\n");
return;
Expand Down Expand Up @@ -142,8 +142,6 @@ FlowState *getFlowState(int16_t pageId, const WidgetCursor &widgetCursor) {
return nullptr;
}

pageId = -pageId - 1;

if (widgetCursor.widget && widgetCursor.widget->type == WIDGET_TYPE_LAYOUT_VIEW) {
if (widgetCursor.flowState) {
auto layoutViewWidget = (LayoutViewWidget *)widgetCursor.widget;
Expand Down
47 changes: 25 additions & 22 deletions src/eez/flow/operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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

#include <eez/flow/operations.h>
Expand All @@ -32,7 +31,12 @@ Value op_add(const Value& a1, const Value& b1) {
if (a.isAnyStringType() || b.isAnyStringType()) {
Value value1 = a.toString(0x84eafaa8);
Value value2 = b.toString(0xd273cab6);
return Value::concatenateString(value1.getString(), value2.getString());
auto res = Value::concatenateString(value1, value2);

char str1[128];
res.toText(str1, sizeof(str1));

return res;
}

if (a.isDouble() || b.isDouble()) {
Expand Down Expand Up @@ -816,16 +820,16 @@ bool do_OPERATION_TYPE_STRING_FIND(EvalStack &stack) {
b = *b.pValueValue;
}

const char *aStr = a.toString(0xf616bf4d).getString();
const char *bStr = b.toString(0x81229133).getString();
if (!aStr || !bStr) {
Value aStr = a.toString(0xf616bf4d);
Value bStr = b.toString(0x81229133);
if (!aStr.getString() || !bStr.getString()) {
if (!stack.push(Value(-1, VALUE_TYPE_INT32))) {
return false;
}
} else {
const char *pos = strstr(aStr, bStr);
const char *pos = strstr(aStr.getString(), bStr.getString());
if (!pos) {
if (!stack.push(Value(pos - aStr, VALUE_TYPE_INT32))) {
if (!stack.push(Value(pos - aStr.getString(), VALUE_TYPE_INT32))) {
return false;
}
} else {
Expand Down Expand Up @@ -855,27 +859,26 @@ bool do_OPERATION_TYPE_STRING_PAD_START(EvalStack &stack) {
c = *c.pValueValue;
}

const char *str = a.toString(0xcf6aabe6).getString();
if (!str) {
auto str = a.toString(0xcf6aabe6);
if (!str.getString()) {
return false;
}
int strLen = strlen(str);
int strLen = strlen(str.getString());

int targetLength;
if (b.isInt32OrLess()) {
targetLength = b.int32Value;
if (targetLength < strLen) {
targetLength = strLen;
}
} else {
int err;
int targetLength = b.toInt32(&err);
if (err) {
return false;
}
if (targetLength < strLen) {
targetLength = strLen;
}

const char *padStr = c.toString(0x81353bd7).getString();
if (!padStr) {
auto padStr = c.toString(0x81353bd7);
if (!padStr.getString()) {
return false;
}
int padStrLen = strlen(padStr);
int padStrLen = strlen(padStr.getString());

Value resultValue = eez::gui::Value::makeStringRef("", targetLength, 0xf43b14dd);
if (resultValue.type == VALUE_TYPE_NULL) {
Expand All @@ -884,10 +887,10 @@ bool do_OPERATION_TYPE_STRING_PAD_START(EvalStack &stack) {
char *resultStr = (char *)resultValue.getString();

auto n = targetLength - strLen;
stringCopy(resultStr + (targetLength - strLen), targetLength, str);
stringCopy(resultStr + (targetLength - strLen), strLen + 1, str.getString());

for (int i = 0; i < n; i++) {
resultStr[i] = padStr[i % padStrLen];
resultStr[i] = padStr.getString()[i % padStrLen];
}

if (!stack.push(resultValue)) {
Expand Down
2 changes: 1 addition & 1 deletion src/eez/flow/private.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static FlowState *initFlowState(Assets *assets, int flowIndex, FlowState *parent
flowState->componenentExecutionStates = (ComponenentExecutionState **)(flowState->values + nValues);

for (unsigned i = 0; i < nValues; i++) {
flowState->values[i].clear();
new (flowState->values + i) Value();
}

auto &undefinedValue = *flowDefinition->constants.item(assets, UNDEFINED_VALUE_INDEX);
Expand Down
2 changes: 0 additions & 2 deletions src/eez/gui/app_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <memory.h>

#include <eez/conf.h>
#include <eez/gui_conf.h>
#include <eez/sound.h>
#include <eez/os.h>
#include <eez/util.h>
Expand All @@ -35,7 +34,6 @@
#endif

#include <eez/gui/gui.h>
#include <eez/gui_conf.h>
#include <eez/gui/widgets/button.h>

#include <eez/hmi.h>
Expand Down
6 changes: 4 additions & 2 deletions src/eez/gui/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

#include <eez/gui/gui.h>
#include <eez/gui/widget.h>
#include <eez/gui_conf.h>

#include <scpi/scpi.h>

Expand Down Expand Up @@ -131,9 +130,12 @@ const PageAsset* getPageAsset(int pageId) {
const PageAsset* getPageAsset(int pageId, WidgetCursor& widgetCursor) {
if (pageId < 0) {
widgetCursor.assets = g_externalAssets;
widgetCursor.flowState = flow::getFlowState(pageId, widgetCursor);
widgetCursor.flowState = flow::getFlowState(-pageId - 1, widgetCursor);
} else {
widgetCursor.assets = g_mainAssets;
if (g_mainAssets->flowDefinition) {
widgetCursor.flowState = flow::getFlowState(pageId - 1, widgetCursor);
}
}
return getPageAsset(pageId);
}
Expand Down
4 changes: 4 additions & 0 deletions src/eez/gui/assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ struct AssetsPtr {
offset = (uint8_t *)ptr - ((uint8_t *)g_mainAssets + 4);
}

explicit operator bool() const {
return offset != 0;
}

private:
uint32_t offset;
};
Expand Down
Loading

0 comments on commit 60b29ce

Please sign in to comment.