radioshack-dmm: Cosmetics, coding-style, cleanups.

Also, drop some uneeded code and simplify some parts.
This commit is contained in:
Uwe Hermann 2012-11-10 12:51:57 +01:00
parent 302c4b5ab5
commit ba6383f855
3 changed files with 272 additions and 312 deletions

View File

@ -49,10 +49,6 @@ static const char *probe_names[] = {
SR_PRIV struct sr_dev_driver radioshackdmm_driver_info; SR_PRIV struct sr_dev_driver radioshackdmm_driver_info;
static struct sr_dev_driver *di = &radioshackdmm_driver_info; static struct sr_dev_driver *di = &radioshackdmm_driver_info;
static const struct radioshackdmm_profile supported_radioshackdmm[] = {
{ RADIOSHACK_22_812, "22-812", 100 },
};
/* Properly close and free all devices. */ /* Properly close and free all devices. */
static int clear_instances(void) static int clear_instances(void)
{ {
@ -101,35 +97,38 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
struct sr_probe *probe; struct sr_probe *probe;
GSList *devices; GSList *devices;
int fd, retry; int fd, retry;
size_t len; size_t len, i, good_packets, dropped;
char buf[128], *b; char buf[128], *b;
const struct rs_22_812_packet *rs_packet;
if ((fd = serial_open(conn, O_RDONLY|O_NONBLOCK)) == -1) { if ((fd = serial_open(conn, O_RDONLY | O_NONBLOCK)) < 0) {
sr_err("Unable to open %s: %s.", conn, strerror(errno)); sr_err("Unable to open '%s': %s.", conn, fd);
return NULL; return NULL;
} }
if (serial_set_paramstr(fd, serialcomm) != SR_OK) { if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
sr_err("Unable to set serial parameters."); sr_err("Unable to set serial parameters.");
return NULL; return NULL;
} }
sr_info("Probing port %s readonly.", conn); sr_info("Probing port '%s' readonly.", conn);
drvc = di->priv; drvc = di->priv;
b = buf; b = buf;
retry = 0; retry = 0;
devices = NULL; devices = NULL;
/* There's no way to get an ID from the multimeter. It just sends data
* periodically, so the best we can do is check if the packets match the /*
* expected format. */ * There's no way to get an ID from the multimeter. It just sends data
while (!devices && retry < 3) * periodically, so the best we can do is check if the packets match
{ * the expected format.
size_t i; */
size_t good_packets = 0; while (!devices && retry < 3) {
good_packets = 0;
retry++; retry++;
serial_flush(fd); serial_flush(fd);
/* Let's get a bit of data and see if we can find a packet */ /* Let's get a bit of data and see if we can find a packet. */
len = sizeof(buf); len = sizeof(buf);
serial_readline(fd, &b, &len, 250); serial_readline(fd, &b, &len, 250);
if ((len == 0) || (len < RS_22_812_PACKET_SIZE)) { if ((len == 0) || (len < RS_22_812_PACKET_SIZE)) {
@ -137,13 +136,10 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
continue; continue;
} }
/* Let's treat our buffer like a stream, and find any /* Treat our buffer as stream, and find any valid packets. */
* valid packets */ for (i = 0; i < len - RS_22_812_PACKET_SIZE + 1;) {
for( i = 0; i < len - RS_22_812_PACKET_SIZE + 1; rs_packet = (void *)(&buf[i]);
/* don't increment i here */ ) if (!rs_22_812_packet_valid(rs_packet)) {
{
const rs_22_812_packet *packet = (void *)(&buf[i]);
if( !rs_22_812_is_packet_valid(packet) ){
i++; i++;
continue; continue;
} }
@ -151,27 +147,26 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
i += RS_22_812_PACKET_SIZE; i += RS_22_812_PACKET_SIZE;
} }
/* If we dropped more than two packets worth of data, something /* If we dropped more than two packets, something is wrong. */
* is wrong */ dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
size_t dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
if (dropped > 2 * RS_22_812_PACKET_SIZE) if (dropped > 2 * RS_22_812_PACKET_SIZE)
continue; continue;
/* Let's see if we have anything good */ /* Let's see if we have anything good. */
if (good_packets == 0) if (good_packets == 0)
continue; continue;
sr_info("Found RS 22-812 on port %s.", conn); sr_info("Found RadioShack 22-812 on port '%s'.", conn);
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack", if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack",
"22-812", ""))) "22-812", "")))
return NULL; return NULL;
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
sr_err("Device context malloc failed."); sr_err("Device context malloc failed.");
return NULL; return NULL;
} }
/* devc->profile = RADIOSHACK_22_812; */
devc->serial = sr_serial_dev_inst_new(conn, -1); devc->serial = sr_serial_dev_inst_new(conn, -1);
devc->serialcomm = g_strdup(serialcomm); devc->serialcomm = g_strdup(serialcomm);
@ -214,7 +209,7 @@ static GSList *hw_scan(GSList *options)
/* Use the provided comm specs. */ /* Use the provided comm specs. */
devices = rs_22_812_scan(conn, serialcomm); devices = rs_22_812_scan(conn, serialcomm);
} else { } else {
/* Then try the default 4800 8n1 */ /* Try the default 4800/8n1. */
devices = rs_22_812_scan(conn, "4800/8n1"); devices = rs_22_812_scan(conn, "4800/8n1");
} }
@ -240,9 +235,8 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
} }
devc->serial->fd = serial_open(devc->serial->port, O_RDONLY); devc->serial->fd = serial_open(devc->serial->port, O_RDONLY);
if (devc->serial->fd == -1) { if (devc->serial->fd < 0) {
sr_err("Couldn't open serial port '%s'.", sr_err("Couldn't open serial port '%s'.", devc->serial->port);
devc->serial->port);
return SR_ERR; return SR_ERR;
} }
if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) { if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
@ -282,7 +276,7 @@ static int hw_cleanup(void)
static int hw_info_get(int info_id, const void **data, static int hw_info_get(int info_id, const void **data,
const struct sr_dev_inst *sdi) const struct sr_dev_inst *sdi)
{ {
(void)sdi; /* Does nothing. prevents "unused parameter" warning */ (void)sdi;
switch (info_id) { switch (info_id) {
case SR_DI_HWOPTS: case SR_DI_HWOPTS:
@ -298,6 +292,7 @@ static int hw_info_get(int info_id, const void **data,
*data = probe_names; *data = probe_names;
break; break;
default: default:
sr_err("Unknown info_id: %d.", info_id);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
@ -325,7 +320,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
break; break;
default: default:
sr_err("Unknown capability: %d.", hwcap); sr_err("Unknown capability: %d.", hwcap);
return SR_ERR; return SR_ERR_ARG;
break; break;
} }
@ -349,9 +344,11 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
devc->cb_data = cb_data; devc->cb_data = cb_data;
/* Reset the number of samples to take. If we've already collected our /*
* Reset the number of samples to take. If we've already collected our
* quota, but we start a new session, and don't reset this, we'll just * quota, but we start a new session, and don't reset this, we'll just
* quit without aquiring any new samples */ * quit without aquiring any new samples.
*/
devc->num_samples = 0; devc->num_samples = 0;
/* Send header packet to the session bus. */ /* Send header packet to the session bus. */
@ -369,9 +366,9 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
meta.num_probes = 1; meta.num_probes = 1;
sr_session_send(devc->cb_data, &packet); sr_session_send(devc->cb_data, &packet);
/* Poll every 100ms, or whenever some data comes in. */ /* Poll every 50ms, or whenever some data comes in. */
sr_source_add(devc->serial->fd, G_IO_IN, 50, sr_source_add(devc->serial->fd, G_IO_IN, 50,
radioshack_receive_data, (void *)sdi ); radioshack_dmm_receive_data, (void *)sdi);
return SR_OK; return SR_OK;
} }
@ -404,7 +401,7 @@ static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
SR_PRIV struct sr_dev_driver radioshackdmm_driver_info = { SR_PRIV struct sr_dev_driver radioshackdmm_driver_info = {
.name = "radioshack-dmm", .name = "radioshack-dmm",
.longname = "Radioshack 22-812/22-039 DMMs", .longname = "RadioShack 22-812/22-039 DMMs",
.api_version = 1, .api_version = 1,
.init = hw_init, .init = hw_init,
.cleanup = hw_cleanup, .cleanup = hw_cleanup,

View File

@ -18,8 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef LIBSIGROK_RADIOSHACK_DMM_H #ifndef LIBSIGROK_HARDWARE_RADIOSHACK_DMM_RADIOSHACK_DMM_H
#define LIBSIGROK_RADIOSHACK_DMM_H #define LIBSIGROK_HARDWARE_RADIOSHACK_DMM_RADIOSHACK_DMM_H
/* Message logging helpers with driver-specific prefix string. */ /* Message logging helpers with driver-specific prefix string. */
#define DRIVER_LOG_DOMAIN "radioshack-dmm: " #define DRIVER_LOG_DOMAIN "radioshack-dmm: "
@ -33,36 +33,36 @@
#define RS_DMM_BUFSIZE 256 #define RS_DMM_BUFSIZE 256
/* Byte 1 of the packet, and the modes it represents */ /* Byte 1 of the packet, and the modes it represents */
#define RS_22_812_IND1_HZ (0x80) #define RS_22_812_IND1_HZ 0x80
#define RS_22_812_IND1_OHM (0x40) #define RS_22_812_IND1_OHM 0x40
#define RS_22_812_IND1_KILO (0x20) #define RS_22_812_IND1_KILO 0x20
#define RS_22_812_IND1_MEGA (0x10) #define RS_22_812_IND1_MEGA 0x10
#define RS_22_812_IND1_FARAD (0x08) #define RS_22_812_IND1_FARAD 0x08
#define RS_22_812_IND1_AMP (0x04) #define RS_22_812_IND1_AMP 0x04
#define RS_22_812_IND1_VOLT (0x02) #define RS_22_812_IND1_VOLT 0x02
#define RS_22_812_IND1_MILI (0x01) #define RS_22_812_IND1_MILI 0x01
/* Byte 2 of the packet, and the modes it represents */ /* Byte 2 of the packet, and the modes it represents */
#define RS_22_812_IND2_MICRO (0x80) #define RS_22_812_IND2_MICRO 0x80
#define RS_22_812_IND2_NANO (0x40) #define RS_22_812_IND2_NANO 0x40
#define RS_22_812_IND2_DBM (0x20) #define RS_22_812_IND2_DBM 0x20
#define RS_22_812_IND2_SEC (0x10) #define RS_22_812_IND2_SEC 0x10
#define RS_22_812_IND2_DUTY (0x08) #define RS_22_812_IND2_DUTY 0x08
#define RS_22_812_IND2_HFE (0x04) #define RS_22_812_IND2_HFE 0x04
#define RS_22_812_IND2_REL (0x02) #define RS_22_812_IND2_REL 0x02
#define RS_22_812_IND2_MIN (0x01) #define RS_22_812_IND2_MIN 0x01
/* Byte 7 of the packet, and the modes it represents */ /* Byte 7 of the packet, and the modes it represents */
#define RS_22_812_INFO_BEEP (0x80) #define RS_22_812_INFO_BEEP 0x80
#define RS_22_812_INFO_DIODE (0x30) #define RS_22_812_INFO_DIODE 0x30
#define RS_22_812_INFO_BAT (0x20) #define RS_22_812_INFO_BAT 0x20
#define RS_22_812_INFO_HOLD (0x10) #define RS_22_812_INFO_HOLD 0x10
#define RS_22_812_INFO_NEG (0x08) #define RS_22_812_INFO_NEG 0x08
#define RS_22_812_INFO_AC (0x04) #define RS_22_812_INFO_AC 0x04
#define RS_22_812_INFO_RS232 (0x02) #define RS_22_812_INFO_RS232 0x02
#define RS_22_812_INFO_AUTO (0x01) #define RS_22_812_INFO_AUTO 0x01
/* Instead of a decimal point, digit 4 carries the MAX flag */ /* Instead of a decimal point, digit 4 carries the MAX flag */
#define RS_22_812_DIG4_MAX (0x08) #define RS_22_812_DIG4_MAX 0x08
/* mask to remove the decimal point fr0m a digit */ /* Mask to remove the decimal point from a digit */
#define RS_22_812_DP_MASK (0x08) #define RS_22_812_DP_MASK 0x08
/* What the LCD values represent */ /* What the LCD values represent */
#define RS_22_812_LCD_0 0xd7 #define RS_22_812_LCD_0 0xd7
@ -86,7 +86,9 @@
#define RS_22_812_LCD_P 0x37 #define RS_22_812_LCD_P 0x37
#define RS_22_812_LCD_r #define RS_22_812_LCD_r
typedef struct { #define RS_22_812_PACKET_SIZE 9
struct rs_22_812_packet {
uint8_t mode; uint8_t mode;
uint8_t indicatrix1; uint8_t indicatrix1;
uint8_t indicatrix2; uint8_t indicatrix2;
@ -96,11 +98,9 @@ typedef struct {
uint8_t digit1; uint8_t digit1;
uint8_t info; uint8_t info;
uint8_t checksum; uint8_t checksum;
} rs_22_812_packet; };
#define RS_22_812_PACKET_SIZE (sizeof(rs_22_812_packet)) enum {
typedef enum {
RS_22_812_MODE_DC_V = 0, RS_22_812_MODE_DC_V = 0,
RS_22_812_MODE_AC_V = 1, RS_22_812_MODE_AC_V = 1,
RS_22_812_MODE_DC_UA = 2, RS_22_812_MODE_DC_UA = 2,
@ -128,26 +128,12 @@ typedef enum {
// RS_22_812_MODE_EF = 24, // RS_22_812_MODE_EF = 24,
RS_22_812_MODE_TEMP = 25, RS_22_812_MODE_TEMP = 25,
RS_22_812_MODE_INVALID = 26, RS_22_812_MODE_INVALID = 26,
} rs_22_812_mode;
SR_PRIV gboolean rs_22_812_is_packet_valid(const rs_22_812_packet *data );
/* Supported models */
typedef enum {
RADIOSHACK_22_812 = 1,
} radioshack_model;
/* Supported device profiles */
struct radioshackdmm_profile {
radioshack_model model;
const char *modelname;
/* How often to poll, in ms. */
int poll_period;
}; };
SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet);
/* Private, per-device-instance driver context. */ /* Private, per-device-instance driver context. */
typedef struct dev_context { struct dev_context {
/* const struct radioshackdmm_profile *profile; */
uint64_t limit_samples; uint64_t limit_samples;
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
char *serialcomm; char *serialcomm;
@ -160,9 +146,8 @@ typedef struct dev_context {
uint8_t buf[RS_DMM_BUFSIZE]; uint8_t buf[RS_DMM_BUFSIZE];
size_t bufoffset; size_t bufoffset;
size_t buflen; size_t buflen;
} rs_dev_ctx; };
SR_PRIV int radioshack_dmm_receive_data(int fd, int revents, void *cb_data);
SR_PRIV int radioshack_receive_data(int fd, int revents, void *cb_data); #endif
#endif /* LIBSIGROK_RADIOSHACK_DMM_H */

View File

@ -18,104 +18,98 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <glib.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
#include "radioshack-dmm.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <glib.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
#include "radioshack-dmm.h"
static gboolean rs_22_812_is_checksum_valid(const rs_22_812_packet *data) enum {
READ_ALL,
READ_TEMP,
};
static gboolean checksum_valid(const struct rs_22_812_packet *rs_packet)
{ {
uint8_t *raw = (void *) data; uint8_t *raw;
uint8_t sum = 0; uint8_t sum = 0;
size_t i; int i;
raw = (void *)rs_packet;
for (i = 0; i < RS_22_812_PACKET_SIZE - 1; i++) for (i = 0; i < RS_22_812_PACKET_SIZE - 1; i++)
sum += raw[i]; sum += raw[i];
/* This is just a funky constant added to the checksum */
/* This is just a funky constant added to the checksum. */
sum += 57; sum += 57;
sum -= data->checksum; sum -= rs_packet->checksum;
return (sum == 0); return (sum == 0);
} }
static gboolean rs_22_812_is_mode_valid(rs_22_812_mode mode) static gboolean selection_good(const struct rs_22_812_packet *rs_packet)
{ {
return(mode < RS_22_812_MODE_INVALID); int count;
}
static gboolean rs_22_812_is_selection_good(const rs_22_812_packet *data)
{
int n_postfix = 0;
int n_type = 0;
/* Does the packet have more than one multiplier ? */ /* Does the packet have more than one multiplier ? */
if(data->indicatrix1 & RS_22_812_IND1_KILO) count = 0;
n_postfix++; count += (rs_packet->indicatrix1 & RS_22_812_IND1_KILO) ? 1 : 0;
if(data->indicatrix1 & RS_22_812_IND1_MEGA) count += (rs_packet->indicatrix1 & RS_22_812_IND1_MEGA) ? 1 : 0;
n_postfix++; count += (rs_packet->indicatrix1 & RS_22_812_IND1_MILI) ? 1 : 0;
if(data->indicatrix1 & RS_22_812_IND1_MILI) count += (rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) ? 1 : 0;
n_postfix++; count += (rs_packet->indicatrix2 & RS_22_812_IND2_NANO) ? 1 : 0;
if(data->indicatrix2 & RS_22_812_IND2_MICRO) if (count > 1) {
n_postfix++; sr_err("More than one multiplier detected in packet.");
if(data->indicatrix2 & RS_22_812_IND2_NANO)
n_postfix++;
if(n_postfix > 1)
return FALSE; return FALSE;
}
/* Does the packet "measure" more than one type of value? */ /* Does the packet "measure" more than one type of value? */
if(data->indicatrix1 & RS_22_812_IND1_HZ) count = 0;
n_type++; count += (rs_packet->indicatrix1 & RS_22_812_IND1_HZ) ? 1 : 0;
if(data->indicatrix1 & RS_22_812_IND1_OHM) count += (rs_packet->indicatrix1 & RS_22_812_IND1_OHM) ? 1 : 0;
n_type++; count += (rs_packet->indicatrix1 & RS_22_812_IND1_FARAD) ? 1 : 0;
if(data->indicatrix1 & RS_22_812_IND1_FARAD) count += (rs_packet->indicatrix1 & RS_22_812_IND1_AMP) ? 1 : 0;
n_type++; count += (rs_packet->indicatrix1 & RS_22_812_IND1_VOLT) ? 1 : 0;
if(data->indicatrix1 & RS_22_812_IND1_AMP) count += (rs_packet->indicatrix2 & RS_22_812_IND2_DBM) ? 1 : 0;
n_type++; count += (rs_packet->indicatrix2 & RS_22_812_IND2_SEC) ? 1 : 0;
if(data->indicatrix1 & RS_22_812_IND1_VOLT) count += (rs_packet->indicatrix2 & RS_22_812_IND2_DUTY) ? 1 : 0;
n_type++; count += (rs_packet->indicatrix2 & RS_22_812_IND2_HFE) ? 1 : 0;
if(data->indicatrix2 & RS_22_812_IND2_DBM) if (count > 1) {
n_type++; sr_err("More than one measurement type detected in packet.");
if(data->indicatrix2 & RS_22_812_IND2_SEC)
n_type++;
if(data->indicatrix2 & RS_22_812_IND2_DUTY)
n_type++;
if(data->indicatrix2 & RS_22_812_IND2_HFE)
n_type++;
if(n_type > 1)
return FALSE; return FALSE;
}
/* OK, no duplicates */
return TRUE; return TRUE;
} }
/* Since the RS 22-812 does not identify itslef in any way shape, or form, /*
* Since the 22-812 does not identify itself in any way, shape, or form,
* we really don't know for sure who is sending the data. We must use every * we really don't know for sure who is sending the data. We must use every
* possible check to filter out bad packets, especially since detection of the * possible check to filter out bad packets, especially since detection of the
* 22-812 depends on how well we can filter the packets */ * 22-812 depends on how well we can filter the packets.
SR_PRIV gboolean rs_22_812_is_packet_valid(const rs_22_812_packet *packet) */
SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet)
{ {
/* Unfortunately, the packet doesn't have a signature, so we must if (!checksum_valid(rs_packet))
* compute its checksum first */
if(!rs_22_812_is_checksum_valid(packet))
return FALSE; return FALSE;
if(!rs_22_812_is_mode_valid(packet->mode)) if (!(rs_packet->mode < RS_22_812_MODE_INVALID))
return FALSE; return FALSE;
if(!rs_22_812_is_selection_good(packet)) { if (!selection_good(rs_packet))
return FALSE; return FALSE;
}
/* Made it here, huh? Then this looks to be a valid packet */
return TRUE; return TRUE;
} }
static uint8_t rs_22_812_to_digit(uint8_t raw_digit) static uint8_t decode_digit(uint8_t raw_digit)
{ {
/* Take out the decimal point, so we can use a simple switch() */ /* Take out the decimal point, so we can use a simple switch(). */
raw_digit &= ~RS_22_812_DP_MASK; raw_digit &= ~RS_22_812_DP_MASK;
switch(raw_digit)
{ switch (raw_digit) {
case 0x00: case 0x00:
case RS_22_812_LCD_0: case RS_22_812_LCD_0:
return 0; return 0;
@ -138,56 +132,45 @@ static uint8_t rs_22_812_to_digit(uint8_t raw_digit)
case RS_22_812_LCD_9: case RS_22_812_LCD_9:
return 9; return 9;
default: default:
sr_err("Invalid digit byte: 0x%02x.", raw_digit);
return 0xff; return 0xff;
} }
} }
typedef enum { static double lcdraw_to_double(const struct rs_22_812_packet *rs_packet,
READ_ALL, int type)
READ_TEMP,
} value_type;
static double lcdraw_to_double(rs_22_812_packet *rs_packet, value_type type)
{ {
/* ********************************************************************* double rawval, multiplier = 1;
* Get a raw floating point value from the data uint8_t digit, raw_digit;
**********************************************************************/
double rawval;
double multiplier = 1;
uint8_t digit;
gboolean dp_reached = FALSE; gboolean dp_reached = FALSE;
int i, end; int i, end;
switch(type) {
case READ_TEMP: /* end = 1: Don't parse last digit. end = 0: Parse all digits. */
/* Do not parse the last digit */ end = (type == READ_TEMP) ? 1 : 0;
end = 1;
break; /* We have 4 digits, and we start from the most significant. */
case READ_ALL: for (i = 3; i >= end; i--) {
default: raw_digit = *(&(rs_packet->digit4) + i);
/* Parse all digits */ digit = decode_digit(raw_digit);
end = 0;
}
/* We have 4 digits, and we start from the most significant */
for(i = 3; i >= end; i--)
{
uint8_t raw_digit = *(&(rs_packet->digit4) + i);
digit = rs_22_812_to_digit(raw_digit);
if (digit == 0xff) { if (digit == 0xff) {
rawval = NAN; rawval = NAN;
break; break;
} }
/* Digit 1 does not have a decimal point. Instead, the decimal /*
* point is used to indicate MAX, so we must avoid testing it */ * Digit 1 does not have a decimal point. Instead, the decimal
* point is used to indicate MAX, so we must avoid testing it.
*/
if ((i < 3) && (raw_digit & RS_22_812_DP_MASK)) if ((i < 3) && (raw_digit & RS_22_812_DP_MASK))
dp_reached = TRUE; dp_reached = TRUE;
if(dp_reached) multiplier /= 10; if (dp_reached)
multiplier /= 10;
rawval = rawval * 10 + digit; rawval = rawval * 10 + digit;
} }
rawval *= multiplier; rawval *= multiplier;
if (rs_packet->info & RS_22_812_INFO_NEG) if (rs_packet->info & RS_22_812_INFO_NEG)
rawval *= -1; rawval *= -1;
/* See if we need to multiply our raw value by anything */ /* See if we need to multiply our raw value by anything. */
if (rs_packet->indicatrix1 & RS_22_812_IND2_NANO) { if (rs_packet->indicatrix1 & RS_22_812_IND2_NANO) {
rawval *= 1E-9; rawval *= 1E-9;
} else if (rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) { } else if (rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) {
@ -203,32 +186,31 @@ static double lcdraw_to_double(rs_22_812_packet *rs_packet, value_type type)
return rawval; return rawval;
} }
static gboolean rs_22_812_is_celsius(rs_22_812_packet *rs_packet) static gboolean is_celsius(struct rs_22_812_packet *rs_packet)
{ {
return ((rs_packet->digit4 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_C); return ((rs_packet->digit4 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_C);
} }
static gboolean rs_22_812_is_shortcirc(rs_22_812_packet *rs_packet) static gboolean is_shortcirc(struct rs_22_812_packet *rs_packet)
{ {
return ((rs_packet->digit2 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_h); return ((rs_packet->digit2 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_h);
} }
static gboolean rs_22_812_is_logic_high(rs_22_812_packet *rs_packet) static gboolean is_logic_high(struct rs_22_812_packet *rs_packet)
{ {
sr_spew("digit 2: %x", rs_packet->digit2 & ~RS_22_812_DP_MASK); sr_spew("Digit 2: 0x%02x.", rs_packet->digit2 & ~RS_22_812_DP_MASK);
return ((rs_packet->digit2 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_H); return ((rs_packet->digit2 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_H);
} }
static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet, static void handle_packet(struct rs_22_812_packet *rs_packet,
rs_dev_ctx *devc) struct dev_context *devc)
{ {
double rawval = lcdraw_to_double(rs_packet, READ_ALL); double rawval;
/* *********************************************************************
* Now see what the value means, and pass that on
**********************************************************************/
struct sr_datafeed_packet packet; struct sr_datafeed_packet packet;
struct sr_datafeed_analog *analog; struct sr_datafeed_analog *analog;
rawval = lcdraw_to_double(rs_packet, READ_ALL);
/* TODO: Check malloc return value. */ /* TODO: Check malloc return value. */
analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)); analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
analog->num_samples = 1; analog->num_samples = 1;
@ -273,7 +255,7 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
case RS_22_812_MODE_CONT: case RS_22_812_MODE_CONT:
analog->mq = SR_MQ_CONTINUITY; analog->mq = SR_MQ_CONTINUITY;
analog->unit = SR_UNIT_BOOLEAN; analog->unit = SR_UNIT_BOOLEAN;
*analog->data = rs_22_812_is_shortcirc(rs_packet); *analog->data = is_shortcirc(rs_packet);
break; break;
case RS_22_812_MODE_DIODE: case RS_22_812_MODE_DIODE:
analog->mq = SR_MQ_VOLTAGE; analog->mq = SR_MQ_VOLTAGE;
@ -287,16 +269,18 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
analog->unit = SR_UNIT_HERTZ; analog->unit = SR_UNIT_HERTZ;
break; break;
case RS_22_812_MODE_LOGIC: case RS_22_812_MODE_LOGIC:
/* No matter whether or not we have an actual voltage reading, /*
* we are measuring voltage, so we set our MQ as VOLTAGE */ * No matter whether or not we have an actual voltage reading,
* we are measuring voltage, so we set our MQ as VOLTAGE.
*/
analog->mq = SR_MQ_VOLTAGE; analog->mq = SR_MQ_VOLTAGE;
if (!isnan(rawval)) { if (!isnan(rawval)) {
/* We have an actual voltage */ /* We have an actual voltage. */
analog->unit = SR_UNIT_VOLT; analog->unit = SR_UNIT_VOLT;
} else { } else {
/* We have either HI or LOW */ /* We have either HI or LOW. */
analog->unit = SR_UNIT_BOOLEAN; analog->unit = SR_UNIT_BOOLEAN;
*analog->data = rs_22_812_is_logic_high(rs_packet); *analog->data = is_logic_high(rs_packet);
} }
break; break;
case RS_22_812_MODE_HFE: case RS_22_812_MODE_HFE:
@ -316,9 +300,9 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
analog->unit = SR_UNIT_SECOND; analog->unit = SR_UNIT_SECOND;
case RS_22_812_MODE_TEMP: case RS_22_812_MODE_TEMP:
analog->mq = SR_MQ_TEMPERATURE; analog->mq = SR_MQ_TEMPERATURE;
/* We need to reparse */ /* We need to reparse. */
*analog->data = lcdraw_to_double(rs_packet, READ_TEMP); *analog->data = lcdraw_to_double(rs_packet, READ_TEMP);
analog->unit = rs_22_812_is_celsius(rs_packet)? analog->unit = is_celsius(rs_packet) ?
SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT; SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT;
break; break;
case RS_22_812_MODE_DBM: case RS_22_812_MODE_DBM:
@ -327,22 +311,18 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
analog->mqflags |= SR_MQFLAG_AC; analog->mqflags |= SR_MQFLAG_AC;
break; break;
default: default:
sr_warn("Unknown mode: %d.", rs_packet->mode); sr_err("Unknown mode: %d.", rs_packet->mode);
break; break;
} }
if(rs_packet->info & RS_22_812_INFO_HOLD) { if (rs_packet->info & RS_22_812_INFO_HOLD)
analog->mqflags |= SR_MQFLAG_HOLD; analog->mqflags |= SR_MQFLAG_HOLD;
} if (rs_packet->digit4 & RS_22_812_DIG4_MAX)
if(rs_packet->digit4 & RS_22_812_DIG4_MAX) {
analog->mqflags |= SR_MQFLAG_MAX; analog->mqflags |= SR_MQFLAG_MAX;
} if (rs_packet->indicatrix2 & RS_22_812_IND2_MIN)
if(rs_packet->indicatrix2 & RS_22_812_IND2_MIN) {
analog->mqflags |= SR_MQFLAG_MIN; analog->mqflags |= SR_MQFLAG_MIN;
} if (rs_packet->info & RS_22_812_INFO_AUTO)
if(rs_packet->info & RS_22_812_INFO_AUTO) {
analog->mqflags |= SR_MQFLAG_AUTORANGE; analog->mqflags |= SR_MQFLAG_AUTORANGE;
}
if (analog->mq != -1) { if (analog->mq != -1) {
/* Got a measurement. */ /* Got a measurement. */
@ -356,40 +336,39 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
g_free(analog); g_free(analog);
} }
static void handle_new_data(rs_dev_ctx *devc, int fd) static void handle_new_data(struct dev_context *devc, int fd)
{ {
int len; int len;
size_t i; size_t i, offset = 0;
size_t offset = 0; struct rs_22_812_packet *rs_packet;
/* Try to get as much data as the buffer can hold */
/* Try to get as much data as the buffer can hold. */
len = RS_DMM_BUFSIZE - devc->buflen; len = RS_DMM_BUFSIZE - devc->buflen;
len = serial_read(fd, devc->buf + devc->buflen, len); len = serial_read(fd, devc->buf + devc->buflen, len);
if (len < 1) { if (len < 1) {
sr_err("Serial port read error!"); sr_err("Serial port read error.");
return; return;
} }
devc->buflen += len; devc->buflen += len;
/* Now look for packets in that data */ /* Now look for packets in that data. */
while((devc->buflen - offset) >= RS_22_812_PACKET_SIZE) while ((devc->buflen - offset) >= RS_22_812_PACKET_SIZE) {
{ rs_packet = (void *)(devc->buf + offset);
rs_22_812_packet * packet = (void *)(devc->buf + offset); if (rs_22_812_packet_valid(rs_packet)) {
if( rs_22_812_is_packet_valid(packet) ) handle_packet(rs_packet, devc);
{
rs_22_812_handle_packet(packet, devc);
offset += RS_22_812_PACKET_SIZE; offset += RS_22_812_PACKET_SIZE;
} else { } else {
offset++; offset++;
} }
} }
/* If we have any data left, move it to the beginning of our buffer */ /* If we have any data left, move it to the beginning of our buffer. */
for (i = 0; i < devc->buflen - offset; i++) for (i = 0; i < devc->buflen - offset; i++)
devc->buf[i] = devc->buf[offset + i]; devc->buf[i] = devc->buf[offset + i];
devc->buflen -= offset; devc->buflen -= offset;
} }
SR_PRIV int radioshack_receive_data(int fd, int revents, void *cb_data) SR_PRIV int radioshack_dmm_receive_data(int fd, int revents, void *cb_data)
{ {
struct sr_dev_inst *sdi; struct sr_dev_inst *sdi;
struct dev_context *devc; struct dev_context *devc;
@ -400,8 +379,7 @@ SR_PRIV int radioshack_receive_data(int fd, int revents, void *cb_data)
if (!(devc = sdi->priv)) if (!(devc = sdi->priv))
return TRUE; return TRUE;
if (revents == G_IO_IN) if (revents == G_IO_IN) {
{
/* Serial data arrived. */ /* Serial data arrived. */
handle_new_data(devc, fd); handle_new_data(devc, fd);
} }