micromanager 0.2.0

Rust port of the MicroManager microscopy control framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/// C wrapper around the Pleora eBUS SDK C++ API.
///
/// Exposes a plain `extern "C"` API so that Rust can call into the C++ SDK
/// without needing a C++ FFI bridge on the Rust side.  All eBUS exceptions
/// are caught here; functions return -1 / nullptr on failure.

#include <PvSystem.h>
#include <PvDevice.h>
#include <PvDeviceGEV.h>
#include <PvDeviceU3V.h>
#include <PvStream.h>
#include <PvStreamGEV.h>
#include <PvStreamU3V.h>
#include <PvBuffer.h>
#include <PvImage.h>
#include <PvGenParameterArray.h>
#include <PvGenEnum.h>
#include <PvGenInteger.h>
#include <PvGenFloat.h>
#include <PvGenString.h>
#include <PvGenCommand.h>

#include <vector>
#include <string>
#include <cstring>
#include <cstdint>
#include <stdexcept>

// ── Internal wrappers ─────────────────────────────────────────────────────────

/// Wraps PvSystem and caches the flattened device list after Find().
struct JaiSystem {
    PvSystem pvSystem;
    // Parallel arrays: connectionId and serialNumber for each found device.
    std::vector<std::string> connectionIds;
    std::vector<std::string> serialNumbers;
};

/// Wraps a connected PvDevice.
struct JaiDevice {
    PvDevice*        pvDevice  = nullptr;
    std::string      connectionId;
};

/// Wraps an open PvStream.
struct JaiStream {
    PvStream* pvStream = nullptr;
};

/// Wraps a PvBuffer (either allocated internally or retrieved from stream).
struct JaiBuffer {
    PvBuffer* pvBuffer = nullptr;
    bool      owned    = true;   // false when retrieved from a stream queue
};

// ── Helpers ───────────────────────────────────────────────────────────────────

static void safe_copy(const char* src, char* dst, int len) {
    if (!src || len <= 0) { if (dst && len > 0) dst[0] = '\0'; return; }
    std::strncpy(dst, src, (size_t)(len - 1));
    dst[len - 1] = '\0';
}

// ── C API ─────────────────────────────────────────────────────────────────────

