Skip to content

Commit

Permalink
Merge branch 'noParamTryout'
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Oct 10, 2023
2 parents a17bf19 + c3824f0 commit 5cd3257
Show file tree
Hide file tree
Showing 19 changed files with 262 additions and 171 deletions.
2 changes: 1 addition & 1 deletion Firmware/Bentuino/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
"FILES_DEFAULT_SD_SCK=14",
"FILES_DEFAULT_SD_CS=15",
"USE_IMU",
"BEHAVIOUR_MAX_COUNT=2",
"BEHAVIOUR_MAX_COUNT=4",
"ESP32",
"ESP_PLATFORM",
"F_CPU=240000000L",
Expand Down
2 changes: 1 addition & 1 deletion Firmware/Bentuino/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ build_flags =

-D USE_IMU

-D BEHAVIOUR_MAX_COUNT=2
-D BEHAVIOUR_MAX_COUNT=4
4 changes: 2 additions & 2 deletions Firmware/Bentuino/server/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ <h1>Bentuino Settings</h1>
<button class="btn" type="submit">Upload</button>
</form>
</div>
<div id="editor"></div>
<div id="editor">Loading Data...</div>

<script>

var local = window.location.hostname.startsWith("192");
var ip = local ? window.location.hostname : "192.168.1.162";
connectToServer();
setTimeout(() => {connectToServer();}, 100);
var data = {};

var oscWS;
Expand Down
153 changes: 96 additions & 57 deletions Firmware/Bentuino/src/Component/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ void Component::update()
if (!enabled.boolValue())
return;

// NDBG("Update");
for (int i = 0; i < numComponents; i++)
components[i]->update();

Expand All @@ -44,22 +43,30 @@ void Component::clear()

void Component::addParameter(Parameter *p)
{
if (numParameters >= MAX_CHILD_PARAMETERS)
{
NDBG("Parameter limit reached ! Trying to add " + p->name);
return;
}

parameters[numParameters] = p;
numParameters++;
AddDefaultParameterListener(Component, p);
// DBG("Parameter added, size = " + String(sizeof(*p)) + " (" + String(sizeof(Parameter)) + ")");
}

Parameter *Component::addParameter(const String &name, var val, var minVal, var maxVal, bool isConfig)
{
Parameter *p = new Parameter(name, val, minVal, maxVal, isConfig);
addParameter(p);
return p;
}
// Parameter *Component::addParameter(const String &name, var val, var minVal, var maxVal, bool isConfig)
// {
// Parameter *p = new Parameter(name, val, minVal, maxVal, isConfig);
// addParameter(p);

Parameter *Component::addConfigParameter(const String &name, var val, var minVal, var maxVal)
{
return addParameter(name, val, minVal, maxVal, true);
}
// return p;
// }

// Parameter *Component::addConfigParameter(const String &name, var val, var minVal, var maxVal)
// {
// return addParameter(name, val, minVal, maxVal, true);
// }

void Component::onParameterEvent(const ParameterEvent &e)
{
Expand Down Expand Up @@ -169,76 +176,91 @@ void Component::fillOSCQueryData(JsonObject o, bool includeConfig, bool recursiv
}
}

