Skip to main content

Unit Validation

OSynaptic-TX validates unit strings at encode time using the OSTX_UNIT() macro. Pass an invalid unit and the encode function returns 0 (no packet emitted).

OSTX_UNIT() Macro

/* Will not compile if "xyz" is not a recognised unit code */
#define MY_UNIT OSTX_UNIT("Cel")

/* Use in pack calls */
ostx_sensor_pack(aid, "T1", OSTX_UNIT("Cel"), tid, ts, scaled, out);

On compilers that support _Static_assert (C11) the validation is a compile-time error. On C89 targets the check falls back to a runtime guard that returns 0 on failure.

Recognised Unit Codes (subset)

The full list is in ostx_unit_table.h. Common categories:

Temperature

CodeFull name
CelDegree Celsius
KKelvin
[degF]Degree Fahrenheit

Humidity & Pressure

CodeFull name
PctPercentage (0–100)
kPaKilopascal
hPaHectopascal (millibar)
PaPascal
mm[Hg]Millimetres of mercury

Concentration & Air Quality

CodeFull name
ppmParts per million
ppbParts per billion
mg/m3Milligrams per cubic metre
ug/m3Micrograms per cubic metre

Electrical

CodeFull name
VVolt
mVMillivolt
AAmpere
mAMilliampere
WWatt
WhWatt-hour
kWhKilowatt-hour
OhmOhm
HzHertz
kHzKilohertz

Length, Speed, Acceleration

CodeFull name
mMetre
cmCentimetre
mmMillimetre
m/sMetre per second
km/hKilometre per hour
m/s2Metre per second squared

Light, Radiation

CodeFull name
lxLux
uW/cm2Microwatt per square centimetre
mSvMillisievert

Count & Dimensionless

CodeFull name
1Dimensionless (ratio)
{count}Raw count
{rpm}Revolutions per minute

SI Prefix Rules

All UCUM SI prefixes are accepted:

PrefixSymbolFactor
petaP10¹⁵
teraT10¹²
gigaG10⁹
megaM10⁶
kilok10³
hectoh10²
decada10¹
decid10⁻¹
centic10⁻²
millim10⁻³
microu10⁻⁶
nanon10⁻⁹
picop10⁻¹²

Example: km, mg, uV, nA are all valid by prefix expansion.

Bypassing Validation

If you have a custom unit string not covered by the table, define OSTX_ALLOW_CUSTOM_UNITS=1 and the validation is skipped. Receivers will still accept the packet; they simply forward the raw unit string as-is.