11#if DETWS_ENABLE_LONWORKS
15size_t detws_lon_build_nv(uint8_t msg_code, uint16_t selector,
const uint8_t *value,
size_t value_len, uint8_t *out,
18 if (!out || (value_len && !value) || selector > Lon::LON_NV_SELECTOR_MAX)
20 size_t n = 3 + value_len;
24 out[1] = (uint8_t)(selector >> 8);
25 out[2] = (uint8_t)selector;
27 memcpy(out + 3, value, value_len);
31bool detws_lon_parse_nv(
const uint8_t *pdu,
size_t len, LonNv *out)
33 if (!pdu || !out || len < 3)
35 out->msg_code = pdu[0];
36 out->selector = (uint16_t)(((pdu[1] & 0x3F) << 8) | pdu[2]);
37 out->value = (len > 3) ? (pdu + 3) :
nullptr;
38 out->value_len = len - 3;
42void detws_lon_snvt_temp_encode(
double celsius, uint8_t out[2])
45 double kelvin_hundredths = (celsius + 273.15) * 100.0;
46 int32_t v = (int32_t)(kelvin_hundredths >= 0 ? kelvin_hundredths + 0.5 : kelvin_hundredths - 0.5);
51 uint16_t u = (uint16_t)v;
52 out[0] = (uint8_t)(u >> 8);
56double detws_lon_snvt_temp_decode(
const uint8_t in[2])
58 int16_t v = (int16_t)((in[0] << 8) | in[1]);
59 return (
double)v / 100.0 - 273.15;
62void detws_lon_snvt_switch_encode(
double percent, uint8_t state, uint8_t out[2])
69 uint8_t v = (uint8_t)(percent * 2.0 + 0.5);
74void detws_lon_snvt_switch_decode(
const uint8_t in[2],
double *percent, uint8_t *state)
77 *percent = (double)in[0] / 2.0;
LonWorks / LON-IP (ISO/IEC 14908) network-variable codec (DETWS_ENABLE_LONWORKS).