Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading #7

Merged
merged 21 commits into from
Feb 18, 2024
Merged
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ set(CMAKE_C_VISIBILITY_PRESET hidden)
set(DMDUTIL_SOURCES
src/Config.cpp
src/DMD.cpp
src/VirtualDMD.cpp
src/LevelDMD.cpp
src/RGB24DMD.cpp
src/Logger.cpp
src/AlphaNumeric.cpp
src/FrameUtil.cpp
Expand Down
118 changes: 69 additions & 49 deletions include/DMDUtil/DMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
#define DMDUTILCALLBACK
#endif

#define DMDUTIL_FRAME_BUFFER_SIZE 16
#define DMDUTIL_MAX_NAME_SIZE 32

#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <mutex>
#include <queue>
#include <shared_mutex>
#include <string>
#include <thread>

Expand Down Expand Up @@ -47,89 +53,103 @@ enum class AlphaNumericLayout
class AlphaNumeric;
class Serum;
class PixelcadeDMD;
class VirtualDMD;
class LevelDMD;
class RGB24DMD;

class DMDUTILAPI DMD
{
public:
DMD(int width, int height, bool sam = false, const char* name = nullptr);
DMD();
~DMD();

void FindDisplays();
static bool IsFinding();
bool HasDisplay() const;
int GetWidth() const { return m_width; }
int GetHeight() const { return m_height; }
int GetLength() const { return m_length; }
VirtualDMD* CreateVirtualDMD();
bool DestroyVirtualDMD(VirtualDMD* pVirtualDMD);
void UpdateData(const uint8_t* pData, int depth, uint8_t r, uint8_t g, uint8_t b);
void UpdateRGB24Data(const uint8_t* pData, int depth, uint8_t r, uint8_t g, uint8_t b);
void DumpDMDTxt();
void DumpDMDRaw();
LevelDMD* CreateLevelDMD(uint16_t width, uint16_t height, bool sam);
bool DestroyLevelDMD(LevelDMD* pLevelDMD);
RGB24DMD* CreateRGB24DMD(uint16_t width, uint16_t height);
bool DestroyRGB24DMD(RGB24DMD* pRGB24DMD);
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,
uint8_t b);
void UpdateRGB24Data(const uint8_t* pData, uint16_t width, uint16_t height);
void UpdateRGB16Data(const uint16_t* pData, uint16_t width, uint16_t height);
void UpdateAlphaNumericData(AlphaNumericLayout layout, const uint16_t* pData1, const uint16_t* pData2, uint8_t r,
uint8_t g, uint8_t b);
uint8_t g, uint8_t b, const char* name = nullptr);

private:
enum class DmdMode
enum class DMDMode
{
Unknown,
Data,
RGB24,
RGB24, // RGB888
RGB16, // RGB565
AlphaNumeric
};

struct DMDUpdate
{
DmdMode mode;
DMDMode mode;
AlphaNumericLayout layout;
int depth;
void* pData;
void* pData2;
uint8_t data[256 * 64 * 3];
uint16_t segData[256 * 64]; // RGB16 or segment data
uint16_t segData2[128];
bool hasData;
bool hasSegData;
bool hasSegData2;
uint8_t r;
uint8_t g;
uint8_t b;
uint16_t width;
uint16_t height;
char name[DMDUTIL_MAX_NAME_SIZE];
};

static constexpr uint8_t LEVELS_WPC[] = {0x14, 0x21, 0x43, 0x64};
static constexpr uint8_t LEVELS_GTS3[] = {0x00, 0x1E, 0x23, 0x28, 0x2D, 0x32, 0x37, 0x3C,
0x41, 0x46, 0x4B, 0x50, 0x55, 0x5A, 0x5F, 0x64};
static constexpr uint8_t LEVELS_SAM[] = {0x00, 0x14, 0x19, 0x1E, 0x23, 0x28, 0x2D, 0x32,
0x37, 0x3C, 0x41, 0x46, 0x4B, 0x50, 0x5A, 0x64};

void FindDevices();
void Run();
void Stop();
bool UpdatePalette(const DMDUpdate* pUpdate);
void UpdateData(const DMDUpdate* pUpdate, bool update);
void UpdateRGB24Data(const DMDUpdate* pUpdate, bool update);
void UpdateAlphaNumericData(const DMDUpdate* pUpdate, bool update);

int m_width;
int m_height;
int m_length;
bool m_sam;
uint8_t* m_pBuffer;
uint8_t* m_pRGB24Buffer;
uint16_t m_segData1[128];
uint16_t m_segData2[128];
uint8_t* m_pLevelData;
uint8_t* m_pRGB24Data;
uint16_t* m_pRGB565Data;
uint8_t m_palette[192];
DMDUpdate* m_updateBuffer[DMDUTIL_FRAME_BUFFER_SIZE];

bool UpdatePalette(uint8_t* pPalette, uint8_t depth, uint8_t r, uint8_t g, uint8_t b);
void UpdateData(const uint8_t* pData, int depth, uint16_t width, uint16_t height, uint8_t r, uint8_t g, uint8_t b,
DMDMode node, const char* name);
void AdjustRGB24Depth(uint8_t* pData, uint8_t* pDstData, int length, uint8_t* palette, uint8_t depth);

void DmdFrameReadyResetThread();
void LevelDMDThread();
void RGB24DMDThread();
void ZeDMDThread();
void DumpDMDTxtThread();
void DumpDMDRawThread();

uint8_t m_updateBufferPosition = 0;
AlphaNumeric* m_pAlphaNumeric;
Serum* m_pSerum;
ZeDMD* m_pZeDMD;
std::vector<LevelDMD*> m_levelDMDs;
std::vector<RGB24DMD*> m_rgb24DMDs;

