diff --git a/OneWireIO.ino b/OneWireIO.ino
index 9bdce34..925f199 100644
--- a/OneWireIO.ino
+++ b/OneWireIO.ino
@@ -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
diff --git a/SerialMonitor/SerialMonitor/App.xaml.cs b/SerialMonitor/SerialMonitor/App.xaml.cs
index 24feecc..ac44671 100644
--- a/SerialMonitor/SerialMonitor/App.xaml.cs
+++ b/SerialMonitor/SerialMonitor/App.xaml.cs
@@ -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;
}
}
diff --git a/SerialMonitor/SerialMonitor/MainWindow.xaml b/SerialMonitor/SerialMonitor/MainWindow.xaml
index 765f627..98617aa 100644
--- a/SerialMonitor/SerialMonitor/MainWindow.xaml
+++ b/SerialMonitor/SerialMonitor/MainWindow.xaml
@@ -5,7 +5,7 @@
-
+
diff --git a/SerialMonitor/SerialMonitor/MainWindow.xaml.cs b/SerialMonitor/SerialMonitor/MainWindow.xaml.cs
index d0f0a1d..9a6318d 100644
--- a/SerialMonitor/SerialMonitor/MainWindow.xaml.cs
+++ b/SerialMonitor/SerialMonitor/MainWindow.xaml.cs
@@ -23,7 +23,7 @@ namespace SerialMonitor
public MainWindow()
{
InitializeComponent();
- var context = new MainWindowContext();
+ var context = new MainWindowContext(this);
context.WriteLine("Connecting...");
this.DataContext = context;
}
diff --git a/SerialMonitor/SerialMonitor/MainWindowContext.cs b/SerialMonitor/SerialMonitor/MainWindowContext.cs
index b16f54f..e329a90 100644
--- a/SerialMonitor/SerialMonitor/MainWindowContext.cs
+++ b/SerialMonitor/SerialMonitor/MainWindowContext.cs
@@ -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
+ {
+ public int Compare(Sequence x, Sequence y)
+ {
+ return x.StartTime.CompareTo(y.StartTime);
+ }
+ }
+ }
+ double ValueToHeight(byte value) { return (double)value + 10; }
+ private SortedSet Sequences = new SortedSet(new Sequence.Comparer());
+ public IEnumerable 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 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)
{