*
* Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
* Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
+ * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include "libsigrok-internal.h"
#include "protocol.h"
-#define SERIALCOMM "2400/8n1"
-
static const int hwopts[] = {
SR_HWOPT_CONN,
SR_HWOPT_SERIALCOMM,
NULL,
};
-SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info;
-static struct sr_dev_driver *di = &tekpower_dmm_driver_info;
+SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info;
+static struct sr_dev_driver *di_tekpower_tp4000zc = &tekpower_tp4000zc_driver_info;
+
+/* After hw_init() this will point to a device-specific entry (see above). */
+static struct sr_dev_driver *di = NULL;
+
+SR_PRIV struct dmm_info dmms[] = {
+ {
+ "TekPower", "TP4000ZC",
+ "2400/8n1", 2400,
+ FS9721_PACKET_SIZE,
+ sr_fs9721_packet_valid,
+ sr_fs9721_parse,
+ dmm_details_tp4000zc,
+ },
+};
/* Properly close and free all devices. */
static int clear_instances(void)
return SR_OK;
}
-static int hw_init(void)
+static int hw_init(int dmm)
{
struct drv_context *drvc;
return SR_ERR_MALLOC;
}
+ if (dmm == TEKPOWER_TP4000ZC)
+ di = di_tekpower_tp4000zc;
+ sr_dbg("Selected '%s' subdriver.", di->name);
+
di->priv = drvc;
return SR_OK;
}
-static GSList *lcd14_scan(const char *conn, const char *serialcomm)
+static int hw_init_tekpower_tp4000zc(void)
+{
+ return hw_init(TEKPOWER_TP4000ZC);
+}
+
+static GSList *scan(const char *conn, const char *serialcomm, int dmm)
{
struct sr_dev_inst *sdi;
struct drv_context *drvc;
/* Let's get a bit of data and see if we can find a packet. */
len = sizeof(buf);
- ret = serial_stream_detect(serial, buf, &len, FS9721_PACKET_SIZE,
- sr_fs9721_packet_valid, 1000, 2400);
+ ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size,
+ dmms[dmm].packet_valid, 1000,
+ dmms[dmm].baudrate);
if (ret != SR_OK)
goto scan_cleanup;
* combination of the nonstandard cable that ships with this device and
* the serial port or USB to serial adapter.
*/
- dropped = len - FS9721_PACKET_SIZE;
- if (dropped > 2 * FS9721_PACKET_SIZE)
+ dropped = len - dmms[dmm].packet_size;
+ if (dropped > 2 * dmms[dmm].packet_size)
sr_warn("Had to drop too much data.");
sr_info("Found device on port %s.", conn);
- if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower",
- "TP4000ZC", "")))
+ if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, dmms[dmm].vendor,
+ dmms[dmm].device, "")))
goto scan_cleanup;
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
struct sr_hwopt *opt;
GSList *l, *devices;
const char *conn, *serialcomm;
+ int dmm;
conn = serialcomm = NULL;
for (l = options; l; l = l->next) {
if (!conn)
return NULL;
+ if (!strcmp(di->name, "tekpower-tp4000zc"))
+ dmm = 0;
+
if (serialcomm) {
/* Use the provided comm specs. */
- devices = lcd14_scan(conn, serialcomm);
+ devices = scan(conn, serialcomm, dmm);
} else {
/* Try the default. */
- devices = lcd14_scan(conn, SERIALCOMM);
+ devices = scan(conn, dmms[dmm].conn, dmm);
}
return devices;
struct sr_datafeed_header header;
struct sr_datafeed_meta_analog meta;
struct dev_context *devc;
+ int (*receive_data)(int, int, void *) = NULL;
if (!(devc = sdi->priv)) {
sr_err("sdi->priv was NULL.");
meta.num_probes = 1;
sr_session_send(devc->cb_data, &packet);
+ if (!strcmp(di->name, "tekpower-tp4000zc"))
+ receive_data = tekpower_tp4000zc_receive_data;
+
/* Poll every 50ms, or whenever some data comes in. */
sr_source_add(devc->serial->fd, G_IO_IN, 50,
- tekpower_dmm_receive_data, (void *)sdi);
+ receive_data, (void *)sdi);
return SR_OK;
}
return SR_OK;
}
-SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info = {
- .name = "tekpower-dmm",
- .longname = "TekPower/Digitek TP4000ZC/DT4000ZC DMM",
+SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info = {
+ .name = "tekpower-tp4000zc",
+ .longname = "TekPower TP4000ZC",
.api_version = 1,
- .init = hw_init,
+ .init = hw_init_tekpower_tp4000zc,
.cleanup = hw_cleanup,
.scan = hw_scan,
.dev_list = hw_dev_list,
* This file is part of the sigrok project.
*
* Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
+ * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include "libsigrok-internal.h"
#include "protocol.h"
-/* User-defined FS9721_LP3 flag 'c2c1_10' means temperature on this DMM. */
-#define is_temperature info.is_c2c1_10
-
static void log_dmm_packet(const uint8_t *buf)
{
sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x"
buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
}
-/* Now see what the value means, and pass that on. */
-static void fs9721_serial_handle_packet(const uint8_t *buf,
- struct dev_context *devc)
+SR_PRIV void dmm_details_tp4000zc(struct sr_datafeed_analog *analog, void *info)
+{
+ struct fs9721_info *info_local;
+
+ info_local = (struct fs9721_info *)info;
+
+ /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature. */
+ if (info_local->is_c2c1_10) {
+ analog->mq = SR_MQ_TEMPERATURE;
+ /* No Kelvin or Fahrenheit from the device, just Celsius. */
+ analog->unit = SR_UNIT_CELSIUS;
+ }
+}
+
+static void handle_packet(const uint8_t *buf, struct dev_context *devc,
+ int dmm, void *info)
{
float floatval;
struct sr_datafeed_packet packet;
struct sr_datafeed_analog *analog;
- struct fs9721_info info;
log_dmm_packet(buf);
analog->num_samples = 1;
analog->mq = -1;
- sr_fs9721_parse(buf, &floatval, analog, &info);
+ dmms[dmm].packet_parse(buf, &floatval, analog, info);
analog->data = &floatval;
- if (is_temperature) {
- analog->mq = SR_MQ_TEMPERATURE;
- /* No Kelvin or Fahrenheit from the device, just Celsius. */
- analog->unit = SR_UNIT_CELSIUS;
- }
+ dmms[dmm].dmm_details(analog, info);
if (analog->mq != -1) {
/* Got a measurement. */
g_free(analog);
}
-static void handle_new_data(struct dev_context *devc)
+static void handle_new_data(struct dev_context *devc, int dmm, void *info)
{
int len, i, offset = 0;
devc->buflen += len;
/* Now look for packets in that data. */
- while ((devc->buflen - offset) >= FS9721_PACKET_SIZE) {
- if (sr_fs9721_packet_valid(devc->buf + offset)) {
- fs9721_serial_handle_packet(devc->buf + offset, devc);
- offset += FS9721_PACKET_SIZE;
+ while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
+ if (dmms[dmm].packet_valid(devc->buf + offset)) {
+ handle_packet(devc->buf + offset, devc, dmm, info);
+ offset += dmms[dmm].packet_size;
} else {
offset++;
}
devc->buflen -= offset;
}
-SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
+static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
{
struct sr_dev_inst *sdi;
struct dev_context *devc;
if (revents == G_IO_IN) {
/* Serial data arrived. */
- handle_new_data(devc);
+ handle_new_data(devc, dmm, info);
}
if (devc->num_samples >= devc->limit_samples) {
return TRUE;
}
+
+SR_PRIV int tekpower_tp4000zc_receive_data(int fd, int revents, void *cb_data)
+{
+ struct fs9721_info info;
+
+ return receive_data(fd, revents, TEKPOWER_TP4000ZC, &info, cb_data);
+}
#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 DMM_BUFSIZE 256
+enum {
+ TEKPOWER_TP4000ZC,
+};
+
+struct dmm_info {
+ char *vendor;
+ char *device;
+ char *conn;
+ uint32_t baudrate;
+ int packet_size;
+ gboolean (*packet_valid)(const uint8_t *);
+ int (*packet_parse)(const uint8_t *, float *,
+ struct sr_datafeed_analog *, void *);
+ void (*dmm_details)(struct sr_datafeed_analog *, void *);
+};
+
+SR_PRIV struct dmm_info dmms[1];
+
+#define DMM_BUFSIZE 256
/** Private, per-device-instance driver context. */
struct dev_context {
int buflen;
};
-SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV int tekpower_tp4000zc_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV void dmm_details_tp4000zc(struct sr_datafeed_analog *analog, void *info);
#endif
extern SR_PRIV struct sr_dev_driver radioshackdmm_driver_info;
#endif
#ifdef HAVE_HW_TEKPOWER_DMM
-extern SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info;
+extern SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info;
#endif
#ifdef HAVE_HW_UNI_T_DMM
extern SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
&radioshackdmm_driver_info,
#endif
#ifdef HAVE_HW_TEKPOWER_DMM
- &tekpower_dmm_driver_info,
+ &tekpower_tp4000zc_driver_info,
#endif
#ifdef HAVE_HW_UNI_T_DMM
&uni_t_ut61d_driver_info,