String Component::getChunkedOSCQueryData(OSCQueryChunkType type, int componentIndex) const
void Component::fillChunkedOSCQueryData(OSCQueryChunk *chunk)
{
DynamicJsonDocument doc(16000);
JsonObject o = doc.to<JsonObject>();

const String fullPath = getFullPath();

switch (type)
switch (chunk->nextType)
{
case Start:
{
o["DESCRIPTION"] = name;
o["FULL_PATH"] = fullPath;
o["ACCESS"] = 0;
JsonObject contents = o.createNestedObject("CONTENTS");
chunk->data = "{\"DESCRIPTION\":\"" + name + "\"," +
"\"FULL_PATH\":\"" + fullPath + "\"," +
"\"ACCESS\":0," +
"\"CONTENTS\":{";

for (int i = 0; i < numParameters; i++)
{
DynamicJsonDocument doc(500);
JsonObject o = doc.to<JsonObject>();
Parameter *p = parameters[i];
JsonObject po = contents.createNestedObject(p->name);
p->fillOSCQueryData(po);
po["FULL_PATH"] = fullPath + "/" + p->name;
p->fillOSCQueryData(o);
o["FULL_PATH"] = fullPath + "/" + p->name;
String str;
serializeJson(o, str);
chunk->data += "\"" + p->name + "\":" + str;
if (i < numParameters - 1)
chunk->data += ",";
}

String str;
serializeJson(o, str);
str.remove(str.length() - 2); // remove CONTENTS } and end }

if (numParameters > 0)
str += ",";

return str;
if (numComponents > 0)
{
chunk->nextType = Start;
chunk->nextComponent = components[0];
if (numParameters > 0)
chunk->data += ",";
chunk->data += "\""+components[0]->name + "\":";
}
else
{
chunk->data += "}";
chunk->nextComponent = (Component *)this;
chunk->nextType = End;
}
}
break;

case Content:
case End:
{
for (int i = 0; i < numComponents; i++)
chunk->data = "}";
if (parentComponent != nullptr)
{
if (componentIndex >= 0 && i != componentIndex)
continue;

if (components[i] == nullptr)
continue;

Component *c = components[i];
JsonObject co = o.createNestedObject(c->name);
c->fillOSCQueryData(co);
parentComponent->setupChunkAfterComponent(chunk, this);
}

String str;
serializeJson(o, str);

str.remove(0, 1);
str.remove(str.length() - 1);

if (componentIndex == -1 || componentIndex == numComponents - 1)
str += "}";
else
str += ",";

return str;
chunk->nextComponent = nullptr;
}

case End:
return "}";

default:
break;
}
}

void Component::setupChunkAfterComponent(OSCQueryChunk *chunk, const Component *c)
{
int index = 0;
for (int i = 0; i < numComponents; i++)
{
if (components[i] == c)
{
index = i;
break;
}
}

return "";
if (index < numComponents - 1) // last one
{
chunk->data += ",\"" + components[index + 1]->name + "\":";
chunk->nextComponent = components[index + 1];
chunk->nextType = Start;
}
else
{
chunk->data += "}";
chunk->nextComponent = this;
chunk->nextType = End;
}
}

String Component::getFullPath(bool includeRoot, bool scriptMode) const
Expand Down Expand Up @@ -279,6 +301,23 @@ void Component::linkScriptFunctions(IM3Module module, bool isLocal)
}
}

Component *Component::addComponent(Component *c, JsonObject o)
{
if (numComponents >= MAX_CHILD_COMPONENTS)
{
NDBG("Component limit reached ! Trying to add " + c->name);
return nullptr;
}

components[numComponents] = (Component *)c;
c->parentComponent = this;
AddDefaultComponentListener(c);
numComponents++;
c->init(o);
// DBG("Component added, size = " + String(sizeof(*c)) + " (" + String(sizeof(Component)) + ")");
return c;
}

