Replace some magic numbers with a #define.

This commit is contained in:
Uwe Hermann 2015-04-01 01:53:50 +02:00
parent dc89faeace
commit 07ffa5b315
10 changed files with 38 additions and 28 deletions

View File

@ -118,7 +118,7 @@ SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
divisor = 1; divisor = 1;
for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
segments = decode_pair(buf + 1 + 2*i); segments = decode_pair(buf + 1 + (2 * i));
for (j = 0; j < ARRAY_SIZE(lookup); j++) { for (j = 0; j < ARRAY_SIZE(lookup); j++) {
if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) { if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) {
value += j * pow(10, i); value += j * pow(10, i);

View File

@ -20,8 +20,10 @@
#include "protocol.h" #include "protocol.h"
#define NUM_CHANNELS 4
struct center_info { struct center_info {
float temp[4]; float temp[NUM_CHANNELS];
gboolean rec, std, max, min, maxmin, t1t2, rel, hold, lowbat, celsius; gboolean rec, std, max, min, maxmin, t1t2, rel, hold, lowbat, celsius;
gboolean memfull, autooff; gboolean memfull, autooff;
gboolean mode_std, mode_rel, mode_max, mode_min, mode_maxmin; gboolean mode_std, mode_rel, mode_max, mode_min, mode_maxmin;
@ -84,21 +86,21 @@ static int packet_parse(const uint8_t *buf, int idx, struct center_info *info)
info->autooff = (buf[2] & (1 << 7)) != 0; info->autooff = (buf[2] & (1 << 7)) != 0;
/* Byte 7+8/9+10/11+12/13+14: channel T1/T2/T3/T4 temperature. */ /* Byte 7+8/9+10/11+12/13+14: channel T1/T2/T3/T4 temperature. */
for (i = 0; i < 4; i++) { for (i = 0; i < NUM_CHANNELS; i++) {
temp_u16 = buf[8 + (i * 2)]; temp_u16 = buf[8 + (i * 2)];
temp_u16 |= ((uint16_t)buf[7 + (i * 2)] << 8); temp_u16 |= ((uint16_t)buf[7 + (i * 2)] << 8);
info->temp[i] = (float)temp_u16; info->temp[i] = (float)temp_u16;
} }
/* Byte 43: Specifies whether we need to divide the value(s) by 10. */ /* Byte 43: Specifies whether we need to divide the value(s) by 10. */
for (i = 0; i < 4; i++) { for (i = 0; i < NUM_CHANNELS; i++) {
/* Bit = 0: Divide by 10. Bit = 1: Don't divide by 10. */ /* Bit = 0: Divide by 10. Bit = 1: Don't divide by 10. */
if ((buf[43] & (1 << i)) == 0) if ((buf[43] & (1 << i)) == 0)
info->temp[i] /= 10; info->temp[i] /= 10;
} }
/* Bytes 39-42: Overflow/overlimit bits, depending on mode. */ /* Bytes 39-42: Overflow/overlimit bits, depending on mode. */
for (i = 0; i < 4; i++) { for (i = 0; i < NUM_CHANNELS; i++) {
if (info->mode_std && ((buf[39] & (1 << i)) != 0)) if (info->mode_std && ((buf[39] & (1 << i)) != 0))
info->temp[i] = INFINITY; info->temp[i] = INFINITY;
/* TODO: Rel. Not available on all models. */ /* TODO: Rel. Not available on all models. */
@ -144,7 +146,7 @@ static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
analog.num_samples = 1; analog.num_samples = 1;
/* Send the values for T1 - T4. */ /* Send the values for T1 - T4. */
for (i = 0; i < 4; i++) { for (i = 0; i < NUM_CHANNELS; i++) {
l = NULL; l = NULL;
l = g_slist_append(l, g_slist_nth_data(sdi->channels, i)); l = g_slist_append(l, g_slist_nth_data(sdi->channels, i));
analog.channels = l; analog.channels = l;

View File

@ -301,13 +301,13 @@ static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
while (timeout_us > g_get_monotonic_time()) { while (timeout_us > g_get_monotonic_time()) {
/* Receive reply (14 bytes) */ /* Receive reply (14 bytes) */
devc->buflen = 0; devc->buflen = 0;
for (cnt = 0; cnt < 14; cnt++) { for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
byte = read_byte(serial, timeout_us); byte = read_byte(serial, timeout_us);
if (byte != -1) if (byte != -1)
devc->buf[devc->buflen++] = (byte & MASK_6BITS); devc->buf[devc->buflen++] = (byte & MASK_6BITS);
} }
if (devc->buflen != 14) if (devc->buflen != GMC_REPLY_SIZE)
continue; continue;
devc->addr = devc->buf[0]; devc->addr = devc->buf[0];

View File

@ -900,7 +900,7 @@ static guchar calc_chksum_14(guchar* dta)
{ {
guchar cnt, chs; guchar cnt, chs;
for (chs = 0, cnt = 0; cnt < 13; cnt++) for (chs = 0, cnt = 0; cnt < (GMC_REPLY_SIZE - 1); cnt++)
chs += dta[cnt]; chs += dta[cnt];
return (64 - chs) & MASK_6BITS; return (64 - chs) & MASK_6BITS;
@ -1263,7 +1263,7 @@ SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data)
*/ */
void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf) void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf)
{ {
uint8_t dta[14]; /* Unencoded message */ uint8_t dta[GMC_REPLY_SIZE]; /* Unencoded message */
int cnt; int cnt;
if (!params || !buf) if (!params || !buf)
@ -1279,17 +1279,17 @@ void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf)
/* 4-12: Copy further parameters */ /* 4-12: Copy further parameters */
for (cnt = 0; cnt < 9; cnt++) for (cnt = 0; cnt < 9; cnt++)
dta[cnt+4] = (params[cnt] & MASK_6BITS); dta[cnt + 4] = (params[cnt] & MASK_6BITS);
/* 13: Checksum (b complement) */ /* 13: Checksum (b complement) */
dta[13] = calc_chksum_14(dta); dta[13] = calc_chksum_14(dta);
/* The whole message is packed into 3 bytes per byte now (lower 6 bits only) the most /* The whole message is packed into 3 bytes per byte now (lower 6 bits only) the most
* peculiar way I have ever seen. Possibly to improve IR communication? */ * peculiar way I have ever seen. Possibly to improve IR communication? */
for (cnt = 0; cnt < 14; cnt++) { for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
buf[3*cnt] = (dta[cnt] & 0x01 ? 0x0f : 0) | (dta[cnt] & 0x02 ? 0xf0 : 0); buf[(3 * cnt) + 0] = (dta[cnt] & 0x01 ? 0x0f : 0) | (dta[cnt] & 0x02 ? 0xf0 : 0);
buf[3*cnt + 1] = (dta[cnt] & 0x04 ? 0x0f : 0) | (dta[cnt] & 0x08 ? 0xf0 : 0); buf[(3 * cnt) + 1] = (dta[cnt] & 0x04 ? 0x0f : 0) | (dta[cnt] & 0x08 ? 0xf0 : 0);
buf[3*cnt + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0); buf[(3 * cnt) + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0);
} }
} }

View File

@ -32,7 +32,8 @@
#define LOG_PREFIX "gmc-mh-1x-2x" #define LOG_PREFIX "gmc-mh-1x-2x"
#define GMC_BUFSIZE 266 #define GMC_BUFSIZE 266
#define GMC_REPLY_SIZE 14
/** Message ID bits 4, 5 */ /** Message ID bits 4, 5 */
#define MSGID_MASK 0x30 /**< Mask to get message ID bits */ #define MSGID_MASK 0x30 /**< Mask to get message ID bits */

View File

@ -40,6 +40,8 @@
#define NUM_TIMEBASE 10 #define NUM_TIMEBASE 10
#define NUM_VDIV 8 #define NUM_VDIV 8
#define NUM_BUFFER_SIZES 2
static const uint32_t scanopts[] = { static const uint32_t scanopts[] = {
SR_CONF_CONN, SR_CONF_CONN,
}; };
@ -543,13 +545,13 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
break; break;
case SR_CONF_BUFFERSIZE: case SR_CONF_BUFFERSIZE:
tmp_u64 = g_variant_get_uint64(data); tmp_u64 = g_variant_get_uint64(data);
for (i = 0; i < 2; i++) { for (i = 0; i < NUM_BUFFER_SIZES; i++) {
if (devc->profile->buffersizes[i] == tmp_u64) { if (devc->profile->buffersizes[i] == tmp_u64) {
devc->framesize = tmp_u64; devc->framesize = tmp_u64;
break; break;
} }
} }
if (i == 2) if (i == NUM_BUFFER_SIZES)
ret = SR_ERR_ARG; ret = SR_ERR_ARG;
break; break;
case SR_CONF_TIMEBASE: case SR_CONF_TIMEBASE:
@ -658,7 +660,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
return SR_ERR_ARG; return SR_ERR_ARG;
devc = sdi->priv; devc = sdi->priv;
*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
devc->profile->buffersizes, 2, sizeof(uint64_t)); devc->profile->buffersizes, NUM_BUFFER_SIZES, sizeof(uint64_t));
break; break;
case SR_CONF_TIMEBASE: case SR_CONF_TIMEBASE:
g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);

View File

@ -26,6 +26,8 @@
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
#include "dso.h" #include "dso.h"
#define NUM_CHANNELS 2
extern struct sr_dev_driver hantek_dso_driver_info; extern struct sr_dev_driver hantek_dso_driver_info;
static int send_begin(const struct sr_dev_inst *sdi) static int send_begin(const struct sr_dev_inst *sdi)
@ -225,7 +227,7 @@ static int get_channel_offsets(const struct sr_dev_inst *sdi)
* since that's how voltage offsets are submitted back to the DSO. * since that's how voltage offsets are submitted back to the DSO.
* Convert to host order now, so we can use them natively. * Convert to host order now, so we can use them natively.
*/ */
for (chan = 0; chan < 2; chan++) { for (chan = 0; chan < NUM_CHANNELS; chan++) {
for (v = 0; v < 9; v++) { for (v = 0; v < 9; v++) {
devc->channel_levels[chan][v][0] = devc->channel_levels[chan][v][0] =
g_ntohs(devc->channel_levels[chan][v][0]); g_ntohs(devc->channel_levels[chan][v][0]);
@ -236,7 +238,7 @@ static int get_channel_offsets(const struct sr_dev_inst *sdi)
if (sr_log_loglevel_get() >= SR_LOG_DBG) { if (sr_log_loglevel_get() >= SR_LOG_DBG) {
gs = g_string_sized_new(128); gs = g_string_sized_new(128);
for (chan = 0; chan < 2; chan++) { for (chan = 0; chan < NUM_CHANNELS; chan++) {
g_string_printf(gs, "CH%d:", chan + 1); g_string_printf(gs, "CH%d:", chan + 1);
for (v = 0; v < 9; v++) { for (v = 0; v < 9; v++) {
g_string_append_printf(gs, " %.4x-%.4x", g_string_append_printf(gs, " %.4x-%.4x",

View File

@ -24,6 +24,8 @@
#include "protocol.h" #include "protocol.h"
#define LINE_LENGTH 20
SR_PRIV const struct nmadmm_req nmadmm_requests[] = { SR_PRIV const struct nmadmm_req nmadmm_requests[] = {
{ NMADMM_REQ_IDN, "IDN?" }, { NMADMM_REQ_IDN, "IDN?" },
{ NMADMM_REQ_IDN, "STATUS?" }, { NMADMM_REQ_IDN, "STATUS?" },
@ -92,19 +94,19 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
devc = sdi->priv; devc = sdi->priv;
devc->buf[20] = '\0'; devc->buf[LINE_LENGTH] = '\0';
sr_spew("Received line '%s'.", devc->buf); sr_spew("Received line '%s'.", devc->buf);
/* Check line. */ /* Check line. */
if (strlen((const char *)devc->buf) != 20) { if (strlen((const char *)devc->buf) != LINE_LENGTH) {
sr_err("line: Invalid status '%s', must be 20 hex digits.", sr_err("line: Invalid status '%s', must be 20 hex digits.",
devc->buf); devc->buf);
devc->buflen = 0; devc->buflen = 0;
return; return;
} }
for (pos = 0; pos < 20; pos++) { for (pos = 0; pos < LINE_LENGTH; pos++) {
if (!isxdigit(devc->buf[pos])) { if (!isxdigit(devc->buf[pos])) {
sr_err("line: Expected hex digit in '%s' at pos %d!", sr_err("line: Expected hex digit in '%s' at pos %d!",
devc->buf, pos); devc->buf, pos);

View File

@ -100,7 +100,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
struct dev_context *devc; struct dev_context *devc;
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
GSList *l, *devices; GSList *l, *devices;
int ret, i; int ret;
unsigned int i;
const char *conn, *serialcomm; const char *conn, *serialcomm;
char buf[8]; char buf[8];
@ -191,7 +192,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sdi->model = g_strdup("Logic Analyzer"); sdi->model = g_strdup("Logic Analyzer");
sdi->version = g_strdup("v1.0"); sdi->version = g_strdup("v1.0");
sdi->driver = di; sdi->driver = di;
for (i = 0; i < 32; i++) for (i = 0; i < ARRAY_SIZE(ols_channel_names); i++)
sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
ols_channel_names[i]); ols_channel_names[i]);
devc = ols_dev_new(); devc = ols_dev_new();

View File

@ -50,7 +50,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
struct sr_dev_inst *sdi; struct sr_dev_inst *sdi;
struct sr_config *src; struct sr_config *src;
GSList *usb_devices, *devices, *l; GSList *usb_devices, *devices, *l;
int i; unsigned int i;
const char *conn; const char *conn;
drvc = di->priv; drvc = di->priv;
@ -80,7 +80,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sdi->driver = di; sdi->driver = di;
sdi->inst_type = SR_INST_USB; sdi->inst_type = SR_INST_USB;
sdi->conn = l->data; sdi->conn = l->data;
for (i = 0; i < 3; i++) for (i = 0; i < ARRAY_SIZE(channel_names); i++)
sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
channel_names[i]); channel_names[i]);
devc = g_malloc0(sizeof(struct dev_context)); devc = g_malloc0(sizeof(struct dev_context));