HansonServo/nodegraph.cpp

138 lines
4.0 KiB
C++

#include "nodegraph.h"
#include <cstring>
// CurveNode evaluation
void CurveNode::evaluate(uint32_t tick) {
//outputValue = getCurveValue(curveID, tick);
}
// ServoNode evaluation
void ServoNode::evaluate(uint32_t tick) {
//setMotorPWM(motorID, inputValue);
}
// NodeGraph tick
void NodeGraph::tick(uint32_t currentTick) {
// First pass: evaluate all nodes
for (Node* node : nodes) {
node->evaluate(currentTick);
}
// Optional: if you want to simulate wiring between nodes,
// you could add a second pass here to propagate values
}
void loadNodeGraph(const uint8_t* packet, size_t length, NodeGraph& graph) {
size_t offset = 0;
// Read node count
uint16_t nodeCount = packet[offset];
offset += 1;
// Parse nodes
for (uint16_t i = 0; i < nodeCount; ++i) {
if (offset + 6 > length) break; // safety check
uint8_t type = packet[offset++];
uint8_t id = packet[offset++];
uint16_t x = packet[offset] | (packet[offset + 1] << 8); offset += 2;
uint16_t y = packet[offset] | (packet[offset + 1] << 8); offset += 2;
Node* node = nullptr;
switch (type) {
case TYPE_CURVENODE: { // CurveNode
if (offset + 1 > length) break;
uint8_t curveID = packet[offset++];
auto* curve = new CurveNode();
curve->id = id;
curve->type = type;
curve->x = x;
curve->y = y;
curve->curveID = curveID;
node = curve;
break;
}
case TYPE_SERVONODE: { // ServoNode
if (offset + 1 > length) break;
uint8_t motorID = packet[offset++];
auto* servo = new ServoNode();
servo->id = id;
servo->type = type;
servo->x = x;
servo->y = y;
servo->motorID = motorID;
node = servo;
break;
}
case TYPE_NOISENODE: { // NoiseNode
if (offset + 17 > length) break;
offset += 17; // skip for now
break;
}
default:
break;
}
if (node) {
graph.nodes.push_back(node);
}
}
// Parse connections
if (offset + 2 > length) return;
uint16_t connectionCount = packet[offset];
offset += 1;
for (uint16_t i = 0; i < connectionCount; ++i) {
if (offset + 2 > length) break;
uint8_t fromID = packet[offset++];
uint8_t toID = packet[offset++];
graph.connections.push_back({fromID, toID});
}
}
String printNodeGraph(const NodeGraph& graph) {
String output = "📦 NodeGraph Dump\n";
output += "Nodes:\n";
for (const Node* node : graph.nodes) {
output += " ID " + String(node->id);
output += " | Type " + String(node->type);
output += " | Pos (" + String(node->x) + ", " + String(node->y) + ")";
switch (node->type) {
case TYPE_CURVENODE: { // CurveNode
const CurveNode* curve = static_cast<const CurveNode*>(node);
output += " | CurveID " + String(curve->curveID);
break;
}
case TYPE_SERVONODE: { // ServoNode
const ServoNode* servo = static_cast<const ServoNode*>(node);
output += " | MotorID " + String(servo->motorID);
break;
}
// case 2: { // NoiseNode
// const NoiseNode* noise = static_cast<const NoiseNode*>(node);
// output += " | Noise a=" + String(noise->a, 2);
// output += " b=" + String(noise->b, 2);
// output += " c=" + String(noise->c, 2);
// output += " d=" + String(noise->d, 2);
// output += " seed=" + String(noise->seed);
// break;
// }
}
output += "\n";
}
output += "Connections:\n";
for (const auto& conn : graph.connections) {
output += " " + String(conn.fromID) + "" + String(conn.toID) + "\n";
}
return output;
}