Skip to content

Commit

Permalink
fixed 16 color custom palettes, libzedmd 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner committed Dec 31, 2023
1 parent 7f02668 commit 48ab3a8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
Binary file modified LibDmd/Costura32/zedmd.dll
Binary file not shown.
Binary file modified LibDmd/Costura64/zedmd64.dll
Binary file not shown.
46 changes: 31 additions & 15 deletions LibDmd/Output/ZeDMD/ZeDMDBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows.Media;
using NLog;
using LibDmd.Frame;
using System.Linq;

namespace LibDmd.Output.ZeDMD
{
Expand All @@ -28,8 +29,6 @@ public abstract class ZeDMDBase

protected IntPtr _pZeDMD = IntPtr.Zero;
protected readonly Logger Logger = LogManager.GetCurrentClassLogger();
// Different modes require different palette sizes. This one should be safe for all.
protected byte[] _paletteBuffer = new byte[64 * 3];
protected ColoredFrame _lastFrame = null;

public void ClearDisplay()
Expand All @@ -44,20 +43,37 @@ public void Dispose()
ClearDisplay();
}

public void SetPalette(Color[] palette)
public void SetPalette(Color[] colors)
{
var paletteChanged = false;
for (var i = 0; i < palette.Length; i++) {
var color = palette[i];
var j = i * 3;
paletteChanged = paletteChanged || (_paletteBuffer[j] != color.R || _paletteBuffer[j + 1] != color.G || _paletteBuffer[j + 2] != color.B);
_paletteBuffer[j] = color.R;
_paletteBuffer[j + 1] = color.G;
_paletteBuffer[j + 2] = color.B;
}

if (paletteChanged && _pZeDMD != IntPtr.Zero) {
ZeDMD_SetPalette(_pZeDMD, _paletteBuffer, palette.Length);
byte[] paletteBuffer = Enumerable.Repeat((byte)0x0, 64 * 3).ToArray();
var numOfColors = colors.Length;

// Custom palettes could be defined with less colors as
// required by the ROM. So we interpolate bigger palettes.
// 2 colors (1 bit) is not supported by ZeDMD.
if (numOfColors == 2) { numOfColors = 4; }

while (numOfColors > 0) {
var palette = ColorUtil.GetPalette(colors, numOfColors);
var pos = 0;

for (int i = 0; i < palette.Length; i++) {
paletteBuffer[pos++] = palette[i].R;
paletteBuffer[pos++] = palette[i].G;
paletteBuffer[pos++] = palette[i].B;
}

if (_pZeDMD != IntPtr.Zero) {
ZeDMD_SetPalette(_pZeDMD, paletteBuffer, numOfColors);
}

if (numOfColors == 4){
numOfColors = 16;
} else if (numOfColors == 16) {
numOfColors = 64;
} else {
numOfColors = 0;
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions PinMameDevice/DmdDevice.ini
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ port =
; if false, doesn't bother looking for a ZeDMD
enabled = false

; if true, ZeDMD dispalys its debug informations
; if true, ZeDMD displays its debug informations
;debug = false

; optionally set the brightness from 0 to 15
Expand All @@ -253,7 +253,7 @@ enabled = false
; if false, doesn't bother looking for a ZeDMD HD
enabled = false

; if true, ZeDMD dispalys its debug informations
; if true, ZeDMD displays its debug informations
;debug = false

; optionally set the brightness from 0 to 15
Expand All @@ -273,7 +273,7 @@ enabled = false
; if false, doesn't bother looking for a ZeDMD WiFi
enabled = false

; if true, ZeDMD dispalys its debug informations
; if true, ZeDMD displays its debug informations
;debug = false

; optionally set the brightness from 0 to 15
Expand All @@ -299,7 +299,7 @@ enabled = false
; if false, doesn't bother looking for a ZeDMD HD WiFi
enabled = false

; if true, ZeDMD dispalys its debug informations
; if true, ZeDMD displays its debug informations
;debug = false

; optionally set the brightness from 0 to 15
Expand Down

0 comments on commit 48ab3a8

Please sign in to comment.