Skip to content

Commit

Permalink
added ConsoleDMD (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner authored Feb 19, 2024
1 parent 20482f5 commit f73c1ec
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ set(DMDUTIL_SOURCES
src/DMD.cpp
src/LevelDMD.cpp
src/RGB24DMD.cpp
src/ConsoleDMD.cpp
src/Logger.cpp
src/AlphaNumeric.cpp
src/FrameUtil.cpp
Expand Down
21 changes: 21 additions & 0 deletions include/DMDUtil/ConsoleDMD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <cstdint>
#include <cstdio>

namespace DMDUtil
{

class ConsoleDMD
{
public:
ConsoleDMD(FILE* f);
~ConsoleDMD();

void Render(uint8_t* buffer, uint16_t width, uint16_t height, uint8_t bitDepth);

private:
FILE* out;
};

} // namespace DMDUtil
6 changes: 6 additions & 0 deletions include/DMDUtil/DMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Serum;
class PixelcadeDMD;
class LevelDMD;
class RGB24DMD;
class ConsoleDMD;

class DMDUTILAPI DMD
{
Expand All @@ -71,6 +72,8 @@ class DMDUTILAPI DMD
bool DestroyLevelDMD(LevelDMD* pLevelDMD);
RGB24DMD* CreateRGB24DMD(uint16_t width, uint16_t height);
bool DestroyRGB24DMD(RGB24DMD* pRGB24DMD);
ConsoleDMD* CreateConsoleDMD(FILE* f = stdout);
bool DestroyConsoleDMD(ConsoleDMD* pConsoleDMD);
void UpdateData(const uint8_t* pData, int depth, uint16_t width, uint16_t height, uint8_t r, uint8_t g, uint8_t b,
const char* name = nullptr);
void UpdateRGB24Data(const uint8_t* pData, int depth, uint16_t width, uint16_t height, uint8_t r, uint8_t g,
Expand Down Expand Up @@ -119,6 +122,7 @@ class DMDUTILAPI DMD
void DmdFrameReadyResetThread();
void LevelDMDThread();
void RGB24DMDThread();
void ConsoleDMDThread();
void ZeDMDThread();
void DumpDMDTxtThread();
void DumpDMDRawThread();
Expand All @@ -129,9 +133,11 @@ class DMDUTILAPI DMD
ZeDMD* m_pZeDMD;
std::vector<LevelDMD*> m_levelDMDs;
std::vector<RGB24DMD*> m_rgb24DMDs;
std::vector<ConsoleDMD*> m_consoleDMDs;

std::thread* m_pLevelDMDThread;
std::thread* m_pRGB24DMDThread;
std::thread* m_pConsoleDMDThread;
std::thread* m_pZeDMDThread;
std::thread* m_pdmdFrameReadyResetThread;
std::thread* m_pDumpDMDTxtThread;
Expand Down
45 changes: 45 additions & 0 deletions src/ConsoleDMD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "DMDUtil/ConsoleDMD.h"

#include <stdio.h>

namespace DMDUtil
{

ConsoleDMD::ConsoleDMD(FILE* f) { out = f; }

ConsoleDMD::~ConsoleDMD() {}

void ConsoleDMD::Render(uint8_t* buffer, uint16_t width, uint16_t height, uint8_t bitDepth)
{
for (uint16_t y = 0; y < height; y++)
{
for (uint16_t x = 0; x < width; x++)
{
uint8_t value = buffer[y * width + x];
if (bitDepth > 2)
{
fprintf(out, "%2x", value);
}
else
{
switch (value)
{
case 0:
fprintf(out, "\033[0;40m⚫\033[0m");
break;
case 1:
fprintf(out, "\033[0;40m🟤\033[0m");
break;
case 2:
fprintf(out, "\033[0;40m🟠\033[0m");
break;
case 3:
fprintf(out, "\033[0;40m🟡\033[0m");
break;
}
}
}
fprintf(out, "\n");
}
}
} // namespace DMDUtil
73 changes: 73 additions & 0 deletions src/DMD.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "DMDUtil/DMD.h"

#include "DMDUtil/Config.h"
#include "DMDUtil/ConsoleDMD.h"
#include "DMDUtil/LevelDMD.h"
#include "DMDUtil/RGB24DMD.h"

Expand Down Expand Up @@ -45,6 +46,7 @@ DMD::DMD()
m_pZeDMDThread = nullptr;
m_pLevelDMDThread = nullptr;
m_pRGB24DMDThread = nullptr;
m_pConsoleDMDThread = nullptr;
m_pDumpDMDTxtThread = nullptr;
m_pDumpDMDRawThread = nullptr;
#if !( \
Expand Down Expand Up @@ -82,6 +84,13 @@ DMD::~DMD()
m_pRGB24DMDThread = nullptr;
}

if (m_pConsoleDMDThread)
{
m_pConsoleDMDThread->join();
delete m_pConsoleDMDThread;
m_pConsoleDMDThread = nullptr;
}

if (m_pZeDMDThread)
{
m_pZeDMDThread->join();
Expand Down Expand Up @@ -124,6 +133,7 @@ DMD::~DMD()

for (LevelDMD* pLevelDMD : m_levelDMDs) delete pLevelDMD;
for (RGB24DMD* pRGB24DMD : m_rgb24DMDs) delete pRGB24DMD;
for (ConsoleDMD* pConsoleDMD : m_consoleDMDs) delete pConsoleDMD;
}

bool DMD::IsFinding() { return m_finding; }
Expand Down Expand Up @@ -195,6 +205,32 @@ bool DMD::DestroyRGB24DMD(RGB24DMD* pRGB24DMD)
return false;
}

ConsoleDMD* DMD::CreateConsoleDMD(FILE* f)
{
ConsoleDMD* const pConsoleDMD = new ConsoleDMD(f);
m_consoleDMDs.push_back(pConsoleDMD);
if (!m_pConsoleDMDThread) m_pConsoleDMDThread = new std::thread(&DMD::ConsoleDMDThread, this);
return pConsoleDMD;
}

bool DMD::DestroyConsoleDMD(ConsoleDMD* pConsoleDMD)
{
auto it = std::find(m_consoleDMDs.begin(), m_consoleDMDs.end(), pConsoleDMD);
if (it != m_consoleDMDs.end())
{
m_consoleDMDs.erase(it);
delete pConsoleDMD;

if (m_consoleDMDs.empty())
{
//@todo terminate ConsoleDMDThread
}

return true;
}
return false;
}

void DMD::UpdateData(const uint8_t* pData, int depth, uint16_t width, uint16_t height, uint8_t r, uint8_t g, uint8_t b,
DMDMode mode, const char* name)
{
Expand Down Expand Up @@ -838,6 +874,43 @@ void DMD::RGB24DMDThread()
}
}

void DMD::ConsoleDMDThread()
{
int bufferPosition = 0;
uint8_t renderBuffer[256 * 64] = {0};

while (true)
{
std::shared_lock<std::shared_mutex> sl(m_dmdSharedMutex);
m_dmdCV.wait(sl, [&]() { return m_dmdFrameReady || m_stopFlag; });
sl.unlock();
if (m_stopFlag)
{
return;
}

while (!m_stopFlag && bufferPosition != m_updateBufferPosition)
{
if (++bufferPosition >= DMDUTIL_FRAME_BUFFER_SIZE) bufferPosition = 0;

if (!m_consoleDMDs.empty() && m_updateBuffer[bufferPosition]->mode == DMDMode::Data &&
m_updateBuffer[bufferPosition]->hasData)
{
int length = m_updateBuffer[bufferPosition]->width * m_updateBuffer[bufferPosition]->height;
if (memcmp(renderBuffer, m_updateBuffer[bufferPosition]->data, length) != 0)
{
memcpy(renderBuffer, m_updateBuffer[bufferPosition]->data, length);
for (ConsoleDMD* pConsoleDMD : m_consoleDMDs)
{
pConsoleDMD->Render(renderBuffer, m_updateBuffer[bufferPosition]->width,
m_updateBuffer[bufferPosition]->height, m_updateBuffer[bufferPosition]->depth);
}
}
}
}
}
}

bool DMD::UpdatePalette(uint8_t* pPalette, uint8_t depth, uint8_t r, uint8_t g, uint8_t b)
{
if (depth != 2 && depth != 4) return false;
Expand Down
4 changes: 4 additions & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "DMDUtil/DMDUtil.h"
#include "DMDUtil/LevelDMD.h"
#include "DMDUtil/RGB24DMD.h"
#include "DMDUtil/ConsoleDMD.h"

void DMDUTILCALLBACK LogCallback(const char* format, va_list args)
{
Expand Down Expand Up @@ -89,11 +90,14 @@ int main(int argc, const char* argv[])
DMDUtil::LevelDMD* pLevelDMD196_4;
DMDUtil::RGB24DMD* pRGB24DMD128;
DMDUtil::RGB24DMD* pRGB24DMD196;
DMDUtil::ConsoleDMD* pConsoleDMD;

int ms = 200;
for (int i = 0; i < 4; i++)
{
if (i == 0) pConsoleDMD = pDmd->CreateConsoleDMD();
if (i == 0) pLevelDMD128_2 = pDmd->CreateLevelDMD(128, 32, 2);
if (i == 1) pDmd->DestroyConsoleDMD(pConsoleDMD);
if (i == 1) pLevelDMD128_4 = pDmd->CreateLevelDMD(128, 32, 4);
if (i == 2) pLevelDMD196_4 = pDmd->CreateLevelDMD(192, 64, 4);
if (i == 3) pDmd->DestroyLevelDMD(pLevelDMD128_2);
Expand Down

0 comments on commit f73c1ec

Please sign in to comment.