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

Commit

Permalink
Reduce CPU usage from 0.5+% (on a Ryzen 5 1600 @ 3.7GHz) to 0.1-0.2%
Browse files Browse the repository at this point in the history
Fix #13
  • Loading branch information
ixjf committed Feb 24, 2019
1 parent b2dec3b commit 690c2c3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
1 change: 1 addition & 0 deletions MSIRGB.DLL/MSIRGB.DLL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="logic\IsaDrv.h" />
<ClInclude Include="logic\math_helper.h" />
<ClInclude Include="logic\module_helper.h" />
<ClInclude Include="logic\pch.h" />
<ClInclude Include="logic\Sio.h" />
Expand Down
3 changes: 3 additions & 0 deletions MSIRGB.DLL/MSIRGB.DLL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<ClInclude Include="logic\module_helper.h">
<Filter>Header Files\logic</Filter>
</ClInclude>
<ClInclude Include="logic\math_helper.h">
<Filter>Header Files\logic</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Header Files">
Expand Down
49 changes: 37 additions & 12 deletions MSIRGB.DLL/logic/Sio.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "pch.h"
#include "Sio.h"
#include "wmi_helper.h"
#include "math_helper.h"

const auto INDEX_REG = 0x4E; // All Mystic Light-supported MBs's chips are found at this port
const auto DATA_REG = 0x4F; // INDEX_REG+1
Expand Down Expand Up @@ -115,19 +116,43 @@ namespace logic {
// F4-F7: each nibble correspond to the green component of colours 1-8 (as shown in the row above)
// F8-FB: each nibble correspond to the blue component of colours 1-8 (as shown in the row above)

std::uint32_t r_cells = chip_read_uint32_from_bank(RGB_LED_BANK, 0xF0);
std::uint32_t g_cells = chip_read_uint32_from_bank(RGB_LED_BANK, 0xF4);
std::uint32_t b_cells = chip_read_uint32_from_bank(RGB_LED_BANK, 0xF8);

std::uint8_t colour_offset = 32 - (4 * index);

// Mask to clear the nibble at the place specified by 'colour_offset' in the uint32 r group
std::uint32_t offset_clear_mask = 0xFFFFFFFF & ~(0xF << colour_offset);

// 'Colour' represents a 12-bit depth RGB colour, so this will only overwrite a nibble each
chip_write_uint32_to_bank(RGB_LED_BANK, 0xF0, (r_cells & offset_clear_mask) | (colour.r << colour_offset));
chip_write_uint32_to_bank(RGB_LED_BANK, 0xF4, (g_cells & offset_clear_mask) | (colour.g << colour_offset));
chip_write_uint32_to_bank(RGB_LED_BANK, 0xF8, (b_cells & offset_clear_mask) | (colour.b << colour_offset));
// The nibble no. from 1 to 8 in order from 0xF0 to 0xF3, 0xF3 to 0xF7, 0xF8 to 0xFB
// So colour 1 = nibble 2
// colour 2 = nibble 1
// colour 3 = nibble 4
// colour 4 = nibble 3
// and so on...
// (Nibble order is reversed in the chip)
std::uint8_t nibble_from_index = (index % 2 == 0) ? (index - 1) : (index + 1);

// The byte number relative to the start pos (0xF0, 0xF4, 0xF8)
// So nibble_from_index = 1, byte = +0
// nibble_from_index = 2, byte = +0
// nibble_from_index = 3, byte = +1
// and so on...
std::uint8_t byte_no = fast_ceil(nibble_from_index, 2) - 1;

// The nibble position relative to the start pos of the byte
// 0 if the first nibble, 1 if the second nibble
std::uint8_t nibble_pos = !(nibble_from_index % 2);

std::uint8_t r_cell = chip_read_uint8_from_bank(RGB_LED_BANK, 0xF0 + byte_no);
std::uint8_t g_cell = chip_read_uint8_from_bank(RGB_LED_BANK, 0xF4 + byte_no);
std::uint8_t b_cell = chip_read_uint8_from_bank(RGB_LED_BANK, 0xF8 + byte_no);

// CLEAR MASK COLOUR
// nibble offset nibble offset
// (colour is in first nibble from right, e.g. 0x1, 0xA, 0xF, 0xC,
// so if nibble_pos = 0 (first nibble from left-to-right),
// then we need to offset it to the left)
std::uint8_t r = (r_cell & (0x0F << (nibble_pos * 4))) | (colour.r << (!nibble_pos * 4));
std::uint8_t g = (g_cell & (0x0F << (nibble_pos * 4))) | (colour.g << (!nibble_pos * 4));
std::uint8_t b = (b_cell & (0x0F << (nibble_pos * 4))) | (colour.b << (!nibble_pos * 4));

chip_write_uint8_to_bank(RGB_LED_BANK, 0xF0 + byte_no, r);
chip_write_uint8_to_bank(RGB_LED_BANK, 0xF4 + byte_no, g);
chip_write_uint8_to_bank(RGB_LED_BANK, 0xF8 + byte_no, b);

return true;
}
Expand Down
8 changes: 8 additions & 0 deletions MSIRGB.DLL/logic/math_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "pch.h"

inline std::uint8_t fast_ceil(std::uint8_t num, std::uint8_t den)
{
return (num / den) + !!(num % den);
}
4 changes: 2 additions & 2 deletions Scripts/Hue Wheel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ while true do
Lighting.SetColour(i, r, g, b)
end

os.sleep(80)
os.sleep(100)

i = i + 1.1
i = i + 1
end

0 comments on commit 690c2c3

Please sign in to comment.