Add "differentiate" node type

Can be used to transform absolute movement to relative movement
This commit is contained in:
Vftdan 2024-08-19 11:01:19 +02:00
parent eaa64f5b4c
commit 12af4b35c7
2 changed files with 85 additions and 1 deletions

View File

@ -12,7 +12,7 @@ CPPFLAGS += $(shell pkg-config --cflags $(DEPS))
LDLIBS += $(shell pkg-config --libs $(DEPS)) LDLIBS += $(shell pkg-config --libs $(DEPS))
INTERP ?= INTERP ?=
MAIN = main MAIN = main
OBJS = main.o events.o processing.o graph.o config.o event_code_names.o hash_table.o module_registry.o event_predicate.o nodes/getchar.o nodes/print.o nodes/evdev.o nodes/tee.o nodes/router.o nodes/modifiers.o nodes/modify_predicate.o nodes/uinput.o nodes/assign.o OBJS = main.o events.o processing.o graph.o config.o event_code_names.o hash_table.o module_registry.o event_predicate.o nodes/getchar.o nodes/print.o nodes/evdev.o nodes/tee.o nodes/router.o nodes/modifiers.o nodes/modify_predicate.o nodes/uinput.o nodes/assign.o nodes/differentiate.o
all: $(MAIN) all: $(MAIN)

84
nodes/differentiate.c Normal file
View File

@ -0,0 +1,84 @@
#include "../graph.h"
#include "../module_registry.h"
typedef struct {
GraphNode as_GraphNode;
int64_t previous;
} DifferentiateGraphNode;
static bool
handle_event(EventPositionBase * self, EventNode * event)
{
DifferentiateGraphNode *node = DOWNCAST(DifferentiateGraphNode, GraphNode, DOWNCAST(GraphNode, EventPositionBase, self));
size_t count = node->as_GraphNode.outputs.length;
if (!count) {
event_destroy(event);
return true;
}
int64_t current = event->data.payload;
event->data.payload = current - node->previous;
node->previous = current;
if (count > 1) {
count = event_replicate(event, count - 1) + 1;
}
for (size_t i = 0; i < count; ++i) {
event->position = &node->as_GraphNode.outputs.elements[i]->as_EventPositionBase;
event = event->next;
}
return true;
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
(void) config;
(void) env;
DifferentiateGraphNode * node = T_ALLOC(1, DifferentiateGraphNode);
if (!node) {
return NULL;
}
int64_t initial = 0;
if (config->options) {
// FIXME check config->options for NULL in all node types
initial = env_resolve_constant(env, config_setting_get_member(config->options, "initial"));
}
*node = (DifferentiateGraphNode) {
.as_GraphNode = {
.as_EventPositionBase = {
.handle_event = &handle_event,
.waiting_new_event = false,
},
.specification = spec,
.inputs = EMPTY_GRAPH_CHANNEL_LIST,
.outputs = EMPTY_GRAPH_CHANNEL_LIST,
},
.previous = initial,
};
return &node->as_GraphNode;
}
static void destroy
(GraphNodeSpecification * self, GraphNode * target)
{
(void) self;
free(target);
}
GraphNodeSpecification nodespec_differentiate = (GraphNodeSpecification) {
.create = &create,
.destroy = &destroy,
.register_io = NULL,
.name = "differentiate",
.documentation = "Subtracts the previous event payload from the current one\nAccepts events on any connector\nSends events on all connectors"
"\nOption 'initial' (optional): the value to subtract from the first event payload"
,
};
MODULE_CONSTRUCTOR(init)
{
register_graph_node_specification(&nodespec_differentiate);
}