extern "C" {

// ── System ───────────────────────────────────────────────────────────────────

JaiSystem* jai_system_new() {
    try { return new JaiSystem(); }
    catch (...) { return nullptr; }
}

void jai_system_free(JaiSystem* s) { delete s; }

/// Enumerate all eBUS-visible devices.  Returns the number of devices found,
/// or -1 on error.  Results are cached inside `s` and accessible via
/// jai_system_get_device_id / jai_system_get_device_serial.
int jai_system_find(JaiSystem* s) {
    if (!s) return -1;
    try {
        s->connectionIds.clear();
        s->serialNumbers.clear();

        PvResult r = s->pvSystem.Find();
        if (!r.IsOK()) return -1;

        for (uint32_t i = 0; i < s->pvSystem.GetInterfaceCount(); i++) {
            const PvInterface* iface = s->pvSystem.GetInterface(i);
            if (!iface) continue;
            for (uint32_t j = 0; j < iface->GetDeviceCount(); j++) {
                const PvDeviceInfo* di = iface->GetDeviceInfo(j);
                if (!di) continue;
                s->connectionIds.push_back(di->GetConnectionID().GetAscii());
                s->serialNumbers.push_back(di->GetSerialNumber().GetAscii());
            }
        }
        return (int)s->connectionIds.size();
    } catch (...) { return -1; }
}

int jai_system_get_device_id(JaiSystem* s, int idx, char* buf, int len) {
    if (!s || idx < 0 || (size_t)idx >= s->connectionIds.size()) return -1;
    safe_copy(s->connectionIds[(size_t)idx].c_str(), buf, len);
    return 0;
}

int jai_system_get_device_serial(JaiSystem* s, int idx, char* buf, int len) {
    if (!s || idx < 0 || (size_t)idx >= s->serialNumbers.size()) return -1;
    safe_copy(s->serialNumbers[(size_t)idx].c_str(), buf, len);
    return 0;
}

// ── Device ───────────────────────────────────────────────────────────────────

JaiDevice* jai_device_connect(const char* connection_id) {
    if (!connection_id) return nullptr;
    try {
        PvResult r;
        PvDevice* dev = PvDevice::CreateAndConnect(PvString(connection_id), &r);
        if (!r.IsOK() || !dev) return nullptr;
        JaiDevice* d = new JaiDevice();
        d->pvDevice     = dev;
        d->connectionId = connection_id;
        return d;
    } catch (...) { return nullptr; }
}

void jai_device_free(JaiDevice* d) {
    if (!d) return;
    try {
        if (d->pvDevice) {
            d->pvDevice->Disconnect();
            PvDevice::Free(d->pvDevice);
            d->pvDevice = nullptr;
        }
    } catch (...) {}
    delete d;
}

int jai_device_get_int(JaiDevice* d, const char* name, int64_t* out) {
    if (!d || !d->pvDevice || !name || !out) return -1;
    try {
        PvGenParameterArray* p = d->pvDevice->GetParameters();
        int64_t v = 0;
        PvResult r = p->GetIntegerValue(PvString(name), v);
        if (!r.IsOK()) return -1;
        *out = v;
        return 0;
    } catch (...) { return -1; }
}

int jai_device_set_int(JaiDevice* d, const char* name, int64_t value) {
    if (!d || !d->pvDevice || !name) return -1;
    try {
        PvGenParameterArray* p = d->pvDevice->GetParameters();
        return p->SetIntegerValue(PvString(name), value).IsOK() ? 0 : -1;
    } catch (...) { return -1; }
}

int jai_device_get_float(JaiDevice* d, const char* name, double* out) {
    if (!d || !d->pvDevice || !name || !out) return -1;
    try {
        PvGenParameterArray* p = d->pvDevice->GetParameters();
        double v = 0.0;
        PvResult r = p->GetFloatValue(PvString(name), v);
        if (!r.IsOK()) return -1;
        *out = v;
        return 0;
    } catch (...) { return -1; }
}

int jai_device_set_float(JaiDevice* d, const char* name, double value) {
    if (!d || !d->pvDevice || !name) return -1;
    try {
        PvGenParameterArray* p = d->pvDevice->GetParameters();
        return p->SetFloatValue(PvString(name), value).IsOK() ? 0 : -1;
    } catch (...) { return -1; }
}

int jai_device_get_string(JaiDevice* d, const char* name, char* buf, int len) {
    if (!d || !d->pvDevice || !name || !buf) return -1;
    try {
        PvGenParameterArray* p = d->pvDevice->GetParameters();
        PvString v;
        PvResult r = p->GetStringValue(PvString(name), v);
        if (!r.IsOK()) return -1;
        safe_copy(v.GetAscii(), buf, len);
        return 0;
    } catch (...) { return -1; }
}

int jai_device_get_enum(JaiDevice* d, const char* name, char* buf, int len) {
    if (!d || !d->pvDevice || !name || !buf) return -1;
    try {
        PvGenParameterArray* p = d->pvDevice->GetParameters();
        PvString v;
        PvResult r = p->GetEnumValue(PvString(name), v);
        if (!r.IsOK()) return -1;
        safe_copy(v.GetAscii(), buf, len);
        return 0;
    } catch (...) { return -1; }
}

int jai_device_set_enum(JaiDevice* d, const char* name, const char* value) {
    if (!d || !d->pvDevice || !name || !value) return -1;
    try {
        PvGenParameterArray* p = d->pvDevice->GetParameters();
        return p->SetEnumValue(PvString(name), PvString(value)).IsOK() ? 0 : -1;
    } catch (...) { return -1; }
}

int jai_device_execute(JaiDevice* d, const char* name) {
    if (!d || !d->pvDevice || !name) return -1;
    try {
        PvGenParameterArray* p = d->pvDevice->GetParameters();
        return p->ExecuteCommand(PvString(name)).IsOK() ? 0 : -1;
    } catch (...) { return -1; }
}

uint64_t jai_device_payload_size(JaiDevice* d) {
    if (!d || !d->pvDevice) return 0;
    try { return (uint64_t)d->pvDevice->GetPayloadSize(); }
    catch (...) { return 0; }
}

int jai_device_stream_enable(JaiDevice* d) {
    if (!d || !d->pvDevice) return -1;
    try { return d->pvDevice->StreamEnable().IsOK() ? 0 : -1; }
    catch (...) { return -1; }
}

int jai_device_stream_disable(JaiDevice* d) {
    if (!d || !d->pvDevice) return -1;
    try { return d->pvDevice->StreamDisable().IsOK() ? 0 : -1; }
    catch (...) { return -1; }
}

/// Returns the connection ID string (used to open the matching stream).
int jai_device_get_connection_id(JaiDevice* d, char* buf, int len) {
    if (!d || !buf) return -1;
    safe_copy(d->connectionId.c_str(), buf, len);
    return 0;
}

// ── Stream ───────────────────────────────────────────────────────────────────

JaiStream* jai_stream_open(const char* connection_id) {
    if (!connection_id) return nullptr;
    try {
        PvResult r;
        PvStream* s = PvStream::CreateAndOpen(PvString(connection_id), &r);
        if (!r.IsOK() || !s) return nullptr;
        JaiStream* js = new JaiStream();
        js->pvStream = s;
        return js;
    } catch (...) { return nullptr; }
}

void jai_stream_free(JaiStream* s) {
    if (!s) return;
    try {
        if (s->pvStream) {
            s->pvStream->Close();
            PvStream::Free(s->pvStream);
            s->pvStream = nullptr;
        }
    } catch (...) {}
    delete s;
}

int jai_stream_queue(JaiStream* s, JaiBuffer* buf) {
    if (!s || !s->pvStream || !buf || !buf->pvBuffer) return -1;
    try { return s->pvStream->QueueBuffer(buf->pvBuffer).IsOK() ? 0 : -1; }
    catch (...) { return -1; }
}

/// Retrieves the next completed buffer from the stream.  Returns a JaiBuffer
/// wrapping the PvBuffer (not owned by the caller — must be re-queued or the
/// stream closed before freeing).  Returns nullptr on timeout / error.
JaiBuffer* jai_stream_retrieve(JaiStream* s, uint32_t timeout_ms) {
    if (!s || !s->pvStream) return nullptr;
    try {
        PvBuffer* pvBuf = nullptr;
        PvResult  opResult;
        PvResult  r = s->pvStream->RetrieveBuffer(&pvBuf, &opResult, timeout_ms);
        if (!r.IsOK() || !opResult.IsOK() || !pvBuf) return nullptr;
        // Wrap without ownership; caller must call jai_buffer_requeue or
        // jai_stream_abort before freeing the JaiBuffer wrapper.
        JaiBuffer* jb = new JaiBuffer();
        jb->pvBuffer = pvBuf;
        jb->owned    = false;
        return jb;
    } catch (...) { return nullptr; }
}

/// Re-queues a retrieved buffer back to the stream for reuse.
int jai_stream_requeue(JaiStream* s, JaiBuffer* buf) {
    if (!s || !s->pvStream || !buf || !buf->pvBuffer) return -1;
    try { return s->pvStream->QueueBuffer(buf->pvBuffer).IsOK() ? 0 : -1; }
    catch (...) { return -1; }
}

int jai_stream_abort(JaiStream* s) {
    if (!s || !s->pvStream) return -1;
    try { return s->pvStream->AbortQueuedBuffers().IsOK() ? 0 : -1; }
    catch (...) { return -1; }
}

// ── Buffer ───────────────────────────────────────────────────────────────────

JaiBuffer* jai_buffer_alloc(uint64_t size) {
    try {
        JaiBuffer* jb = new JaiBuffer();
        jb->pvBuffer = new PvBuffer();
        jb->pvBuffer->Alloc((uint32_t)size);
        jb->owned = true;
        return jb;
    } catch (...) { return nullptr; }
}

void jai_buffer_free(JaiBuffer* buf) {
    if (!buf) return;
    if (buf->owned && buf->pvBuffer) {
        delete buf->pvBuffer;
        buf->pvBuffer = nullptr;
    } else {
        // Not owned: only delete the wrapper, not the underlying PvBuffer
        // (which is still in the stream queue or has been aborted).
    }
    delete buf;
}

uint32_t jai_buffer_width(JaiBuffer* buf) {
    if (!buf || !buf->pvBuffer) return 0;
    try {
        PvImage* img = buf->pvBuffer->GetImage();
        return img ? img->GetWidth() : 0;
    } catch (...) { return 0; }
}

uint32_t jai_buffer_height(JaiBuffer* buf) {
    if (!buf || !buf->pvBuffer) return 0;
    try {
        PvImage* img = buf->pvBuffer->GetImage();
        return img ? img->GetHeight() : 0;
    } catch (...) { return 0; }
}

uint32_t jai_buffer_bits_per_pixel(JaiBuffer* buf) {
    if (!buf || !buf->pvBuffer) return 8;
    try {
        PvImage* img = buf->pvBuffer->GetImage();
        return img ? img->GetBitsPerPixel() : 8;
    } catch (...) { return 8; }
}

uint32_t jai_buffer_bits_per_component(JaiBuffer* buf) {
    if (!buf || !buf->pvBuffer) return 8;
    try {
        PvImage* img = buf->pvBuffer->GetImage();
        if (!img) return 8;
        uint32_t bpc = 8;
        PvImage::GetBitsPerComponent(img->GetPixelType(), bpc);
        return bpc;
    } catch (...) { return 8; }
}

int jai_buffer_is_color(JaiBuffer* buf) {
    if (!buf || !buf->pvBuffer) return 0;
    try {
        PvImage* img = buf->pvBuffer->GetImage();
        if (!img) return 0;
        return PvImage::IsPixelColor(img->GetPixelType()) ? 1 : 0;
    } catch (...) { return 0; }
}

/// Pointer to raw pixel data (valid until the buffer is freed or re-queued).
const uint8_t* jai_buffer_data(JaiBuffer* buf) {
    if (!buf || !buf->pvBuffer) return nullptr;
    try {
        PvImage* img = buf->pvBuffer->GetImage();
        if (!img) return nullptr;
        return reinterpret_cast<const uint8_t*>(img->GetDataPointer());
    } catch (...) { return nullptr; }
}

/// Total size of the pixel data in bytes (width * bytes_per_pixel + paddingX) * height.
uint64_t jai_buffer_data_size(JaiBuffer* buf) {
    if (!buf || !buf->pvBuffer) return 0;
    try {
        PvImage* img = buf->pvBuffer->GetImage();
        if (!img) return 0;
        uint32_t w       = img->GetWidth();
        uint32_t h       = img->GetHeight();
        uint32_t bpp     = img->GetBitsPerPixel();
        uint32_t padding = img->GetPaddingX();
        uint32_t stride  = (w * bpp + 7) / 8 + padding;
        return (uint64_t)stride * h;
    } catch (...) { return 0; }
}

} // extern "C"