Component *Component::getComponentWithName(const String &name)
{
if (name == this->name)
Expand Down
49 changes: 28 additions & 21 deletions Firmware/Bentuino/src/Component/Component.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#define MAX_CHILD_COMPONENTS 16
#define MAX_CHILD_PARAMETERS 16
//#define MAX_EVENT_TYPES 16
#define MAX_CHILD_COMPONENTS 12
#define MAX_CHILD_PARAMETERS 12
// #define MAX_EVENT_TYPES 16

class Component : public EventBroadcaster<ComponentEvent>
{
Expand All @@ -21,7 +21,7 @@ class Component : public EventBroadcaster<ComponentEvent>
String name;

bool isInit;
Parameter enabled{"enabled", true,var(), var(),true};
DeclareConfigParameter(enabled, true);

Component *parentComponent;

Expand Down Expand Up @@ -51,24 +51,15 @@ class Component : public EventBroadcaster<ComponentEvent>
template <class T>
T *addComponent(const String &name, bool _enabled, JsonObject o = JsonObject()) { return addComponent(new T(name, _enabled), o); };

template <class T>
T *addComponent(T *c, JsonObject o = JsonObject())
{
components[numComponents] = (Component *)c;
c->parentComponent = this;
AddDefaultComponentListener(c);
numComponents++;
c->init(o);
return c;
}
Component *addComponent(Component *c, JsonObject o = JsonObject());

Component *getComponentWithName(const String &name);

void addParameter(Parameter* param);
Parameter *addParameter(const String &name, var val, var minVal = var(), var maxVal = var(), bool isConfig = false);
Parameter *addConfigParameter(const String &name, var val, var minVal = var(), var maxVal = var()); // helpers for non ranged config param declaration simplification
Parameter * getParameterWithName(const String &name);
void addParameter(Parameter *param);
// Parameter *addParameter(const String &name, var val, var minVal = var(), var maxVal = var(), bool isConfig = false);
// Parameter *addConfigParameter(const String &name, var val, var minVal = var(), var maxVal = var()); // helpers for non ranged config param declaration simplification
Parameter *getParameterWithName(const String &name);

virtual void onParameterEvent(const ParameterEvent &e);
virtual void onEnabledChanged() {}
virtual void onParameterEventInternal(const ParameterEvent &e) {}
Expand All @@ -80,8 +71,24 @@ class Component : public EventBroadcaster<ComponentEvent>
virtual void fillSettingsData(JsonObject o, bool configOnly = false);
virtual void fillOSCQueryData(JsonObject o, bool includeConfig = true, bool recursive = true);

enum OSCQueryChunkType { Start, Content, End, ChunkTypeMax};
virtual String getChunkedOSCQueryData(OSCQueryChunkType type, int componentIndex = -1) const;
enum OSCQueryChunkType
{
Start,
Content,
End,
ChunkTypeMax
};

struct OSCQueryChunk
{
OSCQueryChunk(Component* c, OSCQueryChunkType t = Start, String d = "") :nextComponent(c), nextType(t), data(d) {}
Component *nextComponent = nullptr;
OSCQueryChunkType nextType = Start;
String data = "";
};

void fillChunkedOSCQueryData(OSCQueryChunk *chunk);
void setupChunkAfterComponent(OSCQueryChunk *result, const Component *c);

String getFullPath(bool includeRoot = false, bool scriptMode = false) const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ void BehaviourComponent::onParameterEventInternal(const ParameterEvent &e)

void BehaviourComponent::onTargetParameterChanged(const ParameterEvent &e)
{
DBG("Target Parameter Changed");
bool isValid = false;

Comparator cmp = (Comparator)comparator.intValue();
Expand Down Expand Up @@ -114,7 +113,6 @@ void BehaviourComponent::onTargetParameterChanged(const ParameterEvent &e)
{
if (isValid)
{
DBG("Behaviour " + name + " is valid");
delayedValidation = false;
timeAtValidation = millis() / 1000.0f;
if (validationTime.floatValue() == 0)
Expand All @@ -129,8 +127,6 @@ void BehaviourComponent::onTargetParameterChanged(const ParameterEvent &e)

void BehaviourComponent::trigger()
{
DBG("Trigger Behaviour " + name);

Action a = (Action)triggerAction.intValue();
switch (a)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void OSCComponent::setupConnection()
udpIsInit = false;
if (shouldConnect)
{
// NDBG("Start OSC Receiver on " + String(OSC_LOCAL_PORT));
NDBG("Start OSC Receiver on " + String(OSC_LOCAL_PORT));
udp.begin(OSC_LOCAL_PORT);
udp.flush();
isAlive.set(true);
Expand Down
22 changes: 22 additions & 0 deletions Firmware/Bentuino/src/Component/components/io/IOComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,26 @@ int IOComponent::getFirstAvailablePWMChannel() const
return i;
}
return -1;
}


//Manager

ImplementSingleton(IOManagerComponent);

bool IOManagerComponent::initInternal(JsonObject o)
{
AddAndSetParameter(numIOs);

for (int i = 0; i < numIOs.intValue(); i++)
{
DBG("Add io " + String(i + 1) + " of " + String(numIOs.intValue()));
ios[i].name = "io" + String(i + 1);
AddOwnedComponent(&ios[i]);
}

// initialize static io here
memset(IOComponent::availablePWMChannels, true, sizeof(IOComponent::availablePWMChannels));

return true;
}
Loading

0 comments on commit 5cd3257

Please sign in to comment.