basic display of oscilloscope values in SerialMonitor (no zoom or scroll yet)
This commit is contained in:
parent
b728db1e26
commit
691ce28d95
@ -10,7 +10,7 @@ const int BufferSize = 128;
|
||||
byte buffer1[BufferSize];
|
||||
byte buffer2[BufferSize];
|
||||
byte* backBuffer = buffer1;
|
||||
byte backBufferPos = 0;
|
||||
volatile byte backBufferPos = 0;
|
||||
byte samplesSkipped = SkipSamples;
|
||||
unsigned long backBufferStartTime = micros();
|
||||
|
||||
@ -44,11 +44,12 @@ void setup()
|
||||
|
||||
sei();//enable interrupts
|
||||
|
||||
Serial.begin(9600);
|
||||
Serial.begin(200000);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
while(backBufferPos < BufferSize / 2) ;
|
||||
cli();//disable interrupts
|
||||
byte* currentBuffer = backBuffer;
|
||||
unsigned long currentBufferStartTime = backBufferStartTime;
|
||||
@ -58,11 +59,7 @@ void loop()
|
||||
backBufferStartTime = micros();
|
||||
sei();//enable interrupts
|
||||
|
||||
debug.write("Starting buffer transmission");
|
||||
|
||||
oscilloscope.write(currentBuffer, currentBufferSize, currentBufferStartTime);
|
||||
|
||||
debug.write("Buffer transmitted");
|
||||
}
|
||||
|
||||
ISR(ADC_vect) {//when new ADC value ready
|
||||
|
@ -28,7 +28,7 @@ namespace SerialMonitor
|
||||
|
||||
Serial = new SerialPort();
|
||||
Serial.PortName = "COM4";
|
||||
Serial.BaudRate = 9600;
|
||||
Serial.BaudRate = 200000;
|
||||
|
||||
Serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(OnDataReceived);
|
||||
|
||||
@ -85,14 +85,7 @@ namespace SerialMonitor
|
||||
MainWindowContext.Get.WriteLine(text);
|
||||
break;
|
||||
case "oscilloscope":
|
||||
/*float visibleDuration = 10.0f;
|
||||
float sampleDelay = 0.000936f;
|
||||
float startOffset = (float)message.SendTime / 1000000;
|
||||
for (int i = 1; i < message.Data.Length; ++i )
|
||||
{
|
||||
var line = new Line();
|
||||
canvas.Children.Add(line);
|
||||
}*/
|
||||
MainWindowContext.Get.AddSequence(message.SendTime, message.Data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
<Grid>
|
||||
<TextBlock TextWrapping="Wrap" VerticalAlignment="Bottom" Height="150" Text="{Binding ConsoleText}" Style="{DynamicResource Console}" Grid.ColumnSpan="2"/>
|
||||
<ScrollViewer HorizontalScrollBarVisibility="Visible" Margin="0,0,0,155" Grid.ColumnSpan="2">
|
||||
<Canvas Style="{DynamicResource Oscilloscope}"/>
|
||||
<Canvas Name="Oscilloscope" Style="{DynamicResource Oscilloscope}"/>
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
@ -23,7 +23,7 @@ namespace SerialMonitor
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
var context = new MainWindowContext();
|
||||
var context = new MainWindowContext(this);
|
||||
context.WriteLine("Connecting...");
|
||||
this.DataContext = context;
|
||||
}
|
||||
|
@ -4,13 +4,19 @@ using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace SerialMonitor
|
||||
{
|
||||
public class MainWindowContext : INotifyPropertyChanged
|
||||
{
|
||||
public MainWindowContext()
|
||||
private MainWindow Window;
|
||||
|
||||
public MainWindowContext(MainWindow window)
|
||||
{
|
||||
Window = window;
|
||||
Get = this;
|
||||
}
|
||||
|
||||
@ -27,6 +33,78 @@ namespace SerialMonitor
|
||||
OnPropertyChanged("ConsoleText");
|
||||
}
|
||||
|
||||
private class Sequence
|
||||
{
|
||||
public ulong StartTime { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
public class Comparer : IComparer<Sequence>
|
||||
{
|
||||
public int Compare(Sequence x, Sequence y)
|
||||
{
|
||||
return x.StartTime.CompareTo(y.StartTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
double ValueToHeight(byte value) { return (double)value + 10; }
|
||||
private SortedSet<Sequence> Sequences = new SortedSet<Sequence>(new Sequence.Comparer());
|
||||
public IEnumerable<Line> Oscilloscope
|
||||
{
|
||||
get
|
||||
{
|
||||
if(!Sequences.Any())
|
||||
yield break;
|
||||
|
||||
ulong startTime = Sequences.ElementAt(0).StartTime;
|
||||
|
||||
foreach (var sequence in Sequences)
|
||||
{
|
||||
foreach (var line in ConvertToLines(sequence, startTime))
|
||||
yield return line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Line> ConvertToLines(Sequence sequence, ulong displayStartTime)
|
||||
{
|
||||
double sampleDelay = 0.000936; // in seconds
|
||||
double scale = 10; // in pixels per second
|
||||
|
||||
double pos = (sequence.StartTime - displayStartTime) / 1000000.0 * scale;
|
||||
if (pos > 1000)
|
||||
yield break;
|
||||
double prevHeight = ValueToHeight(sequence.Data[0]);
|
||||
for (int idx = 1; idx < sequence.Data.Length; ++idx)
|
||||
{
|
||||
byte value = sequence.Data[idx];
|
||||
var line = new Line();
|
||||
line.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
|
||||
line.X1 = pos;
|
||||
pos += sampleDelay * scale;
|
||||
line.X2 = pos;
|
||||
line.Y1 = prevHeight;
|
||||
prevHeight = ValueToHeight(value);
|
||||
line.Y2 = prevHeight;
|
||||
line.HorizontalAlignment = HorizontalAlignment.Left;
|
||||
line.VerticalAlignment = VerticalAlignment.Center;
|
||||
line.StrokeThickness = 1;
|
||||
yield return line;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddSequence(ulong startTime, byte[] data)
|
||||
{
|
||||
var sequence = new Sequence { StartTime = startTime, Data = data };
|
||||
Sequences.Add(sequence);
|
||||
OnPropertyChanged("Oscilloscope");
|
||||
var canvas = (Canvas)Window.FindName("Oscilloscope");
|
||||
/*canvas.Children.Clear();
|
||||
foreach (var line in Oscilloscope)
|
||||
canvas.Children.Add(line);*/
|
||||
foreach (var line in ConvertToLines(sequence, Sequences.ElementAt(0).StartTime))
|
||||
canvas.Children.Add(line);
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
protected void OnPropertyChanged(string name)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user