From 79516796d5f3fcda732b8215fe23d655706dcbca Mon Sep 17 00:00:00 2001 From: Allen Hill Date: Fri, 15 May 2026 23:47:48 +0000 Subject: [PATCH] Replace iffy bitfield use with uin8_t fields and enums --- src/avclandrv.c | 28 +++++++++++----------------- src/avclandrv.h | 41 +++++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/avclandrv.c b/src/avclandrv.c index 7de0c53..db4a93a 100644 --- a/src/avclandrv.c +++ b/src/avclandrv.c @@ -293,16 +293,10 @@ void AVCLAN_init() { AVCLAN_muteDevice(0); // unmute AVCLAN bus TX - cd_status.cd1 = 1; + cd_status.cds = cd_CD1; cd_status.disc = 1; - cd_status.cd2 = cd_status.cd3 = cd_status.cd4 = cd_status.cd5 = - cd_status.cd6 = 0; cd_status.state = cd_SEEKING_TRACK; - cd_status.disk_random = 0; - cd_status.random = 0; - cd_status.disk_repeat = 0; - cd_status.repeat = 0; - cd_status.scan = 0; + cd_status.flags = 0; cd_status.flags2 = 0xC0; cd_status.track = 1; @@ -325,12 +319,13 @@ static uint8_t toBCD(uint8_t x) { return (uint8_t)(((x / 10) << 4) | (x % 10)); } -// Copy cd_status to a wire response, applying BCD conversion to time fields. +// Serialize cd_status into the wire format. The struct layout mirrors the wire +// format byte-for-byte, except for mins/secs, which need converted from integer +// to BCD static void serializeCDStatus(uint8_t *dst) { - AVCLAN_CD_Status_t wire = cd_status; - wire.mins = toBCD(wire.mins); - wire.secs = toBCD(wire.secs); - memcpy(dst, &wire, sizeof(wire)); + memcpy(dst, &cd_status, sizeof(cd_status)); + dst[4] = toBCD(cd_status.mins); + dst[5] = toBCD(cd_status.secs); } uint8_t AVCLAN_isPlaying() { return (CD_Mode == stPlay); } @@ -1055,7 +1050,7 @@ response_t AVCLAN_handleframe(const AVCLAN_frame_t *in, AVCLAN_frame_t *out) { (*cd_Track)++; *cd_Time_Min = 0xff; *cd_Time_Sec = 0x7f; - cd_status.scan = 1; + cd_status.flags |= cd_SCAN; cd_status.flags2 = 0xc0; respond = r_TrackChange; AVCLAN_generateStatus(out); @@ -1065,7 +1060,7 @@ response_t AVCLAN_handleframe(const AVCLAN_frame_t *in, AVCLAN_frame_t *out) { (*cd_Track)--; *cd_Time_Min = 0xff; *cd_Time_Sec = 0x7f; - cd_status.scan = 1; + cd_status.flags |= cd_SCAN; cd_status.flags2 = 0xc0; respond = r_TrackChange; AVCLAN_generateStatus(out); @@ -1266,8 +1261,7 @@ void AVCLAN_generateStatus(AVCLAN_frame_t *status) { void AVCLAN_normalizeState() { // if (cd_status.state != cd_PLAYBACK) { cd_status.state = cd_PLAYBACK; - cd_status.disk_scan = 0; - cd_status.scan = 0; + cd_status.flags &= (uint8_t)~(cd_DISK_SCAN | cd_SCAN); cd_status.flags2 = 0x80; // } } diff --git a/src/avclandrv.h b/src/avclandrv.h index 8f9fd12..b9569f6 100644 --- a/src/avclandrv.h +++ b/src/avclandrv.h @@ -133,27 +133,32 @@ typedef enum : uint8_t { cd_LOADING = 0x80, } cd_state; +typedef enum : uint8_t { + cd_CD1 = 1 << 0, + cd_CD2 = 1 << 1, + cd_CD3 = 1 << 2, + cd_CD4 = 1 << 3, + cd_CD5 = 1 << 4, + cd_CD6 = 1 << 5, +} cd_present_t; + +typedef enum : uint8_t { + cd_DISK_RANDOM = 1 << 1, + cd_RANDOM = 1 << 2, + cd_DISK_REPEAT = 1 << 3, + cd_REPEAT = 1 << 4, + cd_DISK_SCAN = 1 << 5, + cd_SCAN = 1 << 6, +} cd_flag_t; + typedef struct AVCLAN_CD_Status { - _Bool cd1 : 1; - _Bool cd2 : 1; - _Bool cd3 : 1; - _Bool cd4 : 1; - _Bool cd5 : 1; - _Bool cd6 : 1; - int : 2; // padding + uint8_t cds; uint8_t state; uint8_t disc; uint8_t track; uint8_t mins; uint8_t secs; - int : 1; // padding - _Bool disk_random : 1; - _Bool random : 1; - _Bool disk_repeat : 1; - _Bool repeat : 1; - _Bool disk_scan : 1; - _Bool scan : 1; - int : 1; // padding + uint8_t flags; uint8_t flags2; } AVCLAN_CD_Status_t; @@ -170,9 +175,9 @@ typedef enum : uint8_t { } response_t; typedef struct print_struct { - _Bool print : 1; // print at all - _Bool binary : 1; // when also printing, format as binary instead of text - _Bool verbose : 1; // include extra context in error reports + bool print : 1; // print at all + bool binary : 1; // when also printing, format as binary instead of text + bool verbose : 1; // include extra context in error reports } log_t; typedef struct AVCLAN_frame_struct {