|
16 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | */ |
18 | 18 |
|
| 19 | +#include <stdio.h> |
| 20 | + |
19 | 21 | #include <eez/flow.h> |
20 | 22 | #include <eez/flow_defs_v3.h> |
21 | 23 |
|
22 | | -#include <eez/gui/assets.h> |
| 24 | +using namespace eez::gui; |
23 | 25 |
|
24 | 26 | namespace eez { |
25 | 27 | namespace flow { |
26 | 28 |
|
27 | | -void run() { |
| 29 | +static Assets *g_assets; |
| 30 | + |
| 31 | +static const unsigned QUEUE_SIZE = 100; |
| 32 | +static struct { |
| 33 | + unsigned flowIndex; |
| 34 | + unsigned componentIndex; |
| 35 | +} g_queue[QUEUE_SIZE]; |
| 36 | +static unsigned g_queueHead; |
| 37 | +static unsigned g_queueTail; |
| 38 | +static bool g_queueIsFull = false; |
| 39 | + |
| 40 | +void addToQueue(unsigned flowIndex, unsigned componentIndex) { |
| 41 | + g_queue[g_queueTail].flowIndex = flowIndex; |
| 42 | + g_queue[g_queueTail].componentIndex = componentIndex; |
| 43 | + |
| 44 | + g_queueTail = (g_queueTail + 1) % QUEUE_SIZE; |
| 45 | + |
| 46 | + if (g_queueHead == g_queueTail) { |
| 47 | + g_queueIsFull = true; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +bool removeFromQueue(unsigned &flowIndex, unsigned &componentIndex) { |
| 52 | + if (g_queueHead == g_queueTail && !g_queueIsFull) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + flowIndex = g_queue[g_queueHead].flowIndex; |
| 57 | + componentIndex = g_queue[g_queueHead].componentIndex; |
| 58 | + |
| 59 | + g_queueHead = (g_queueHead + 1) % QUEUE_SIZE; |
| 60 | + g_queueIsFull = false; |
| 61 | + |
| 62 | + return true; |
| 63 | +} |
| 64 | + |
| 65 | +bool allInputsDefined(FlowDefinition &flowDefinition, unsigned flowIndex, unsigned componentIndex) { |
| 66 | + auto &flow = flowDefinition.flows.first[flowIndex]; |
| 67 | + auto &component = flow.components.first[componentIndex]; |
| 68 | + |
| 69 | + for (unsigned inputIndex = 0; inputIndex < component.inputs.count; inputIndex++) { |
| 70 | + auto &componentInput = component.inputs.first[inputIndex]; |
| 71 | + |
| 72 | + auto valueIndex = componentInput.values.first[0].valueIndex; |
| 73 | + |
| 74 | + auto &value = flowDefinition.flowValues.first[valueIndex]; |
| 75 | + if (value.header.type == FLOW_VALUE_TYPE_UNDEFINED) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | +} |
| 82 | + |
| 83 | +unsigned start(Assets *assets) { |
| 84 | + FlowDefinition &flowDefinition = *assets->flowDefinition; |
| 85 | + if (flowDefinition.flows.count == 0) { |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | + g_assets = assets; |
| 90 | + |
| 91 | + unsigned flowIndex = 0; |
| 92 | + |
| 93 | + const Flow &flow = flowDefinition.flows.first[flowIndex]; |
| 94 | + for (unsigned componentIndex = 0; componentIndex < flow.components.count; componentIndex++) { |
| 95 | + if (allInputsDefined(flowDefinition, flowIndex, componentIndex)) { |
| 96 | + addToQueue(flowIndex, componentIndex); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return 1; |
| 101 | +} |
| 102 | + |
| 103 | +void executeComponent(FlowDefinition &flowDefinition, unsigned flowIndex, unsigned componentIndex) { |
| 104 | + printf("Execute component %d\n", componentIndex); |
| 105 | +} |
| 106 | + |
| 107 | +void tick(unsigned flowHandle) { |
| 108 | + if (flowHandle != 1) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + Assets *assets = g_assets; |
| 113 | + FlowDefinition &flowDefinition = *assets->flowDefinition; |
| 114 | + |
| 115 | + unsigned flowIndex; |
| 116 | + unsigned componentIndex; |
| 117 | + if (removeFromQueue(flowIndex, componentIndex)) { |
| 118 | + executeComponent(flowDefinition, flowIndex, componentIndex); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +void dumpFlow(FlowDefinition &flowDefinition) { |
| 124 | + // printf("Flows:\n"); |
| 125 | + // for (unsigned i = 0; i < flowDefinition.flows.count; i++) { |
| 126 | + // printf("\t%d:\n", i); |
| 127 | + // const Flow &flow = flowDefinition.flows.first[i]; |
| 128 | + // for (unsigned j = 0; j < flow.components.count; j++) { |
| 129 | + // const Component &component = flow.components.first[j]; |
| 130 | + // printf("\t\t%d\n", j); |
| 131 | + // printf("\t\t\tType: %d\n", (int)component.type); |
| 132 | + |
| 133 | + // printf("\t\t\tInputs:\n"); |
| 134 | + // for (unsigned k = 0; k < component.inputs.count; k++) { |
| 135 | + // const ComponentInput &componentInput = component.inputs.first[k]; |
| 136 | + // printf("\t\t\t\t%d\n", componentInput.values.count); |
| 137 | + // } |
| 138 | + |
| 139 | + // printf("\t\t\tOutputs:\n"); |
| 140 | + // for (unsigned k = 0; k < component.outputs.count; k++) { |
| 141 | + // const ComponentOutput &componentOutput = component.outputs.first[k]; |
| 142 | + // printf("\t\t\t\t%d\n", componentOutput.connections.count); |
| 143 | + // } |
| 144 | + // } |
| 145 | + // } |
| 146 | + |
| 147 | + // printf("Values:\n"); |
| 148 | + // for (unsigned i = 0; i < flowDefinition.flowValues.count; i++) { |
| 149 | + // const FlowValue &flowValue = flowDefinition.flowValues.first[i]; |
| 150 | + // printf("\t%d: %d\n", i, (int)flowValue.header.type); |
| 151 | + // } |
28 | 152 | } |
29 | 153 |
|
30 | 154 | } // namespace flow |
|
0 commit comments