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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
pub mod trigger;

/**
 * Type representing decimation used at acquiring signal.
 */
pub use crate::rp::rp_acq_decimation_t as Decimation;

/**
 * Type representing acquire signal sampling rate.
 */
pub use crate::rp::rp_acq_sampling_rate_t as SamplingRate;

pub enum Gain {
    LV,
    HV,
}

impl std::convert::Into<super::pin::State> for Gain {
    fn into(self) -> super::pin::State {
        match self {
            Gain::LV => super::pin::State::RP_LOW,
            Gain::HV => super::pin::State::RP_HIGH,
        }
    }
}

impl std::convert::From<super::pin::State> for Gain {
    fn from(state: super::pin::State) -> Self {
        match state {
            super::pin::State::RP_LOW => Gain::LV,
            super::pin::State::RP_HIGH => Gain::HV,
        }
    }
}

impl std::convert::From<String> for Gain {
    fn from(direction: String) -> Self {
        match direction.as_str() {
            "LV" => Gain::LV,
            "HV" => Gain::HV,
            _ => unimplemented!(),
        }
    }
}

impl std::convert::Into<String> for Gain {
    fn into(self) -> String {
        match self {
            Gain::LV => "LV",
            Gain::HV => "HV",
        }.to_owned()
    }
}

/**
 * Enables continous acquirement even after trigger has happened.
 */
pub fn set_arm_keep(enable: bool) -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqSetArmKeep(enable)
    )
}

/**
 * Gets status of continous acquirement even after trigger has happened.
 */
pub fn arm_keep() -> crate::Result<bool>
{
    let mut state = false;

    match handle_unsafe!(
        crate::rp::rp_AcqGetArmKeep(&mut state)
    ) {
        Ok(_) => Ok(state),
        Err(err) => Err(err),
    }
}

/**
 * Indicates whether the ADC buffer was full of data. The length of the buffer is determined by the
 * delay. By default, the delay is half the buffer.
 */
pub fn buffer_fill_state() -> crate::Result<bool>
{
    let mut state = false;

    match handle_unsafe!(
        crate::rp::rp_AcqGetBufferFillState(&mut state)
    ) {
        Ok(_) => Ok(state),
        Err(err) => Err(err),
    }
}

/**
 * Sets the decimation used at acquiring signal.
 *
 * There is only a set of pre-defined decimation
 *
 * values which can be specified. See the `Decimation` enum values.
 */
pub fn set_decimation(decimat: Decimation) -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqSetDecimation(decimat)
    )
}

/**
 * Gets the decimation used at acquiring signal.
 *
 * There is only a set of pre-defined decimation values which can be specified.
 * See the `Decimation` enum values.
 */
pub fn decimation() -> crate::Result<Decimation>
{
    let mut decimat = Decimation::RP_DEC_1;

    match handle_unsafe!(
        crate::rp::rp_AcqGetDecimation(&mut decimat)
    ) {
        Ok(_) => Ok(decimat),
        Err(err) => Err(err),
    }
}

/**
 * Sets the decimation used at acquiring signal.
 */
pub fn acq_set_decimation_factor(decimation: Decimation) -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqSetDecimationFactor(decimation.into())
    )
}

/**
 * Gets the decimation factor used at acquiring signal in a numerical form.
 *
 * Although this method returns an integer value representing the current factor
 * of the decimation, there is only a set of pre-defined decimation factor
 * values which can be returned. See the `Decimation` enum values.
 */
pub fn decimation_factor() -> crate::Result<u32>
{
    let mut decimation = 0;

    match handle_unsafe!(
        crate::rp::rp_AcqGetDecimationFactor(&mut decimation)
    ) {
        Ok(_) => Ok(decimation),
        Err(err) => Err(err),
    }
}

/**
 * Sets the sampling rate for acquiring signal.
 *
 * There is only a set of pre-defined sampling rate values which can be
 * specified. See the `SamplingRate` enum values.
 */
pub fn set_sampling_rate(sampling_rate: SamplingRate ) -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqSetSamplingRate(sampling_rate)
    )
}

/**
 * Gets the sampling rate for acquiring signal.
 *
 * There is only a set of pre-defined sampling rate values which can be
 * returned. See the `SamplingRate` enum values.
 */
pub fn sampling_rate() -> crate::Result<SamplingRate>
{
    let mut sampling_rate = SamplingRate::RP_SMP_125M;

    match handle_unsafe!(
        crate::rp::rp_AcqGetSamplingRate(&mut sampling_rate)
    ) {
        Ok(_) => Ok(sampling_rate),
        Err(err) => Err(err),
    }
}

/**
 * Gets the sampling rate for acquiring signal in a numerical form in Hz.
 *
 * Although this method returns a float value representing the current value of
 * the sampling rate, there is only a set of pre-defined sampling rate values
 * which can be returned. See the `SamplingRate` enum values.
 */
