Skip to content

Commit

Permalink
add test, must not lock during dcamwait_start (#9)
Browse files Browse the repository at this point in the history
closes #8
  • Loading branch information
nclack authored May 12, 2023
1 parent 5297f0a commit 4e14b55
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/dcam.camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ select_trigger(struct CameraProperties* settings)
return 0;
}

static int
disable_external_triggering(HDCAM h)
{
DCAM(dcamprop_setvalue(
h, DCAM_IDPROP_TRIGGER_MODE, DCAMPROP_TRIGGER_MODE__NORMAL));
DCAM(dcamprop_setvalue(
h, DCAM_IDPROP_TRIGGERSOURCE, DCAMPROP_TRIGGERSOURCE__INTERNAL));
return 1;
Error:
return 0;
}

static int
set_input_triggering(HDCAM h, struct CameraProperties* settings)
{
Expand All @@ -182,10 +194,7 @@ set_input_triggering(HDCAM h, struct CameraProperties* settings)
struct Trigger* event = select_trigger(settings);
if (!event) {
// None are enabled
DCAM(dcamprop_setvalue(
h, DCAM_IDPROP_TRIGGER_MODE, DCAMPROP_TRIGGER_MODE__NORMAL));
DCAM(dcamprop_setvalue(
h, DCAM_IDPROP_TRIGGERSOURCE, DCAMPROP_TRIGGERSOURCE__INTERNAL));
CHECK(disable_external_triggering(h));
} else {
use_software_trigger = (event->line == LINE_SOFTWARE);
CHECK(event->kind == Signal_Input);
Expand Down Expand Up @@ -878,22 +887,15 @@ aq_dcam_get_frame(struct Camera* self_,
.eventmask = (int32)DCAMWAIT_CAPEVENT_FRAMEREADY,
.timeout = (int32)DCAMWAIT_TIMEOUT_INFINITE,
};
lock_acquire(&self->lock);
DCAMERR dcamwait_start_result = dcamwait_start(self->wait, &p);

CHECK(self_->state == DeviceState_Running);
TRACE("DCAM device id: %d\tdcam: %p\thwait: %p",
(int)self->camera.device.identifier.device_id,
self->hdcam,
self->wait);
{
DCAMERR dcamwait_start_result = dcamwait_start(self->wait, &p);
if (dcamwait_start_result == DCAMERR_ABORT) {
*nbytes = 0;
LOG("CAMERA ABORT");
return Device_Ok;
}
DCAM(dcamwait_start_result);
lock_acquire(&self->lock);
if (dcamwait_start_result == DCAMERR_ABORT) {
*nbytes = 0;
LOG("CAMERA ABORT");
goto Error;
}
DCAM(dcamwait_start_result);

{
struct image_descriptor d;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ else()
#
set(tests
list-devices
abort-finite-acquisition
dcam-check
dcam-list-triggers
dcam-reset-on-fail
Expand Down
117 changes: 117 additions & 0 deletions tests/abort-finite-acquisition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include "acquire.h"
#include "device/hal/device.manager.h"
#include "device/props/device.h"
#include "platform.h"
#include "logger.h"

#include <cstdio>
#include <exception>
#include <stdexcept>

void
reporter(int is_error,
const char* file,
int line,
const char* function,
const char* msg)
{
fprintf(is_error ? stderr : stdout,
"%s%s(%d) - %s: %s\n",
is_error ? "ERROR " : "",
file,
line,
function,
msg);
}

/// Helper for passing size static strings as function args.
/// For a function: `f(char*,size_t)` use `f(SIZED("hello"))`.
/// Expands to `f("hello",5)`.
#define SIZED(str) str, sizeof(str)

#define L (aq_logger)
#define LOG(...) L(0, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define ERR(...) L(1, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define EXPECT(e, ...) \
do { \
if (!(e)) { \
char buf[1 << 8] = { 0 }; \
ERR(__VA_ARGS__); \
snprintf(buf, sizeof(buf) - 1, __VA_ARGS__); \
throw std::runtime_error(buf); \
} \
} while (0)
#define CHECK(e) EXPECT(e, "Expression evaluated as false: %s", #e)
#define DEVOK(e) CHECK(Device_Ok == (e))
#define OK(e) CHECK(AcquireStatus_Ok == (e))

void
monitor_proc(void*)
{
struct clock clock;
clock_init(&clock);
clock_sleep_ms(&clock, 20000);
ERR("ABORT - TIMEOUT");
abort();
}

int
main()
{

auto runtime = acquire_init(reporter);
try {
auto dm = acquire_device_manager(runtime);
CHECK(runtime);
CHECK(dm);

AcquireProperties props = {};
OK(acquire_get_configuration(runtime, &props));

DEVOK(device_manager_select(dm,
DeviceKind_Camera,
SIZED("hamamatsu.*") - 1,
&props.video[0].camera.identifier));
DEVOK(device_manager_select(dm,
DeviceKind_Storage,
SIZED("trash") - 1,
&props.video[0].storage.identifier));
OK(acquire_configure(runtime, &props));

AcquirePropertyMetadata metadata = { 0 };
OK(acquire_get_configuration_metadata(runtime, &metadata));

props.video[0].camera.settings.binning = 1;
props.video[0].camera.settings.pixel_type = SampleType_u16;
props.video[0].camera.settings.shape = {
.x = (uint32_t)metadata.video[0].camera.shape.x.high,
.y = (uint32_t)metadata.video[0].camera.shape.y.high,
};
props.video[0].camera.settings.exposure_time_us = 1e4;
props.video[0].max_frame_count = 10;
props.video[0].camera.settings.input_triggers.frame_start.line = 0;
props.video[0].camera.settings.input_triggers.frame_start.enable = 1;

OK(acquire_configure(runtime, &props));
CHECK(
props.video[0].camera.settings.input_triggers.frame_start.enable ==
1);
OK(acquire_start(runtime));

struct thread monitor;
thread_init(&monitor);
thread_create(&monitor, monitor_proc, 0);
clock_sleep_ms(0, 1000);
OK(acquire_abort(runtime));
OK(acquire_shutdown(runtime));
LOG("OK. Done.");
return 0;
} catch (const std::exception& e) {
ERR("Exception: %s", e.what());
} catch (...) {
ERR("Exception: (unknown)");
}
if (runtime)
acquire_shutdown(runtime);
return 1;
}

0 comments on commit 4e14b55

Please sign in to comment.