std::thread* m_pLevelDMDThread;
std::thread* m_pRGB24DMDThread;
std::thread* m_pZeDMDThread;
std::thread* m_pdmdFrameReadyResetThread;
std::thread* m_pDumpDMDTxtThread;
std::thread* m_pDumpDMDRawThread;
std::shared_mutex m_dmdSharedMutex;
std::condition_variable_any m_dmdCV;
std::atomic<bool> m_dmdFrameReady = false;
std::atomic<bool> m_stopFlag = false;

static bool m_finding;

#if !( \
(defined(__APPLE__) && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_TV) && TARGET_OS_TV))) || \
defined(__ANDROID__))
void PixelcadeDMDThread();
PixelcadeDMD* m_pPixelcadeDMD;
std::thread* m_pPixelcadeDMDThread;
#endif
std::vector<VirtualDMD*> m_virtualDMDs;

std::thread* m_pThread;
std::queue<DMDUpdate*> m_updates;
std::mutex m_mutex;
bool m_running;

static bool m_finding;
};

} // namespace DMDUtil
5 changes: 3 additions & 2 deletions include/DMDUtil/DMDUtil.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#define DMDUTIL_VERSION_MAJOR 0 // X Digits
#define DMDUTIL_VERSION_MINOR 2 // Max 2 Digits
#define DMDUTIL_VERSION_MINOR 3 // Max 2 Digits
#define DMDUTIL_VERSION_PATCH 0 // Max 2 Digits

#define _DMDUTIL_STR(x) #x
Expand All @@ -13,4 +13,5 @@

#include "Config.h"
#include "DMD.h"
#include "VirtualDMD.h"
#include "LevelDMD.h"
#include "RGB24DMD.h"
46 changes: 46 additions & 0 deletions include/DMDUtil/LevelDMD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#ifdef _MSC_VER
#define DMDUTILAPI __declspec(dllexport)
#define DMDUTILCALLBACK __stdcall
#else
#define DMDUTILAPI __attribute__((visibility("default")))
#define DMDUTILCALLBACK
#endif

#include <cstdint>

namespace DMDUtil
{

class DMDUTILAPI LevelDMD
{
public:
LevelDMD(uint16_t width, uint16_t height, bool sam);
~LevelDMD();

void Update(uint8_t* pLevelData, uint8_t depth);
int GetWidth() { return m_width; }
int GetHeight() { return m_height; }
int GetLength() const { return m_length; }
int GetPitch() const { return m_pitch; }
uint8_t* GetData();

private:
static constexpr uint8_t LEVELS_WPC[] = {0x14, 0x21, 0x43, 0x64};
static constexpr uint8_t LEVELS_GTS3[] = {0x00, 0x1E, 0x23, 0x28, 0x2D, 0x32, 0x37, 0x3C,
0x41, 0x46, 0x4B, 0x50, 0x55, 0x5A, 0x5F, 0x64};
static constexpr uint8_t LEVELS_SAM[] = {0x00, 0x14, 0x19, 0x1E, 0x23, 0x28, 0x2D, 0x32,
0x37, 0x3C, 0x41, 0x46, 0x4B, 0x50, 0x5A, 0x64};

uint16_t m_width;
uint16_t m_height;
int m_length;
int m_pitch;
int m_update;
bool m_sam;

uint8_t* m_pData;
};

} // namespace DMDUtil
18 changes: 8 additions & 10 deletions include/DMDUtil/VirtualDMD.h → include/DMDUtil/RGB24DMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@
namespace DMDUtil
{

class DMDUTILAPI VirtualDMD
class DMDUTILAPI RGB24DMD
{
public:
VirtualDMD(int width, int height);
~VirtualDMD();
RGB24DMD(uint16_t width, uint16_t height);
~RGB24DMD();

void Update(uint8_t* pLevelData, uint8_t* pRGB24Data);
void Update(uint8_t* pRGB24Data);
int GetWidth() { return m_width; }
int GetHeight() { return m_height; }
int GetLength() const { return m_length; }
int GetPitch() const { return m_pitch; }
uint8_t* GetLevelData();
uint8_t* GetRGB24Data();
uint8_t* GetData();

private:
int m_width;
int m_height;
uint16_t m_width;
uint16_t m_height;
int m_length;
int m_pitch;
int m_update;

uint8_t* m_pLevelData;
uint8_t* m_pRGB24Data;
uint8_t* m_pData;
};

} // namespace DMDUtil
2 changes: 1 addition & 1 deletion platforms/android/arm64-v8a/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

if [[ $(uname) == "Linux" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion platforms/ios/arm64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

NUM_PROCS=$(sysctl -n hw.ncpu)
Expand Down
2 changes: 1 addition & 1 deletion platforms/linux/aarch64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

NUM_PROCS=$(nproc)
Expand Down
2 changes: 1 addition & 1 deletion platforms/linux/x64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

NUM_PROCS=$(nproc)
Expand Down
2 changes: 1 addition & 1 deletion platforms/macos/arm64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

NUM_PROCS=$(sysctl -n hw.ncpu)
Expand Down
2 changes: 1 addition & 1 deletion platforms/macos/x64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

NUM_PROCS=$(sysctl -n hw.ncpu)
Expand Down
2 changes: 1 addition & 1 deletion platforms/tvos/arm64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

NUM_PROCS=$(sysctl -n hw.ncpu)
Expand Down
2 changes: 1 addition & 1 deletion platforms/win/x64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

echo "Building libraries..."
Expand Down
2 changes: 1 addition & 1 deletion platforms/win/x86/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

LIBZEDMD_SHA=b2190a3efaa52d705c9a5c62f00b418376a2604d
LIBZEDMD_SHA=7d2b0fc39475940b61b0126f3ff308dd193fe2a8
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5

echo "Building libraries..."
Expand Down
Loading