Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
Fix #149
Browse files Browse the repository at this point in the history
  • Loading branch information
ixjf committed Sep 3, 2021
1 parent 7dec096 commit c4265b1
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 8 deletions.
35 changes: 35 additions & 0 deletions MSIRGB.DLL/MSIRGB.Lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,41 @@ namespace MSIRGB {
return Nullable<Color>(Color::FromRgb(colour->r, colour->g, colour->b));
}

Boolean GetDefaultColourChannelsInvertedSetting()
{
return lighting->get_default_colour_channels_inverted_setting();
}

void SetRChannelInverted(Boolean inverted)
{
lighting->set_r_channel_inverted(inverted);
}

void SetGChannelInverted(Boolean inverted)
{
lighting->set_g_channel_inverted(inverted);
}

void SetBChannelInverted(Boolean inverted)
{
lighting->set_b_channel_inverted(inverted);
}

Boolean IsRChannelInverted()
{
return lighting->is_r_channel_inverted();
}

Boolean IsGChannelInverted()
{
return lighting->is_g_channel_inverted();
}

Boolean IsBChannelInverted()
{
return lighting->is_b_channel_inverted();
}

bool SetBreathingModeEnabled(Boolean enabled)
{
return lighting->set_breathing_mode_enabled(enabled);
Expand Down
110 changes: 104 additions & 6 deletions MSIRGB.DLL/logic/Lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ namespace logic {
throw Exception(ErrorCode::DriverLoadFailed);
}
leave_critical_section();

// Set default inverted colour channels setting
bool default_colour_channels_inv = get_default_colour_channels_inverted_setting();
set_r_channel_inverted(default_colour_channels_inv);
set_g_channel_inverted(default_colour_channels_inv);
set_b_channel_inverted(default_colour_channels_inv);
}

Lighting::~Lighting()
Expand Down Expand Up @@ -227,6 +233,74 @@ namespace logic {
return std::make_optional(Colour{ r, g, b });
}

bool Lighting::get_default_colour_channels_inverted_setting() const
{
return mb_flags & MbFlags::INVERTED_COLOUR_CHANNELS;
}

void Lighting::set_r_channel_inverted(bool inverted)
{
curr_batch.r_channel_inverted = inverted;

if (!batch_calls) {
batch_commit();
}
}

void Lighting::set_g_channel_inverted(bool inverted)
{
curr_batch.g_channel_inverted = inverted;

if (!batch_calls) {
batch_commit();
}
}

void Lighting::set_b_channel_inverted(bool inverted)
{
curr_batch.b_channel_inverted = inverted;

if (!batch_calls) {
batch_commit();
}
}

bool Lighting::is_r_channel_inverted() const
{
// See batch_commit for details
enter_critical_section();

std::uint8_t val_at_ff = sio->read_uint8_from_bank(RGB_BANK, 0xFF);

leave_critical_section();

return val_at_ff & 0b00010000;
}

bool Lighting::is_g_channel_inverted() const
{
// See batch_commit for details
enter_critical_section();

std::uint8_t val_at_ff = sio->read_uint8_from_bank(RGB_BANK, 0xFF);

leave_critical_section();

return val_at_ff & 0b00001000;
}

bool Lighting::is_b_channel_inverted() const
{
// See batch_commit for details
enter_critical_section();

std::uint8_t val_at_ff = sio->read_uint8_from_bank(RGB_BANK, 0xFF);

leave_critical_section();

return val_at_ff & 0b00000100;
}

