events.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #define _IPECAMERA_IMAGE_C
  2. #define _BSD_SOURCE
  3. #define _GNU_SOURCE
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <sys/time.h>
  9. #include <pthread.h>
  10. #include <assert.h>
  11. #include <ufodecode.h>
  12. #include "../tools.h"
  13. #include "../error.h"
  14. #include "pcilib.h"
  15. #include "public.h"
  16. #include "private.h"
  17. #include "events.h"
  18. int ipecamera_stream(pcilib_context_t *vctx, pcilib_event_callback_t callback, void *user) {
  19. int err = 0;
  20. int do_stop = 0;
  21. ipecamera_event_info_t info;
  22. ipecamera_t *ctx = (ipecamera_t*)vctx;
  23. if (!ctx) {
  24. pcilib_error("IPECamera imaging is not initialized");
  25. return PCILIB_ERROR_NOTINITIALIZED;
  26. }
  27. ctx->streaming = 1;
  28. ctx->run_streamer = 1;
  29. if (!ctx->started) {
  30. err = ipecamera_start(vctx, PCILIB_EVENTS_ALL, PCILIB_EVENT_FLAGS_DEFAULT);
  31. if (err) {
  32. ctx->streaming = 0;
  33. return err;
  34. }
  35. do_stop = 1;
  36. }
  37. // This loop iterates while the generation
  38. while ((ctx->run_streamer)||(ctx->reported_id != ctx->event_id)) {
  39. while (ctx->reported_id != ctx->event_id) {
  40. if ((ctx->event_id - ctx->reported_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) ctx->reported_id = ctx->event_id - (ctx->buffer_size - 1) - IPECAMERA_RESERVE_BUFFERS;
  41. else ++ctx->reported_id;
  42. memcpy(&info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
  43. if ((ctx->event_id - ctx->reported_id) < ctx->buffer_size) {
  44. err = callback(ctx->reported_id, (pcilib_event_info_t*)&info, user);
  45. if (err <= 0) {
  46. if (err < 0) err = -err;
  47. break;
  48. }
  49. }
  50. }
  51. usleep(IPECAMERA_NOFRAME_SLEEP);
  52. }
  53. ctx->streaming = 0;
  54. if (do_stop) {
  55. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  56. }
  57. return err;
  58. }
  59. int ipecamera_next_event(pcilib_context_t *vctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info) {
  60. struct timeval tv;
  61. ipecamera_t *ctx = (ipecamera_t*)vctx;
  62. if (!ctx) {
  63. pcilib_error("IPECamera imaging is not initialized");
  64. return PCILIB_ERROR_NOTINITIALIZED;
  65. }
  66. if (!ctx->started) {
  67. pcilib_error("IPECamera is not in grabbing mode");
  68. return PCILIB_ERROR_INVALID_REQUEST;
  69. }
  70. if (ctx->reported_id == ctx->event_id) {
  71. if (timeout) {
  72. pcilib_calc_deadline(&tv, timeout);
  73. while ((pcilib_calc_time_to_deadline(&tv) > 0)&&(ctx->reported_id == ctx->event_id))
  74. usleep(IPECAMERA_NOFRAME_SLEEP);
  75. }
  76. if (ctx->reported_id == ctx->event_id) return PCILIB_ERROR_TIMEOUT;
  77. }
  78. retry:
  79. if ((ctx->event_id - ctx->reported_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) ctx->reported_id = ctx->event_id - (ctx->buffer_size - 1) - IPECAMERA_RESERVE_BUFFERS;
  80. else ++ctx->reported_id;
  81. if (evid) *evid = ctx->reported_id;
  82. if (info) {
  83. if (info_size >= sizeof(ipecamera_event_info_t))
  84. memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
  85. else if (info_size >= sizeof(pcilib_event_info_t))
  86. memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(pcilib_event_info_t));
  87. else
  88. return PCILIB_ERROR_INVALID_ARGUMENT;
  89. }
  90. if ((ctx->event_id - ctx->reported_id) >= ctx->buffer_size) goto retry;
  91. return 0;
  92. }