pub fn sampling_rate_hz() -> crate::Result<f32>
{
    let mut sampling_rate = 0.0;

    match handle_unsafe!(
        crate::rp::rp_AcqGetSamplingRateHz(&mut sampling_rate)
    ) {
        Ok(_) => Ok(sampling_rate),
        Err(err) => Err(err),
    }
}

/**
 * Enables or disables averaging of data between samples.
 *
 * Data between samples can be averaged by setting the averaging flag in the
 * Data decimation register.
 */
pub fn set_averaging(enabled: bool) -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqSetAveraging(enabled)
    )
}

/**
 * Returns information if averaging of data between samples is enabled or
 * disabled.
 *
 * Data between samples can be averaged by setting the averaging flag in the
 * Data decimation register.
 */
pub fn averaging() -> crate::Result<bool>
{
    let mut enabled = false;

    match handle_unsafe!(
        crate::rp::rp_AcqGetAveraging(&mut enabled)
    ) {
        Ok(_) => Ok(enabled),
        Err(err) => Err(err),
    }
}

/**
 * Sets the acquire gain state.
 *
 * The gain should be set to the same value as it is set on the Red Pitaya
 * hardware by the LV/HV gain jumpers. LV = 1V; HV = 20V.
 */
pub fn set_gain(channel: super::Channel, gain: Gain) -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqSetGain(channel, gain.into())
    )
}

/**
 * Returns the currently set acquire gain state in the library.
 *
 * It may not be set to the same value as it is set on the Red Pitaya hardware
 * by the LV/HV gain jumpers. LV = 1V; HV = 20V.
 */
pub fn gain(channel: super::Channel) -> crate::Result<Gain>
{
    let mut state = super::pin::State::RP_LOW;

    match handle_unsafe!(
        crate::rp::rp_AcqGetGain(channel, &mut state)
    ) {
        Ok(_) => Ok(state.into()),
        Err(err) => Err(err),
    }
}

/**
 * Returns the currently set acquire gain in the library.
 *
 * It may not be set to the same value as it is set on the Red Pitaya hardware
 * by the LV/HV gain jumpers. Returns value in Volts.
 */
pub fn gain_v(channel: super::Channel) -> crate::Result<f32>
{
    let mut voltage = 0.0;

    match handle_unsafe!(
        crate::rp::rp_AcqGetGainV(channel, &mut voltage)
    ) {
        Ok(_) => Ok(voltage),
        Err(err) => Err(err),
    }
}

/**
 * Returns current position of ADC write pointer.
 */
pub fn write_pointer() -> crate::Result<u32>
{
    let mut pos = 0;

    match handle_unsafe!(
        crate::rp::rp_AcqGetWritePointer(&mut pos)
    ) {
        Ok(_) => Ok(pos),
        Err(err) => Err(err),
    }
}

/**
 * Returns position of ADC write pointer at time when trigger arrived.
 */
pub fn write_pointer_at_trig() -> crate::Result<u32>
{
    let mut pos = 0;

    match handle_unsafe!(
        crate::rp::rp_AcqGetWritePointerAtTrig(&mut pos)
    ) {
        Ok(_) => Ok(pos),
        Err(err) => Err(err),
    }
}

/**
 * Starts the acquire.
 *
 * Signals coming from the input channels are acquired and written into memory.
 */
pub fn start() -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqStart()
    )
}

/**
 * Stops the acquire.
 */
pub fn stop() -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqStop()
    )
}

/**
 * Resets the acquire writing state machine.
 */
pub fn reset() -> crate::Result<()>
{
    handle_unsafe!(
        crate::rp::rp_AcqReset()
    )
}

/**
 * Normalizes the ADC buffer position.
 *
 * Returns the modulo operation of ADC buffer size...
 */
pub fn normalized_data_pos(pos: u32) -> u32
{
    unsafe {
        crate::rp::rp_AcqGetNormalizedDataPos(pos)
    }
}

/**
 * Returns the ADC buffer in raw units from start to end position.
 */
