diff --git a/bin/dash2can.c b/bin/dash2can.c index f51e51e..202932f 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_table_entry table[4] = { +static hexagram_schedule_slot 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_table_entry *table, +static void table_update(hexagram_schedule_slot *table, hexagram_telemetry_dash_packet *packet) { float speed_ms = sqrt(powf(packet->velocity.x, 2) + powf(packet->velocity.z, 2)); @@ -105,16 +105,16 @@ int hexagram_main_dash2can(int argc, char **argv) { memset(table[i].frame.data, '\0', sizeof(table[i].frame.data)); } - if ((schedule = hexagram_schedule_create(table, count, can_if)) == NULL) { - goto error_schedule_create; - } - /* Engine temperature */ table[1].frame.data[1] = (uint8_t)((87.0 / 0.75) + 48); /* Fuel status */ table[2].frame.data[2] = 0x0e; + if ((schedule = hexagram_schedule_create(table, count, can_if)) == NULL) { + goto error_schedule_create; + } + hexagram_schedule_run(schedule); while (1) { diff --git a/include/hexagram/schedule.h b/include/hexagram/schedule.h index 5e8d711..338eab3 100644 --- a/include/hexagram/schedule.h +++ b/include/hexagram/schedule.h @@ -11,15 +11,10 @@ typedef int (*hexagram_schedule_handler)(struct can_frame *, void *); -typedef struct _hexagram_schedule_table_entry { +typedef struct _hexagram_schedule_slot { 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_table_entry *entry; } hexagram_schedule_slot; typedef struct _hexagram_schedule { @@ -30,13 +25,13 @@ typedef struct _hexagram_schedule { size_t current, count; - hexagram_schedule_table_entry *table; + hexagram_schedule_slot *table; void *ctx; int error; } hexagram_schedule; -hexagram_schedule *hexagram_schedule_create(hexagram_schedule_table_entry *table, +hexagram_schedule *hexagram_schedule_create(hexagram_schedule_slot *table, size_t count, void *ctx); diff --git a/src/schedule.c b/src/schedule.c index e800f32..78773d9 100644 --- a/src/schedule.c +++ b/src/schedule.c @@ -14,7 +14,7 @@ static int _slot_cmp(const void *item_a, const void *item_b) { } static inline hexagram_schedule_slot *_slot(hexagram_schedule *schedule, size_t i) { - return &((hexagram_schedule_slot *)(schedule + 1))[i]; + return ((hexagram_schedule_slot **)(schedule + 1))[i]; }; static inline time_t _slot_interval_ns(hexagram_schedule *schedule, size_t i) { @@ -40,10 +40,9 @@ static void _ev_notify(union sigval sv) { while (schedule->current < schedule->count) { hexagram_schedule_slot *slot = _slot(schedule, schedule->current); - hexagram_schedule_table_entry *entry = slot->entry; time_t delay = slot->interval_us; - if (entry->handler(&entry->frame, schedule->ctx) < 0) { + if (slot->handler(&slot->frame, schedule->ctx) < 0) { schedule->error = errno; break; @@ -65,13 +64,13 @@ static void _ev_notify(union sigval sv) { _slot_set_interval(schedule, schedule->current); } -hexagram_schedule *hexagram_schedule_create(hexagram_schedule_table_entry *table, +hexagram_schedule *hexagram_schedule_create(hexagram_schedule_slot *table, size_t count, void *ctx) { hexagram_schedule *schedule; size_t i; - if ((schedule = malloc(sizeof(*schedule) + count * sizeof(hexagram_schedule_slot))) == 0) { + if ((schedule = malloc(sizeof(*schedule) + count * sizeof(void *))) == 0) { goto error_malloc_schedule; } @@ -92,13 +91,10 @@ hexagram_schedule *hexagram_schedule_create(hexagram_schedule_table_entry *table } for (i=0; iinterval_us = table[i].interval_us; - slot->entry = &table[i]; + ((hexagram_schedule_slot **)(schedule + 1))[i] = &table[i]; } - qsort(schedule + 1, count, sizeof(hexagram_schedule_slot), _slot_cmp); + qsort(schedule + 1, count, sizeof(hexagram_schedule_slot *), _slot_cmp); return schedule;