You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.0 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Signal_Generator
{
public class DrawTask : Task, INotifyPropertyChanged
{
Canvas canvas;
System.Timers.Timer taskTimer;
Stopwatch stopwatch = new Stopwatch();
MultiSignal multiSignal;
private State _state;
public State state
{
get { return _state; }
set
{
_state = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("state"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public enum State
{
Created, Running, Paused, Finished, Canceled
}
public DrawTask(Action a) : base(a) { }
public void buildTask(Canvas c)
{
canvas = c;
multiSignal = MultiSignal.getInstance();
state = State.Running;
stopwatch.Start();
taskTimer = new System.Timers.Timer(2000);
taskTimer.Elapsed += updateTextForms;
taskTimer.Enabled = true;
drawSignalAtCanvas();
}
private void updateTextForms(object sender, ElapsedEventArgs e)
{
var elapsed = stopwatch.ElapsedMilliseconds;
}
private void drawSignalAtCanvas()
{
double lastX = 0, lastY = 0;
double w = canvas.ActualWidth;
for (int i = 0; i != (int)w; i++)
{
double y = multiSignal.currentAmplitude(i / 5.0);
drawLine(lastX, lastY, i, y);
lastX = i;
lastY = y;
Thread.Sleep(10);
switch (state)
{
case State.Paused:
while (state == State.Paused)
Thread.Sleep(10);
break;
case State.Canceled: return;
}
}
state = State.Finished;
}
private void drawLine(double x1, double y1, double x2, double y2)
{
Action a = () => {
Line l = new Line();
l.StrokeThickness = 1;
l.Stroke = Brushes.Red;
l.X1 = x1;
l.Y1 = remap(y1);
l.X2 = x2;
l.Y2 = remap(y2);
canvas.Children.Add(l);
};
canvas.Dispatcher.Invoke(a);
}
private double remap(double x, double oldMin = -100.0, double oldMax = 100.0, double newMin = 100.0, double newMax = 0.0)
{
newMin = canvas.ActualHeight;
return newMin + (x - oldMin) * (newMax - newMin) / (oldMax - oldMin);
}
}
}