Remove hexagram_schedule_table_entry type
This commit is contained in:
parent
a9cd2a86df
commit
87bd264900
3 changed files with 15 additions and 24 deletions
|
@ -42,14 +42,14 @@ static int ev_handler(struct can_frame *frame, void *ctx) {
|
||||||
return hexagram_can_if_write(can_if, frame);
|
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 = 0x280, .can_dlc = 8 }, ev_handler },
|
||||||
{ 16670, { .can_id = 0x288, .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 = 0x320, .can_dlc = 8 }, ev_handler },
|
||||||
{ 16670, { .can_id = 0x5a0, .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) {
|
hexagram_telemetry_dash_packet *packet) {
|
||||||
float speed_ms = sqrt(powf(packet->velocity.x, 2)
|
float speed_ms = sqrt(powf(packet->velocity.x, 2)
|
||||||
+ powf(packet->velocity.z, 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));
|
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 */
|
/* Engine temperature */
|
||||||
table[1].frame.data[1] = (uint8_t)((87.0 / 0.75) + 48);
|
table[1].frame.data[1] = (uint8_t)((87.0 / 0.75) + 48);
|
||||||
|
|
||||||
/* Fuel status */
|
/* Fuel status */
|
||||||
table[2].frame.data[2] = 0x0e;
|
table[2].frame.data[2] = 0x0e;
|
||||||
|
|
||||||
|
if ((schedule = hexagram_schedule_create(table, count, can_if)) == NULL) {
|
||||||
|
goto error_schedule_create;
|
||||||
|
}
|
||||||
|
|
||||||
hexagram_schedule_run(schedule);
|
hexagram_schedule_run(schedule);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
|
@ -11,15 +11,10 @@
|
||||||
|
|
||||||
typedef int (*hexagram_schedule_handler)(struct can_frame *, void *);
|
typedef int (*hexagram_schedule_handler)(struct can_frame *, void *);
|
||||||
|
|
||||||
typedef struct _hexagram_schedule_table_entry {
|
typedef struct _hexagram_schedule_slot {
|
||||||
time_t interval_us;
|
time_t interval_us;
|
||||||
struct can_frame frame;
|
struct can_frame frame;
|
||||||
hexagram_schedule_handler handler;
|
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;
|
} hexagram_schedule_slot;
|
||||||
|
|
||||||
typedef struct _hexagram_schedule {
|
typedef struct _hexagram_schedule {
|
||||||
|
@ -30,13 +25,13 @@ typedef struct _hexagram_schedule {
|
||||||
size_t current,
|
size_t current,
|
||||||
count;
|
count;
|
||||||
|
|
||||||
hexagram_schedule_table_entry *table;
|
hexagram_schedule_slot *table;
|
||||||
|
|
||||||
void *ctx;
|
void *ctx;
|
||||||
int error;
|
int error;
|
||||||
} hexagram_schedule;
|
} hexagram_schedule;
|
||||||
|
|
||||||
hexagram_schedule *hexagram_schedule_create(hexagram_schedule_table_entry *table,
|
hexagram_schedule *hexagram_schedule_create(hexagram_schedule_slot *table,
|
||||||
size_t count,
|
size_t count,
|
||||||
void *ctx);
|
void *ctx);
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
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) {
|
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) {
|
while (schedule->current < schedule->count) {
|
||||||
hexagram_schedule_slot *slot = _slot(schedule, schedule->current);
|
hexagram_schedule_slot *slot = _slot(schedule, schedule->current);
|
||||||
hexagram_schedule_table_entry *entry = slot->entry;
|
|
||||||
time_t delay = slot->interval_us;
|
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;
|
schedule->error = errno;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -65,13 +64,13 @@ static void _ev_notify(union sigval sv) {
|
||||||
_slot_set_interval(schedule, schedule->current);
|
_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,
|
size_t count,
|
||||||
void *ctx) {
|
void *ctx) {
|
||||||
hexagram_schedule *schedule;
|
hexagram_schedule *schedule;
|
||||||
size_t i;
|
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;
|
goto error_malloc_schedule;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,13 +91,10 @@ hexagram_schedule *hexagram_schedule_create(hexagram_schedule_table_entry *table
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<count; i++) {
|
for (i=0; i<count; i++) {
|
||||||
hexagram_schedule_slot *slot = _slot(schedule, i);
|
((hexagram_schedule_slot **)(schedule + 1))[i] = &table[i];
|
||||||
|
|
||||||
slot->interval_us = table[i].interval_us;
|
|
||||||
slot->entry = &table[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qsort(schedule + 1, count, sizeof(hexagram_schedule_slot), _slot_cmp);
|
qsort(schedule + 1, count, sizeof(hexagram_schedule_slot *), _slot_cmp);
|
||||||
|
|
||||||
return schedule;
|
return schedule;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue