main.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include <memory>
  2. #include <windows.h>
  3. #include "logger.hpp"
  4. #include "simplelogger.hpp"
  5. #include "picofunctions.h"
  6. #include "parser.hpp"
  7. extern std::ostream out(std::cout.rdbuf());
  8. extern SimpleLogger newlogger(out, "pico", "picologs");
  9. HANDLE semaphore_open()
  10. {
  11. HANDLE sem = OpenSemaphoreA(SEMAPHORE_MODIFY_STATE, FALSE, "wait-semaphore-5d95950d-a278-4733-a041-ee9fb05ad4e4");
  12. if(sem == NULL)
  13. {
  14. newlogger << LogPref::Flag(ERROR) << "Cannot open semaphore: " << GetLastError() << endl;
  15. return NULL;
  16. }
  17. else
  18. {
  19. return sem;
  20. }
  21. }
  22. int semaphore_release(HANDLE sem)
  23. {
  24. if(!ReleaseSemaphore(sem, 1, NULL))
  25. {
  26. newlogger << LogPref::Flag(ERROR) << "Cannot release semaphore: " << GetLastError() << endl;
  27. return 0;
  28. }
  29. return 1;
  30. }
  31. int
  32. main(int argc, char* argv[])
  33. {
  34. bool flag_debug = false;
  35. bool flag_below = false;
  36. const auto &data_set = parse_xml_function(argv[1]);
  37. const auto &times = string_to_vector(std::get<2>(data_set));
  38. auto points_vec = string_to_vector(std::get<0>(data_set));
  39. const int32_t POINTS_VALUE = *(std::max_element(points_vec.begin(), points_vec.end()));
  40. const uint32_t NUMBER_OF_CHANNELS = std::get<1>(data_set);
  41. const uint32_t SAMPLE_FREQUENCY = std::get<3>(data_set);
  42. Parser parse(argc, argv);
  43. parse.cmdOption("--debug") == true ? flag_debug = true : flag_debug = false;
  44. parse.cmdOption("--below") == true ? flag_below = true : flag_below = false;
  45. newlogger << "PICO BELOW: " << flag_below << endl;
  46. //const std::string& file_name = parse.setFilename("-f", POINTS_VALUE, NUMBER_OF_CHANNELS, SAMPLE_FREQUENCY);
  47. //Logger logger(file_name, flag_debug);
  48. if ((NUMBER_OF_CHANNELS) <= 0 && (NUMBER_OF_CHANNELS) > 8)
  49. {
  50. newlogger << LogPref::Flag(ERROR) << "Incorrect NUMBER OF CHANNELS" << endl;
  51. return EOF;
  52. }
  53. if (SAMPLE_FREQUENCY < 0)
  54. {
  55. newlogger << LogPref::Flag(ERROR) << "Incorrect SAMPLE FREQUENCY" << endl;
  56. return EOF;
  57. }
  58. const auto &channels = create_channel(NUMBER_OF_CHANNELS);
  59. const string filepath{""};
  60. int16_t handle{0};
  61. string rs;
  62. if(flag_debug)
  63. newlogger << "handle = " << handle << endl << endl;
  64. auto retval_open = ps4000aOpenUnit(&handle, NULL);
  65. newlogger << "OPEN PICO: " << retval_open;
  66. int16_t enabled{true};
  67. PS4000A_COUPLING type_AC{PS4000A_AC};
  68. PS4000A_COUPLING type_DC{PS4000A_DC};
  69. PICO_CONNECT_PROBE_RANGE test_range{PICO_X1_PROBE_5V};
  70. //float analogOffset1{1.65};
  71. float analogOffset{0};
  72. int16_t start{10};
  73. auto retval2 = ps4000aFlashLed(handle, start);
  74. newlogger << retval2 << endl;
  75. int32_t timeIntervalNanoseconds{0};
  76. int32_t maxSamples{0};
  77. uint32_t segmentIndex{0};
  78. //logger.logInfo("SET TRIGGER CHANNEL CONDITIONS:");
  79. PS4000A_CONDITION conditions[NUMBER_OF_CHANNELS];
  80. conditions[0].source = channels[0];
  81. conditions[0].condition = PS4000A_CONDITION_TRUE;
  82. for (unsigned int i = 1; i < NUMBER_OF_CHANNELS; i++)
  83. {
  84. conditions[i].source = channels[i];
  85. conditions[i].condition = PS4000A_CONDITION_FALSE;
  86. }
  87. PS4000A_CONDITIONS_INFO info{PS4000A_CLEAR};
  88. /*
  89. retval2 = ps4000aSetTriggerChannelConditions(handle, conditions, NUMBER_OF_CHANNELS, info);
  90. logger.log_output(retval2);
  91. */
  92. newlogger << "SET CHANNEL PICO" << endl;
  93. retval2 =
  94. ps4000aSetChannel(handle, conditions[0].source, enabled, type_DC, test_range, analogOffset);
  95. newlogger << retval2 << endl;
  96. for (int32_t i = 1; i < NUMBER_OF_CHANNELS; i++)
  97. {
  98. retval2 = ps4000aSetChannel(handle, conditions[i].source, enabled, type_AC, test_range,
  99. analogOffset);
  100. newlogger << retval2 << endl;
  101. }
  102. /*
  103. logger.logInfo("SET TRIGGER CHANNEL DIRECTIONS: ");
  104. PS4000A_DIRECTION directions[1];
  105. directions[0].channel = channels[0];
  106. directions[0].direction = PS4000A_ABOVE;
  107. retval2 = ps4000aSetTriggerChannelDirections(handle, directions, 1);
  108. logger.log_output(retval2);
  109. logger.logInfo("SET TRIGGER CHANNEL PROPERTIES: ");
  110. PS4000A_TRIGGER_CHANNEL_PROPERTIES channel_properties[1];
  111. channel_properties[0].channel = channels[0];
  112. channel_properties[0].thresholdUpper = 16381;
  113. channel_properties[0].thresholdUpperHysteresis = 4095;
  114. channel_properties[0].thresholdMode = PS4000A_LEVEL;
  115. int16_t auxOutputEnable = 0;
  116. int32_t autoTriggerMilliseconds{10000};
  117. retval2 = ps4000aSetTriggerChannelProperties(handle, channel_properties, 1, auxOutputEnable,
  118. autoTriggerMilliseconds);
  119. logger.log_output(retval2);
  120. logger.logInfo("SET TRIGER DELAY: ");
  121. uint32_t delay{0};
  122. retval2 = ps4000aSetTriggerDelay(handle, delay);
  123. logger.log_output(retval2);
  124. */
  125. // Simple trigger
  126. newlogger << "Set simple trigger" << endl;
  127. int16_t enable_trigger{1};
  128. PS4000A_CHANNEL trig_channel{PS4000A_CHANNEL_A};
  129. //int16_t trig_threshhold {16381};
  130. int16_t trig_threshhold {8381};
  131. PS4000A_THRESHOLD_DIRECTION th_direction;
  132. if(flag_below)
  133. {
  134. th_direction = PS4000A_BELOW;
  135. }
  136. else
  137. {
  138. th_direction = PS4000A_ABOVE;
  139. }
  140. uint32_t trig_delay {10};
  141. int16_t trig_autoTrigger_ms {10000};
  142. auto retval_simple_trig = ps4000aSetSimpleTrigger
  143. (
  144. handle,
  145. enable_trigger,
  146. trig_channel,
  147. trig_threshhold,
  148. th_direction,
  149. trig_delay,
  150. trig_autoTrigger_ms
  151. );
  152. newlogger << "Simple trigger returned" << endl;
  153. newlogger << retval_simple_trig << endl;
  154. std::vector<int16_t *> vec_buffer(NUMBER_OF_CHANNELS, nullptr);
  155. int32_t bufferLth{POINTS_VALUE};
  156. int16_t triggerEnabled{0};
  157. int16_t PulseWithQualiferEnabled{0};
  158. newlogger << ps4000aIsTriggerOrPulseWidthQualifierEnabled(handle, &triggerEnabled, &PulseWithQualiferEnabled) << endl;
  159. newlogger << "prepare_for_hackrf: open semaphore" << endl;
  160. HANDLE sem = semaphore_open();
  161. if(sem == NULL)
  162. {
  163. retval2 = ps4000aStop(handle);
  164. newlogger << retval2 << endl;
  165. ps4000aCloseUnit(handle);
  166. newlogger << "PICO CLOSED" << endl;
  167. free_buffers(vec_buffer);
  168. return -1;
  169. }
  170. for (size_t i = 0; i < times.size(); i++)
  171. {
  172. newlogger << "GET TIMEBASE" << endl;
  173. uint32_t timebase = timebase_choice(SAMPLE_FREQUENCY);
  174. newlogger << "Timebase = " << timebase << endl;
  175. retval2 = ps4000aGetTimebase(handle, timebase, points_vec[i], &timeIntervalNanoseconds,
  176. &maxSamples, segmentIndex);
  177. newlogger << retval2 << endl;
  178. newlogger << "RUN BLOCK" << endl;
  179. int32_t noOfPreTriggerSamples{0};
  180. int32_t noOfPostTriggerSamples{POINTS_VALUE};
  181. retval2 = ps4000aRunBlock(handle, noOfPreTriggerSamples, noOfPostTriggerSamples, timebase,
  182. nullptr, segmentIndex, nullptr, nullptr);
  183. newlogger << retval2 << endl;
  184. int16_t ready{0};
  185. newlogger << "ready_to_read: release semaphore" << endl;
  186. if(semaphore_release(sem) == 0)
  187. {
  188. retval2 = ps4000aStop(handle);
  189. newlogger << retval2 << endl;
  190. ps4000aCloseUnit(handle);
  191. newlogger << "PICO CLOSED" << endl;
  192. free_buffers(vec_buffer);
  193. return -1;
  194. }
  195. while (ready == 0)
  196. {
  197. retval2 = ps4000aIsReady(handle, &ready);
  198. if(flag_debug)
  199. {
  200. newlogger << retval2 << endl;
  201. }
  202. }
  203. newlogger << "SET DATA BUFFER: " << endl;
  204. for (int32_t i = 0; i < NUMBER_OF_CHANNELS; i++)
  205. {
  206. vec_buffer[i] = new int16_t[POINTS_VALUE];
  207. }
  208. PS4000A_RATIO_MODE mode{PS4000A_RATIO_MODE_NONE};
  209. for (int32_t i = 0; i < NUMBER_OF_CHANNELS; i++)
  210. {
  211. retval2 = ps4000aSetDataBuffer(handle, channels[i], vec_buffer[i], bufferLth,
  212. segmentIndex, mode);
  213. }
  214. newlogger << retval2 << endl;
  215. newlogger << "GET VALUES" << endl;
  216. uint32_t startIndex{0};
  217. uint32_t noOfSamples{static_cast<uint32_t>(POINTS_VALUE)};
  218. uint32_t downSampleRatio{1};
  219. PS4000A_RATIO_MODE downSampleRatioMode{PS4000A_RATIO_MODE_NONE};
  220. int16_t overflow{0};
  221. retval2 = ps4000aGetValues(handle, startIndex, &noOfSamples, downSampleRatio,
  222. downSampleRatioMode, segmentIndex, &overflow);
  223. newlogger << retval2 << endl;
  224. writing_data_fixed_name(vec_buffer, bufferLth, NUMBER_OF_CHANNELS);
  225. if (times[times.size() - 1]) break;
  226. std::this_thread::sleep_for(
  227. std::chrono::duration<int64_t, std::milli>(times[i + 1] - times[i]));
  228. }
  229. newlogger << "PICO STOPED" << endl;
  230. retval2 = ps4000aStop(handle);
  231. newlogger << retval2 << endl;
  232. ps4000aCloseUnit(handle);
  233. newlogger << "PICO CLOSED" << endl;
  234. free_buffers(vec_buffer);
  235. return 0;
  236. }