示例#1
0
文件: demo.c 项目: russdill/libsigrok
/* Callback handling data */
static int receive_data(int fd, int revents, void *cb_data)
{
	struct dev_context *devc = cb_data;
	struct sr_datafeed_packet packet;
	struct sr_datafeed_logic logic;
	uint8_t buf[BUFSIZE];
	static uint64_t samples_to_send, expected_samplenum, sending_now;
	int64_t time, elapsed;

	(void)fd;
	(void)revents;

	/* How many "virtual" samples should we have collected by now? */
	time = g_get_monotonic_time();
	elapsed = time - devc->starttime;
	expected_samplenum = elapsed * devc->cur_samplerate / 1000000;
	/* Of those, how many do we still have to send? */
	samples_to_send = expected_samplenum - devc->samples_counter;

	if (devc->limit_samples) {
		samples_to_send = MIN(samples_to_send,
				 devc->limit_samples - devc->samples_counter);
	}

	while (samples_to_send > 0) {
		sending_now = MIN(samples_to_send, sizeof(buf));
		samples_to_send -= sending_now;
		samples_generator(buf, sending_now, devc);

		packet.type = SR_DF_LOGIC;
		packet.payload = &logic;
		logic.length = sending_now;
		logic.unitsize = 1;
		logic.data = buf;
		sr_session_send(devc->cb_data, &packet);
		devc->samples_counter += sending_now;
	}

	if (devc->limit_samples &&
		devc->samples_counter >= devc->limit_samples) {
		sr_info("Requested number of samples reached.");
		dev_acquisition_stop(devc->sdi, cb_data);
		return TRUE;
	}

	return TRUE;
}
示例#2
0
文件: demo.c 项目: gsmcmullin/sigrok
/* Thread function */
static void thread_func(void *data)
{
	struct databag *mydata = data;
	uint8_t buf[BUFSIZE];
	uint64_t nb_to_send = 0;
	int bytes_written;
	double time_cur, time_last, time_diff;

	time_last = g_timer_elapsed(mydata->timer, NULL);

	while (thread_running) {
		/* Rate control */
		time_cur = g_timer_elapsed(mydata->timer, NULL);

		time_diff = time_cur - time_last;
		time_last = time_cur;

		nb_to_send = cur_samplerate * time_diff;

		if (limit_samples) {
			nb_to_send = MIN(nb_to_send,
				      limit_samples - mydata->samples_counter);
		}

		/* Make sure we don't overflow. */
		nb_to_send = MIN(nb_to_send, BUFSIZE);

		if (nb_to_send) {
			samples_generator(buf, nb_to_send, data);
			mydata->samples_counter += nb_to_send;

			g_io_channel_write_chars(channels[1], (gchar *)&buf,
				nb_to_send, (gsize *)&bytes_written, NULL);
		}

		/* Check if we're done. */
		if ((limit_msec && time_cur * 1000 > limit_msec) ||
		    (limit_samples && mydata->samples_counter >= limit_samples))
		{
			close(mydata->pipe_fds[1]);
			thread_running = 0;
		}

		g_usleep(10);
	}
}
示例#3
0
文件: demo.c 项目: contandreev/DSView
/* Callback handling data */
static int receive_data(int fd, int revents, const struct sr_dev_inst *sdi)
{
    struct dev_context *devc = sdi->priv;
    struct sr_datafeed_packet packet;
    struct sr_datafeed_logic logic;
    struct sr_datafeed_dso dso;
    struct sr_datafeed_analog analog;
    static uint64_t samples_to_send = 0, expected_samplenum, sending_now;
	int64_t time, elapsed;
    static uint16_t last_sample = 0;
    uint16_t cur_sample;
    int i;

	(void)fd;
	(void)revents;

	/* How many "virtual" samples should we have collected by now? */
	time = g_get_monotonic_time();
	elapsed = time - devc->starttime;
    devc->starttime = time;
    //expected_samplenum = ceil(elapsed / 1000000.0 * devc->cur_samplerate);
	/* Of those, how many do we still have to send? */
    //samples_to_send = (expected_samplenum - devc->samples_counter) / CONST_LEN * CONST_LEN;
    //samples_to_send = expected_samplenum / CONST_LEN * CONST_LEN;
    samples_to_send += ceil(elapsed / 1000000.0 * devc->cur_samplerate);

    if (devc->limit_samples) {
        if (sdi->mode == DSO && !devc->instant)
            samples_to_send = MIN(samples_to_send,
                                  devc->limit_samples - devc->pre_index);
        else if (sdi->mode == ANALOG)
            samples_to_send = MIN(samples_to_send * g_slist_length(sdi->channels),
                                  devc->limit_samples - devc->pre_index);
        else
            samples_to_send = MIN(samples_to_send,
                     devc->limit_samples - devc->samples_counter);
    }

    if (samples_to_send > 0 && !devc->stop) {
        sending_now = MIN(samples_to_send, (sdi->mode == DSO ) ? DSO_BUFSIZE : BUFSIZE);
        samples_generator(devc->buf, sending_now, sdi, devc);

        if (devc->trigger_stage != 0) {
            for (i = 0; i < sending_now; i++) {
                if (devc->trigger_edge == 0) {
                    if ((*(devc->buf + i) | devc->trigger_mask) ==
                            (devc->trigger_value | devc->trigger_mask)) {
                        devc->trigger_stage = 0;
                        break;
                    }
                } else {
                    cur_sample = *(devc->buf + i);
                    if (((last_sample & devc->trigger_edge) ==
                         (~devc->trigger_value & devc->trigger_edge)) &&
                        ((cur_sample | devc->trigger_mask) ==
                         (devc->trigger_value | devc->trigger_mask)) &&
                        ((cur_sample & devc->trigger_edge) ==
                         (devc->trigger_value & devc->trigger_edge))) {
                        devc->trigger_stage = 0;
                        break;
                    }
                    last_sample = cur_sample;
                }
            }
            if (devc->trigger_stage == 0) {
                struct ds_trigger_pos demo_trigger_pos;
                demo_trigger_pos.real_pos = i;
                packet.type = SR_DF_TRIGGER;
                packet.payload = &demo_trigger_pos;
                sr_session_send(sdi, &packet);
            }
        }

        if (sdi->mode == ANALOG)
            devc->samples_counter += sending_now/g_slist_length(sdi->channels);
        else
            devc->samples_counter += sending_now;
        if (sdi->mode == DSO && !devc->instant &&
            devc->samples_counter > devc->limit_samples)
            devc->samples_counter = devc->limit_samples;

        if (devc->trigger_stage == 0){
            samples_to_send -= sending_now;
            if (sdi->mode == LOGIC) {
                packet.type = SR_DF_LOGIC;
                packet.payload = &logic;
                logic.length = sending_now * (NUM_PROBES >> 3);
                logic.unitsize = (NUM_PROBES >> 3);
                logic.data = devc->buf;
            } else if (sdi->mode == DSO) {
示例#4
0
文件: demo.c 项目: AkosLukacs/DSLogic
/* Callback handling data */
static int receive_data(int fd, int revents, const struct sr_dev_inst *sdi)
{
    struct dev_context *devc = sdi->priv;
    struct sr_datafeed_packet packet;
    struct sr_datafeed_logic logic;
    struct sr_datafeed_dso dso;
    struct sr_datafeed_analog analog;
    //uint16_t buf[BUFSIZE];
    uint16_t *buf;
	static uint64_t samples_to_send, expected_samplenum, sending_now;
	int64_t time, elapsed;
    static uint16_t last_sample = 0;
    uint16_t cur_sample;
    int i;

	(void)fd;
	(void)revents;

    if (!(buf = g_try_malloc(BUFSIZE*sizeof(uint16_t)))) {
        sr_err("buf for receive_data malloc failed.");
        return FALSE;
    }

	/* How many "virtual" samples should we have collected by now? */
	time = g_get_monotonic_time();
	elapsed = time - devc->starttime;
    devc->starttime = time;
	expected_samplenum = elapsed * devc->cur_samplerate / 1000000;
	/* Of those, how many do we still have to send? */
    //samples_to_send = (expected_samplenum - devc->samples_counter) / CONST_LEN * CONST_LEN;
    samples_to_send = expected_samplenum / CONST_LEN * CONST_LEN;

    if (devc->limit_samples) {
        if (sdi->mode == LOGIC)
            samples_to_send = MIN(samples_to_send,
                     devc->limit_samples - devc->samples_counter);
        else
            samples_to_send = MIN(samples_to_send,
                     devc->limit_samples);
    }

    while (samples_to_send > 0) {
        sending_now = MIN(samples_to_send, BUFSIZE);
		samples_generator(buf, sending_now, devc);

        if (devc->trigger_stage != 0) {
            for (i = 0; i < sending_now; i++) {
                if (devc->trigger_edge == 0) {
                    if ((*(buf + i) | devc->trigger_mask) ==
                            (devc->trigger_value | devc->trigger_mask)) {
                        devc->trigger_stage = 0;
                        break;
                    }
                } else {
                    cur_sample = *(buf + i);
                    if (((last_sample & devc->trigger_edge) ==
                         (~devc->trigger_value & devc->trigger_edge)) &&
                        ((cur_sample | devc->trigger_mask) ==
                         (devc->trigger_value | devc->trigger_mask)) &&
                        ((cur_sample & devc->trigger_edge) ==
                         (devc->trigger_value & devc->trigger_edge))) {
                        devc->trigger_stage = 0;
                        break;
                    }
                    last_sample = cur_sample;
                }
            }
            if (devc->trigger_stage == 0) {
                struct ds_trigger_pos demo_trigger_pos;
                demo_trigger_pos.real_pos = i;
                packet.type = SR_DF_TRIGGER;
                packet.payload = &demo_trigger_pos;
                sr_session_send(sdi, &packet);
            }
        }

        if (devc->trigger_stage == 0){
            samples_to_send -= sending_now;
            if (sdi->mode == LOGIC) {
                packet.type = SR_DF_LOGIC;
                packet.payload = &logic;
                logic.length = sending_now * (NUM_PROBES >> 3);
                logic.unitsize = (NUM_PROBES >> 3);
                logic.data = buf;
            } else if (sdi->mode == DSO) {