Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Added Color Modes #131

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ You have another idea for a feature? Please create an issue.

## Thanks

* This is a fork from the originally ambilight clone project [bambilight by MrBoe](https://github.com/MrBoe/Bambilight) and therefore (and to met the MIT licence) a big thank you goes to [MrBoe](https://github.com/MrBoe)
* This is an extended fork of a fork from the originally ambilight clone project [bambilight by MrBoe](https://github.com/MrBoe/Bambilight) and therefore (and to met the MIT licence) a big thank you goes to [MrBoe](https://github.com/MrBoe)
* More thanks goes to [jasonpong](https://github.com/jasonpang) for his [sample code for the Desktop Duplication API](https://github.com/jasonpang/desktop-duplication-net)

## Changelog
Expand Down
4 changes: 2 additions & 2 deletions adrilight.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2015
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "adrilight", "adrilight\adrilight.csproj", "{1AB593C8-38E2-493B-B4B8-16BC406231AE}"
EndProject
Expand Down
29 changes: 29 additions & 0 deletions adrilight/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Windows.Threading;
using Ninject.Extensions.Conventions;
using adrilight.Resources;
using adrilight.Settings;
using adrilight.Util;

namespace adrilight
Expand Down Expand Up @@ -227,6 +228,8 @@ private void SetupNotifyIcon()
var contextMenu = new System.Windows.Forms.ContextMenu();
contextMenu.MenuItems.Add(CreateSendingMenuItem());

contextMenu.MenuItems.Add(CreateColorModeMenuItem());

contextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("Settings...", (s, e) => OpenSettingsWindow()));
contextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("Exit", (s, e) => Shutdown(0)));

Expand Down Expand Up @@ -262,6 +265,32 @@ void UpdateMenuItem()
return menuItem;
}

private System.Windows.Forms.MenuItem CreateColorModeMenuItem()
{
var modeMenuItem = new System.Windows.Forms.MenuItem("Color Mode");

foreach (ColorModeEnum mode in Enum.GetValues(typeof(ColorModeEnum)))
{
var modeItem = new System.Windows.Forms.MenuItem(mode.ToString(), (s, e) => UserSettings.ColorMode = mode);


void UpdateMenuItem()
{
modeItem.Checked = (mode == UserSettings.ColorMode);
}

//initial update
UpdateMenuItem();

//update on changed setting
UserSettings.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(UserSettings.ColorMode)) { UpdateMenuItem(); } };


modeMenuItem.MenuItems.Add(modeItem);
}
return modeMenuItem;
}

public static bool IsPrivateBuild { get; private set; }
public static string VersionNumber { get; private set; }

Expand Down
5 changes: 5 additions & 0 deletions adrilight/Fakes/UserSettingsFake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ class UserSettingsFake : IUserSettings
public string AdrilightVersion { get; set; } = "2.0.6";

public AlternateWhiteBalanceModeEnum AlternateWhiteBalanceMode { get; set; } = AlternateWhiteBalanceModeEnum.Off;
public ColorModeEnum ColorMode { get; set; } = ColorModeEnum.Ambilight;
#pragma warning disable CS0067
public event PropertyChangedEventHandler PropertyChanged;
#pragma warning restore CS0067

public byte StaticColorModeRed { get; set; } = 100;
public byte StaticColorModeGreen { get; set; } = 100;
public byte StaticColorModeBlue { get; set; } = 100;
}
}
15 changes: 15 additions & 0 deletions adrilight/Settings/ColorModeEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace adrilight.Settings
{
public enum ColorModeEnum
{
Ambilight = 0,
Static = 1,
Rainbow = 2
}
}
5 changes: 5 additions & 0 deletions adrilight/Settings/IUserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ public interface IUserSettings : INotifyPropertyChanged

string AdrilightVersion { get; set; }
AlternateWhiteBalanceModeEnum AlternateWhiteBalanceMode { get; set; }
ColorModeEnum ColorMode { get; set; }

byte StaticColorModeRed { get; set; }
byte StaticColorModeGreen { get; set; }
byte StaticColorModeBlue { get; set; }
}
}
16 changes: 14 additions & 2 deletions adrilight/Settings/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ internal class UserSettings : ViewModelBase, IUserSettings
private bool _sendRandomColors = false;
private int _limitFps = 60;
private int _configFileVersion = 2;
private AlternateWhiteBalanceModeEnum _alternateWhiteBalanceMode = AlternateWhiteBalanceModeEnum.Off;
private AlternateWhiteBalanceModeEnum _alternateWhiteBalanceModeEnum = AlternateWhiteBalanceModeEnum.Off;
private ColorModeEnum _colorModeEnum = ColorModeEnum.Ambilight;

private byte _staticColorModeRed = 100;
private byte _staticColorModeGreen = 100;
private byte _staticColorModeBlue = 100;

