|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reactive; |
| 5 | +using System.Reactive.Linq; |
| 6 | +using System.Reactive.Subjects; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows.Media.Imaging; |
| 11 | +using NLog; |
| 12 | + |
| 13 | +namespace LibDmd.Input.ProPinball |
| 14 | +{ |
| 15 | + |
| 16 | + public class ProPinballSlave : IFrameSource |
| 17 | + { |
| 18 | + const int FrameSize = 128 * 32; |
| 19 | + |
| 20 | + public string Name { get; } = "Pro Pinball"; |
| 21 | + |
| 22 | + public IObservable<Unit> OnResume => _onResume; |
| 23 | + public IObservable<Unit> OnPause => _onPause; |
| 24 | + |
| 25 | + private readonly ISubject<Unit> _onResume = new Subject<Unit>(); |
| 26 | + private readonly ISubject<Unit> _onPause = new Subject<Unit>(); |
| 27 | + |
| 28 | + private readonly ISubject<BitmapSource> _frames = new Subject<BitmapSource>(); |
| 29 | + private ProPinballBridge.ProPinballDmd _bridge; |
| 30 | + |
| 31 | + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Starts sending frames. |
| 35 | + /// </summary> |
| 36 | + private void StartCapturing() |
| 37 | + { |
| 38 | + _bridge = new ProPinballBridge.ProPinballDmd(); |
| 39 | + |
| 40 | + Logger.Info("DMD status: {0}", _bridge.Status); |
| 41 | + if (_bridge.Status != 0) { |
| 42 | + unsafe { |
| 43 | + Logger.Error("Error: {0}", new string(_bridge.Error)); |
| 44 | + } |
| 45 | + } else { |
| 46 | + Logger.Info("Subscribing to Pro Pinball's message queue..."); |
| 47 | + unsafe { |
| 48 | + _bridge.GetFrames(frame => { |
| 49 | + Console.WriteLine("Got frame!"); |
| 50 | + var f = new byte[FrameSize]; |
| 51 | + Marshal.Copy((IntPtr)frame, f, 0, FrameSize); |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public IObservable<BitmapSource> GetFrames() |
| 58 | + { |
| 59 | + StartCapturing(); |
| 60 | + return _frames; |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | +} |
0 commit comments