From: Uwe Hermann Date: Tue, 31 Mar 2015 23:53:50 +0000 (+0200) Subject: Replace some magic numbers with a #define. X-Git-Tag: libsigrok-0.4.0~545 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=07ffa5b315caab0d6d703e04f09035bc0f2b3ee4;p=libsigrok.git Replace some magic numbers with a #define. --- diff --git a/src/dmm/ut372.c b/src/dmm/ut372.c index d9c7720f..2de3da34 100644 --- a/src/dmm/ut372.c +++ b/src/dmm/ut372.c @@ -118,7 +118,7 @@ SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval, divisor = 1; 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++) { if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) { value += j * pow(10, i); diff --git a/src/hardware/center-3xx/protocol.c b/src/hardware/center-3xx/protocol.c index e7af1039..4cb1b95a 100644 --- a/src/hardware/center-3xx/protocol.c +++ b/src/hardware/center-3xx/protocol.c @@ -20,8 +20,10 @@ #include "protocol.h" +#define NUM_CHANNELS 4 + struct center_info { - float temp[4]; + float temp[NUM_CHANNELS]; gboolean rec, std, max, min, maxmin, t1t2, rel, hold, lowbat, celsius; gboolean memfull, autooff; 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; /* 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 |= ((uint16_t)buf[7 + (i * 2)] << 8); info->temp[i] = (float)temp_u16; } /* 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. */ if ((buf[43] & (1 << i)) == 0) info->temp[i] /= 10; } /* 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)) info->temp[i] = INFINITY; /* 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; /* Send the values for T1 - T4. */ - for (i = 0; i < 4; i++) { + for (i = 0; i < NUM_CHANNELS; i++) { l = NULL; l = g_slist_append(l, g_slist_nth_data(sdi->channels, i)); analog.channels = l; diff --git a/src/hardware/gmc-mh-1x-2x/api.c b/src/hardware/gmc-mh-1x-2x/api.c index 8f9be220..efb9e009 100644 --- a/src/hardware/gmc-mh-1x-2x/api.c +++ b/src/hardware/gmc-mh-1x-2x/api.c @@ -301,13 +301,13 @@ static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options) while (timeout_us > g_get_monotonic_time()) { /* Receive reply (14 bytes) */ devc->buflen = 0; - for (cnt = 0; cnt < 14; cnt++) { + for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) { byte = read_byte(serial, timeout_us); if (byte != -1) devc->buf[devc->buflen++] = (byte & MASK_6BITS); } - if (devc->buflen != 14) + if (devc->buflen != GMC_REPLY_SIZE) continue; devc->addr = devc->buf[0]; diff --git a/src/hardware/gmc-mh-1x-2x/protocol.c b/src/hardware/gmc-mh-1x-2x/protocol.c index 719a225f..5e12e06f 100644 --- a/src/hardware/gmc-mh-1x-2x/protocol.c +++ b/src/hardware/gmc-mh-1x-2x/protocol.c @@ -900,7 +900,7 @@ static guchar calc_chksum_14(guchar* dta) { 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]; 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) { - uint8_t dta[14]; /* Unencoded message */ + uint8_t dta[GMC_REPLY_SIZE]; /* Unencoded message */ int cnt; if (!params || !buf) @@ -1279,17 +1279,17 @@ void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf) /* 4-12: Copy further parameters */ for (cnt = 0; cnt < 9; cnt++) - dta[cnt+4] = (params[cnt] & MASK_6BITS); + dta[cnt + 4] = (params[cnt] & MASK_6BITS); /* 13: Checksum (b complement) */ dta[13] = calc_chksum_14(dta); /* 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? */ - for (cnt = 0; cnt < 14; cnt++) { - buf[3*cnt] = (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 + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0); + for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) { + 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) + 2] = (dta[cnt] & 0x10 ? 0x0f : 0) | (dta[cnt] & 0x20 ? 0xf0 : 0); } } diff --git a/src/hardware/gmc-mh-1x-2x/protocol.h b/src/hardware/gmc-mh-1x-2x/protocol.h index 5d20e921..87034e6b 100644 --- a/src/hardware/gmc-mh-1x-2x/protocol.h +++ b/src/hardware/gmc-mh-1x-2x/protocol.h @@ -32,7 +32,8 @@ #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 */ #define MSGID_MASK 0x30 /**< Mask to get message ID bits */ diff --git a/src/hardware/hantek-dso/api.c b/src/hardware/hantek-dso/api.c index 2b039ed2..f29d4a0e 100644 --- a/src/hardware/hantek-dso/api.c +++ b/src/hardware/hantek-dso/api.c @@ -40,6 +40,8 @@ #define NUM_TIMEBASE 10 #define NUM_VDIV 8 +#define NUM_BUFFER_SIZES 2 + static const uint32_t scanopts[] = { SR_CONF_CONN, }; @@ -543,13 +545,13 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd break; case SR_CONF_BUFFERSIZE: 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) { devc->framesize = tmp_u64; break; } } - if (i == 2) + if (i == NUM_BUFFER_SIZES) ret = SR_ERR_ARG; break; 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; devc = sdi->priv; *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; case SR_CONF_TIMEBASE: g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); diff --git a/src/hardware/hantek-dso/dso.c b/src/hardware/hantek-dso/dso.c index 7a2d2379..156efd93 100644 --- a/src/hardware/hantek-dso/dso.c +++ b/src/hardware/hantek-dso/dso.c @@ -26,6 +26,8 @@ #include "libsigrok-internal.h" #include "dso.h" +#define NUM_CHANNELS 2 + extern struct sr_dev_driver hantek_dso_driver_info; 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. * 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++) { 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) { 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); for (v = 0; v < 9; v++) { g_string_append_printf(gs, " %.4x-%.4x", diff --git a/src/hardware/norma-dmm/protocol.c b/src/hardware/norma-dmm/protocol.c index 28f733c6..2c700ad5 100644 --- a/src/hardware/norma-dmm/protocol.c +++ b/src/hardware/norma-dmm/protocol.c @@ -24,6 +24,8 @@ #include "protocol.h" +#define LINE_LENGTH 20 + SR_PRIV const struct nmadmm_req nmadmm_requests[] = { { NMADMM_REQ_IDN, "IDN?" }, { NMADMM_REQ_IDN, "STATUS?" }, @@ -92,19 +94,19 @@ static void nma_process_line(const struct sr_dev_inst *sdi) devc = sdi->priv; - devc->buf[20] = '\0'; + devc->buf[LINE_LENGTH] = '\0'; sr_spew("Received line '%s'.", devc->buf); /* 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.", devc->buf); devc->buflen = 0; return; } - for (pos = 0; pos < 20; pos++) { + for (pos = 0; pos < LINE_LENGTH; pos++) { if (!isxdigit(devc->buf[pos])) { sr_err("line: Expected hex digit in '%s' at pos %d!", devc->buf, pos); diff --git a/src/hardware/openbench-logic-sniffer/api.c b/src/hardware/openbench-logic-sniffer/api.c index 620f5e1b..246b900a 100644 --- a/src/hardware/openbench-logic-sniffer/api.c +++ b/src/hardware/openbench-logic-sniffer/api.c @@ -100,7 +100,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct dev_context *devc; struct sr_serial_dev_inst *serial; GSList *l, *devices; - int ret, i; + int ret; + unsigned int i; const char *conn, *serialcomm; char buf[8]; @@ -191,7 +192,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sdi->model = g_strdup("Logic Analyzer"); sdi->version = g_strdup("v1.0"); 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, ols_channel_names[i]); devc = ols_dev_new(); diff --git a/src/hardware/uni-t-ut32x/api.c b/src/hardware/uni-t-ut32x/api.c index a4868287..136182e2 100644 --- a/src/hardware/uni-t-ut32x/api.c +++ b/src/hardware/uni-t-ut32x/api.c @@ -50,7 +50,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct sr_dev_inst *sdi; struct sr_config *src; GSList *usb_devices, *devices, *l; - int i; + unsigned int i; const char *conn; drvc = di->priv; @@ -80,7 +80,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sdi->driver = di; sdi->inst_type = SR_INST_USB; 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, channel_names[i]); devc = g_malloc0(sizeof(struct dev_context));