|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Reactive.Subjects; |
| 4 | +using Bonsai; |
| 5 | + |
| 6 | +namespace OpenEphys.Onix1 |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Configures a persistent heartbeat device whose data stream cannot be disabled. |
| 10 | + /// </summary> |
| 11 | + /// <remarks> |
| 12 | + /// This configuration operator can be linked to a data IO operator, such as <see cref="HeartbeatData"/>, |
| 13 | + /// using a shared <c>DeviceName</c>. |
| 14 | + /// </remarks> |
| 15 | + [Description("Configures a heartbeat device.")] |
| 16 | + public class ConfigurePersistentHeartbeat : SingleDeviceFactory |
| 17 | + { |
| 18 | + readonly BehaviorSubject<uint> beatsPerSecond = new(100); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Initializes a new instance of the <see cref="ConfigurePersistentHeartbeat"/> class. |
| 22 | + /// </summary> |
| 23 | + public ConfigurePersistentHeartbeat() |
| 24 | + : base(typeof(PersistentHeartbeat)) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Gets or sets the rate at which beats are produced in Hz. |
| 30 | + /// </summary> |
| 31 | + [Range(100, 10e6)] |
| 32 | + [Category(AcquisitionCategory)] |
| 33 | + [Description("Rate at which beats are produced (Hz).")] |
| 34 | + public uint BeatsPerSecond |
| 35 | + { |
| 36 | + get => beatsPerSecond.Value; |
| 37 | + set => beatsPerSecond.OnNext(value); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Configures a persistent heartbeat device. |
| 42 | + /// </summary> |
| 43 | + /// <remarks> |
| 44 | + /// This will schedule configuration actions to be applied by a <see cref="StartAcquisition"/> |
| 45 | + /// instance prior to data acquisition. |
| 46 | + /// </remarks> |
| 47 | + /// <param name="source">A sequence of <see cref="ContextTask"/> instances that holds configuration |
| 48 | + /// actions.</param> |
| 49 | + /// <returns>The original sequence modified by adding additional configuration actions required to |
| 50 | + /// configure a persistent heartbeat device./></returns> |
| 51 | + public override IObservable<ContextTask> Process(IObservable<ContextTask> source) |
| 52 | + { |
| 53 | + //var enable = Enable; |
| 54 | + var deviceName = DeviceName; |
| 55 | + var deviceAddress = DeviceAddress; |
| 56 | + return source.ConfigureDevice((context, observer) => |
| 57 | + { |
| 58 | + var device = context.GetDeviceContext(deviceAddress, DeviceType); |
| 59 | + var subscription = beatsPerSecond.SubscribeSafe(observer, newValue => |
| 60 | + { |
| 61 | + var clkHz = device.ReadRegister(PersistentHeartbeat.CLK_HZ); |
| 62 | + var minHeartbeatHz = device.ReadRegister(PersistentHeartbeat.MIN_HB_HZ); |
| 63 | + |
| 64 | + if (newValue < minHeartbeatHz || newValue > 10e6) |
| 65 | + { |
| 66 | + throw new InvalidOperationException($"Value must be between {minHeartbeatHz} Hz and 10 MHz."); |
| 67 | + } |
| 68 | + |
| 69 | + device.WriteRegister(PersistentHeartbeat.CLK_DIV, clkHz / newValue); |
| 70 | + }); |
| 71 | + |
| 72 | + return DeviceManager.RegisterDevice(deviceName, device, DeviceType); |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + [EquivalentDataSource(typeof(Heartbeat))] |
| 78 | + static class PersistentHeartbeat |
| 79 | + { |
| 80 | + public const int ID = 35; |
| 81 | + |
| 82 | + public const uint ENABLE = 0; // Heartbeat enable state (read only; always enabled for this device). |
| 83 | + public const uint CLK_DIV = 1; // Heartbeat clock divider ratio. Minimum value is CLK_HZ / 10e6. |
| 84 | + // Maximum value is CLK_HZ / MIN_HB_HZ.Attempting to set to a value outside |
| 85 | + // this range will result in error. |
| 86 | + public const uint CLK_HZ = 2; // The frequency parameter, CLK_HZ, used in the calculation of CLK_DIV |
| 87 | + public const uint MIN_HB_HZ = 3; // The minimum allowed beat frequency, in Hz, for this device |
| 88 | + |
| 89 | + internal class NameConverter : DeviceNameConverter |
| 90 | + { |
| 91 | + public NameConverter() |
| 92 | + : base(typeof(PersistentHeartbeat)) |
| 93 | + { |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments