Code example¶
The following C++ source code shows how to initialize and configure the Ndigo5G and read a limited amount of packets.
To get the example running on your system, please refer to the instructions on our GitHub repository.
1//
2// ndigo_user_guide_example.cpp:
3// User guide example for Ndigo5G driver programming
4//
5
6#include "Ndigo_interface.h"
7#include <stdio.h>
8#include <stdlib.h>
9
10int main(int argc, char* argv[])
11{
12 printf("cronologic ndigo_user_guide_example using driver: %s\n",
13 ndigo_get_driver_revision_str());
14
15 ndigo_init_parameters params;
16 ndigo_get_default_init_parameters(¶ms);
17
18 params.card_index = 0;
19 params.buffer_size[0] = 1 << 23;
20 params.drive_external_clock = false;
21 params.multiboard_sync = false;
22 params.use_external_clock = false;
23 params.hptdc_sync_enabled = false;
24 params.is_slave = false;
25
26 int error_code;
27 const char *error_message;
28 ndigo_device* ndgo = ndigo_init(¶ms, &error_code, &error_message);
29 if (error_code != NDIGO_OK) {
30 printf("\nError %d: %s\n", error_code, error_message);
31 exit(-1);
32 }
33
34 ndigo_configuration config;
35 ndigo_get_default_configuration(ndgo, &config);
36 config.adc_mode = NDIGO_ADC_MODE_ABCD;
37 config.output_mode = NDIGO_OUTPUT_MODE_SIGNED16;
38
39 // disable unused trigger blocks
40 config.trigger_block[1].enabled = false;
41 config.trigger_block[2].enabled = false;
42 config.trigger_block[3].enabled = false;
43 config.trigger_block[4].enabled = false;
44
45 // configure trigger block 0
46 config.trigger_block[0].enabled = true;
47 config.trigger_block[0].minimum_free_packets = 1.0;
48 config.trigger_block[0].precursor = 1;
49 config.trigger_block[0].retrigger = 0;
50 config.trigger_block[0].sources = NDIGO_TRIGGER_SOURCE_A0;
51 config.trigger_block[0].length = 16;
52 config.trigger_block[0].gates = NDIGO_TRIGGER_GATE_NONE;
53
54 config.analog_offset[0] = 0.1;
55
56 config.trigger[NDIGO_TRIGGER_A0].edge = true;
57 config.trigger[NDIGO_TRIGGER_A0].rising = false;
58 config.trigger[NDIGO_TRIGGER_A0].threshold = 0;
59
60 if (ndigo_configure(ndgo, &config) != NDIGO_OK) {
61 printf("\nFatal configuration error. Aborting...\n");
62 exit(-1);
63 }
64
65 ndigo_start_capture(ndgo);
66
67 // counts the number of packets received
68 int count = 0;
69
70 while (count < 10) {
71 ndigo_read_in in;
72 // Do not wait for data
73 // (if set to 1 the ndigo_acknowledge function has to be removed)
74 in.acknowledge_last_read = 0;
75 ndigo_read_out out;
76 int result = ndigo_read(ndgo, &in, &out);
77 if ((NDIGO_OK == result) && (out.first_packet != nullptr))
78 {
79 // buffer received with one or more packets
80 volatile ndigo_packet *packet = out.first_packet;
81 while (packet <= out.last_packet) {
82 int length = 0;
83 if (!(packet->type & NDIGO_PACKET_TYPE_TIMESTAMP_ONLY))
84 length = packet->length;
85
86 printf("Card %d, Channel %d, Flags %d, Length %d, Timestamp %lu \n",
87 packet->card,
88 packet->channel,
89 packet->flags,
90 length,
91 packet->timestamp);
92
93 if (!(packet->type & NDIGO_PACKET_TYPE_TIMESTAMP_ONLY))
94 {
95 short* data = (short*)packet->data;
96 for (unsigned int i = 0; i < packet->length * 4; i++)
97 printf("%6d, ", *(data++));
98 printf("\n\n");
99 }
100 // current packet pointer is invalid after
101 // call to ndigo_acknowledge()
102 volatile ndigo_packet *next_packet = ndigo_next_packet(packet);
103 ndigo_acknowledge(ndgo, packet);
104 packet = next_packet;
105 count++;
106 }
107 }
108 }
109 ndigo_close(ndgo);
110
111 system("pause");
112
113 return 0;
114}