From b6e552e178982ffdc0eaaa7f0d7a932da1a915be Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 27 Dec 2023 13:59:55 -0500 Subject: [PATCH] That's better, I think --- bin/dash2can.c | 11 ++++++++--- include/hexagram/schedule.h | 14 +++++++++++--- src/schedule.c | 12 +++++++----- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/bin/dash2can.c b/bin/dash2can.c index 69c7bd6..f51e51e 100644 --- a/bin/dash2can.c +++ b/bin/dash2can.c @@ -42,14 +42,14 @@ static int ev_handler(struct can_frame *frame, void *ctx) { return hexagram_can_if_write(can_if, frame); } -static hexagram_schedule_slot table[4] = { +static hexagram_schedule_table_entry table[4] = { { 16670, { .can_id = 0x280, .can_dlc = 8 }, ev_handler }, { 16670, { .can_id = 0x288, .can_dlc = 8 }, ev_handler }, { 16670, { .can_id = 0x320, .can_dlc = 8 }, ev_handler }, { 16670, { .can_id = 0x5a0, .can_dlc = 8 }, ev_handler } }; -static void table_update(hexagram_schedule_slot *table, +static void table_update(hexagram_schedule_table_entry *table, hexagram_telemetry_dash_packet *packet) { float speed_ms = sqrt(powf(packet->velocity.x, 2) + powf(packet->velocity.z, 2)); @@ -67,6 +67,7 @@ static void table_update(hexagram_schedule_slot *table, } int hexagram_main_dash2can(int argc, char **argv) { + size_t i, count = 4; int sock; struct sockaddr_in sockaddr; @@ -100,7 +101,11 @@ int hexagram_main_dash2can(int argc, char **argv) { goto error_can_if_open; } - if ((schedule = hexagram_schedule_create(table, 4, can_if)) == NULL) { + for (i=0; i +#include #include #include +#include #include #include typedef int (*hexagram_schedule_handler)(struct can_frame *, void *); -typedef struct _hexagram_schedule_slot { +typedef struct _hexagram_schedule_table_entry { time_t interval_us; struct can_frame frame; hexagram_schedule_handler handler; +} hexagram_schedule_table_entry; + +typedef struct _hexagram_schedule_slot { + time_t interval_us; + hexagram_schedule_handler handler; } hexagram_schedule_slot; typedef struct _hexagram_schedule { @@ -24,11 +30,13 @@ typedef struct _hexagram_schedule { size_t current, count; + hexagram_schedule_table_entry *table; + void *ctx; int error; } hexagram_schedule; -hexagram_schedule *hexagram_schedule_create(hexagram_schedule_slot *table, +hexagram_schedule *hexagram_schedule_create(hexagram_schedule_table_entry *table, size_t count, void *ctx); diff --git a/src/schedule.c b/src/schedule.c index b097318..afbfc0d 100644 --- a/src/schedule.c +++ b/src/schedule.c @@ -42,7 +42,8 @@ static void _ev_notify(union sigval sv) { hexagram_schedule_slot *slot = _slot(schedule, schedule->current); time_t delay = slot->interval_us; - if (slot->handler(&slot->frame, schedule->ctx) < 0) { + if (slot->handler(&(schedule->table[schedule->current].frame), + schedule->ctx) < 0) { schedule->error = errno; break; @@ -65,7 +66,7 @@ static void _ev_notify(union sigval sv) { } static int _schedule_init(hexagram_schedule *schedule, - hexagram_schedule_slot *table, + hexagram_schedule_table_entry *table, size_t count, void *ctx) { size_t i; @@ -75,6 +76,7 @@ static int _schedule_init(hexagram_schedule *schedule, schedule->current = 0; schedule->error = 0; schedule->count = count; + schedule->table = table; schedule->ctx = ctx; schedule->ev.sigev_notify = SIGEV_THREAD; @@ -90,8 +92,8 @@ static int _schedule_init(hexagram_schedule *schedule, for (i=0; iframe.data, '\0', sizeof(*(slot->frame.data))); - memcpy(slot, &table[i], sizeof(hexagram_schedule_slot)); + slot->interval_us = table[i].interval_us; + slot->handler = table[i].handler; } qsort(schedule + 1, count, sizeof(hexagram_schedule_slot), _slot_cmp); @@ -102,7 +104,7 @@ error_timer_create: return -1; } -hexagram_schedule *hexagram_schedule_create(hexagram_schedule_slot *table, +hexagram_schedule *hexagram_schedule_create(hexagram_schedule_table_entry *table, size_t count, void *ctx) { hexagram_schedule *schedule;