- }
- else {
- /* this is the main loop telling us a timeout was reached, or we've
- * acquired all the samples we asked for -- we're done */
- tcflush(fd, TCIOFLUSH);
+ } else {
+ /*
+ * This is the main loop telling us a timeout was reached, or
+ * we've acquired all the samples we asked for -- we're done.
+ * Send the (properly-ordered) buffer to the frontend.
+ */
+ if (trigger_at != -1) {
+ /* a trigger was set up, so we need to tell the frontend
+ * about it.
+ */
+ if (trigger_at > 0) {
+ /* there are pre-trigger samples, send those first */
+ packet.type = DF_LOGIC;
+ packet.length = trigger_at * 4;
+ packet.unitsize = 4;
+ packet.payload = raw_sample_buf;
+ session_bus(user_data, &packet);
+ }
+
+ packet.type = DF_TRIGGER;
+ packet.length = 0;
+ session_bus(user_data, &packet);
+
+ packet.type = DF_LOGIC;
+ packet.length = (limit_samples * 4) - (trigger_at * 4);
+ packet.unitsize = 4;
+ packet.payload = raw_sample_buf + trigger_at * 4;
+ session_bus(user_data, &packet);
+ } else {
+ packet.type = DF_LOGIC;
+ packet.length = limit_samples * 4;
+ packet.unitsize = 4;
+ packet.payload = raw_sample_buf;
+ session_bus(user_data, &packet);
+ }
+ free(raw_sample_buf);
+
+ serial_flush(fd);