bool Lighting::set_breathing_mode_enabled(bool enable)
{
if (get_flash_speed() != FlashingSpeed::Disabled &&
Expand Down Expand Up @@ -505,17 +579,41 @@ namespace logic {
// I think it's only supported on other MBs that have RGB headers but are
// different somehow - those function differently from the MBs supported
// here.
if (mb_flags & MbFlags::INVERTED_COLOUR_CHANNELS) {
val_at_ff |= 0b00011100;

if (mb_flags & MbFlags::INVERTED_COLOUR_CHANNELS) { // rainbow crap
// NOTE: This is within this IF because it seems to only be applied
// by Mystic Light when colour channels are inverted.
std::uint8_t val_at_fd = sio->read_uint8_from_bank(RGB_BANK, 0xFD);

val_at_fd &= 0b11111000;

sio->write_uint8_to_bank(RGB_BANK, 0xFD, val_at_fd);
}
else {
val_at_ff &= 0b11100011;

if (curr_batch.r_channel_inverted.has_value()) {
if (*(curr_batch.r_channel_inverted) == true) {
val_at_ff |= 0b00010000;
}
else {
val_at_ff &= 0b11101111;
}
}

if (curr_batch.g_channel_inverted.has_value()) {
if (*(curr_batch.g_channel_inverted) == true) {
val_at_ff |= 0b00001000;
}
else {
val_at_ff &= 0b11110111;
}
}

if (curr_batch.b_channel_inverted.has_value()) {
if (*(curr_batch.b_channel_inverted) == true) {
val_at_ff |= 0b00000100;
}
else {
val_at_ff &= 0b11111011;
}
}

// Fade in value is first 3 bits: BGR, in this order, from leftmost bit to rightmost bit
Expand Down
10 changes: 10 additions & 0 deletions MSIRGB.DLL/logic/Lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ namespace logic {
void set_led_enabled(bool enable);
bool set_colour(std::uint8_t index, Colour colour);
std::optional<Colour> get_colour(std::uint8_t index) const;
bool get_default_colour_channels_inverted_setting() const;
void set_r_channel_inverted(bool inverted);
void set_g_channel_inverted(bool inverted);
void set_b_channel_inverted(bool inverted);
bool is_r_channel_inverted() const;
bool is_g_channel_inverted() const;
bool is_b_channel_inverted() const;
bool set_breathing_mode_enabled(bool enable);
bool is_breathing_mode_enabled() const;
bool set_step_duration(std::uint16_t step_duration);
Expand Down Expand Up @@ -95,6 +102,9 @@ namespace logic {

struct Batch {
std::unordered_map<std::uint8_t, Colour> colours;
std::optional<bool> r_channel_inverted;
std::optional<bool> g_channel_inverted;
std::optional<bool> b_channel_inverted;
std::optional<bool> enabled;
std::optional<bool> breathing_mode_enabled;
std::optional<std::uint16_t> step_duration;
Expand Down
31 changes: 31 additions & 0 deletions MSIRGB.GUI/MainWindowModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public MainWindowModel()
public void GetCurrentConfig(ref List<Color> colours,
out ushort stepDuration,
out bool breathingEnabled,
out bool invertedRChannel,
out bool invertedGChannel,
out bool invertedBChannel,
out FlashingSpeed flashingSpeed)
{
foreach (byte index in Range(1, 8))
Expand All @@ -60,12 +63,19 @@ public void GetCurrentConfig(ref List<Color> colours,

breathingEnabled = _lighting.IsBreathingModeEnabled();

invertedRChannel = _lighting.IsRChannelInverted();
invertedGChannel = _lighting.IsGChannelInverted();
invertedBChannel = _lighting.IsBChannelInverted();

flashingSpeed = (FlashingSpeed)_lighting.GetFlashingSpeed();
}

public void ApplyConfig(List<Color> colours,
ushort stepDuration,
bool breathingEnabled,
bool invertedRChannel,
bool invertedGChannel,
bool invertedBChannel,
FlashingSpeed flashingSpeed)
{
_lighting.BatchBegin();
Expand All @@ -76,6 +86,23 @@ public void ApplyConfig(List<Color> colours,
c.R /= 0x11; // Colour must be passed with 12-bit depth
c.G /= 0x11;
c.B /= 0x11;

if (invertedRChannel) // if inverting colour channels, transform colours from
// colour picker appropriately (which are never inverted)
{
c.R = (byte)(0x0F - c.R);
}

if (invertedGChannel)
{
c.G = (byte)(0x0F - c.G);
}

if (invertedBChannel)
{
c.B = (byte)(0x0F - c.B);
}

_lighting.SetColour(index, c);
}

Expand All @@ -87,6 +114,10 @@ public void ApplyConfig(List<Color> colours,

_lighting.SetBreathingModeEnabled(breathingEnabled);

_lighting.SetRChannelInverted(invertedRChannel);
_lighting.SetGChannelInverted(invertedGChannel);
_lighting.SetBChannelInverted(invertedBChannel);

_lighting.BatchEnd();
}

Expand Down
35 changes: 35 additions & 0 deletions MSIRGB.GUI/MainWindowView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,41 @@
</i:Interaction.Triggers>
</Button>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="55,0,0,0">
<TextBlock Style="{StaticResource CustomTextBlockStyle}"
Text="Invert channels: "
VerticalAlignment="Center"
Margin="0,0,0,0"
ToolTip="Changes to inverted R channel on the chip. Colours are more vivid." />

<TextBlock Style="{StaticResource CustomTextBlockStyle}"
Text="R"
VerticalAlignment="Center"
Margin="0,0,0,0" />
<CheckBox Style="{StaticResource CustomCheckBoxStyle}"
VerticalAlignment="Stretch"
Margin="5,1,0,0"
IsChecked="{Binding Path=InvertedRChannel, Mode=TwoWay}" />

<TextBlock Style="{StaticResource CustomTextBlockStyle}"
Text="G"
VerticalAlignment="Center"
Margin="5,0,0,0" />
<CheckBox Style="{StaticResource CustomCheckBoxStyle}"
VerticalAlignment="Stretch"
Margin="5,1,0,0"
IsChecked="{Binding Path=InvertedGChannel, Mode=TwoWay}" />

<TextBlock Style="{StaticResource CustomTextBlockStyle}"
Text="B"
VerticalAlignment="Center"
Margin="5,0,0,0" />
<CheckBox Style="{StaticResource CustomCheckBoxStyle}"
VerticalAlignment="Stretch"
Margin="5,1,0,0"
IsChecked="{Binding Path=InvertedBChannel, Mode=TwoWay}" />
</StackPanel>
</StackPanel>
</StackPanel>
</GroupBox>
Expand Down
49 changes: 49 additions & 0 deletions MSIRGB.GUI/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ public class MainWindowViewModel : DependencyObject
typeof(MainWindowViewModel),
new PropertyMetadata(false));

public static readonly DependencyProperty InvertedRChannelProperty = DependencyProperty.Register(
"InvertedRChannel",
typeof(bool),
typeof(MainWindowViewModel),
new PropertyMetadata(false));

public static readonly DependencyProperty InvertedGChannelProperty = DependencyProperty.Register(
"InvertedGChannel",
typeof(bool),
typeof(MainWindowViewModel),
new PropertyMetadata(false));

public static readonly DependencyProperty InvertedBChannelProperty = DependencyProperty.Register(
"InvertedBChannel",
typeof(bool),
typeof(MainWindowViewModel),
new PropertyMetadata(false));

public static readonly DependencyProperty FlashingSpeedIndexProperty = DependencyProperty.Register(
"FlashingSpeedIndex",
typeof(int),
Expand Down Expand Up @@ -102,6 +120,27 @@ public bool BreathingModeEnabled
set { SetValue(BreathingModeEnabledProperty, value); }
}


public bool InvertedRChannel
{
get { return (bool)GetValue(InvertedRChannelProperty); }
set { SetValue(InvertedRChannelProperty, value); }
}


public bool InvertedGChannel
{
get { return (bool)GetValue(InvertedGChannelProperty); }
set { SetValue(InvertedGChannelProperty, value); }
}


public bool InvertedBChannel
{
get { return (bool)GetValue(InvertedBChannelProperty); }
set { SetValue(InvertedBChannelProperty, value); }
}

public int FlashingSpeedSelectedIndex
{
get { return (int)GetValue(FlashingSpeedIndexProperty); }
Expand Down Expand Up @@ -194,6 +233,9 @@ public void Apply()
_model.ApplyConfig(colours,
Convert.ToUInt16(StepDurationText),
BreathingModeEnabled,
InvertedRChannel,
InvertedGChannel,
InvertedBChannel,
FlashingSpeedList[FlashingSpeedSelectedIndex].Type);
}

Expand Down Expand Up @@ -231,6 +273,9 @@ public void LoadedEvent()
_model.GetCurrentConfig(ref colours,
out ushort stepDuration,
out bool breathingEnabled,
out bool invertedRChannel,
out bool invertedGChannel,
out bool invertedBChannel,
out MainWindowModel.FlashingSpeed flashingSpeed);

foreach(Byte index in Range(1, 8))
Expand All @@ -242,6 +287,10 @@ public void LoadedEvent()

BreathingModeEnabled = breathingEnabled;

InvertedRChannel = invertedRChannel;
InvertedGChannel = invertedGChannel;
InvertedBChannel = invertedBChannel;

FlashingSpeedSelectedIndex = FlashingSpeedList.IndexOf(FlashingSpeedList.Single(x => x.Type == flashingSpeed));

ListSelectedColourIndex = 0;
Expand Down
Loading

0 comments on commit c4265b1

Please sign in to comment.