Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
libscpi/src/units.c@0:be95bfb06686, 2022-01-17 (annotated)
- Committer:
- wuliqunyy
- Date:
- Mon Jan 17 13:20:09 2022 +0000
- Revision:
- 0:be95bfb06686
a working non_flat + adc_didt for ehp regulation version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| wuliqunyy | 0:be95bfb06686 | 1 | /*- |
| wuliqunyy | 0:be95bfb06686 | 2 | * BSD 2-Clause License |
| wuliqunyy | 0:be95bfb06686 | 3 | * |
| wuliqunyy | 0:be95bfb06686 | 4 | * Copyright (c) 2012-2018, Jan Breuer |
| wuliqunyy | 0:be95bfb06686 | 5 | * All rights reserved. |
| wuliqunyy | 0:be95bfb06686 | 6 | * |
| wuliqunyy | 0:be95bfb06686 | 7 | * Redistribution and use in source and binary forms, with or without |
| wuliqunyy | 0:be95bfb06686 | 8 | * modification, are permitted provided that the following conditions are met: |
| wuliqunyy | 0:be95bfb06686 | 9 | * |
| wuliqunyy | 0:be95bfb06686 | 10 | * * Redistributions of source code must retain the above copyright notice, this |
| wuliqunyy | 0:be95bfb06686 | 11 | * list of conditions and the following disclaimer. |
| wuliqunyy | 0:be95bfb06686 | 12 | * |
| wuliqunyy | 0:be95bfb06686 | 13 | * * Redistributions in binary form must reproduce the above copyright notice, |
| wuliqunyy | 0:be95bfb06686 | 14 | * this list of conditions and the following disclaimer in the documentation |
| wuliqunyy | 0:be95bfb06686 | 15 | * and/or other materials provided with the distribution. |
| wuliqunyy | 0:be95bfb06686 | 16 | * |
| wuliqunyy | 0:be95bfb06686 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| wuliqunyy | 0:be95bfb06686 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| wuliqunyy | 0:be95bfb06686 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| wuliqunyy | 0:be95bfb06686 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| wuliqunyy | 0:be95bfb06686 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| wuliqunyy | 0:be95bfb06686 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| wuliqunyy | 0:be95bfb06686 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| wuliqunyy | 0:be95bfb06686 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| wuliqunyy | 0:be95bfb06686 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| wuliqunyy | 0:be95bfb06686 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| wuliqunyy | 0:be95bfb06686 | 27 | */ |
| wuliqunyy | 0:be95bfb06686 | 28 | |
| wuliqunyy | 0:be95bfb06686 | 29 | /** |
| wuliqunyy | 0:be95bfb06686 | 30 | * @file scpi_units.c |
| wuliqunyy | 0:be95bfb06686 | 31 | * @date Thu Nov 15 10:58:45 UTC 2012 |
| wuliqunyy | 0:be95bfb06686 | 32 | * |
| wuliqunyy | 0:be95bfb06686 | 33 | * @brief SCPI units |
| wuliqunyy | 0:be95bfb06686 | 34 | * |
| wuliqunyy | 0:be95bfb06686 | 35 | * |
| wuliqunyy | 0:be95bfb06686 | 36 | */ |
| wuliqunyy | 0:be95bfb06686 | 37 | |
| wuliqunyy | 0:be95bfb06686 | 38 | #include <string.h> |
| wuliqunyy | 0:be95bfb06686 | 39 | #include "scpi/parser.h" |
| wuliqunyy | 0:be95bfb06686 | 40 | #include "scpi/units.h" |
| wuliqunyy | 0:be95bfb06686 | 41 | #include "utils_private.h" |
| wuliqunyy | 0:be95bfb06686 | 42 | #include "scpi/utils.h" |
| wuliqunyy | 0:be95bfb06686 | 43 | #include "scpi/error.h" |
| wuliqunyy | 0:be95bfb06686 | 44 | #include "lexer_private.h" |
| wuliqunyy | 0:be95bfb06686 | 45 | |
| wuliqunyy | 0:be95bfb06686 | 46 | |
| wuliqunyy | 0:be95bfb06686 | 47 | /* |
| wuliqunyy | 0:be95bfb06686 | 48 | * multipliers IEEE 488.2-1992 tab 7-2 |
| wuliqunyy | 0:be95bfb06686 | 49 | * 1E18 EX |
| wuliqunyy | 0:be95bfb06686 | 50 | * 1E15 PE |
| wuliqunyy | 0:be95bfb06686 | 51 | * 1E12 T |
| wuliqunyy | 0:be95bfb06686 | 52 | * 1E9 G |
| wuliqunyy | 0:be95bfb06686 | 53 | * 1E6 MA (use M for OHM and HZ) |
| wuliqunyy | 0:be95bfb06686 | 54 | * 1E3 K |
| wuliqunyy | 0:be95bfb06686 | 55 | * 1E-3 M (disaalowed for OHM and HZ) |
| wuliqunyy | 0:be95bfb06686 | 56 | * 1E-6 U |
| wuliqunyy | 0:be95bfb06686 | 57 | * 1E-9 N |
| wuliqunyy | 0:be95bfb06686 | 58 | * 1E-12 P |
| wuliqunyy | 0:be95bfb06686 | 59 | * 1E-15 F |
| wuliqunyy | 0:be95bfb06686 | 60 | * 1E-18 A |
| wuliqunyy | 0:be95bfb06686 | 61 | */ |
| wuliqunyy | 0:be95bfb06686 | 62 | |
| wuliqunyy | 0:be95bfb06686 | 63 | /* |
| wuliqunyy | 0:be95bfb06686 | 64 | * units definition IEEE 488.2-1992 tab 7-1 |
| wuliqunyy | 0:be95bfb06686 | 65 | */ |
| wuliqunyy | 0:be95bfb06686 | 66 | const scpi_unit_def_t scpi_units_def[] = { |
| wuliqunyy | 0:be95bfb06686 | 67 | #if USE_UNITS_PARTICLES |
| wuliqunyy | 0:be95bfb06686 | 68 | /* Absorbet dose */ |
| wuliqunyy | 0:be95bfb06686 | 69 | {/* name */ "GY", /* unit */ SCPI_UNIT_GRAY, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 70 | |
| wuliqunyy | 0:be95bfb06686 | 71 | /* Activity of radionuclide */ |
| wuliqunyy | 0:be95bfb06686 | 72 | {/* name */ "BQ", /* unit */ SCPI_UNIT_BECQUEREL, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 73 | |
| wuliqunyy | 0:be95bfb06686 | 74 | /* Amount of substance */ |
| wuliqunyy | 0:be95bfb06686 | 75 | {/* name */ "MOL", /* unit */ SCPI_UNIT_MOLE, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 76 | |
| wuliqunyy | 0:be95bfb06686 | 77 | /* Dose equivalent */ |
| wuliqunyy | 0:be95bfb06686 | 78 | {/* name */ "NSV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e-9}, |
| wuliqunyy | 0:be95bfb06686 | 79 | {/* name */ "USV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 80 | {/* name */ "MSV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 81 | {/* name */ "SV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 82 | {/* name */ "KSV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 83 | {/* name */ "MASV", /* unit */ SCPI_UNIT_SIEVERT, /* mult */ 1e6}, |
| wuliqunyy | 0:be95bfb06686 | 84 | |
| wuliqunyy | 0:be95bfb06686 | 85 | /* Energy */ |
| wuliqunyy | 0:be95bfb06686 | 86 | {/* name */ "EV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 87 | {/* name */ "KEV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 88 | {/* name */ "MAEV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1e6}, |
| wuliqunyy | 0:be95bfb06686 | 89 | {/* name */ "GEV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1e9}, |
| wuliqunyy | 0:be95bfb06686 | 90 | {/* name */ "TEV", /* unit */ SCPI_UNIT_ELECTRONVOLT, /* mult */ 1e12}, |
| wuliqunyy | 0:be95bfb06686 | 91 | |
| wuliqunyy | 0:be95bfb06686 | 92 | /* Mass */ |
| wuliqunyy | 0:be95bfb06686 | 93 | {/* name */ "U", /* unit */ SCPI_UNIT_ATOMIC_MASS, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 94 | #endif /* USE_UNITS_PARTICLES */ |
| wuliqunyy | 0:be95bfb06686 | 95 | |
| wuliqunyy | 0:be95bfb06686 | 96 | #if USE_UNITS_ANGLE |
| wuliqunyy | 0:be95bfb06686 | 97 | /* Angle */ |
| wuliqunyy | 0:be95bfb06686 | 98 | {/* name */ "DEG", /* unit */ SCPI_UNIT_DEGREE, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 99 | {/* name */ "GON", /* unit */ SCPI_UNIT_GRADE, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 100 | {/* name */ "MNT", /* unit */ SCPI_UNIT_DEGREE, /* mult */ 1. / 60.}, |
| wuliqunyy | 0:be95bfb06686 | 101 | {/* name */ "RAD", /* unit */ SCPI_UNIT_RADIAN, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 102 | {/* name */ "SEC", /* unit */ SCPI_UNIT_DEGREE, /* mult */ 1. / 3600.}, |
| wuliqunyy | 0:be95bfb06686 | 103 | {/* name */ "REV", /* unit */ SCPI_UNIT_REVOLUTION, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 104 | {/* name */ "RS", /* unit */ SCPI_UNIT_STERADIAN, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 105 | #endif /* USE_UNITS_ANGLE */ |
| wuliqunyy | 0:be95bfb06686 | 106 | |
| wuliqunyy | 0:be95bfb06686 | 107 | #if USE_UNITS_ELECTRIC |
| wuliqunyy | 0:be95bfb06686 | 108 | /* Electric - capacitance */ |
| wuliqunyy | 0:be95bfb06686 | 109 | {/* name */ "PF", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1e-12}, |
| wuliqunyy | 0:be95bfb06686 | 110 | {/* name */ "NF", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1e-9}, |
| wuliqunyy | 0:be95bfb06686 | 111 | {/* name */ "UF", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 112 | {/* name */ "MF", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 113 | {/* name */ "F", /* unit */ SCPI_UNIT_FARAD, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 114 | |
| wuliqunyy | 0:be95bfb06686 | 115 | /* Electric - current */ |
| wuliqunyy | 0:be95bfb06686 | 116 | {/* name */ "UA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 117 | {/* name */ "MA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 118 | {/* name */ "A", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 119 | {/* name */ "KA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 120 | |
| wuliqunyy | 0:be95bfb06686 | 121 | /* Electric - potential */ |
| wuliqunyy | 0:be95bfb06686 | 122 | {/* name */ "UV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 123 | {/* name */ "MV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 124 | {/* name */ "V", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 125 | {/* name */ "KV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 126 | |
| wuliqunyy | 0:be95bfb06686 | 127 | /* Electric - resistance */ |
| wuliqunyy | 0:be95bfb06686 | 128 | {/* name */ "OHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 129 | {/* name */ "KOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 130 | {/* name */ "MOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e6}, |
| wuliqunyy | 0:be95bfb06686 | 131 | |
| wuliqunyy | 0:be95bfb06686 | 132 | /* Inductance */ |
| wuliqunyy | 0:be95bfb06686 | 133 | {/* name */ "UH", /* unit */ SCPI_UNIT_HENRY, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 134 | {/* name */ "MH", /* unit */ SCPI_UNIT_HENRY, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 135 | {/* name */ "H", /* unit */ SCPI_UNIT_HENRY, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 136 | #endif /* USE_UNITS_ELECTRIC */ |
| wuliqunyy | 0:be95bfb06686 | 137 | |
| wuliqunyy | 0:be95bfb06686 | 138 | #if USE_UNITS_ELECTRIC_CHARGE_CONDUCTANCE |
| wuliqunyy | 0:be95bfb06686 | 139 | /* Electric - charge */ |
| wuliqunyy | 0:be95bfb06686 | 140 | {/* name */ "C", /* unit */ SCPI_UNIT_COULOMB, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 141 | |
| wuliqunyy | 0:be95bfb06686 | 142 | /* Electric - conductance */ |
| wuliqunyy | 0:be95bfb06686 | 143 | {/* name */ "USIE", /* unit */ SCPI_UNIT_SIEMENS, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 144 | {/* name */ "MSIE", /* unit */ SCPI_UNIT_SIEMENS, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 145 | {/* name */ "SIE", /* unit */ SCPI_UNIT_SIEMENS, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 146 | #endif /* USE_UNITS_ELECTRIC_CHARGE_CONDUCTANCE */ |
| wuliqunyy | 0:be95bfb06686 | 147 | |
| wuliqunyy | 0:be95bfb06686 | 148 | #if USE_UNITS_ENERGY_FORCE_MASS |
| wuliqunyy | 0:be95bfb06686 | 149 | /* Energy */ |
| wuliqunyy | 0:be95bfb06686 | 150 | {/* name */ "J", /* unit */ SCPI_UNIT_JOULE, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 151 | {/* name */ "KJ", /* unit */ SCPI_UNIT_JOULE, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 152 | {/* name */ "MAJ", /* unit */ SCPI_UNIT_JOULE, /* mult */ 1e6}, |
| wuliqunyy | 0:be95bfb06686 | 153 | |
| wuliqunyy | 0:be95bfb06686 | 154 | /* Force */ |
| wuliqunyy | 0:be95bfb06686 | 155 | {/* name */ "N", /* unit */ SCPI_UNIT_NEWTON, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 156 | {/* name */ "KN", /* unit */ SCPI_UNIT_NEWTON, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 157 | |
| wuliqunyy | 0:be95bfb06686 | 158 | /* Pressure */ |
| wuliqunyy | 0:be95bfb06686 | 159 | {/* name */ "ATM", /* unit */ SCPI_UNIT_ATMOSPHERE, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 160 | {/* name */ "INHG", /* unit */ SCPI_UNIT_INCH_OF_MERCURY, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 161 | {/* name */ "MMHG", /* unit */ SCPI_UNIT_MM_OF_MERCURY, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 162 | |
| wuliqunyy | 0:be95bfb06686 | 163 | {/* name */ "TORR", /* unit */ SCPI_UNIT_TORT, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 164 | {/* name */ "BAR", /* unit */ SCPI_UNIT_BAR, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 165 | |
| wuliqunyy | 0:be95bfb06686 | 166 | {/* name */ "PAL", /* unit */ SCPI_UNIT_PASCAL, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 167 | {/* name */ "KPAL", /* unit */ SCPI_UNIT_PASCAL, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 168 | {/* name */ "MAPAL", /* unit */ SCPI_UNIT_PASCAL, /* mult */ 1e6}, |
| wuliqunyy | 0:be95bfb06686 | 169 | |
| wuliqunyy | 0:be95bfb06686 | 170 | /* Viscosity kinematic */ |
| wuliqunyy | 0:be95bfb06686 | 171 | {/* name */ "ST", /* unit */ SCPI_UNIT_STROKES, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 172 | |
| wuliqunyy | 0:be95bfb06686 | 173 | /* Viscosity dynamic */ |
| wuliqunyy | 0:be95bfb06686 | 174 | {/* name */ "P", /* unit */ SCPI_UNIT_POISE, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 175 | |
| wuliqunyy | 0:be95bfb06686 | 176 | /* Viscosity dynamic */ |
| wuliqunyy | 0:be95bfb06686 | 177 | {/* name */ "L", /* unit */ SCPI_UNIT_LITER, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 178 | |
| wuliqunyy | 0:be95bfb06686 | 179 | /* Mass */ |
| wuliqunyy | 0:be95bfb06686 | 180 | {/* name */ "MG", /* unit */ SCPI_UNIT_KILOGRAM, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 181 | {/* name */ "G", /* unit */ SCPI_UNIT_KILOGRAM, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 182 | {/* name */ "KG", /* unit */ SCPI_UNIT_KILOGRAM, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 183 | {/* name */ "TNE", /* unit */ SCPI_UNIT_KILOGRAM, /* mult */ 1000}, |
| wuliqunyy | 0:be95bfb06686 | 184 | #endif /* USE_UNITS_ENERGY_FORCE_MASS */ |
| wuliqunyy | 0:be95bfb06686 | 185 | |
| wuliqunyy | 0:be95bfb06686 | 186 | #if USE_UNITS_FREQUENCY |
| wuliqunyy | 0:be95bfb06686 | 187 | /* Frequency */ |
| wuliqunyy | 0:be95bfb06686 | 188 | {/* name */ "HZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 189 | {/* name */ "KHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 190 | {/* name */ "MHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e6}, |
| wuliqunyy | 0:be95bfb06686 | 191 | {/* name */ "GHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e9}, |
| wuliqunyy | 0:be95bfb06686 | 192 | #endif /* USE_UNITS_FREQUENCY */ |
| wuliqunyy | 0:be95bfb06686 | 193 | |
| wuliqunyy | 0:be95bfb06686 | 194 | #if USE_UNITS_DISTANCE |
| wuliqunyy | 0:be95bfb06686 | 195 | /* Length */ |
| wuliqunyy | 0:be95bfb06686 | 196 | {/* name */ "ASU", /* unit */ SCPI_UNIT_ASTRONOMIC_UNIT, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 197 | {/* name */ "PRS", /* unit */ SCPI_UNIT_PARSEC, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 198 | #if USE_UNITS_IMPERIAL |
| wuliqunyy | 0:be95bfb06686 | 199 | {/* name */ "IN", /* unit */ SCPI_UNIT_INCH, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 200 | {/* name */ "FT", /* unit */ SCPI_UNIT_FOOT, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 201 | {/* name */ "MI", /* unit */ SCPI_UNIT_MILE, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 202 | {/* name */ "NAMI", /* unit */ SCPI_UNIT_NAUTICAL_MILE, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 203 | #endif /* USE_UNITS_IMPERIAL */ |
| wuliqunyy | 0:be95bfb06686 | 204 | |
| wuliqunyy | 0:be95bfb06686 | 205 | {/* name */ "NM", /* unit */ SCPI_UNIT_METER, /* mult */ 1e-9}, |
| wuliqunyy | 0:be95bfb06686 | 206 | {/* name */ "UM", /* unit */ SCPI_UNIT_METER, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 207 | {/* name */ "MM", /* unit */ SCPI_UNIT_METER, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 208 | {/* name */ "M", /* unit */ SCPI_UNIT_METER, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 209 | {/* name */ "KM", /* unit */ SCPI_UNIT_METER, /* mult */ 1e3}, |
| wuliqunyy | 0:be95bfb06686 | 210 | #endif /* USE_UNITS_DISTANCE */ |
| wuliqunyy | 0:be95bfb06686 | 211 | |
| wuliqunyy | 0:be95bfb06686 | 212 | #if USE_UNITS_LIGHT |
| wuliqunyy | 0:be95bfb06686 | 213 | /* Illuminance */ |
| wuliqunyy | 0:be95bfb06686 | 214 | {/* name */ "LX", /* unit */ SCPI_UNIT_LUX, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 215 | |
| wuliqunyy | 0:be95bfb06686 | 216 | /* Luminous flux */ |
| wuliqunyy | 0:be95bfb06686 | 217 | {/* name */ "LM", /* unit */ SCPI_UNIT_LUMEN, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 218 | |
| wuliqunyy | 0:be95bfb06686 | 219 | /* Luminous intensity */ |
| wuliqunyy | 0:be95bfb06686 | 220 | {/* name */ "CD", /* unit */ SCPI_UNIT_CANDELA, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 221 | #endif /* USE_UNITS_LIGHT */ |
| wuliqunyy | 0:be95bfb06686 | 222 | |
| wuliqunyy | 0:be95bfb06686 | 223 | #if USE_UNITS_MAGNETIC |
| wuliqunyy | 0:be95bfb06686 | 224 | /* Magnetic flux */ |
| wuliqunyy | 0:be95bfb06686 | 225 | {/* name */ "WB", /* unit */ SCPI_UNIT_WEBER, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 226 | |
| wuliqunyy | 0:be95bfb06686 | 227 | /* Magnetic induction */ |
| wuliqunyy | 0:be95bfb06686 | 228 | {/* name */ "NT", /* unit */ SCPI_UNIT_TESLA, /* mult */ 1e-9}, |
| wuliqunyy | 0:be95bfb06686 | 229 | {/* name */ "UT", /* unit */ SCPI_UNIT_TESLA, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 230 | {/* name */ "MT", /* unit */ SCPI_UNIT_TESLA, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 231 | {/* name */ "T", /* unit */ SCPI_UNIT_TESLA, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 232 | #endif /* USE_UNITS_MAGNETIC */ |
| wuliqunyy | 0:be95bfb06686 | 233 | |
| wuliqunyy | 0:be95bfb06686 | 234 | #if USE_UNITS_POWER |
| wuliqunyy | 0:be95bfb06686 | 235 | /* Power */ |
| wuliqunyy | 0:be95bfb06686 | 236 | {/* name */ "W", /* unit */ SCPI_UNIT_WATT, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 237 | {/* name */ "DBM", /* unit */ SCPI_UNIT_DBM, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 238 | {/* name */ "DBMW", /* unit */ SCPI_UNIT_DBM, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 239 | #endif /* USE_UNITS_POWER */ |
| wuliqunyy | 0:be95bfb06686 | 240 | |
| wuliqunyy | 0:be95bfb06686 | 241 | #if USE_UNITS_RATIO |
| wuliqunyy | 0:be95bfb06686 | 242 | /* Ratio */ |
| wuliqunyy | 0:be95bfb06686 | 243 | {/* name */ "DB", /* unit */ SCPI_UNIT_DECIBEL, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 244 | {/* name */ "PCT", /* unit */ SCPI_UNIT_UNITLESS, /* mult */ 1e-2}, |
| wuliqunyy | 0:be95bfb06686 | 245 | {/* name */ "PPM", /* unit */ SCPI_UNIT_UNITLESS, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 246 | #endif /* USE_UNITS_RATIO */ |
| wuliqunyy | 0:be95bfb06686 | 247 | |
| wuliqunyy | 0:be95bfb06686 | 248 | #if USE_UNITS_TEMPERATURE |
| wuliqunyy | 0:be95bfb06686 | 249 | /* Temperature */ |
| wuliqunyy | 0:be95bfb06686 | 250 | {/* name */ "CEL", /* unit */ SCPI_UNIT_CELSIUS, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 251 | #if USE_UNITS_IMPERIAL |
| wuliqunyy | 0:be95bfb06686 | 252 | {/* name */ "FAR", /* unit */ SCPI_UNIT_FAHRENHEIT, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 253 | #endif /* USE_UNITS_IMPERIAL */ |
| wuliqunyy | 0:be95bfb06686 | 254 | {/* name */ "K", /* unit */ SCPI_UNIT_KELVIN, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 255 | #endif /* USE_UNITS_TEMPERATURE */ |
| wuliqunyy | 0:be95bfb06686 | 256 | |
| wuliqunyy | 0:be95bfb06686 | 257 | #if USE_UNITS_TIME |
| wuliqunyy | 0:be95bfb06686 | 258 | /* Time */ |
| wuliqunyy | 0:be95bfb06686 | 259 | {/* name */ "PS", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1e-12}, |
| wuliqunyy | 0:be95bfb06686 | 260 | {/* name */ "NS", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1e-9}, |
| wuliqunyy | 0:be95bfb06686 | 261 | {/* name */ "US", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1e-6}, |
| wuliqunyy | 0:be95bfb06686 | 262 | {/* name */ "MS", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1e-3}, |
| wuliqunyy | 0:be95bfb06686 | 263 | {/* name */ "S", /* unit */ SCPI_UNIT_SECOND, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 264 | {/* name */ "MIN", /* unit */ SCPI_UNIT_SECOND, /* mult */ 60}, |
| wuliqunyy | 0:be95bfb06686 | 265 | {/* name */ "HR", /* unit */ SCPI_UNIT_SECOND, /* mult */ 3600}, |
| wuliqunyy | 0:be95bfb06686 | 266 | {/* name */ "D", /* unit */ SCPI_UNIT_DAY, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 267 | {/* name */ "ANN", /* unit */ SCPI_UNIT_YEAR, /* mult */ 1}, |
| wuliqunyy | 0:be95bfb06686 | 268 | #endif /* USE_UNITS_TIME */ |
| wuliqunyy | 0:be95bfb06686 | 269 | |
| wuliqunyy | 0:be95bfb06686 | 270 | SCPI_UNITS_LIST_END, |
| wuliqunyy | 0:be95bfb06686 | 271 | }; |
| wuliqunyy | 0:be95bfb06686 | 272 | |
| wuliqunyy | 0:be95bfb06686 | 273 | /* |
| wuliqunyy | 0:be95bfb06686 | 274 | * Special number values definition |
| wuliqunyy | 0:be95bfb06686 | 275 | */ |
| wuliqunyy | 0:be95bfb06686 | 276 | const scpi_choice_def_t scpi_special_numbers_def[] = { |
| wuliqunyy | 0:be95bfb06686 | 277 | {/* name */ "MINimum", /* type */ SCPI_NUM_MIN}, |
| wuliqunyy | 0:be95bfb06686 | 278 | {/* name */ "MAXimum", /* type */ SCPI_NUM_MAX}, |
| wuliqunyy | 0:be95bfb06686 | 279 | {/* name */ "DEFault", /* type */ SCPI_NUM_DEF}, |
| wuliqunyy | 0:be95bfb06686 | 280 | {/* name */ "UP", /* type */ SCPI_NUM_UP}, |
| wuliqunyy | 0:be95bfb06686 | 281 | {/* name */ "DOWN", /* type */ SCPI_NUM_DOWN}, |
| wuliqunyy | 0:be95bfb06686 | 282 | {/* name */ "NAN", /* type */ SCPI_NUM_NAN}, |
| wuliqunyy | 0:be95bfb06686 | 283 | {/* name */ "INFinity", /* type */ SCPI_NUM_INF}, |
| wuliqunyy | 0:be95bfb06686 | 284 | {/* name */ "NINF", /* type */ SCPI_NUM_NINF}, |
| wuliqunyy | 0:be95bfb06686 | 285 | {/* name */ "AUTO", /* type */ SCPI_NUM_AUTO}, |
| wuliqunyy | 0:be95bfb06686 | 286 | SCPI_CHOICE_LIST_END, |
| wuliqunyy | 0:be95bfb06686 | 287 | }; |
| wuliqunyy | 0:be95bfb06686 | 288 | |
| wuliqunyy | 0:be95bfb06686 | 289 | /** |
| wuliqunyy | 0:be95bfb06686 | 290 | * Convert string describing unit to its representation |
| wuliqunyy | 0:be95bfb06686 | 291 | * @param units units patterns |
| wuliqunyy | 0:be95bfb06686 | 292 | * @param unit text representation of unknown unit |
| wuliqunyy | 0:be95bfb06686 | 293 | * @param len length of text representation |
| wuliqunyy | 0:be95bfb06686 | 294 | * @return pointer of related unit definition or NULL |
| wuliqunyy | 0:be95bfb06686 | 295 | */ |
| wuliqunyy | 0:be95bfb06686 | 296 | static const scpi_unit_def_t * translateUnit(const scpi_unit_def_t * units, const char * unit, size_t len) { |
| wuliqunyy | 0:be95bfb06686 | 297 | int i; |
| wuliqunyy | 0:be95bfb06686 | 298 | |
| wuliqunyy | 0:be95bfb06686 | 299 | if (units == NULL) { |
| wuliqunyy | 0:be95bfb06686 | 300 | return NULL; |
| wuliqunyy | 0:be95bfb06686 | 301 | } |
| wuliqunyy | 0:be95bfb06686 | 302 | |
| wuliqunyy | 0:be95bfb06686 | 303 | for (i = 0; units[i].name != NULL; i++) { |
| wuliqunyy | 0:be95bfb06686 | 304 | if (compareStr(unit, len, units[i].name, strlen(units[i].name))) { |
| wuliqunyy | 0:be95bfb06686 | 305 | return &units[i]; |
| wuliqunyy | 0:be95bfb06686 | 306 | } |
| wuliqunyy | 0:be95bfb06686 | 307 | } |
| wuliqunyy | 0:be95bfb06686 | 308 | |
| wuliqunyy | 0:be95bfb06686 | 309 | return NULL; |
| wuliqunyy | 0:be95bfb06686 | 310 | } |
| wuliqunyy | 0:be95bfb06686 | 311 | |
| wuliqunyy | 0:be95bfb06686 | 312 | /** |
| wuliqunyy | 0:be95bfb06686 | 313 | * Convert unit definition to string |
| wuliqunyy | 0:be95bfb06686 | 314 | * @param units units definitions (patterns) |
| wuliqunyy | 0:be95bfb06686 | 315 | * @param unit type of unit |
| wuliqunyy | 0:be95bfb06686 | 316 | * @return string representation of unit |
| wuliqunyy | 0:be95bfb06686 | 317 | */ |
| wuliqunyy | 0:be95bfb06686 | 318 | static const char * translateUnitInverse(const scpi_unit_def_t * units, const scpi_unit_t unit) { |
| wuliqunyy | 0:be95bfb06686 | 319 | int i; |
| wuliqunyy | 0:be95bfb06686 | 320 | |
| wuliqunyy | 0:be95bfb06686 | 321 | if (units == NULL) { |
| wuliqunyy | 0:be95bfb06686 | 322 | return NULL; |
| wuliqunyy | 0:be95bfb06686 | 323 | } |
| wuliqunyy | 0:be95bfb06686 | 324 | |
| wuliqunyy | 0:be95bfb06686 | 325 | for (i = 0; units[i].name != NULL; i++) { |
| wuliqunyy | 0:be95bfb06686 | 326 | if ((units[i].unit == unit) && (units[i].mult == 1)) { |
| wuliqunyy | 0:be95bfb06686 | 327 | return units[i].name; |
| wuliqunyy | 0:be95bfb06686 | 328 | } |
| wuliqunyy | 0:be95bfb06686 | 329 | } |
| wuliqunyy | 0:be95bfb06686 | 330 | |
| wuliqunyy | 0:be95bfb06686 | 331 | return NULL; |
| wuliqunyy | 0:be95bfb06686 | 332 | } |
| wuliqunyy | 0:be95bfb06686 | 333 | |
| wuliqunyy | 0:be95bfb06686 | 334 | /** |
| wuliqunyy | 0:be95bfb06686 | 335 | * Transform number to base units |
| wuliqunyy | 0:be95bfb06686 | 336 | * @param context |
| wuliqunyy | 0:be95bfb06686 | 337 | * @param unit text representation of unit |
| wuliqunyy | 0:be95bfb06686 | 338 | * @param len length of text representation |
| wuliqunyy | 0:be95bfb06686 | 339 | * @param value preparsed numeric value |
| wuliqunyy | 0:be95bfb06686 | 340 | * @return TRUE if value parameter was converted to base units |
| wuliqunyy | 0:be95bfb06686 | 341 | */ |
| wuliqunyy | 0:be95bfb06686 | 342 | static scpi_bool_t transformNumber(scpi_t * context, const char * unit, size_t len, scpi_number_t * value) { |
| wuliqunyy | 0:be95bfb06686 | 343 | size_t s; |
| wuliqunyy | 0:be95bfb06686 | 344 | const scpi_unit_def_t * unitDef; |
| wuliqunyy | 0:be95bfb06686 | 345 | s = skipWhitespace(unit, len); |
| wuliqunyy | 0:be95bfb06686 | 346 | |
| wuliqunyy | 0:be95bfb06686 | 347 | if (s == len) { |
| wuliqunyy | 0:be95bfb06686 | 348 | value->unit = SCPI_UNIT_NONE; |
| wuliqunyy | 0:be95bfb06686 | 349 | return TRUE; |
| wuliqunyy | 0:be95bfb06686 | 350 | } |
| wuliqunyy | 0:be95bfb06686 | 351 | |
| wuliqunyy | 0:be95bfb06686 | 352 | unitDef = translateUnit(context->units, unit + s, len - s); |
| wuliqunyy | 0:be95bfb06686 | 353 | |
| wuliqunyy | 0:be95bfb06686 | 354 | if (unitDef == NULL) { |
| wuliqunyy | 0:be95bfb06686 | 355 | SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SUFFIX); |
| wuliqunyy | 0:be95bfb06686 | 356 | return FALSE; |
| wuliqunyy | 0:be95bfb06686 | 357 | } |
| wuliqunyy | 0:be95bfb06686 | 358 | |
| wuliqunyy | 0:be95bfb06686 | 359 | value->content.value *= unitDef->mult; |
| wuliqunyy | 0:be95bfb06686 | 360 | value->unit = unitDef->unit; |
| wuliqunyy | 0:be95bfb06686 | 361 | |
| wuliqunyy | 0:be95bfb06686 | 362 | return TRUE; |
| wuliqunyy | 0:be95bfb06686 | 363 | } |
| wuliqunyy | 0:be95bfb06686 | 364 | |
| wuliqunyy | 0:be95bfb06686 | 365 | /** |
| wuliqunyy | 0:be95bfb06686 | 366 | * Parse parameter as number, number with unit or special value (min, max, default, ...) |
| wuliqunyy | 0:be95bfb06686 | 367 | * @param context |
| wuliqunyy | 0:be95bfb06686 | 368 | * @param value return value |
| wuliqunyy | 0:be95bfb06686 | 369 | * @param mandatory if the parameter is mandatory |
| wuliqunyy | 0:be95bfb06686 | 370 | * @return |
| wuliqunyy | 0:be95bfb06686 | 371 | */ |
| wuliqunyy | 0:be95bfb06686 | 372 | scpi_bool_t SCPI_ParamNumber(scpi_t * context, const scpi_choice_def_t * special, scpi_number_t * value, scpi_bool_t mandatory) { |
| wuliqunyy | 0:be95bfb06686 | 373 | scpi_token_t token; |
| wuliqunyy | 0:be95bfb06686 | 374 | lex_state_t state; |
| wuliqunyy | 0:be95bfb06686 | 375 | scpi_parameter_t param; |
| wuliqunyy | 0:be95bfb06686 | 376 | scpi_bool_t result; |
| wuliqunyy | 0:be95bfb06686 | 377 | int32_t tag; |
| wuliqunyy | 0:be95bfb06686 | 378 | |
| wuliqunyy | 0:be95bfb06686 | 379 | if (!value) { |
| wuliqunyy | 0:be95bfb06686 | 380 | SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR); |
| wuliqunyy | 0:be95bfb06686 | 381 | return FALSE; |
| wuliqunyy | 0:be95bfb06686 | 382 | } |
| wuliqunyy | 0:be95bfb06686 | 383 | |
| wuliqunyy | 0:be95bfb06686 | 384 | result = SCPI_Parameter(context, ¶m, mandatory); |
| wuliqunyy | 0:be95bfb06686 | 385 | |
| wuliqunyy | 0:be95bfb06686 | 386 | if (!result) { |
| wuliqunyy | 0:be95bfb06686 | 387 | return result; |
| wuliqunyy | 0:be95bfb06686 | 388 | } |
| wuliqunyy | 0:be95bfb06686 | 389 | |
| wuliqunyy | 0:be95bfb06686 | 390 | state.buffer = param.ptr; |
| wuliqunyy | 0:be95bfb06686 | 391 | state.pos = state.buffer; |
| wuliqunyy | 0:be95bfb06686 | 392 | state.len = param.len; |
| wuliqunyy | 0:be95bfb06686 | 393 | |
| wuliqunyy | 0:be95bfb06686 | 394 | switch (param.type) { |
| wuliqunyy | 0:be95bfb06686 | 395 | case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA: |
| wuliqunyy | 0:be95bfb06686 | 396 | case SCPI_TOKEN_HEXNUM: |
| wuliqunyy | 0:be95bfb06686 | 397 | case SCPI_TOKEN_OCTNUM: |
| wuliqunyy | 0:be95bfb06686 | 398 | case SCPI_TOKEN_BINNUM: |
| wuliqunyy | 0:be95bfb06686 | 399 | case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX: |
| wuliqunyy | 0:be95bfb06686 | 400 | case SCPI_TOKEN_PROGRAM_MNEMONIC: |
| wuliqunyy | 0:be95bfb06686 | 401 | value->unit = SCPI_UNIT_NONE; |
| wuliqunyy | 0:be95bfb06686 | 402 | value->special = FALSE; |
| wuliqunyy | 0:be95bfb06686 | 403 | result = TRUE; |
| wuliqunyy | 0:be95bfb06686 | 404 | break; |
| wuliqunyy | 0:be95bfb06686 | 405 | default: |
| wuliqunyy | 0:be95bfb06686 | 406 | break; |
| wuliqunyy | 0:be95bfb06686 | 407 | } |
| wuliqunyy | 0:be95bfb06686 | 408 | |
| wuliqunyy | 0:be95bfb06686 | 409 | switch (param.type) { |
| wuliqunyy | 0:be95bfb06686 | 410 | case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA: |
| wuliqunyy | 0:be95bfb06686 | 411 | case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX: |
| wuliqunyy | 0:be95bfb06686 | 412 | case SCPI_TOKEN_PROGRAM_MNEMONIC: |
| wuliqunyy | 0:be95bfb06686 | 413 | value->base = 10; |
| wuliqunyy | 0:be95bfb06686 | 414 | break; |
| wuliqunyy | 0:be95bfb06686 | 415 | case SCPI_TOKEN_BINNUM: |
| wuliqunyy | 0:be95bfb06686 | 416 | value->base = 2; |
| wuliqunyy | 0:be95bfb06686 | 417 | break; |
| wuliqunyy | 0:be95bfb06686 | 418 | case SCPI_TOKEN_HEXNUM: |
| wuliqunyy | 0:be95bfb06686 | 419 | value->base = 16; |
| wuliqunyy | 0:be95bfb06686 | 420 | break; |
| wuliqunyy | 0:be95bfb06686 | 421 | case SCPI_TOKEN_OCTNUM: |
| wuliqunyy | 0:be95bfb06686 | 422 | value->base = 8; |
| wuliqunyy | 0:be95bfb06686 | 423 | break; |
| wuliqunyy | 0:be95bfb06686 | 424 | default: |
| wuliqunyy | 0:be95bfb06686 | 425 | break; |
| wuliqunyy | 0:be95bfb06686 | 426 | } |
| wuliqunyy | 0:be95bfb06686 | 427 | |
| wuliqunyy | 0:be95bfb06686 | 428 | switch (param.type) { |
| wuliqunyy | 0:be95bfb06686 | 429 | case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA: |
| wuliqunyy | 0:be95bfb06686 | 430 | SCPI_ParamToDouble(context, ¶m, &(value->content.value)); |
| wuliqunyy | 0:be95bfb06686 | 431 | break; |
| wuliqunyy | 0:be95bfb06686 | 432 | case SCPI_TOKEN_HEXNUM: |
| wuliqunyy | 0:be95bfb06686 | 433 | SCPI_ParamToDouble(context, ¶m, &(value->content.value)); |
| wuliqunyy | 0:be95bfb06686 | 434 | break; |
| wuliqunyy | 0:be95bfb06686 | 435 | case SCPI_TOKEN_OCTNUM: |
| wuliqunyy | 0:be95bfb06686 | 436 | SCPI_ParamToDouble(context, ¶m, &(value->content.value)); |
| wuliqunyy | 0:be95bfb06686 | 437 | break; |
| wuliqunyy | 0:be95bfb06686 | 438 | case SCPI_TOKEN_BINNUM: |
| wuliqunyy | 0:be95bfb06686 | 439 | SCPI_ParamToDouble(context, ¶m, &(value->content.value)); |
| wuliqunyy | 0:be95bfb06686 | 440 | break; |
| wuliqunyy | 0:be95bfb06686 | 441 | case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX: |
| wuliqunyy | 0:be95bfb06686 | 442 | scpiLex_DecimalNumericProgramData(&state, &token); |
| wuliqunyy | 0:be95bfb06686 | 443 | scpiLex_WhiteSpace(&state, &token); |
| wuliqunyy | 0:be95bfb06686 | 444 | scpiLex_SuffixProgramData(&state, &token); |
| wuliqunyy | 0:be95bfb06686 | 445 | |
| wuliqunyy | 0:be95bfb06686 | 446 | SCPI_ParamToDouble(context, ¶m, &(value->content.value)); |
| wuliqunyy | 0:be95bfb06686 | 447 | |
| wuliqunyy | 0:be95bfb06686 | 448 | result = transformNumber(context, token.ptr, token.len, value); |
| wuliqunyy | 0:be95bfb06686 | 449 | break; |
| wuliqunyy | 0:be95bfb06686 | 450 | case SCPI_TOKEN_PROGRAM_MNEMONIC: |
| wuliqunyy | 0:be95bfb06686 | 451 | scpiLex_WhiteSpace(&state, &token); |
| wuliqunyy | 0:be95bfb06686 | 452 | scpiLex_CharacterProgramData(&state, &token); |
| wuliqunyy | 0:be95bfb06686 | 453 | |
| wuliqunyy | 0:be95bfb06686 | 454 | /* convert string to special number type */ |
| wuliqunyy | 0:be95bfb06686 | 455 | result = SCPI_ParamToChoice(context, &token, special, &tag); |
| wuliqunyy | 0:be95bfb06686 | 456 | |
| wuliqunyy | 0:be95bfb06686 | 457 | value->special = TRUE; |
| wuliqunyy | 0:be95bfb06686 | 458 | value->content.tag = tag; |
| wuliqunyy | 0:be95bfb06686 | 459 | |
| wuliqunyy | 0:be95bfb06686 | 460 | break; |
| wuliqunyy | 0:be95bfb06686 | 461 | default: |
| wuliqunyy | 0:be95bfb06686 | 462 | result = FALSE; |
| wuliqunyy | 0:be95bfb06686 | 463 | } |
| wuliqunyy | 0:be95bfb06686 | 464 | |
| wuliqunyy | 0:be95bfb06686 | 465 | return result; |
| wuliqunyy | 0:be95bfb06686 | 466 | } |
| wuliqunyy | 0:be95bfb06686 | 467 | |
| wuliqunyy | 0:be95bfb06686 | 468 | /** |
| wuliqunyy | 0:be95bfb06686 | 469 | * Convert scpi_number_t to string |
| wuliqunyy | 0:be95bfb06686 | 470 | * @param context |
| wuliqunyy | 0:be95bfb06686 | 471 | * @param value number value |
| wuliqunyy | 0:be95bfb06686 | 472 | * @param str target string |
| wuliqunyy | 0:be95bfb06686 | 473 | * @param len max length of string including null-character termination |
| wuliqunyy | 0:be95bfb06686 | 474 | * @return number of chars written to string |
| wuliqunyy | 0:be95bfb06686 | 475 | */ |
| wuliqunyy | 0:be95bfb06686 | 476 | size_t SCPI_NumberToStr(scpi_t * context, const scpi_choice_def_t * special, scpi_number_t * value, char * str, size_t len) { |
| wuliqunyy | 0:be95bfb06686 | 477 | const char * type; |
| wuliqunyy | 0:be95bfb06686 | 478 | const char * unit; |
| wuliqunyy | 0:be95bfb06686 | 479 | size_t result; |
| wuliqunyy | 0:be95bfb06686 | 480 | |
| wuliqunyy | 0:be95bfb06686 | 481 | if (!value || !str || len==0) { |
| wuliqunyy | 0:be95bfb06686 | 482 | return 0; |
| wuliqunyy | 0:be95bfb06686 | 483 | } |
| wuliqunyy | 0:be95bfb06686 | 484 | |
| wuliqunyy | 0:be95bfb06686 | 485 | if (value->special) { |
| wuliqunyy | 0:be95bfb06686 | 486 | if (SCPI_ChoiceToName(special, value->content.tag, &type)) { |
| wuliqunyy | 0:be95bfb06686 | 487 | strncpy(str, type, len); |
| wuliqunyy | 0:be95bfb06686 | 488 | result = SCPIDEFINE_strnlen(str, len - 1); |
| wuliqunyy | 0:be95bfb06686 | 489 | str[result] = '\0'; |
| wuliqunyy | 0:be95bfb06686 | 490 | return result; |
| wuliqunyy | 0:be95bfb06686 | 491 | } else { |
| wuliqunyy | 0:be95bfb06686 | 492 | str[0] = '\0'; |
| wuliqunyy | 0:be95bfb06686 | 493 | return 0; |
| wuliqunyy | 0:be95bfb06686 | 494 | } |
| wuliqunyy | 0:be95bfb06686 | 495 | } |
| wuliqunyy | 0:be95bfb06686 | 496 | |
| wuliqunyy | 0:be95bfb06686 | 497 | result = SCPI_DoubleToStr(value->content.value, str, len); |
| wuliqunyy | 0:be95bfb06686 | 498 | |
| wuliqunyy | 0:be95bfb06686 | 499 | if (result + 1 < len) { |
| wuliqunyy | 0:be95bfb06686 | 500 | unit = translateUnitInverse(context->units, value->unit); |
| wuliqunyy | 0:be95bfb06686 | 501 | |
| wuliqunyy | 0:be95bfb06686 | 502 | if (unit) { |
| wuliqunyy | 0:be95bfb06686 | 503 | strncat(str, " ", len - result); |
| wuliqunyy | 0:be95bfb06686 | 504 | if (result + 2 < len) { |
| wuliqunyy | 0:be95bfb06686 | 505 | strncat(str, unit, len - result - 1); |
| wuliqunyy | 0:be95bfb06686 | 506 | } |
| wuliqunyy | 0:be95bfb06686 | 507 | result = strlen(str); |
| wuliqunyy | 0:be95bfb06686 | 508 | } |
| wuliqunyy | 0:be95bfb06686 | 509 | } |
| wuliqunyy | 0:be95bfb06686 | 510 | |
| wuliqunyy | 0:be95bfb06686 | 511 | return result; |
| wuliqunyy | 0:be95bfb06686 | 512 | } |
| wuliqunyy | 0:be95bfb06686 | 513 |