pub fn data_pos_raw(channel: super::Channel, start_pos: u32, end_pos: u32) -> crate::Result<Vec<i16>>
{
    let mut slice = [0; 16_384];
    let mut length = slice.len() as u32 + 1;

    match handle_unsafe!(
        crate::rp::rp_AcqGetDataPosRaw(channel, start_pos, end_pos, slice.as_mut_ptr(), &mut length)
    ) {
        Ok(_) => {
            let mut vec = slice.to_vec();
            vec.truncate(length as usize);

            Ok(vec)
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the ADC buffer in Volt units from start to end position.
 */
pub fn data_pos_v(channel: super::Channel, start_pos: u32, end_pos: u32) -> crate::Result<Vec<f32>>
{
    let mut slice = [0.0; 16_384];
    let mut length = slice.len() as u32 + 1;

    match handle_unsafe!(
        crate::rp::rp_AcqGetDataPosV(channel, start_pos, end_pos, slice.as_mut_ptr(), &mut length)
    ) {
        Ok(_) => {
            let mut vec = slice.to_vec();
            vec.truncate(length as usize);

            Ok(vec)
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the ADC buffer in raw units from specified position.
 */
pub fn data_raw(channel: super::Channel, pos: u32, size: u32) -> crate::Result<Vec<i16>>
{
    let mut length = size;
    let mut slice = [0; 16_384];

    match handle_unsafe!(
        crate::rp::rp_AcqGetDataRaw(channel, pos, &mut length, slice.as_mut_ptr())
    ) {
        Ok(_) => {
            let mut vec = slice.to_vec();
            vec.truncate(length as usize);

            Ok(vec)
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the ADC buffer in raw units from specified position.
 */
pub fn data_raw_v2(pos: u32, size: u32) -> crate::Result<[Vec<u16>;2]>
{
    let mut length = size;
    let mut slice1 = [0; 16_384];
    let mut slice2 = [0; 16_384];

    match handle_unsafe!(
        crate::rp::rp_AcqGetDataRawV2(pos, &mut length, slice1.as_mut_ptr(), slice2.as_mut_ptr())
    ) {
        Ok(_) => {
            let mut vec1 = slice1.to_vec();
            vec1.truncate(length as usize);

            let mut vec2 = slice2.to_vec();
            vec2.truncate(length as usize);

            Ok([vec1, vec2])
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the ADC buffer in raw units from the oldest sample to the newest one.
 *
 * CAUTION: Use this method only when write pointer has stopped (Trigger
 * happened and writing stopped).
 */
pub fn oldest_data_raw(channel: super::Channel, size: u32) -> crate::Result<Vec<i16>>
{
    let mut length = size;
    let mut slice = [0; 16_384];

    match handle_unsafe!(
        crate::rp::rp_AcqGetOldestDataRaw(channel, &mut length, slice.as_mut_ptr())
    ) {
        Ok(_) => {
            let mut vec = slice.to_vec();
            vec.truncate(length as usize);

            Ok(vec)
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the latest ADC buffer samples in raw units.
 */
pub fn latest_data_raw(channel: super::Channel, size: u32) -> crate::Result<Vec<i16>>
{
    let mut length = size;
    let mut slice = [0; 16_384];

    match handle_unsafe!(
        crate::rp::rp_AcqGetLatestDataRaw(channel, &mut length, slice.as_mut_ptr())
    ) {
        Ok(_) => {
            let mut vec = slice.to_vec();
            vec.truncate(length as usize);

            Ok(vec)
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the ADC buffer in Volt units from specified position.
 */
pub fn data_v(channel: super::Channel, pos: u32, size: u32) -> crate::Result<Vec<f32>>
{
    let mut length = size;
    let mut slice = [0.0; 16_384];

    match handle_unsafe!(
        crate::rp::rp_AcqGetDataV(channel, pos, &mut length, slice.as_mut_ptr())
    ) {
        Ok(_) => {
            let mut vec = slice.to_vec();
            vec.truncate(length as usize);

            Ok(vec)
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the ADC buffer in Volt units from specified position.
 */
pub fn data_v2(pos: u32, size: u32) -> crate::Result<[Vec<f32>;2]>
{
    let mut length = size;
    let mut slice1 = [0.0; 16_384];
    let mut slice2 = [0.0; 16_384];

    match handle_unsafe!(
        crate::rp::rp_AcqGetDataV2(pos, &mut length, slice1.as_mut_ptr(), slice2.as_mut_ptr())
    ) {
        Ok(_) => {
            let mut vec1 = slice1.to_vec();
            vec1.truncate(length as usize);

            let mut vec2 = slice2.to_vec();
            vec2.truncate(length as usize);

            Ok([vec1, vec2])
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the ADC buffer in Volt units from the oldest sample to the newest one.
 *
 * CAUTION: Use this method only when write pointer has stopped (Trigger happened and writing stopped).
 */
pub fn oldest_data_v(channel: super::Channel, size: u32) -> crate::Result<Vec<f32>>
{
    let mut length = size;
    let mut slice = [0.0; 16_384];

    match handle_unsafe!(
        crate::rp::rp_AcqGetOldestDataV(channel, &mut length, slice.as_mut_ptr())
    ) {
        Ok(_) => {
            let mut vec = slice.to_vec();
            vec.truncate(length as usize);

            Ok(vec)
        },
        Err(err) => Err(err),
    }
}

/**
 * Returns the latest ADC buffer samples in Volt units.
 */
pub fn latest_data_v(channel: super::Channel, size: u32) -> crate::Result<Vec<f32>>
{
    let mut length = size;
    let mut slice = [0.0; 16_384];

    match handle_unsafe!(
        crate::rp::rp_AcqGetLatestDataV(channel, &mut length, slice.as_mut_ptr())
    ) {
        Ok(_) => {
            let mut vec = slice.to_vec();
            vec.truncate(length as usize);

            Ok(vec)
        },
        Err(err) => Err(err),
    }
}

pub fn buffer_size() -> crate::Result<u32>
{
    let mut size = 0;

    match handle_unsafe!(
        crate::rp::rp_AcqGetBufSize(&mut size)
    ) {
        Ok(_) => Ok(size),
        Err(err) => Err(err),
    }
}