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