Begin supporting current gear in Python cluster

This commit is contained in:
XANTRONIX Development 2024-01-09 15:52:11 -05:00
parent d650417b9d
commit 8a6f6b7b6b
3 changed files with 39 additions and 10 deletions

View file

@ -43,11 +43,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] = {
#define SCHEDULE_SLOT_COUNT 5
static hexagram_schedule_slot table[SCHEDULE_SLOT_COUNT] = {
{ 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 }
{ 16670, { .can_id = 0x540, .can_dlc = 8 }, ev_handler },
{ 16670, { .can_id = 0x5a0, .can_dlc = 8 }, ev_handler },
};
static void table_update(hexagram_schedule_slot *table,
@ -62,13 +65,17 @@ static void table_update(hexagram_schedule_slot *table,
table[0].frame.data[3] = (engine_rpm & 0xff00) >> 8;
table[0].frame.data[2] = engine_rpm & 0x00ff;
/* Current gear */
printf("Current gear: %x\n", packet->race.gear);
table[3].frame.data[7] = 0xc | (packet->race.gear + 2) << 4;
/* Vehicle speed */
table[3].frame.data[1] = (speed_rps & 0xff00) >> 8;
table[3].frame.data[2] = speed_rps & 0x00ff;
table[4].frame.data[1] = (speed_rps & 0xff00) >> 8;
table[4].frame.data[2] = speed_rps & 0x00ff;
}
int hexagram_main_dash2can(int argc, char **argv) {
size_t i, count = 4;
size_t i;
int sock;
struct sockaddr_in sockaddr;
@ -102,7 +109,7 @@ int hexagram_main_dash2can(int argc, char **argv) {
goto error_can_if_open;
}
for (i=0; i<count; i++) {
for (i=0; i<SCHEDULE_SLOT_COUNT; i++) {
memset(table[i].frame.data, '\0', sizeof(table[i].frame.data));
}
@ -112,7 +119,9 @@ int hexagram_main_dash2can(int argc, char **argv) {
/* Fuel status */
table[2].frame.data[2] = 0x0e;
if ((schedule = hexagram_schedule_create(table, count, can_if)) == NULL) {
if ((schedule = hexagram_schedule_create(table,
SCHEDULE_SLOT_COUNT,
can_if)) == NULL) {
goto error_schedule_create;
}
@ -124,7 +133,7 @@ int hexagram_main_dash2can(int argc, char **argv) {
if ((len = recv(sock, &packet, sizeof(packet), 0)) < 0) {
goto error_io;
} else if (len != (ssize_t)sizeof(packet)) {
} else if (len > (ssize_t)sizeof(packet)) {
continue;
}

View file

@ -11,6 +11,7 @@ from hexagram.can cimport *
from hexagram.window cimport *
from hexagram.cluster import Cluster
from hexagram.trans import Gear
CLUSTER_RPM_MIN = 1200
CLUSTER_RPM_REDLINE = 6500
@ -36,6 +37,14 @@ cdef handle_xevents(hexagram_window *window,
cluster.draw_fg(fg)
hexagram_window_swap_buffer(window)
GEAR_VALUES = {
0: Gear.P,
1: Gear.R,
2: Gear.N,
3: Gear.D,
4: Gear.S
}
cdef cluster_update(cluster: Cluster,
fg: cairo.Context,
hexagram_window *window,
@ -49,6 +58,13 @@ cdef cluster_update(cluster: Cluster,
cluster.thermo.value = 0.75 * float(frame.data[1] - 48)
elif frame.can_id == 0x320:
cluster.fuel.value = frame.data[2] & 0xf
elif frame.can_id == 0x540:
value = ((frame.data[7] & 0xf0) >> 4) - 2
if (frame.data[7] & 0xc) == 0xc:
cluster.tacho.gear = Gear(value)
else:
cluster.tacho.gear = GEAR_VALUES[value]
elif frame.can_id == 0x5a0:
rps = 0.001 * float(frame.data[1] | (frame.data[2] << 8))
kph = (2.00152 * rps * 3600) / 1000.0

View file

@ -1,6 +1,9 @@
import enum
class Gear(enum.Enum):
S = -4
D = -3
P = -2
R = -1
N = 0
GEAR_1 = 1
@ -15,8 +18,9 @@ class Gear(enum.Enum):
GEAR_10 = 10
__strings__ = {
-1: 'R', 0: 'N', 1: '1', 2: '2', 3: '3', 4: '4',
5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: '10'
-2: 'P', -1: 'R', 0: 'N',
1: '1', 2: '2', 3: '3', 4: '4', 5: '5',
6: '6', 7: '7', 8: '8', 9: '9', 10: '10'
}
def __str__(self):