-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Display.cs
153 lines (127 loc) · 5.4 KB
/
Display.cs
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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace SDRSharp.Tetra
{
[DesignTimeVisible(true)]
[Category("SDRSharp")]
[Description("Display Panel")]
public unsafe partial class Display : UserControl
{
private Bitmap _buffer;
private Graphics _graphics;
public Display()
{
_buffer = new Bitmap(ClientRectangle.Width, ClientRectangle.Height, PixelFormat.Format32bppArgb);
_graphics = Graphics.FromImage(_buffer);
InitializeComponent();
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
public void Perform(float* displayInputBuffer, int length)
{
if (displayInputBuffer == null) return;
var graphics = _graphics;
var graphicsRect = ClientRectangle;
var xCenter = (graphicsRect.Width * 0.5f);
var yCenter = (graphicsRect.Height * 0.5f);
var gainY = (float)(yCenter / Math.PI);
var gainX = (float)graphicsRect.Width / length;
var showLength = graphicsRect.Width;
graphics.Clear(Color.Black);
using (var spectrumPen = new Pen(Color.Green, 1.0f))
using (var linePen = new Pen(Color.LightGreen, 1.0f))
using (var gridPen = new Pen(Color.Gray))
using (var textFont = new Font("Arial", 8f))
using (var textBrush = new SolidBrush(Color.White))
{
var eyeLength = 1;
var newX = 0.0f;
var newY = (float)yCenter;
var gridPi2 = (int)(Math.PI * 0.5f * gainY);
graphics.DrawLine(gridPen, 0, yCenter + gridPi2, graphicsRect.Width, yCenter + gridPi2);
//graphics.DrawString("Pi/2", textFont, textBrush, 0, yCenter - gridPi2);
graphics.DrawLine(gridPen, 0, yCenter - gridPi2, graphicsRect.Width, yCenter - gridPi2);
//graphics.DrawString("-Pi/2", textFont, textBrush, 0, yCenter + gridPi2);
graphics.DrawLine(gridPen, 0, yCenter, graphicsRect.Width, yCenter);
//graphics.DrawString("0", textFont, textBrush, 0, yCenter);
for (int i = 0; i < length; i++)
{
newY = (float)(yCenter - (displayInputBuffer[i] * gainY));
newX = i * gainX;
if (newY > graphicsRect.Height) newY = graphicsRect.Height;
else if (newY < 0) newY = 0;
if (float.IsNaN(newY)) newY = 0;
if (newX > graphicsRect.Width) newX = graphicsRect.Width;
else if (newX < 0) newX = 0;
graphics.DrawLine(linePen, newX, newY, newX + eyeLength, newY);
}
}
}
public static void ConfigureGraphics(Graphics graphics)
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.SmoothingMode = SmoothingMode.None;
graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
graphics.InterpolationMode = InterpolationMode.Low;
}
protected override void OnPaint(PaintEventArgs e)
{
ConfigureGraphics(e.Graphics);
e.Graphics.DrawImageUnscaled(_buffer, 0, 0);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (ClientRectangle.Width > 0 && ClientRectangle.Height > 0)
{
_buffer.Dispose();
_graphics.Dispose();
_buffer = new Bitmap(ClientRectangle.Width, ClientRectangle.Height, PixelFormat.Format32bppArgb);
_graphics = Graphics.FromImage(_buffer);
}
}
/// <summary>
/// A constructor variable is required.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Free up all used resources.
/// </summary>
/// <param name="disposing">true if the managed resource should be deleted; otherwise, it is false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Code automatically generated by the component designer
/// <summary>
/// Required method for constructor support - do not modify
/// the contents of this method using a code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Display
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.DoubleBuffered = true;
this.Name = "Display";
this.ResumeLayout(false);
}
#endregion
}
}