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;
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. */
static int clear_instances(void)
{
@ -101,49 +97,49 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
struct sr_probe *probe;
GSList *devices;
int fd, retry;
size_t len;
size_t len, i, good_packets, dropped;
char buf[128], *b;
const struct rs_22_812_packet *rs_packet;
if ((fd = serial_open(conn, O_RDONLY|O_NONBLOCK)) == -1) {
sr_err("Unable to open %s: %s.", conn, strerror(errno));
if ((fd = serial_open(conn, O_RDONLY | O_NONBLOCK)) < 0) {
sr_err("Unable to open '%s': %s.", conn, fd);
return NULL;
}
if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
sr_err("Unable to set serial parameters.");
return NULL;
}
sr_info("Probing port %s readonly.", conn);
sr_info("Probing port '%s' readonly.", conn);
drvc = di->priv;
b = buf;
retry = 0;
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. */
while (!devices && retry < 3)
{
size_t i;
size_t good_packets = 0;
/*
* 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.
*/
while (!devices && retry < 3) {
good_packets = 0;
retry++;
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);
serial_readline(fd, &b, &len, 250);
if( (len == 0) || (len < RS_22_812_PACKET_SIZE) ) {
/* Not enough data received, is the DMM connected ? */
if ((len == 0) || (len < RS_22_812_PACKET_SIZE)) {
/* Not enough data received, is the DMM connected? */
continue;
}
/* Let's treat our buffer like a stream, and find any
* valid packets */
for( i = 0; i < len - RS_22_812_PACKET_SIZE + 1;
/* don't increment i here */ )
{
const rs_22_812_packet *packet = (void *)(&buf[i]);
if( !rs_22_812_is_packet_valid(packet) ){
/* Treat our buffer as stream, and find any valid packets. */
for (i = 0; i < len - RS_22_812_PACKET_SIZE + 1;) {
rs_packet = (void *)(&buf[i]);
if (!rs_22_812_packet_valid(rs_packet)) {
i++;
continue;
}
@ -151,27 +147,26 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
i += RS_22_812_PACKET_SIZE;
}
/* If we dropped more than two packets worth of data, something
* is wrong */
size_t dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
if(dropped > 2 * RS_22_812_PACKET_SIZE)
/* If we dropped more than two packets, something is wrong. */
dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
if (dropped > 2 * RS_22_812_PACKET_SIZE)
continue;
/* Let's see if we have anything good */
/* Let's see if we have anything good. */
if (good_packets == 0)
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",
"22-812", "")))
return NULL;
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
sr_err("Device context malloc failed.");
return NULL;
}
/* devc->profile = RADIOSHACK_22_812; */
devc->serial = sr_serial_dev_inst_new(conn, -1);
devc->serialcomm = g_strdup(serialcomm);
@ -189,7 +184,7 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
return devices;
}
static GSList *hw_scan(GSList *options)
static GSList *hw_scan(GSList * options)
{
struct sr_hwopt *opt;
GSList *l, *devices;
@ -214,7 +209,7 @@ static GSList *hw_scan(GSList *options)
/* Use the provided comm specs. */
devices = rs_22_812_scan(conn, serialcomm);
} else {
/* Then try the default 4800 8n1 */
/* Try the default 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);
if (devc->serial->fd == -1) {
sr_err("Couldn't open serial port '%s'.",
devc->serial->port);
if (devc->serial->fd < 0) {
sr_err("Couldn't open serial port '%s'.", devc->serial->port);
return SR_ERR;
}
if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
@ -280,9 +274,9 @@ static int hw_cleanup(void)
}
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) {
case SR_DI_HWOPTS:
@ -298,6 +292,7 @@ static int hw_info_get(int info_id, const void **data,
*data = probe_names;
break;
default:
sr_err("Unknown info_id: %d.", info_id);
return SR_ERR_ARG;
}
@ -305,7 +300,7 @@ static int hw_info_get(int info_id, const void **data,
}
static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
const void *value)
const void *value)
{
struct dev_context *devc;
@ -325,7 +320,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
break;
default:
sr_err("Unknown capability: %d.", hwcap);
return SR_ERR;
return SR_ERR_ARG;
break;
}
@ -333,7 +328,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
}
static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
void *cb_data)
void *cb_data)
{
struct sr_datafeed_packet packet;
struct sr_datafeed_header header;
@ -349,15 +344,17 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
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
* quit without aquiring any new samples */
* quit without aquiring any new samples.
*/
devc->num_samples = 0;
/* Send header packet to the session bus. */
sr_dbg("Sending SR_DF_HEADER.");
packet.type = SR_DF_HEADER;
packet.payload = (uint8_t *)&header;
packet.payload = (uint8_t *) & header;
header.feed_version = 1;
gettimeofday(&header.starttime, NULL);
sr_session_send(devc->cb_data, &packet);
@ -369,9 +366,9 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
meta.num_probes = 1;
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,
radioshack_receive_data, (void *)sdi );
radioshack_dmm_receive_data, (void *)sdi);
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 = {
.name = "radioshack-dmm",
.longname = "Radioshack 22-812/22-039 DMMs",
.longname = "RadioShack 22-812/22-039 DMMs",
.api_version = 1,
.init = hw_init,
.cleanup = hw_cleanup,

View File

@ -18,8 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBSIGROK_RADIOSHACK_DMM_H
#define LIBSIGROK_RADIOSHACK_DMM_H
#ifndef LIBSIGROK_HARDWARE_RADIOSHACK_DMM_RADIOSHACK_DMM_H
#define LIBSIGROK_HARDWARE_RADIOSHACK_DMM_RADIOSHACK_DMM_H
/* Message logging helpers with driver-specific prefix string. */
#define DRIVER_LOG_DOMAIN "radioshack-dmm: "
@ -30,63 +30,65 @@
#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
#define RS_DMM_BUFSIZE 256
#define RS_DMM_BUFSIZE 256
/* Byte 1 of the packet, and the modes it represents */
#define RS_22_812_IND1_HZ (0x80)
#define RS_22_812_IND1_OHM (0x40)
#define RS_22_812_IND1_KILO (0x20)
#define RS_22_812_IND1_MEGA (0x10)
#define RS_22_812_IND1_FARAD (0x08)
#define RS_22_812_IND1_AMP (0x04)
#define RS_22_812_IND1_VOLT (0x02)
#define RS_22_812_IND1_MILI (0x01)
#define RS_22_812_IND1_HZ 0x80
#define RS_22_812_IND1_OHM 0x40
#define RS_22_812_IND1_KILO 0x20
#define RS_22_812_IND1_MEGA 0x10
#define RS_22_812_IND1_FARAD 0x08
#define RS_22_812_IND1_AMP 0x04
#define RS_22_812_IND1_VOLT 0x02
#define RS_22_812_IND1_MILI 0x01
/* Byte 2 of the packet, and the modes it represents */
#define RS_22_812_IND2_MICRO (0x80)
#define RS_22_812_IND2_NANO (0x40)
#define RS_22_812_IND2_DBM (0x20)
#define RS_22_812_IND2_SEC (0x10)
#define RS_22_812_IND2_DUTY (0x08)
#define RS_22_812_IND2_HFE (0x04)
#define RS_22_812_IND2_REL (0x02)
#define RS_22_812_IND2_MIN (0x01)
#define RS_22_812_IND2_MICRO 0x80
#define RS_22_812_IND2_NANO 0x40
#define RS_22_812_IND2_DBM 0x20
#define RS_22_812_IND2_SEC 0x10
#define RS_22_812_IND2_DUTY 0x08
#define RS_22_812_IND2_HFE 0x04
#define RS_22_812_IND2_REL 0x02
#define RS_22_812_IND2_MIN 0x01
/* Byte 7 of the packet, and the modes it represents */
#define RS_22_812_INFO_BEEP (0x80)
#define RS_22_812_INFO_DIODE (0x30)
#define RS_22_812_INFO_BAT (0x20)
#define RS_22_812_INFO_HOLD (0x10)
#define RS_22_812_INFO_NEG (0x08)
#define RS_22_812_INFO_AC (0x04)
#define RS_22_812_INFO_RS232 (0x02)
#define RS_22_812_INFO_AUTO (0x01)
#define RS_22_812_INFO_BEEP 0x80
#define RS_22_812_INFO_DIODE 0x30
#define RS_22_812_INFO_BAT 0x20
#define RS_22_812_INFO_HOLD 0x10
#define RS_22_812_INFO_NEG 0x08
#define RS_22_812_INFO_AC 0x04
#define RS_22_812_INFO_RS232 0x02
#define RS_22_812_INFO_AUTO 0x01
/* Instead of a decimal point, digit 4 carries the MAX flag */
#define RS_22_812_DIG4_MAX (0x08)
/* mask to remove the decimal point fr0m a digit */
#define RS_22_812_DP_MASK (0x08)
#define RS_22_812_DIG4_MAX 0x08
/* Mask to remove the decimal point from a digit */
#define RS_22_812_DP_MASK 0x08
/* What the LCD values represent */
#define RS_22_812_LCD_0 0xd7
#define RS_22_812_LCD_1 0x50
#define RS_22_812_LCD_2 0xb5
#define RS_22_812_LCD_3 0xf1
#define RS_22_812_LCD_4 0x72
#define RS_22_812_LCD_5 0xe3
#define RS_22_812_LCD_6 0xe7
#define RS_22_812_LCD_7 0x51
#define RS_22_812_LCD_8 0xf7
#define RS_22_812_LCD_9 0xf3
#define RS_22_812_LCD_0 0xd7
#define RS_22_812_LCD_1 0x50
#define RS_22_812_LCD_2 0xb5
#define RS_22_812_LCD_3 0xf1
#define RS_22_812_LCD_4 0x72
#define RS_22_812_LCD_5 0xe3
#define RS_22_812_LCD_6 0xe7
#define RS_22_812_LCD_7 0x51
#define RS_22_812_LCD_8 0xf7
#define RS_22_812_LCD_9 0xf3
#define RS_22_812_LCD_C 0x87
#define RS_22_812_LCD_C 0x87
#define RS_22_812_LCD_E
#define RS_22_812_LCD_F
#define RS_22_812_LCD_h 0x66
#define RS_22_812_LCD_H 0x76
#define RS_22_812_LCD_h 0x66
#define RS_22_812_LCD_H 0x76
#define RS_22_812_LCD_I
#define RS_22_812_LCD_n
#define RS_22_812_LCD_P 0x37
#define RS_22_812_LCD_P 0x37
#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 indicatrix1;
uint8_t indicatrix2;
@ -96,58 +98,42 @@ typedef struct {
uint8_t digit1;
uint8_t info;
uint8_t checksum;
} rs_22_812_packet;
#define RS_22_812_PACKET_SIZE (sizeof(rs_22_812_packet))
typedef enum {
RS_22_812_MODE_DC_V = 0,
RS_22_812_MODE_AC_V = 1,
RS_22_812_MODE_DC_UA = 2,
RS_22_812_MODE_DC_MA = 3,
RS_22_812_MODE_DC_A = 4,
RS_22_812_MODE_AC_UA = 5,
RS_22_812_MODE_AC_MA = 6,
RS_22_812_MODE_AC_A = 7,
RS_22_812_MODE_OHM = 8,
RS_22_812_MODE_FARAD = 9,
RS_22_812_MODE_HZ = 10,
RS_22_812_MODE_VOLT_HZ = 11,
RS_22_812_MODE_AMP_HZ = 12,
RS_22_812_MODE_DUTY = 13,
RS_22_812_MODE_VOLT_DUTY= 14,
RS_22_812_MODE_AMP_DUTY = 15,
RS_22_812_MODE_WIDTH = 16,
RS_22_812_MODE_VOLT_WIDTH = 17,
RS_22_812_MODE_AMP_WIDTH = 18,
RS_22_812_MODE_DIODE = 19,
RS_22_812_MODE_CONT = 20,
RS_22_812_MODE_HFE = 21,
RS_22_812_MODE_LOGIC = 22,
RS_22_812_MODE_DBM = 23,
//RS_22_812_MODE_ EF = 24,
RS_22_812_MODE_TEMP = 25,
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;
};
enum {
RS_22_812_MODE_DC_V = 0,
RS_22_812_MODE_AC_V = 1,
RS_22_812_MODE_DC_UA = 2,
RS_22_812_MODE_DC_MA = 3,
RS_22_812_MODE_DC_A = 4,
RS_22_812_MODE_AC_UA = 5,
RS_22_812_MODE_AC_MA = 6,
RS_22_812_MODE_AC_A = 7,
RS_22_812_MODE_OHM = 8,
RS_22_812_MODE_FARAD = 9,
RS_22_812_MODE_HZ = 10,
RS_22_812_MODE_VOLT_HZ = 11,
RS_22_812_MODE_AMP_HZ = 12,
RS_22_812_MODE_DUTY = 13,
RS_22_812_MODE_VOLT_DUTY = 14,
RS_22_812_MODE_AMP_DUTY = 15,
RS_22_812_MODE_WIDTH = 16,
RS_22_812_MODE_VOLT_WIDTH = 17,
RS_22_812_MODE_AMP_WIDTH = 18,
RS_22_812_MODE_DIODE = 19,
RS_22_812_MODE_CONT = 20,
RS_22_812_MODE_HFE = 21,
RS_22_812_MODE_LOGIC = 22,
RS_22_812_MODE_DBM = 23,
// RS_22_812_MODE_EF = 24,
RS_22_812_MODE_TEMP = 25,
RS_22_812_MODE_INVALID = 26,
};
SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet);
/* Private, per-device-instance driver context. */
typedef struct dev_context {
/* const struct radioshackdmm_profile *profile; */
struct dev_context {
uint64_t limit_samples;
struct sr_serial_dev_inst *serial;
char *serialcomm;
@ -160,9 +146,8 @@ typedef struct dev_context {
uint8_t buf[RS_DMM_BUFSIZE];
size_t bufoffset;
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 /* LIBSIGROK_RADIOSHACK_DMM_H */
#endif

View File

@ -18,104 +18,98 @@
* 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 <math.h>
#include <string.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;
size_t i;
for(i = 0; i < RS_22_812_PACKET_SIZE - 1; i++)
int i;
raw = (void *)rs_packet;
for (i = 0; i < RS_22_812_PACKET_SIZE - 1; 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 -= data->checksum;
return(sum == 0);
sum -= rs_packet->checksum;
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 ? */
if(data->indicatrix1 & RS_22_812_IND1_KILO)
n_postfix++;
if(data->indicatrix1 & RS_22_812_IND1_MEGA)
n_postfix++;
if(data->indicatrix1 & RS_22_812_IND1_MILI)
n_postfix++;
if(data->indicatrix2 & RS_22_812_IND2_MICRO)
n_postfix++;
if(data->indicatrix2 & RS_22_812_IND2_NANO)
n_postfix++;
if(n_postfix > 1)
return FALSE;
/* Does the packet "measure" more than one type of value ?*/
if(data->indicatrix1 & RS_22_812_IND1_HZ)
n_type++;
if(data->indicatrix1 & RS_22_812_IND1_OHM)
n_type++;
if(data->indicatrix1 & RS_22_812_IND1_FARAD)
n_type++;
if(data->indicatrix1 & RS_22_812_IND1_AMP)
n_type++;
if(data->indicatrix1 & RS_22_812_IND1_VOLT)
n_type++;
if(data->indicatrix2 & RS_22_812_IND2_DBM)
n_type++;
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;
/* OK, no duplicates */
return TRUE;
}
/* Since the RS 22-812 does not identify itslef in any way shape, or form,
* 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
* 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)
{
/* Unfortunately, the packet doesn't have a signature, so we must
* compute its checksum first */
if(!rs_22_812_is_checksum_valid(packet))
return FALSE;
if(!rs_22_812_is_mode_valid(packet->mode))
return FALSE;
if(!rs_22_812_is_selection_good(packet)) {
count = 0;
count += (rs_packet->indicatrix1 & RS_22_812_IND1_KILO) ? 1 : 0;
count += (rs_packet->indicatrix1 & RS_22_812_IND1_MEGA) ? 1 : 0;
count += (rs_packet->indicatrix1 & RS_22_812_IND1_MILI) ? 1 : 0;
count += (rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) ? 1 : 0;
count += (rs_packet->indicatrix2 & RS_22_812_IND2_NANO) ? 1 : 0;
if (count > 1) {
sr_err("More than one multiplier detected in packet.");
return FALSE;
}
/* Made it here, huh? Then this looks to be a valid packet */
/* Does the packet "measure" more than one type of value? */
count = 0;
count += (rs_packet->indicatrix1 & RS_22_812_IND1_HZ) ? 1 : 0;
count += (rs_packet->indicatrix1 & RS_22_812_IND1_OHM) ? 1 : 0;
count += (rs_packet->indicatrix1 & RS_22_812_IND1_FARAD) ? 1 : 0;
count += (rs_packet->indicatrix1 & RS_22_812_IND1_AMP) ? 1 : 0;
count += (rs_packet->indicatrix1 & RS_22_812_IND1_VOLT) ? 1 : 0;
count += (rs_packet->indicatrix2 & RS_22_812_IND2_DBM) ? 1 : 0;
count += (rs_packet->indicatrix2 & RS_22_812_IND2_SEC) ? 1 : 0;
count += (rs_packet->indicatrix2 & RS_22_812_IND2_DUTY) ? 1 : 0;
count += (rs_packet->indicatrix2 & RS_22_812_IND2_HFE) ? 1 : 0;
if (count > 1) {
sr_err("More than one measurement type detected in packet.");
return FALSE;
}
return TRUE;
}
static uint8_t rs_22_812_to_digit(uint8_t raw_digit)
/*
* 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
* possible check to filter out bad packets, especially since detection of the
* 22-812 depends on how well we can filter the packets.
*/
SR_PRIV gboolean rs_22_812_packet_valid(const struct rs_22_812_packet *rs_packet)
{
/* Take out the decimal point, so we can use a simple switch() */
if (!checksum_valid(rs_packet))
return FALSE;
if (!(rs_packet->mode < RS_22_812_MODE_INVALID))
return FALSE;
if (!selection_good(rs_packet))
return FALSE;
return TRUE;
}
static uint8_t decode_digit(uint8_t raw_digit)
{
/* Take out the decimal point, so we can use a simple switch(). */
raw_digit &= ~RS_22_812_DP_MASK;
switch(raw_digit)
{
switch (raw_digit) {
case 0x00:
case RS_22_812_LCD_0:
return 0;
@ -138,97 +132,85 @@ static uint8_t rs_22_812_to_digit(uint8_t raw_digit)
case RS_22_812_LCD_9:
return 9;
default:
sr_err("Invalid digit byte: 0x%02x.", raw_digit);
return 0xff;
}
}
typedef enum {
READ_ALL,
READ_TEMP,
} value_type;
static double lcdraw_to_double(rs_22_812_packet *rs_packet, value_type type)
static double lcdraw_to_double(const struct rs_22_812_packet *rs_packet,
int type)
{
/* *********************************************************************
* Get a raw floating point value from the data
**********************************************************************/
double rawval;
double multiplier = 1;
uint8_t digit;
double rawval, multiplier = 1;
uint8_t digit, raw_digit;
gboolean dp_reached = FALSE;
int i, end;
switch(type) {
case READ_TEMP:
/* Do not parse the last digit */
end = 1;
break;
case READ_ALL:
default:
/* Parse all digits */
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) {
/* end = 1: Don't parse last digit. end = 0: Parse all digits. */
end = (type == READ_TEMP) ? 1 : 0;
/* We have 4 digits, and we start from the most significant. */
for (i = 3; i >= end; i--) {
raw_digit = *(&(rs_packet->digit4) + i);
digit = decode_digit(raw_digit);
if (digit == 0xff) {
rawval = NAN;
break;
}
/* 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) )
/*
* 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))
dp_reached = TRUE;
if(dp_reached) multiplier /= 10;
if (dp_reached)
multiplier /= 10;
rawval = rawval * 10 + digit;
}
rawval *= multiplier;
if(rs_packet->info & RS_22_812_INFO_NEG)
if (rs_packet->info & RS_22_812_INFO_NEG)
rawval *= -1;
/* See if we need to multiply our raw value by anything */
if(rs_packet->indicatrix1 & RS_22_812_IND2_NANO) {
/* See if we need to multiply our raw value by anything. */
if (rs_packet->indicatrix1 & RS_22_812_IND2_NANO) {
rawval *= 1E-9;
} else if(rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) {
} else if (rs_packet->indicatrix2 & RS_22_812_IND2_MICRO) {
rawval *= 1E-6;
} else if(rs_packet->indicatrix1 & RS_22_812_IND1_MILI) {
} else if (rs_packet->indicatrix1 & RS_22_812_IND1_MILI) {
rawval *= 1E-3;
} else if(rs_packet->indicatrix1 & RS_22_812_IND1_KILO) {
} else if (rs_packet->indicatrix1 & RS_22_812_IND1_KILO) {
rawval *= 1E3;
} else if(rs_packet->indicatrix1 & RS_22_812_IND1_MEGA) {
} else if (rs_packet->indicatrix1 & RS_22_812_IND1_MEGA) {
rawval *= 1E6;
}
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);
return((rs_packet->digit2 & ~RS_22_812_DP_MASK) == RS_22_812_LCD_H);
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);
}
static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
rs_dev_ctx *devc)
static void handle_packet(struct rs_22_812_packet *rs_packet,
struct dev_context *devc)
{
double rawval = lcdraw_to_double(rs_packet, READ_ALL);
/* *********************************************************************
* Now see what the value means, and pass that on
**********************************************************************/
double rawval;
struct sr_datafeed_packet packet;
struct sr_datafeed_analog *analog;
rawval = lcdraw_to_double(rs_packet, READ_ALL);
/* TODO: Check malloc return value. */
analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
analog->num_samples = 1;
@ -237,7 +219,7 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
*analog->data = (float)rawval;
analog->mq = -1;
switch(rs_packet->mode) {
switch (rs_packet->mode) {
case RS_22_812_MODE_DC_V:
analog->mq = SR_MQ_VOLTAGE;
analog->unit = SR_UNIT_VOLT;
@ -273,7 +255,7 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
case RS_22_812_MODE_CONT:
analog->mq = SR_MQ_CONTINUITY;
analog->unit = SR_UNIT_BOOLEAN;
*analog->data = rs_22_812_is_shortcirc(rs_packet);
*analog->data = is_shortcirc(rs_packet);
break;
case RS_22_812_MODE_DIODE:
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;
break;
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;
if(!isnan(rawval)) {
/* We have an actual voltage */
if (!isnan(rawval)) {
/* We have an actual voltage. */
analog->unit = SR_UNIT_VOLT;
} else {
/* We have either HI or LOW */
/* We have either HI or LOW. */
analog->unit = SR_UNIT_BOOLEAN;
*analog->data = rs_22_812_is_logic_high(rs_packet);
*analog->data = is_logic_high(rs_packet);
}
break;
case RS_22_812_MODE_HFE:
@ -316,10 +300,10 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
analog->unit = SR_UNIT_SECOND;
case RS_22_812_MODE_TEMP:
analog->mq = SR_MQ_TEMPERATURE;
/* We need to reparse */
/* We need to reparse. */
*analog->data = lcdraw_to_double(rs_packet, READ_TEMP);
analog->unit = rs_22_812_is_celsius(rs_packet)?
SR_UNIT_CELSIUS:SR_UNIT_FAHRENHEIT;
analog->unit = is_celsius(rs_packet) ?
SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT;
break;
case RS_22_812_MODE_DBM:
analog->mq = SR_MQ_POWER;
@ -327,22 +311,18 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
analog->mqflags |= SR_MQFLAG_AC;
break;
default:
sr_warn("Unknown mode: %d.", rs_packet->mode);
sr_err("Unknown mode: %d.", rs_packet->mode);
break;
}
if(rs_packet->info & RS_22_812_INFO_HOLD) {
if (rs_packet->info & RS_22_812_INFO_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;
}
if(rs_packet->indicatrix2 & RS_22_812_IND2_MIN) {
if (rs_packet->indicatrix2 & RS_22_812_IND2_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;
}
if (analog->mq != -1) {
/* Got a measurement. */
@ -356,40 +336,39 @@ static void rs_22_812_handle_packet(rs_22_812_packet *rs_packet,
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;
size_t i;
size_t offset = 0;
/* Try to get as much data as the buffer can hold */
size_t i, offset = 0;
struct rs_22_812_packet *rs_packet;
/* Try to get as much data as the buffer can hold. */
len = RS_DMM_BUFSIZE - devc->buflen;
len = serial_read(fd, devc->buf + devc->buflen, len);
if (len < 1) {
sr_err("Serial port read error!");
sr_err("Serial port read error.");
return;
}
devc->buflen += len;
/* Now look for packets in that data */
while((devc->buflen - offset) >= RS_22_812_PACKET_SIZE)
{
rs_22_812_packet * packet = (void *)(devc->buf + offset);
if( rs_22_812_is_packet_valid(packet) )
{
rs_22_812_handle_packet(packet, devc);
/* Now look for packets in that data. */
while ((devc->buflen - offset) >= RS_22_812_PACKET_SIZE) {
rs_packet = (void *)(devc->buf + offset);
if (rs_22_812_packet_valid(rs_packet)) {
handle_packet(rs_packet, devc);
offset += RS_22_812_PACKET_SIZE;
} else {
offset++;
}
}
/* If we have any data left, move it to the beginning of our buffer */
for(i = 0; i < devc->buflen - offset; i++)
/* If we have any data left, move it to the beginning of our buffer. */
for (i = 0; i < devc->buflen - offset; i++)
devc->buf[i] = devc->buf[offset + i];
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 dev_context *devc;
@ -400,8 +379,7 @@ SR_PRIV int radioshack_receive_data(int fd, int revents, void *cb_data)
if (!(devc = sdi->priv))
return TRUE;
if (revents == G_IO_IN)
{
if (revents == G_IO_IN) {
/* Serial data arrived. */
handle_new_data(devc, fd);
}