Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-photo authored Mar 22, 2022
1 parent 66b2f91 commit 8d951ec
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 119 deletions.
28 changes: 13 additions & 15 deletions AutoFocus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task<AutoFocusResult> RunAF(ICameraMediator camera, IImagingMediato
{
Notification.ShowError("Could not run AF: No canon camera connected");
ReportUpdate(string.Empty);
return new AutoFocusResult(false, new List<FocusPoint>(), TimeSpan.Zero, DateTime.Now, StepSizeToString());
return new AutoFocusResult(false, new List<FocusPoint>(), new FocusPoint(), TimeSpan.Zero, DateTime.Now, StepSizeToString());
}

EDSDK.EdsGetPropertyData(canon, 0x00000416, 0, out uint HasLens); // Check if a lens is attached
Expand All @@ -82,14 +82,17 @@ public async Task<AutoFocusResult> RunAF(ICameraMediator camera, IImagingMediato
Notification.ShowError("Can't start AF: No lens attached");
Logger.Error("Can't start AF: No lens attached");
ReportUpdate(string.Empty);
return new AutoFocusResult(false, new List<FocusPoint>(), TimeSpan.Zero, DateTime.Now, StepSizeToString());
return new AutoFocusResult(false, new List<FocusPoint>(), new FocusPoint(), TimeSpan.Zero, DateTime.Now, StepSizeToString());
}
DateTime start = DateTime.Now;
bool Focused = false;
int iteration = 0;
Method = GetSelectedLogic();
LensAFVM.Instance.AutoFocusIsRunning = true;
FocusControlVM.Instance.ManualFocusControl = false;

List<FocusPoint> FocusPoints = new List<FocusPoint>();
FocusPoint FinalFocusPoint = null;
try
{
// Needed Variables
Expand All @@ -111,6 +114,7 @@ await liveViewEnumerable.ForEachAsync(async _ =>
{
ReportUpdate("Finishing Autofocus");
int iterations = DetermineFinalFocusPoint(FocusPoints, settings.Iterations);
FinalFocusPoint = FocusPoints[settings.Iterations - iterations];
for (int i = 0; i < iterations; i++)
{
DriveFocus(canon, FocusDirection.Far);
Expand Down Expand Up @@ -150,14 +154,14 @@ await liveViewEnumerable.ForEachAsync(async _ =>
if (Method == AutoFocusLogic.STARHFR)
{
StarDetectionResult detection = await PrepareImageForStarHFR(data);
FocusPoints.Add(new FocusPoint(detection));
FocusPoints.Add(new FocusPoint(detection, iteration));

AddToPlot(detection.AverageHFR, iteration);
}
else
{
ContrastDetectionResult detection = PrepareImageForContrast(data);
FocusPoints.Add(new FocusPoint(detection));
FocusPoints.Add(new FocusPoint(detection, iteration));

AddToPlot(detection.AverageContrast, iteration);
}
Expand All @@ -179,7 +183,7 @@ await liveViewEnumerable.ForEachAsync(async _ =>
LensAFVM.Instance.AutoFocusIsRunning = false;

LastAF = DateTime.Now;
AutoFocusResult res = new AutoFocusResult(Focused, FocusPoints, LastAF - start, LastAF, StepSizeToString());
AutoFocusResult res = new AutoFocusResult(Focused, FocusPoints, FinalFocusPoint, LastAF - start, LastAF, StepSizeToString());
Util.CameraInfo info = new Util.CameraInfo(canon);
GenerateLog(settings, res, info);
if (LensAFVM.Instance != null)
Expand Down Expand Up @@ -222,7 +226,7 @@ private string StepSizeToString()
return $"{Settings.Default.SelectedStepSize + 1}";
}

private int DetermineFinalFocusPoint(List<FocusPoint> points, int iterations)
public int DetermineFinalFocusPoint(List<FocusPoint> points, int iterations)
{
int iteration;
if (Method == AutoFocusLogic.STARHFR)
Expand All @@ -235,13 +239,9 @@ private int DetermineFinalFocusPoint(List<FocusPoint> points, int iterations)
temp.Add(point.HFR);
}
hfrs.Sort();
int count = 0;

do
{
iteration = temp.IndexOf(hfrs[count]);
count++;
} while (hfrs[count] == 0);
hfrs.RemoveAll(x => x == 0);
iteration = temp.IndexOf(hfrs[0]);
}
else
{
Expand Down Expand Up @@ -587,12 +587,10 @@ private ContrastDetectionResult PrepareImageForContrast(IRenderedImage exposure)

private void CalibrateLens(IntPtr ptr)
{
int i = 0;
while (i != 7)
for (int i = 0; i < 15; i++)
{
EDSDK.EdsSendCommand(ptr, EDSDK.CameraCommand_DriveLensEvf, (int)EDSDK.EvfDriveLens_Far3);
Thread.Sleep(350); // Let Focus Settle, EDSDK does not wait for the lens to finish moving the focus
i++;
}
}

Expand Down
66 changes: 35 additions & 31 deletions Dockable/FocusControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfutil="clr-namespace:NINA.WPF.Base.Utility;assembly=NINA.WPF.Base">

<DrawingImage x:Key="ArrowLeft">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
<DrawingGroup Opacity="1">
<GeometryDrawing Brush="White" Geometry="F1 M24,24z M0,0z M16.23,23.58L17.453051,22.393559C17.65025,22.197971,17.65025,21.879147,17.453051,21.683559L8.4852542,12.050847 17.533898,2.189322C17.731097,1.9937336,17.731097,1.6749104,17.533898,1.479322L16.26,0.42C16.064412,0.22280112,15.745588,0.22280112,15.55,0.42L5.2,12 15.52,23.54C15.704268,23.748652,16.023462,23.766635,16.23,23.58z" />
</DrawingGroup>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>

<DrawingImage x:Key="ArrowRight">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
<DrawingGroup Opacity="1">
<GeometryDrawing Brush="White" Geometry="F1 M24,24z M0,0z M6.6517976,0.4011297L5.4287466,1.5875707C5.2315476,1.7831587,5.2315476,2.1019827,5.4287466,2.2975707L14.396543,11.930283 5.3478996,21.791808C5.1507006,21.987396,5.1507006,22.306219,5.3478996,22.501808L6.6217976,23.56113C6.8173856,23.758329,7.1362096,23.758329,7.3317976,23.56113L17.681797,11.98113 7.3617976,0.4411297C7.1775296,0.2324777,6.8583356,0.2144947,6.6517976,0.4011297z" />
</DrawingGroup>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>

<DataTemplate x:Key="LensAF.Dockable.FocusControlVM_Dockable">
<DataTemplate.Resources>
<ResourceDictionary>
Expand All @@ -41,15 +21,21 @@
</ResourceDictionary>
</DataTemplate.Resources>

<StackPanel VerticalAlignment="Stretch" Orientation="Vertical">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
<Image
Grid.Row="0"
Width="Auto"
Height="Auto"
Margin="10"
Source="{Binding Image}" />


<StackPanel
Grid.Row="1"
Margin="0,10,0,10"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Expand All @@ -60,12 +46,21 @@
Margin="10,2,10,2"
HorizontalAlignment="Left"
HorizontalContentAlignment="Stretch"
Command="{Binding MoveLeft}"
IsEnabled="{Binding ManualFocusControl}">

<Image Margin="5" Source="{StaticResource ArrowLeft}" />
Command="{Binding MoveLeftBig}"
Content="&lt;&lt;"
FontSize="20"
IsEnabled="{Binding ManualFocusControl}" />

</Button>
<Button
Width="40"
Height="40"
Margin="10,2,10,2"
HorizontalAlignment="Left"
HorizontalContentAlignment="Stretch"
Command="{Binding MoveLeft}"
Content="&lt;"
FontSize="20"
IsEnabled="{Binding ManualFocusControl}" />

<Button
Width="200"
Expand Down Expand Up @@ -93,13 +88,22 @@
HorizontalAlignment="Right"
HorizontalContentAlignment="Stretch"
Command="{Binding MoveRight}"
IsEnabled="{Binding ManualFocusControl}">

<Image Margin="5" Source="{StaticResource ArrowRight}" />
Content="&gt;"
FontSize="20"
IsEnabled="{Binding ManualFocusControl}" />

</Button>
<Button
Width="40"
Height="40"
Margin="10,2,10,2"
HorizontalAlignment="Right"
HorizontalContentAlignment="Stretch"
Command="{Binding MoveRightBig}"
Content="&gt;&gt;"
FontSize="20"
IsEnabled="{Binding ManualFocusControl}" />
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>

</ResourceDictionary>
26 changes: 22 additions & 4 deletions Dockable/FocusControlVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ public class FocusControlVM : DockableVM
private bool _manualFocusControl = false;
private CancellationTokenSource FocusControlToken;

public static FocusControlVM Instance;

public AsyncCommand<bool> StartFocusControl { get; set; }
public RelayCommand StopFocusControl { get; set; }
public RelayCommand MoveLeft { get; set; }
public RelayCommand MoveLeftBig { get; set; }
public RelayCommand MoveRight { get; set; }
public RelayCommand MoveRightBig { get; set; }

private BitmapSource image;
public BitmapSource Image
Expand All @@ -70,10 +74,13 @@ public bool ManualFocusControl
public FocusControlVM(IProfileService profileService, ICameraMediator cam, IImagingMediator imaging) : base(profileService)
{
Camera = cam;
Instance = this;

Title = "Manual Focus Control";
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("/LensAF;component/Options.xaml", UriKind.RelativeOrAbsolute);
ResourceDictionary dict = new ResourceDictionary
{
Source = new Uri("/LensAF;component/Options.xaml", UriKind.RelativeOrAbsolute)
};
ImageGeometry = (GeometryGroup)dict["PluginSVG"];
ImageGeometry.Freeze();

Expand All @@ -85,15 +92,16 @@ public FocusControlVM(IProfileService profileService, ICameraMediator cam, IImag
{
foreach (string issue in Issues)
{
Notification.ShowError($"Can't start AutoFocus: {issue}");
Logger.Error($"Can't start AutoFocus: {issue}");
Notification.ShowError($"Can't start Focus Control: {issue}");
Logger.Error($"Can't start Focus Control: {issue}");
}
return false;
}
FocusControlToken = new CancellationTokenSource();
IAsyncEnumerable<IExposureData> LiveView = Camera.LiveView(FocusControlToken.Token);
ManualFocusControl = true;


await LiveView.ForEachAsync(async exposure =>
{
IImageData data = await exposure.ToImageData();
Expand Down Expand Up @@ -125,10 +133,20 @@ await LiveView.ForEachAsync(async exposure =>
EDSDK.EdsSendCommand(Utility.GetCamera(Camera), EDSDK.CameraCommand_DriveLensEvf, (int)EDSDK.EvfDriveLens_Far1);
});

MoveRightBig = new RelayCommand(_ =>
{
EDSDK.EdsSendCommand(Utility.GetCamera(Camera), EDSDK.CameraCommand_DriveLensEvf, (int)EDSDK.EvfDriveLens_Far2);
});

MoveLeft = new RelayCommand(_ =>
{
EDSDK.EdsSendCommand(Utility.GetCamera(Camera), EDSDK.CameraCommand_DriveLensEvf, (int)EDSDK.EvfDriveLens_Near1);
});

MoveLeftBig = new RelayCommand(_ =>
{
EDSDK.EdsSendCommand(Utility.GetCamera(Camera), EDSDK.CameraCommand_DriveLensEvf, (int)EDSDK.EvfDriveLens_Near2);
});
}

private bool Validate()
Expand Down
1 change: 1 addition & 0 deletions Dockable/LensAFVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public LensAFVM(IProfileService profileService, ICameraMediator camera, IImaging

RunAF = new AsyncCommand<bool>(async () =>
{

if (!Validate())
{
foreach (string issue in Issues)
Expand Down
3 changes: 3 additions & 0 deletions LensAF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public bool PrepareImage
{
Settings.Default.PrepareImage = value;
CoreUtil.SaveSettings(Settings.Default);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PrepareImage)));
}
}

Expand All @@ -206,6 +207,7 @@ public double Stretchfactor
{
Settings.Default.Stretchfactor = value;
CoreUtil.SaveSettings(Settings.Default);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Stretchfactor)));
}
}

Expand All @@ -219,6 +221,7 @@ public double Blackclipping
{
Settings.Default.Blackclipping = value;
CoreUtil.SaveSettings(Settings.Default);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Blackclipping)));
}
}

Expand Down
Loading

0 comments on commit 8d951ec

Please sign in to comment.