Refactor units to use scpi context
| | |
| | | bool_t cmd_error; |
| | | scpi_error_queue_t error_queue; |
| | | scpi_reg_val_t * registers; |
| | | const scpi_unit_def_t * units; |
| | | const scpi_special_number_def_t * special_numbers; |
| | | }; |
| | | |
| | | enum _scpi_unit_t { |
| | |
| | | static scpi_special_number_t translateSpecialNumber(const scpi_special_number_def_t * specs, const char * str, size_t len) { |
| | | int i; |
| | | |
| | | if (specs == NULL) { |
| | | return SCPI_NUM_NUMBER; |
| | | } |
| | | |
| | | for (i = 0; specs[i].name != NULL; i++) { |
| | | if (matchPattern(specs[i].name, strlen(specs[i].name), str, len)) { |
| | | return specs[i].type; |
| | |
| | | static const char * translateSpecialNumberInverse(const scpi_special_number_def_t * specs, scpi_special_number_t type) { |
| | | int i; |
| | | |
| | | if (specs == NULL) { |
| | | return NULL; |
| | | } |
| | | |
| | | for (i = 0; specs[i].name != NULL; i++) { |
| | | if (specs[i].type == type) { |
| | | return specs[i].name; |
| | |
| | | |
| | | static const scpi_unit_def_t * translateUnit(const scpi_unit_def_t * units, const char * unit, size_t len) { |
| | | int i; |
| | | |
| | | if (units == NULL) { |
| | | return NULL; |
| | | } |
| | | |
| | | for (i = 0; units[i].name != NULL; i++) { |
| | | if (compareStr(unit, len, units[i].name, strlen(units[i].name))) { |
| | | return &units[i]; |
| | |
| | | |
| | | static const char * translateUnitInverse(const scpi_unit_def_t * units, const scpi_unit_t unit) { |
| | | int i; |
| | | |
| | | if (units == NULL) { |
| | | return NULL; |
| | | } |
| | | |
| | | for (i = 0; units[i].name != NULL; i++) { |
| | | if ((units[i].unit == unit) && (units[i].mult == 1)) { |
| | | return units[i].name; |
| | |
| | | size_t len; |
| | | size_t numlen; |
| | | |
| | | // TODO: get scpi_special_numbers_def and scpi_units_def from context |
| | | |
| | | result = SCPI_ParamString(context, ¶m, &len, mandatory); |
| | | |
| | | if (!value) { |
| | |
| | | |
| | | value->unit = SCPI_UNIT_NONE; |
| | | value->value = 0.0; |
| | | value->type = translateSpecialNumber(scpi_special_numbers_def, param, len); |
| | | value->type = translateSpecialNumber(context->special_numbers, param, len); |
| | | |
| | | if (value->type != SCPI_NUM_NUMBER) { |
| | | // found special type |
| | |
| | | numlen = strToDouble(param, &value->value); |
| | | |
| | | if (numlen <= len) { |
| | | if (transformNumber(scpi_units_def, param + numlen, len - numlen, value)) { |
| | | if (transformNumber(context->units, param + numlen, len - numlen, value)) { |
| | | return TRUE; |
| | | } else { |
| | | SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SUFFIX); |
| | |
| | | const char * unit; |
| | | size_t result; |
| | | |
| | | (void) context; // TODO: get scpi_special_numbers_def and scpi_units_def from context |
| | | |
| | | |
| | | if (!value || !str) { |
| | | return 0; |
| | | } |
| | | |
| | | type = translateSpecialNumberInverse(scpi_special_numbers_def, value->type); |
| | | type = translateSpecialNumberInverse(context->special_numbers, value->type); |
| | | |
| | | if (type) { |
| | | strncpy(str, type, len); |
| | |
| | | |
| | | result = doubleToStr(value->value, str, len); |
| | | |
| | | unit = translateUnitInverse(scpi_units_def, value->unit); |
| | | unit = translateUnitInverse(context->units, value->unit); |
| | | |
| | | if (unit) { |
| | | strncat(str, " ", len); |
| | |
| | | extern "C" { |
| | | #endif |
| | | |
| | | extern const scpi_unit_def_t scpi_units_def[]; |
| | | extern const scpi_special_number_def_t scpi_special_numbers_def[]; |
| | | |
| | | bool_t SCPI_ParamNumber(scpi_t * context, scpi_number_t * value, bool_t mandatory); |
| | | size_t SCPI_NumberToStr(scpi_t * context, scpi_number_t * value, char * str, size_t len); |
| | | |
| | |
| | | return SCPI_RES_OK; |
| | | } |
| | | |
| | | static scpi_command_t scpi_commands[] = { |
| | | static const scpi_command_t scpi_commands[] = { |
| | | /* IEEE Mandated Commands (SCPI std V1999.0 4.1.1) */ |
| | | { .pattern = "*CLS", .callback = SCPI_CoreCls,}, |
| | | { .pattern = "*ESE", .callback = SCPI_CoreEse,}, |
| | |
| | | }, |
| | | .interface = &scpi_interface, |
| | | .registers = scpi_regs, |
| | | .units = scpi_units_def, |
| | | .special_numbers = scpi_special_numbers_def, |
| | | }; |
| | | |
| | | /* |