]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/center-3xx/protocol.c
center-3xx: Fix incorrect values due to endianness issue.
[libsigrok.git] / src / hardware / center-3xx / protocol.c
index ca11449f4f751eec86c1ca92afbf43823261dd65..35fe15538642ee71fa43372d92ca0f07d11bf2ed 100644 (file)
@@ -14,8 +14,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <config.h>
@@ -25,6 +24,7 @@
 
 struct center_info {
        float temp[NUM_CHANNELS];
+       int digits[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;
@@ -64,7 +64,7 @@ static void log_packet(const uint8_t *buf, int idx)
 static int packet_parse(const uint8_t *buf, int idx, struct center_info *info)
 {
        int i;
-       uint16_t temp_u16;
+       int16_t temp_i16;
 
        log_packet(buf, idx);
 
@@ -89,16 +89,19 @@ static int packet_parse(const uint8_t *buf, int idx, struct center_info *info)
 
        /* Byte 7+8/9+10/11+12/13+14: channel T1/T2/T3/T4 temperature. */
        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;
+               temp_i16 = RB16S(&buf[7 + 2 * i]);
+               info->temp[i] = (float)temp_i16;
        }
 
        /* Byte 43: Specifies whether we need to divide the value(s) by 10. */
        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)
+               if ((buf[43] & (1 << i)) == 0) {
                        info->temp[i] /= 10;
+                       info->digits[i] = 1;
+               } else {
+                       info->digits[i] = 0;
+               }
        }
 
        /* Bytes 39-42: Overflow/overlimit bits, depending on mode. */
@@ -134,6 +137,7 @@ static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
 
        devc = sdi->priv;
 
+       /* Note: digits/spec_digits will be overridden later. */
        sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
        memset(&info, 0, sizeof(struct center_info));
 
@@ -155,6 +159,8 @@ static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
                l = NULL;
                l = g_slist_append(l, g_slist_nth_data(sdi->channels, i));
                analog.meaning->channels = l;
+               analog.encoding->digits = info.digits[i];
+               analog.spec->spec_digits = info.digits[i];
                analog.data = &(info.temp[i]);
                sr_session_send(sdi, &packet);
                g_slist_free(l);
@@ -170,7 +176,7 @@ static gboolean handle_new_data(struct sr_dev_inst *sdi, int idx)
 {
        struct dev_context *devc;
        struct sr_serial_dev_inst *serial;
-       int len, i, offset = 0, ret = FALSE;
+       int len, offset, ret = FALSE;
 
        devc = sdi->priv;
        serial = sdi->conn;
@@ -186,6 +192,7 @@ static gboolean handle_new_data(struct sr_dev_inst *sdi, int idx)
        devc->buflen += len;
 
        /* Now look for packets in that data. */
+       offset = 0;
        while ((devc->buflen - offset) >= center_devs[idx].packet_size) {
                if (center_devs[idx].packet_valid(devc->buf + offset)) {
                        handle_packet(devc->buf + offset, sdi, idx);
@@ -197,8 +204,8 @@ static gboolean handle_new_data(struct sr_dev_inst *sdi, int idx)
        }
 
        /* 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];
+       if (offset < devc->buflen)
+               memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
        devc->buflen -= offset;
 
        return ret;
@@ -236,7 +243,7 @@ static int receive_data(int fd, int revents, int idx, void *cb_data)
        }
 
        if (sr_sw_limits_check(&devc->sw_limits))
-               sdi->driver->dev_acquisition_stop(sdi);
+               sr_dev_acquisition_stop(sdi);
 
        return TRUE;
 }