Refactor dash2can using hexagram_schedule

This commit is contained in:
XANTRONIX Development 2023-12-27 01:01:37 -05:00
parent e7f2de41ca
commit 41abc005cd

View file

@ -13,6 +13,7 @@
#include <hexagram/can.h>
#include <hexagram/capture.h>
#include <hexagram/schedule.h>
#include <hexagram/telemetry/dash.h>
#include "dash2can.h"
@ -35,11 +36,22 @@ static void usage(int argc, char **argv, const char *message, ...) {
exit(1);
}
static hexagram_schedule_slot table[] = {
{ 16670, NULL, { .can_id = 0x280, .can_dlc = 8 }},
{ 16670, NULL, { .can_id = 0x288, .can_dlc = 8 }},
{ 16670, NULL, { .can_id = 0x320, .can_dlc = 8 }},
{ 16670, NULL, { .can_id = 0x5a0, .can_dlc = 8 }},
{ 0, NULL, {} }
};
int hexagram_main_dash2can(int argc, char **argv) {
int sock;
struct sockaddr_in sockaddr;
hexagram_can_if *can_if;
hexagram_schedule *schedule;
size_t i, count = 4;
if (argc < 2) {
usage(argc, argv, "No listening UDP port provided");
@ -68,9 +80,24 @@ int hexagram_main_dash2can(int argc, char **argv) {
goto error_can_if_open;
}
for (i=0; i<count; i++) {
table[i].iface = can_if;
}
/* 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)) == NULL) {
goto error_schedule_create;
}
hexagram_schedule_run(schedule);
while (1) {
hexagram_telemetry_dash_packet packet;
struct can_frame frame;
ssize_t len;
float speed_ms = sqrt(powf(packet.velocity.x, 2)
@ -86,48 +113,15 @@ int hexagram_main_dash2can(int argc, char **argv) {
}
/* Engine speed */
memset(&frame, '\0', sizeof(frame));
frame.can_id = 0x280;
frame.can_dlc = 8;
frame.data[3] = (engine_rpm & 0xff00) >> 8;
frame.data[2] = engine_rpm & 0x00ff;
if (hexagram_can_if_write(can_if, &frame) < 0) {
goto error_io;
}
/* Engine temperature */
memset(frame.data, '\0', sizeof(frame.data));
frame.can_id = 0x288;
frame.can_dlc = 8;
frame.data[1] = (uint8_t)((87.0 / 0.75) + 48);
if (hexagram_can_if_write(can_if, &frame) < 0) {
goto error_io;
}
/* Fuel status */
memset(frame.data, '\0', sizeof(frame.data));
frame.can_id = 0x320;
frame.can_dlc = 8;
frame.data[2] = 0x0e;
if (hexagram_can_if_write(can_if, &frame) < 0) {
goto error_io;
}
table[0].frame.data[3] = (engine_rpm & 0xff00) >> 8;
table[0].frame.data[2] = engine_rpm & 0x00ff;
/* Vehicle speed */
memset(frame.data, '\0', sizeof(frame.data));
frame.can_id = 0x5a0;
frame.can_dlc = 8;
frame.data[1] = (speed_rps & 0xff00) >> 8;
frame.data[2] = speed_rps & 0x00ff;
if (hexagram_can_if_write(can_if, &frame) < 0) {
goto error_io;
}
table[3].frame.data[1] = (speed_rps & 0xff00) >> 8;
table[3].frame.data[2] = speed_rps & 0x00ff;
}
hexagram_schedule_destroy(schedule);
hexagram_can_if_close(can_if);
close(sock);
@ -135,6 +129,9 @@ int hexagram_main_dash2can(int argc, char **argv) {
return 0;
error_io:
hexagram_schedule_destroy(schedule);
error_schedule_create:
hexagram_can_if_close(can_if);
error_can_if_open: