summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSolomon Peachy <pizza@shaftnet.org>2013-08-21 22:38:54 -0400
committerSolomon Peachy <pizza@shaftnet.org>2013-08-21 22:38:54 -0400
commitdfd39d483423ed9c460454f6f4eebd0163fa8a44 (patch)
tree5d0b2e365606ffe8eb2101e2434b2d37a6f1c8e4
parent352b81d3545d0ada48b9e980b15fff4daee7d70e (diff)
downloadlinuxaldl-dfd39d483423ed9c460454f6f4eebd0163fa8a44.tar.gz
linuxaldl-dfd39d483423ed9c460454f6f4eebd0163fa8a44.tar.bz2
linuxaldl-dfd39d483423ed9c460454f6f4eebd0163fa8a44.zip
flesh out support for MAP and BIT types.
The UI representation isn't done for BITs yet.
-rw-r--r--src/linuxaldl.c147
-rw-r--r--src/linuxaldl.h42
2 files changed, 100 insertions, 89 deletions
diff --git a/src/linuxaldl.c b/src/linuxaldl.c
index 3c16cb7..8ffc4be 100644
--- a/src/linuxaldl.c
+++ b/src/linuxaldl.c
@@ -29,9 +29,9 @@
#include <string.h> // for memcpy
#include <errno.h>
#include "linuxaldl.h"
+#include "sts_serial.h"
#include "linuxaldl_definitions.h"
#include "linuxaldl_gui.h"
-#include "sts_serial.h"
// global variables
// =================================================
@@ -254,7 +254,6 @@ int send_aldl_message(char* msg_buf, unsigned int size)
int get_mode1_message(char* inbuffer, unsigned int size)
{
int res;
- char checkval;
char outbuffer[__MAX_REQUEST_SIZE]; // max request size defined in linuxaldl_definitions.h
aldl_definition* def = aldl_settings.definition;
@@ -372,88 +371,90 @@ void aldl_update_sets(int flags)
unsigned int i=0;
byte_def_t* defs = aldl_settings.definition->mode1_def;
byte_def_t* cur_def;
- float converted_val;
- char* new_data_string=NULL;
- while (defs[i].label != NULL) { // while not at the last defined byte
+ for (i = 0 ; defs[i].label ; i++) {
cur_def = defs+i;
- // if the item is a seperator, skip it
- if (cur_def->operation == ALDL_OP_SEPERATOR) {
- i++;
- continue;
+
+ switch (cur_def->operation) {
+ case ALDL_OP_SCALAR: {
+ float converted_val;
+ char *new_data_string = NULL;
+
+ if (cur_def->bits == 8) {
+ converted_val = (((float)aldl_settings.data_set_raw[cur_def->byte_offset-1]) * cur_def->op_factor) + cur_def->op_offset;
+ } else if (cur_def->bits == 16) {
+ converted_val = ((float)aldl_settings.data_set_raw[cur_def->byte_offset-1]) * 256 + ((float)aldl_settings.data_set_raw[cur_def->byte_offset]);
+ converted_val = (converted_val*cur_def->op_factor) + cur_def->op_offset;
+ } else {
+ // XXX log some sort of error for non 8/16bpp?
+ continue;
+ }
+ if ((flags & ALDL_UPDATE_FLOATS) && aldl_settings.data_set_floats != NULL)
+ aldl_settings.data_set_floats[i] = converted_val;
+
+ // convert the result to a string
+ if ((flags & ALDL_UPDATE_STRINGS) && aldl_settings.data_set_strings != NULL) {
+ new_data_string=malloc(10); // allocate ten bytes for the string
+ snprintf(new_data_string,10,"%.1f",converted_val); // convert the floating point value to a string
+ // if there is currently a string registered, free it
+ if (aldl_settings.data_set_strings[i] != NULL)
+ free(aldl_settings.data_set_strings[i]);
+
+ // register the new string in the table
+ aldl_settings.data_set_strings[i] = new_data_string;
+ }
+ break;
+ }
+ case ALDL_OP_MAP: {
+ float converted_val;
+ char *new_data_string = NULL;
+
+ if (cur_def->bits == 8) {
+ converted_val = cur_def->map[(unsigned char)aldl_settings.data_set_raw[cur_def->byte_offset-1]];
+ } else {
+ // XXX log some sort of error for non-8bpp?
+ continue;
+ }
+ if ((flags & ALDL_UPDATE_FLOATS) && aldl_settings.data_set_floats != NULL)
+ aldl_settings.data_set_floats[i] = converted_val;
+
+ // convert the result to a string
+ if ((flags & ALDL_UPDATE_STRINGS) && aldl_settings.data_set_strings != NULL) {
+ new_data_string=malloc(10); // allocate ten bytes for the string
+ snprintf(new_data_string,10,"%.1f",converted_val); // convert the floating point value to a string
+ // if there is currently a string registered, free it
+ if (aldl_settings.data_set_strings[i] != NULL)
+ free(aldl_settings.data_set_strings[i]);
+
+ // register the new string in the table
+ aldl_settings.data_set_strings[i] = new_data_string;
+ }
+ break;
}
+ case ALDL_OP_BIT:
+ if (flags & ALDL_UPDATE_FLOATS) {
+ aldl_settings.data_set_floats[i] = (aldl_settings.data_set_raw[cur_def->byte_offset-1] & cur_def->b_bit) ? 1 : 0;
+ }
+ if (flags & ALDL_UPDATE_STRINGS) {
+ // if there is currently a string registered, free it
+ if (aldl_settings.data_set_strings[i] != NULL)
+ free(aldl_settings.data_set_strings[i]);
+ // register the new string in the table
+ aldl_settings.data_set_strings[i] = strdup((aldl_settings.data_set_raw[cur_def->byte_offset-1] & cur_def->b_bit) ? cur_def->b_set : cur_def->b_unset);
+ }
- // convert the raw data to a float based on the byte definition
- if (cur_def->bits==8) {
- converted_val = aldl_raw8_to_float(aldl_settings.data_set_raw[cur_def->byte_offset-1],
- cur_def->operation, cur_def->op_factor, cur_def->op_offset);
-
- } else if (cur_def->bits==16) {
- converted_val = aldl_raw16_to_float(aldl_settings.data_set_raw[cur_def->byte_offset-1],
- aldl_settings.data_set_raw[cur_def->byte_offset],
- cur_def->operation, cur_def->op_factor, cur_def->op_offset);
- } else { // other numbers of bits not supported
- i++;
+ break;
+ case ALDL_OP_SEPERATOR:
+ break; /* Deliberately ignore this */
+ default:
+ // XXX log some sort of error here?
continue;
}
- if ((flags & ALDL_UPDATE_FLOATS) && aldl_settings.data_set_floats != NULL)
- aldl_settings.data_set_floats[i] = converted_val;
-
- // convert the result to a string
- if ((flags & ALDL_UPDATE_STRINGS) && aldl_settings.data_set_strings != NULL) {
- new_data_string=malloc(10); // allocate ten bytes for the string
-
- snprintf(new_data_string,10,"%.1f",converted_val); // convert the floating point value to a string
- // if there is currently a string registered, free it
- if (aldl_settings.data_set_strings[i] != NULL)
- free(aldl_settings.data_set_strings[i]);
-
- // register the new string in the table
- aldl_settings.data_set_strings[i] = new_data_string;
- }
//fprintf(stderr,"Updating element %s\n",cur_def->label);
- i++;
}
}
-// converts the raw 8-bit data value val into a float by performing operation
-// using op_factor and op_offset.
-// see the documentation for the byte_def_t struct in linuxaldl.h for more information
-float aldl_raw8_to_float(unsigned char val, int operation, float op_factor, float op_offset)
-{
- float result = val;
- if (operation == ALDL_OP_MULTIPLY)
- result = ((float)val*op_factor)+op_offset;
- else if (operation == ALDL_OP_DIVIDE)
- result = (op_factor/val)+op_offset;
- else {
- result = -999;
- fprintf(stderr," aldl_raw_to_float8() error: undefined operation: %d",operation);
- }
-
- return result;
-
-}
-
-
-// converts the raw 16-bit data value val into a float by performing operation
-// using op_factor and op_offset.
-// see the documentation for the byte_def_t struct in linuxaldl.h for more information
-float aldl_raw16_to_float(unsigned char msb, unsigned char lsb, int operation, float op_factor, float op_offset)
-{
- float result = ((float)msb*256)+(float)lsb;
- if (operation == ALDL_OP_MULTIPLY)
- result = (result*op_factor)+op_offset;
- else if (operation == ALDL_OP_DIVIDE)
- result = (op_factor/result)+op_offset;
- else {
- result = -999.0;
- fprintf(stderr," aldl_raw_to_float16() error: undefined operation: %d",operation);
- }
- return result;
-
-}
diff --git a/src/linuxaldl.h b/src/linuxaldl.h
index 21da6f9..85db449 100644
--- a/src/linuxaldl.h
+++ b/src/linuxaldl.h
@@ -51,30 +51,23 @@ typedef struct _linuxaldl_byte_definition{
unsigned char byte_offset; /* Offset from the 1st byte of the data part of the mode1 message */
unsigned char bits; /* 8 or 16 are currently supported */
enum ALDL_OP operation;
-#if 0
- union {
- struct { /* (val*factor)+offset */
- float factor;
- float offset;
- const char* units; /* Text string */
- } scalar;
- struct {
- unsigned char bit;
- const char *set; /* Display if set */
- const char *unset; /* Display if not set*/
- } bit;
- } op;
-#endif
+
float op_factor; // factor for the operation
float op_offset; // offset for the operation
const char* units; /* Text string */
+
+ const float map[256];
+
+ unsigned char b_bit;
+ const char *b_set;
+ const char *b_unset;
} byte_def_t;
#define ALDL_DEF_SCALAR8(__label, __offset, __factor, __shift, __units ) \
{ .label = __label, \
.byte_offset = __offset, \
- .bits = 8, \
.operation = ALDL_OP_SCALAR, \
+ .bits = 8, \
.op_factor = __factor, \
.op_offset = __shift, \
.units = __units, \
@@ -82,12 +75,29 @@ typedef struct _linuxaldl_byte_definition{
#define ALDL_DEF_SCALAR16(__label, __offset, __factor, __shift, __units ) \
{ .label = __label, \
.byte_offset = __offset, \
- .bits = 16, \
.operation = ALDL_OP_SCALAR, \
+ .bits = 16, \
.op_factor = __factor, \
.op_offset = __shift, \
.units = __units, \
}
+#define ALDL_DEF_MAP8(__label, __offset, __map, __units ) \
+ { .label = __label, \
+ .byte_offset = __offset, \
+ .operation = ALDL_OP_MAP, \
+ .bits = 8, \
+ .map = __map, \
+ .units = __units, \
+ }
+#define ALDL_DEF_BIT(__label, __offset, __bit, __set, __unset ) \
+ { .label = __label, \
+ .byte_offset = __offset, \
+ .operation = ALDL_OP_BIT, \
+ .bits = 8, \
+ .b_bit = __bit, \
+ .b_set = __set, \
+ .b_unset = __unset, \
+ }
#define ALDL_DEF_SEPERATOR(__label) \
{ .label = __label, \
.operation = ALDL_OP_SEPERATOR, \