From e7d3092dd149ce455eaa934cc63104b6273302f5 Mon Sep 17 00:00:00 2001 From: Jan Breuer Date: Mon, 22 Feb 2016 01:19:09 +0100 Subject: [PATCH] Add all possible units from IEEE488.2 to address some points of #26 --- libscpi/inc/scpi/config.h | 56 ++++++++++ libscpi/inc/scpi/types.h | 50 ++++++++- libscpi/src/units.c | 220 +++++++++++++++++++++++++++++++++----- 3 files changed, 298 insertions(+), 28 deletions(-) diff --git a/libscpi/inc/scpi/config.h b/libscpi/inc/scpi/config.h index 2a8c90c9..6371bb02 100644 --- a/libscpi/inc/scpi/config.h +++ b/libscpi/inc/scpi/config.h @@ -101,6 +101,62 @@ extern "C" { #define USE_CUSTOM_DTOSTR 0 #endif +#ifndef USE_UNITS_IMPERIAL +#define USE_UNITS_IMPERIAL 0 +#endif + +#ifndef USE_UNITS_ANGLE +#define USE_UNITS_ANGLE SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_PARICLES +#define USE_UNITS_PARICLES SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_DISTANCE +#define USE_UNITS_DISTANCE SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_MAGNETIC +#define USE_UNITS_MAGNETIC SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_LIGHT +#define USE_UNITS_LIGHT SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_ENERGY_FORCE_MASS +#define USE_UNITS_ENERGY_FORCE_MASS SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_TIME +#define USE_UNITS_TIME SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_TEMPERATURE +#define USE_UNITS_TEMPERATURE SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_RATIO +#define USE_UNITS_RATIO SYSTEM_TYPE +#endif + +#ifndef USE_UNITS_POWER +#define USE_UNITS_POWER 1 +#endif + +#ifndef USE_UNITS_FREQUENCY +#define USE_UNITS_FREQUENCY 1 +#endif + +#ifndef USE_UNITS_ELECTRIC +#define USE_UNITS_ELECTRIC 1 +#endif + +#ifndef USE_UNITS_ELECTRIC_CHARGE_CONDUCTANCE +#define USE_UNITS_ELECTRIC_CHARGE_CONDUCTANCE SYSTEM_TYPE +#endif + /* Compiler specific */ /* RealView/Keil ARM Compiler, e.g. Cortex-M CPUs */ #if defined(__CC_ARM) diff --git a/libscpi/inc/scpi/types.h b/libscpi/inc/scpi/types.h index 0147c037..01611be0 100644 --- a/libscpi/inc/scpi/types.h +++ b/libscpi/inc/scpi/types.h @@ -218,8 +218,54 @@ extern "C" { SCPI_UNIT_OHM, SCPI_UNIT_HERTZ, SCPI_UNIT_CELSIUS, - SCPI_UNIT_SECONDS, - SCPI_UNIT_DISTANCE + SCPI_UNIT_SECOND, + SCPI_UNIT_METER, + SCPI_UNIT_GRAY, + SCPI_UNIT_BECQUEREL, + SCPI_UNIT_MOLE, + SCPI_UNIT_DEGREE, + SCPI_UNIT_GRADE, + SCPI_UNIT_RADIAN, + SCPI_UNIT_REVOLUTION, + SCPI_UNIT_STERADIAN, + SCPI_UNIT_SIEVERT, + SCPI_UNIT_FARAD, + SCPI_UNIT_COULOMB, + SCPI_UNIT_SIEMENS, + SCPI_UNIT_ELECTRONVOLT, + SCPI_UNIT_JOULE, + SCPI_UNIT_NEWTON, + SCPI_UNIT_LUX, + SCPI_UNIT_HENRY, + SCPI_UNIT_ASTRONOMIC_UNIT, + SCPI_UNIT_INCH, + SCPI_UNIT_FOOT, + SCPI_UNIT_PARSEC, + SCPI_UNIT_MILE, + SCPI_UNIT_NAUTICAL_MILE, + SCPI_UNIT_LUMEN, + SCPI_UNIT_CANDELA, + SCPI_UNIT_WEBER, + SCPI_UNIT_TESLA, + SCPI_UNIT_ATOMIC_MASS, + SCPI_UNIT_KILOGRAM, + SCPI_UNIT_WATT, + SCPI_UNIT_DBM, + SCPI_UNIT_ATMOSPHERE, + SCPI_UNIT_INCH_OF_MERCURY, + SCPI_UNIT_MM_OF_MERCURY, + SCPI_UNIT_PASCAL, + SCPI_UNIT_TORT, + SCPI_UNIT_BAR, + SCPI_UNIT_DECIBEL, + SCPI_UNIT_UNITLESS, + SCPI_UNIT_FAGRENHEIT, + SCPI_UNIT_KELVIN, + SCPI_UNIT_DAY, + SCPI_UNIT_YEAR, + SCPI_UNIT_STROKES, + SCPI_UNIT_POISE, + SCPI_UNIT_LITER }; typedef enum _scpi_unit_t scpi_unit_t; diff --git a/libscpi/src/units.c b/libscpi/src/units.c index f59fe86b..b13b4678 100644 --- a/libscpi/src/units.c +++ b/libscpi/src/units.c @@ -2,7 +2,7 @@ * Copyright (c) 2012-2013 Jan Breuer, * * All Rights Reserved - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: @@ -11,7 +11,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -28,10 +28,10 @@ /** * @file scpi_units.c * @date Thu Nov 15 10:58:45 UTC 2012 - * + * * @brief SCPI units - * - * + * + * */ #include @@ -63,40 +63,208 @@ * units definition IEEE 488.2-1992 tab 7-1 */ const scpi_unit_def_t scpi_units_def[] = { - /* voltage */ - {/* name */ "UV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-6}, - {/* name */ "MV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-3}, - {/* name */ "V", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1}, - {/* name */ "KV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e3}, - - /* current */ +#if USE_UNITS_PARICLES + /* Absorbet dose */ + {/* name */ "GY", /* unit */ SCPI_UNIT_GRAY, /* mult */ 1}, + + /* Activity of radionuclide */ + {/* name */ "BQ", /* unit */ SCPI_UNIT_BECQUEREL, /* mult */ 1}, + + /* Amount of substance */ + {/* name */ "MOL", /* unit */ SCPI_UNIT_MOLE, /* mult */ 1}, + + /* Dose equivalent */ + {/* name */ "NSV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e-9}, + {/* name */ "USV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e-6}, + {/* name */ "MSV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e-3}, + {/* name */ "SV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1}, + {/* name */ "KSV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e3}, + {/* name */ "MASV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e6}, + + /* Energy */ + {/* name */ "EV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1}, + {/* name */ "KEV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1e3}, + {/* name */ "MAEV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1e6}, + {/* name */ "GEV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1e9}, + {/* name */ "TEV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1e12}, + + /* Mass */ + {/* name */ "U", /* unit */ SCPI_UNIT_ATOMIC_MASS, /* mult */ 1}, +#endif /* USE_UNITS_PARICLES */ + +#if USE_UNITS_ANGLE + /* Angle */ + {/* name */ "DEG", /* unit */ SCPI_UNIT_DEGREE, /* mult */ 1}, + {/* name */ "GON", /* unit */ SCPI_UNIT_GRADE, /* mult */ 1}, + {/* name */ "MNT", /* unit */ SCPI_UNIT_DEGREE, /* mult */ 1. / 60.}, + {/* name */ "RAD", /* unit */ SCPI_UNIT_RADIAN, /* mult */ 1}, + {/* name */ "SEC", /* unit */ SCPI_UNIT_DEGREE, /* mult */ 1. / 3600.}, + {/* name */ "REV", /* unit */ SCPI_UNIT_REVOLUTION, /* mult */ 1}, + {/* name */ "RS", /* unit */ SCPI_UNIT_STERADIAN, /* mult */ 1}, +#endif /* USE_UNITS_ANGLE */ + +#if USE_UNITS_ELECTRIC + /* Electric - capacitance */ + {/* name */ "PF", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1e-12}, + {/* name */ "NF", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1e-9}, + {/* name */ "UF", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1e-6}, + {/* name */ "MF", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1e-3}, + {/* name */ "F", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1}, + + /* Electric - current */ {/* name */ "UA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-6}, {/* name */ "MA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-3}, {/* name */ "A", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1}, {/* name */ "KA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e3}, - /* resistance */ + /* Electric - potential */ + {/* name */ "UV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-6}, + {/* name */ "MV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-3}, + {/* name */ "V", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1}, + {/* name */ "KV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e3}, + + /* Electric - resistance */ {/* name */ "OHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1}, {/* name */ "KOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e3}, {/* name */ "MOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e6}, - /* frequency */ + /* Inductance */ + {/* name */ "UH", /* unit */ SCPI_UNIT_HENRY, /* mult */ 1e-6}, + {/* name */ "MH", /* unit */ SCPI_UNIT_HENRY, /* mult */ 1e-3}, + {/* name */ "H", /* unit */ SCPI_UNIT_HENRY, /* mult */ 1}, +#endif /* USE_UNITS_ELECTRIC */ + +#if USE_UNITS_ELECTRIC_CHARGE_CONDUCTANCE + /* Electric - charge */ + {/* name */ "C", /* unit */ SCPI_UNIT_COULOMB, /* mult */ 1}, + + /* Electric - conductance */ + {/* name */ "USIE", /* unit */ SCPI_UNIT_SIEMENS, /* mult */ 1e-6}, + {/* name */ "MSIE", /* unit */ SCPI_UNIT_SIEMENS, /* mult */ 1e-3}, + {/* name */ "SIE", /* unit */ SCPI_UNIT_SIEMENS, /* mult */ 1}, +#endif /* USE_UNITS_ELECTRIC_CHARGE_CONDUCTANCE */ + +#if USE_UNITS_ENERGY_FORCE_MASS + /* Energy */ + {/* name */ "J", /* unit */ SCPI_UNIT_JOULE, /* mult */ 1}, + {/* name */ "KJ", /* unit */ SCPI_UNIT_JOULE, /* mult */ 1e3}, + {/* name */ "MAJ", /* unit */ SCPI_UNIT_JOULE, /* mult */ 1e6}, + + /* Force */ + {/* name */ "N", /* unit */ SCPI_UNIT_NEWTON, /* mult */ 1}, + {/* name */ "KN", /* unit */ SCPI_UNIT_NEWTON, /* mult */ 1e3}, + + /* Pressure */ + {/* name */ "ATM", /* unit */ SCPI_UNIT_ATMOSPHERE, /* mult */ 1}, + {/* name */ "INHG", /* unit */ SCPI_UNIT_INCH_OF_MERCURY, /* mult */ 1}, + {/* name */ "MMHG", /* unit */ SCPI_UNIT_MM_OF_MERCURY, /* mult */ 1}, + + {/* name */ "TORR", /* unit */ SCPI_UNIT_TORT, /* mult */ 1}, + {/* name */ "BAR", /* unit */ SCPI_UNIT_BAR, /* mult */ 1}, + + {/* name */ "PAL", /* unit */ SCPI_UNIT_PASCAL, /* mult */ 1}, + {/* name */ "KPAL", /* unit */ SCPI_UNIT_PASCAL, /* mult */ 1e3}, + {/* name */ "MAPAL", /* unit */ SCPI_UNIT_PASCAL, /* mult */ 1e6}, + + /* Viscosity kinematic */ + {/* name */ "ST", /* unit */ SCPI_UNIT_STROKES, /* mult */ 1}, + + /* Viscosity dynamic */ + {/* name */ "P", /* unit */ SCPI_UNIT_POISE, /* mult */ 1}, + + /* Viscosity dynamic */ + {/* name */ "L", /* unit */ SCPI_UNIT_LITER, /* mult */ 1}, + + /* Mass */ + {/* name */ "MG", /* unit */ SCPI_UNIT_KILOGRAM, /* mult */ 1e-6}, + {/* name */ "G", /* unit */ SCPI_UNIT_KILOGRAM, /* mult */ 1e-3}, + {/* name */ "KG", /* unit */ SCPI_UNIT_KILOGRAM, /* mult */ 1}, + {/* name */ "TNE", /* unit */ SCPI_UNIT_KILOGRAM, /* mult */ 1000}, +#endif /* USE_UNITS_ENERGY_FORCE_MASS */ + +#if USE_UNITS_FREQUENCY + /* Frequency */ {/* name */ "HZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1}, {/* name */ "KHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e3}, {/* name */ "MHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e6}, {/* name */ "GHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e9}, - - /* temperature */ +#endif /* USE_UNITS_FREQUENCY */ + +#if USE_UNITS_DISTANCE + /* Length */ + {/* name */ "ASU", /* unit */ SCPI_UNIT_ASTRONOMIC_UNIT, /* mult */ 1}, + {/* name */ "PRS", /* unit */ SCPI_UNIT_PARSEC, /* mult */ 1}, +#if USE_UNITS_IMPERIAL + {/* name */ "IN", /* unit */ SCPI_UNIT_INCH, /* mult */ 1}, + {/* name */ "FT", /* unit */ SCPI_UNIT_FOOT, /* mult */ 1}, + {/* name */ "MI", /* unit */ SCPI_UNIT_MILE, /* mult */ 1}, + {/* name */ "NAMI", /* unit */ SCPI_UNIT_NAUTICAL_MILE, /* mult */ 1}, +#endif /* USE_UNITS_IMPERIAL */ + + {/* name */ "NM", /* unit */ SCPI_UNIT_METER, /* mult */ 1e-9}, + {/* name */ "UM", /* unit */ SCPI_UNIT_METER, /* mult */ 1e-6}, + {/* name */ "MM", /* unit */ SCPI_UNIT_METER, /* mult */ 1e-3}, + {/* name */ "M", /* unit */ SCPI_UNIT_METER, /* mult */ 1}, + {/* name */ "KM", /* unit */ SCPI_UNIT_METER, /* mult */ 1e3}, +#endif /* USE_UNITS_DISTANCE */ + +#if USE_UNITS_LIGHT + /* Illuminance */ + {/* name */ "LX", /* unit */ SCPI_UNIT_LUX, /* mult */ 1}, + + /* Luminous flux */ + {/* name */ "LM", /* unit */ SCPI_UNIT_LUMEN, /* mult */ 1}, + + /* Luminous intensity */ + {/* name */ "CD", /* unit */ SCPI_UNIT_CANDELA, /* mult */ 1}, +#endif /* USE_UNITS_LIGHT */ + +#if USE_UNITS_MAGNETIC + /* Magnetic flux */ + {/* name */ "WB", /* unit */ SCPI_UNIT_WEBER, /* mult */ 1}, + + /* Magnetic induction */ + {/* name */ "NT", /* unit */ SCPI_UNIT_TESLA, /* mult */ 1e-9}, + {/* name */ "UT", /* unit */ SCPI_UNIT_TESLA, /* mult */ 1e-6}, + {/* name */ "MT", /* unit */ SCPI_UNIT_TESLA, /* mult */ 1e-3}, + {/* name */ "T", /* unit */ SCPI_UNIT_TESLA, /* mult */ 1}, +#endif /* USE_UNITS_MAGNETIC */ + +#if USE_UNITS_POWER + /* Power */ + {/* name */ "W", /* unit */ SCPI_UNIT_WATT, /* mult */ 1}, + {/* name */ "DBM", /* unit */ SCPI_UNIT_DBM, /* mult */ 1}, + {/* name */ "DBMW", /* unit */ SCPI_UNIT_DBM, /* mult */ 1}, +#endif /* USE_UNITS_POWER */ + +#if USE_UNITS_RATIO + /* Ratio */ + {/* name */ "DB", /* unit */ SCPI_UNIT_DECIBEL, /* mult */ 1}, + {/* name */ "PCT", /* unit */ SCPI_UNIT_UNITLESS, /* mult */ 1e-2}, + {/* name */ "PPM", /* unit */ SCPI_UNIT_UNITLESS, /* mult */ 1e-6}, +#endif /* USE_UNITS_RATIO */ + +#if USE_UNITS_TEMPERATURE + /* Temperature */ {/* name */ "CEL", /* unit */ SCPI_UNIT_CELSIUS, /* mult */ 1}, - - /* time */ - {/* name */ "PS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-12}, - {/* name */ "NS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-9}, - {/* name */ "US", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-6}, - {/* name */ "MS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-3}, - {/* name */ "S", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1}, - {/* name */ "MIN", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 60}, - {/* name */ "HR", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 3600}, +#if USE_UNITS_IMPERIAL + {/* name */ "FAR", /* unit */ SCPI_UNIT_FAGRENHEIT, /* mult */ 1}, +#endif /* USE_UNITS_IMPERIAL */ + {/* name */ "K", /* unit */ SCPI_UNIT_KELVIN, /* mult */ 1}, +#endif /* USE_UNITS_TEMPERATURE */ + +#if USE_UNITS_TIME + /* Time */ + {/* name */ "PS", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1e-12}, + {/* name */ "NS", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1e-9}, + {/* name */ "US", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1e-6}, + {/* name */ "MS", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1e-3}, + {/* name */ "S", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1}, + {/* name */ "MIN", /* unit */ SCPI_UNIT_SECOND, /* mult */ 60}, + {/* name */ "HR", /* unit */ SCPI_UNIT_SECOND, /* mult */ 3600}, + {/* name */ "D", /* unit */ SCPI_UNIT_DAY, /* mult */ 1}, + {/* name */ "ANN", /* unit */ SCPI_UNIT_YEAR, /* mult */ 1}, +#endif /* USE_UNITS_TIME */ SCPI_UNITS_LIST_END, }; @@ -198,7 +366,7 @@ static scpi_bool_t transformNumber(scpi_t * context, const char * unit, size_t l * @param context * @param value return value * @param mandatory if the parameter is mandatory - * @return + * @return */ scpi_bool_t SCPI_ParamNumber(scpi_t * context, const scpi_choice_def_t * special, scpi_number_t * value, scpi_bool_t mandatory) { scpi_token_t token;