//support future config file migration
public int ConfigFileVersion { get => _configFileVersion; set { Set(() => ConfigFileVersion, ref _configFileVersion, value); } }
Expand Down Expand Up @@ -82,6 +87,13 @@ internal class UserSettings : ViewModelBase, IUserSettings
public bool SendRandomColors { get => _sendRandomColors; set { Set(() => SendRandomColors, ref _sendRandomColors, value); } }

public Guid InstallationId { get; set; } = Guid.NewGuid();
public AlternateWhiteBalanceModeEnum AlternateWhiteBalanceMode { get => _alternateWhiteBalanceMode; set { Set(() => AlternateWhiteBalanceMode, ref _alternateWhiteBalanceMode, value); } }
public AlternateWhiteBalanceModeEnum AlternateWhiteBalanceMode { get => _alternateWhiteBalanceModeEnum; set { Set(() => AlternateWhiteBalanceMode, ref _alternateWhiteBalanceModeEnum, value); } }

public ColorModeEnum ColorMode { get => _colorModeEnum; set { Set(() => ColorMode, ref _colorModeEnum, value); } }

public byte StaticColorModeRed { get => _staticColorModeRed; set { Set(() => StaticColorModeRed, ref _staticColorModeRed, value); } }
public byte StaticColorModeGreen { get => _staticColorModeGreen; set { Set(() => StaticColorModeGreen, ref _staticColorModeGreen, value); } }
public byte StaticColorModeBlue { get => _staticColorModeBlue; set { Set(() => StaticColorModeBlue, ref _staticColorModeBlue, value); } }

}
}
54 changes: 34 additions & 20 deletions adrilight/Util/SerialStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using adrilight.Settings;
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading;
Expand Down Expand Up @@ -50,7 +51,6 @@ private void RefreshTransferState()
{
if (IsValid())
{

//start it
_log.Debug("starting the serial stream");
Start();
Expand All @@ -70,7 +70,7 @@ private void RefreshTransferState()
}

private readonly byte[] _messagePreamble = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
private readonly byte[] _messagePostamble = { 85, 204, 165 };
private readonly byte[] _messagePostamble = {85, 204, 165};


private Thread _workerThread;
Expand All @@ -86,8 +86,7 @@ public void Start()
if (_workerThread != null) return;

_cancellationTokenSource = new CancellationTokenSource();
_workerThread = new Thread(DoWork)
{
_workerThread = new Thread(DoWork) {
Name = "Serial sending",
IsBackground = true
};
Expand Down Expand Up @@ -119,25 +118,33 @@ public void Stop()
{
const int colorsPerLed = 3;
int bufferLength = _messagePreamble.Length
+ (SpotSet.Spots.Length * colorsPerLed)
+ _messagePostamble.Length;
+ (SpotSet.Spots.Length * colorsPerLed)
+ _messagePostamble.Length;


outputStream = ArrayPool<byte>.Shared.Rent(bufferLength);

Buffer.BlockCopy(_messagePreamble, 0, outputStream, 0, _messagePreamble.Length);
Buffer.BlockCopy(_messagePostamble, 0, outputStream, bufferLength - _messagePostamble.Length, _messagePostamble.Length);
Buffer.BlockCopy(_messagePostamble, 0, outputStream, bufferLength - _messagePostamble.Length,
_messagePostamble.Length);

var allBlack = true;
foreach (Spot spot in SpotSet.Spots)
{
if (!UserSettings.SendRandomColors)
if (UserSettings.ColorMode.Equals(ColorModeEnum.Static))
{
outputStream[counter++] = UserSettings.StaticColorModeBlue; // blue
outputStream[counter++] = UserSettings.StaticColorModeGreen; // green
outputStream[counter++] = UserSettings.StaticColorModeRed; // red

allBlack = allBlack && UserSettings.StaticColorModeRed == 0 &&
UserSettings.StaticColorModeGreen == 0 && UserSettings.StaticColorModeBlue == 0;
}
else if (!UserSettings.SendRandomColors)
{
outputStream[counter++] = spot.Blue; // blue
outputStream[counter++] = spot.Green; // green
outputStream[counter++] = spot.Red; // red

allBlack = allBlack && spot.Red == 0 && spot.Green == 0 && spot.Blue == 0;
}
else
{
Expand Down Expand Up @@ -187,16 +194,18 @@ private void DoWork(object tokenObject)
if (openedComPort != UserSettings.ComPort)
{
serialPort?.Close();

serialPort = UserSettings.ComPort!= "Fake Port"
? (ISerialPortWrapper) new WrappedSerialPort(new SerialPort(UserSettings.ComPort, baudRate))

serialPort = UserSettings.ComPort != "Fake Port"
? (ISerialPortWrapper) new WrappedSerialPort(new SerialPort(UserSettings.ComPort,
baudRate))
: new FakeSerialPort();

try
{
serialPort.Open();
}
catch {
catch
{
// useless UnauthorizedAccessException
}

Expand All @@ -208,6 +217,7 @@ private void DoWork(object tokenObject)
Thread.Sleep(500);
continue;
}

openedComPort = UserSettings.ComPort;
}

Expand All @@ -219,15 +229,18 @@ private void DoWork(object tokenObject)
{
//there is maybe something wrong here because most frames where black. report it once per run only
var settingsJson = JsonConvert.SerializeObject(UserSettings, Formatting.None);
_log.Info($"Sent {frameCounter} frames already. {blackFrameCounter} were completely black. Settings= {settingsJson}");
_log.Info(
$"Sent {frameCounter} frames already. {blackFrameCounter} were completely black. Settings= {settingsJson}");
}

ArrayPool<byte>.Shared.Return(outputBuffer);

//ws2812b LEDs need 30 µs = 0.030 ms for each led to set its color so there is a lower minimum to the allowed refresh rate
//receiving over serial takes it time as well and the arduino does both tasks in sequence
//+1 ms extra safe zone
var fastLedTime = (streamLength - _messagePreamble.Length - _messagePostamble.Length) /3.0*0.030d;
var serialTransferTime = streamLength * 10.0*1000.0/ baudRate;
var fastLedTime = (streamLength - _messagePreamble.Length - _messagePostamble.Length) / 3.0 *
0.030d;
var serialTransferTime = streamLength * 10.0 * 1000.0 / baudRate;
var minTimespan = (int) (fastLedTime + serialTransferTime) + 1;

Thread.Sleep(minTimespan);
Expand All @@ -241,7 +254,8 @@ private void DoWork(object tokenObject)
}
catch (Exception ex)
{
if (ex.GetType() != typeof(AccessViolationException) && ex.GetType() != typeof(UnauthorizedAccessException))
if (ex.GetType() != typeof(AccessViolationException) &&
ex.GetType() != typeof(UnauthorizedAccessException))
{
_log.Debug(ex, "Exception catched.");
}
Expand All @@ -251,6 +265,7 @@ private void DoWork(object tokenObject)
{
serialPort.Close();
}

serialPort?.Dispose();
serialPort = null;

Expand All @@ -266,7 +281,6 @@ private void DoWork(object tokenObject)
}
}
}

}

public void Dispose()
Expand Down
96 changes: 96 additions & 0 deletions adrilight/View/SettingsWindowComponents/ColorModeSetup.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<UserControl x:Class="adrilight.View.SettingsWindowComponents.ColorModeSetup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"

TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="13"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="{StaticResource MaterialDesignFont}"

d:DesignHeight="900" d:DesignWidth="900">
<Grid Width="900">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>

<materialDesign:Card Margin="4 4 2 4" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Stretch">
<StackPanel Margin="38 28 38 18">
<TextBlock Margin="2,-10,72.4,49.4" FontWeight="Bold"
RenderTransformOrigin="0.5,0.5" Foreground="#FFFD7474" TextDecorations="{x:Null}">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-7.268"/>
<TranslateTransform/>
</TransformGroup>
</TextBlock.RenderTransform>EXPERIMENTAL!</TextBlock>
<TextBlock Margin="58 18 38 8" FontWeight="bold">Selected color mode:</TextBlock>
<ComboBox Margin="58 0 0 40"
Width="150"
HorizontalAlignment="Left"
ItemsSource="{Binding ColorModes}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedValue="{Binding Settings.ColorMode, Mode=TwoWay}"/>

</StackPanel>
</materialDesign:Card>

<materialDesign:Card Margin="4 4 2 4" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Stretch">

<Grid Margin="38 28 8 38">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>

<TextBlock Grid.Row="0" Grid.ColumnSpan="3" VerticalAlignment="Bottom" FontWeight="Bold">
Static Color
</TextBlock>

<TextBlock Grid.Row="1" VerticalAlignment="Bottom">Red</TextBlock>
<TextBlock Grid.Row="2" VerticalAlignment="Bottom">Green</TextBlock>
<TextBlock Grid.Row="3" VerticalAlignment="Bottom">Blue</TextBlock>

<Slider Grid.Row="1" Grid.Column="1" Width="200"
Minimum="1" Maximum="100" Value="{Binding Settings.StaticColorModeRed}"
Style="{StaticResource MaterialDesignDiscreteSlider}"
/>
<Slider Grid.Row="2" Grid.Column="1" Width="200"
Minimum="1" Maximum="100" Value="{Binding Settings.StaticColorModeGreen}"
Style="{StaticResource MaterialDesignDiscreteSlider}"
/>
<Slider Grid.Row="3" Grid.Column="1" Width="200"
Minimum="1" Maximum="100" Value="{Binding Settings.StaticColorModeBlue}"
Style="{StaticResource MaterialDesignDiscreteSlider}"
/>

<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding Settings.StaticColorModeRed}" VerticalAlignment="Bottom" />
<TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding Settings.StaticColorModeGreen}" VerticalAlignment="Bottom" />
<TextBlock Grid.Row="3" Grid.Column="2" Text="{Binding Settings.StaticColorModeBlue}" VerticalAlignment="Bottom" />
</Grid>

</materialDesign:Card>
</Grid>
</UserControl>
Loading