diff --git a/src/ZeDMD.cpp b/src/ZeDMD.cpp index 6083515..eaa3e11 100644 --- a/src/ZeDMD.cpp +++ b/src/ZeDMD.cpp @@ -163,19 +163,19 @@ void ZeDMD::DisableUpscaling() void ZeDMD::SetWiFiSSID(const char *const ssid) { int size = strlen(ssid); - uint8_t data[129] = {0}; + uint8_t data[33] = {0}; data[0] = (uint8_t)size; memcpy(&data[1], (uint8_t *)ssid, size); - m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiSSID, data, 129); + m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiSSID, data, size + 1); } void ZeDMD::SetWiFiPassword(const char *const password) { int size = strlen(password); - uint8_t data[129] = {0}; + uint8_t data[33] = {0}; data[0] = (uint8_t)size; memcpy(&data[1], (uint8_t *)password, size); - m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiPassword, data, 129); + m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiPassword, data, size + 1); } void ZeDMD::SetWiFiPort(int port) diff --git a/src/ZeDMDComm.cpp b/src/ZeDMDComm.cpp index 6c9bf9a..91b4d34 100644 --- a/src/ZeDMDComm.cpp +++ b/src/ZeDMDComm.cpp @@ -54,35 +54,61 @@ void ZeDMDComm::Run() if (m_frames.empty()) { - m_delayedFrameMutex.lock(); - if (m_delayedFrameReady) { + m_delayedFrameMutex.lock(); + if (m_delayedFrameReady) { while (m_delayedFrames.size() > 0) { - m_frames.push(m_delayedFrames.front()); + ZeDMDFrame frame = m_delayedFrames.front(); + lastStreamId = frame.streamId; + m_frames.push(frame); m_delayedFrames.pop(); } m_delayedFrameReady = false; + m_frameCounter = 1; m_delayedFrameMutex.unlock(); m_frameQueueMutex.unlock(); continue; - } + } m_delayedFrameMutex.unlock(); m_frameQueueMutex.unlock(); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); continue; } + else + { + bool delay = false; + m_delayedFrameMutex.lock(); + delay = m_delayedFrameReady; + m_delayedFrameMutex.unlock(); + + if (delay && m_frameCounter > 2) { + while (m_frames.size() > 0) + { + m_frames.pop(); + } + m_frameCounter = 0; + m_frameQueueMutex.unlock(); + continue; + } + } ZeDMDFrame frame = m_frames.front(); m_frames.pop(); - if (frame.streamId > 0) + if (frame.streamId != -1) { if (frame.streamId != lastStreamId) { + if (lastStreamId != -1) + { + m_frameCounter--; + } + lastStreamId = frame.streamId; - m_frameCounter--; } } + else { + m_frameCounter--; + } m_frameQueueMutex.unlock(); bool success = StreamBytes(&frame); @@ -126,7 +152,7 @@ void ZeDMDComm::QueueCommand(char command, uint8_t *data, int size, int8_t strea } // delayed standard frame - if (streamId == -1 && GetQueuedFramesCount() > ZEDMD_COMM_FRAME_QUEUE_SIZE_MAX) + if (streamId == -1 && FillDelayed()) { m_delayedFrameMutex.lock(); while (m_delayedFrames.size() > 0) @@ -136,6 +162,7 @@ void ZeDMDComm::QueueCommand(char command, uint8_t *data, int size, int8_t strea m_delayedFrames.push(frame); m_delayedFrameReady = true; m_delayedFrameMutex.unlock(); + m_lastStreamId = -1; } // delayed streamed zones else if (streamId != -1 && delayed) @@ -143,6 +170,7 @@ void ZeDMDComm::QueueCommand(char command, uint8_t *data, int size, int8_t strea m_delayedFrameMutex.lock(); m_delayedFrames.push(frame); m_delayedFrameMutex.unlock(); + m_lastStreamId = streamId; } else { @@ -176,11 +204,11 @@ void ZeDMDComm::QueueCommand(char command, uint8_t *data, int size, uint16_t wid if (++m_streamId > 64) { - m_streamId = 1; + m_streamId = 0; } bool delayed = false; - if (GetQueuedFramesCount() > ZEDMD_COMM_FRAME_QUEUE_SIZE_MAX) + if (FillDelayed()) { delayed = true; m_delayedFrameMutex.lock(); @@ -190,6 +218,8 @@ void ZeDMDComm::QueueCommand(char command, uint8_t *data, int size, uint16_t wid m_delayedFrames.pop(); } m_delayedFrameMutex.unlock(); + // A delayed frame needs to be complete. + memset(m_zoneHashes, 0, 128); } for (uint16_t y = 0; y < height; y += m_zoneHeight) @@ -233,13 +263,15 @@ void ZeDMDComm::QueueCommand(char command, uint8_t *data, int size, uint16_t wid } } -uint8_t ZeDMDComm::GetQueuedFramesCount() +bool ZeDMDComm::FillDelayed() { uint8_t count = 0; + bool delayed = false; m_frameQueueMutex.lock(); count = m_frameCounter; + delayed = m_delayedFrameReady; m_frameQueueMutex.unlock(); - return count; + return (count > ZEDMD_COMM_FRAME_QUEUE_SIZE_MAX) || delayed; } void ZeDMDComm::IgnoreDevice(const char *ignore_device) diff --git a/src/ZeDMDComm.h b/src/ZeDMDComm.h index deb4ec9..685f63f 100644 --- a/src/ZeDMDComm.h +++ b/src/ZeDMDComm.h @@ -75,7 +75,7 @@ struct ZeDMDFrame #endif #define ZEDMD_COMM_FRAME_SIZE_COMMAND_LIMIT 10 -#define ZEDMD_COMM_FRAME_QUEUE_SIZE_MAX 4 +#define ZEDMD_COMM_FRAME_QUEUE_SIZE_MAX 8 #ifdef __ANDROID__ typedef void *(*ZeDMD_AndroidGetJNIEnvFunc)(); @@ -110,7 +110,7 @@ class ZeDMDComm void QueueCommand(char command, uint8_t *buffer, int size, int8_t streamId = -1, bool delayed = false); void QueueCommand(char command); void QueueCommand(char command, uint8_t value); - uint8_t GetQueuedFramesCount(); + bool FillDelayed(); uint16_t GetWidth(); uint16_t GetHeight();