]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/kern-scale/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / kern-scale / protocol.c
index 182a9d80b03da39f4e30dfe095b6288b6af0cdc4..722e06256ee3a177464e404409205f2d3be359f8 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>
@@ -34,27 +33,31 @@ static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
        float floatval;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
        struct dev_context *devc;
 
        scale = (struct scale_info *)sdi->driver;
 
        devc = sdi->priv;
 
-       memset(&analog, 0, sizeof(struct sr_datafeed_analog));
+       /* Note: digits/spec_digits will be overridden later. */
+       sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
 
-       analog.channels = sdi->channels;
+       analog.meaning->channels = sdi->channels;
        analog.num_samples = 1;
-       analog.mq = -1;
+       analog.meaning->mq = 0;
 
        scale->packet_parse(buf, &floatval, &analog, info);
        analog.data = &floatval;
 
-       if (analog.mq != -1) {
+       if (analog.meaning->mq != 0) {
                /* Got a measurement. */
                packet.type = SR_DF_ANALOG;
                packet.payload = &analog;
-               sr_session_send(devc->cb_data, &packet);
-               devc->num_samples++;
+               sr_session_send(sdi, &packet);
+               sr_sw_limits_update_samples_read(&devc->limits, 1);
        }
 }
 
@@ -62,7 +65,7 @@ static void handle_new_data(struct sr_dev_inst *sdi, void *info)
 {
        struct scale_info *scale;
        struct dev_context *devc;
-       int len, i, offset = 0;
+       int len, offset;
        struct sr_serial_dev_inst *serial;
 
        scale = (struct scale_info *)sdi->driver;
@@ -82,6 +85,7 @@ static void handle_new_data(struct sr_dev_inst *sdi, void *info)
        devc->buflen += len;
 
        /* Now look for packets in that data. */
+       offset = 0;
        while ((devc->buflen - offset) >= scale->packet_size) {
                if (scale->packet_valid(devc->buf + offset)) {
                        handle_packet(devc->buf + offset, sdi, info);
@@ -92,8 +96,8 @@ static void handle_new_data(struct sr_dev_inst *sdi, void *info)
        }
 
        /* 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;
 }
 
@@ -102,7 +106,6 @@ SR_PRIV int kern_scale_receive_data(int fd, int revents, void *cb_data)
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
        struct scale_info *scale;
-       int64_t time;
        void *info;
 
        (void)fd;
@@ -122,20 +125,8 @@ SR_PRIV int kern_scale_receive_data(int fd, int revents, void *cb_data)
                g_free(info);
        }
 
-       if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
-               sr_info("Requested number of samples reached.");
-               sdi->driver->dev_acquisition_stop(sdi, cb_data);
-               return TRUE;
-       }
-
-       if (devc->limit_msec) {
-               time = (g_get_monotonic_time() - devc->starttime) / 1000;
-               if (time > (int64_t)devc->limit_msec) {
-                       sr_info("Requested time limit reached.");
-                       sdi->driver->dev_acquisition_stop(sdi, cb_data);
-                       return TRUE;
-               }
-       }
+       if (sr_sw_limits_check(&devc->limits))
+               sr_dev_acquisition_stop(sdi);
 
        return TRUE;
 }