added function to get crc_extra

This commit is contained in:
Thies Lennart Alff 2025-02-20 20:48:46 +01:00
parent d341ea723e
commit 1f31fe8bed
Signed by: lennartalff
GPG key ID: 4EC67D34D594104D

View file

@ -8,7 +8,42 @@
#define HYDROLINK_MAX_PAYLOAD_LEN 6 #define HYDROLINK_MAX_PAYLOAD_LEN 6
#define HYDROLINK_MAX_MSG_LEN (HYDROLINK_MAX_PAYLOAD_LEN + HYDROLINK_NON_PAYLOAD_LEN) #define HYDROLINK_MAX_MSG_LEN (HYDROLINK_MAX_PAYLOAD_LEN + HYDROLINK_NON_PAYLOAD_LEN)
#define HYDROLINK_MSG_CRCS {73, 58, 250, 38, 28} typedef struct {
uint8_t msg_id;
uint8_t crc_extra;
} hydrolink_crc_extra_t;
#define HYDROLINK_MSG_CRCS {{1, 73}, {2, 58}, {3, 250}, {4, 38}, {5, 28}}
/**
* @brief Get the crc_extra byte corresponding to msg_id
*
* @param msg_id the message's id
* @param crc[out] the crc_extra byte as output of this function
* @return Return 1 if successfull, 0 otherwise.
static inline uint8_t hydrolink_get_crc_extra(uint8_t msg_id, uint8_t *crc) {
static const hydrolink_crc_extra_t crcs[] = HYDROLINK_MSG_CRCS;
uint8_t left = 0;
uint8_t right = sizeof(crcs) / sizeof(crcs[0]) - 1;
while (left < right) {
uint8_t center = (left+right+1) / 2;
if (msg_id < crcs[center].msg_id) {
right = center - 1;
continue;
}
if (msg_id > crcs[center].msg_id) {
left = center;
continue;
}
left = mid;
break;
}
if (crcs[left].msg_id != msg_id) {
return 0;
}
*crc = crcs[left].crc_extra;
return 1;
}
struct hydrolink_msg_s; struct hydrolink_msg_s;