Added led matrix
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file ledmatrix.cpp
|
||||
/// \brief Description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include "ledmatrix.h"
|
||||
#include "logger.h"
|
||||
#include "string.h"
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
SemaphoreHandle_t LEDMatrix::taskSemaphore = 0;
|
||||
TaskHandle_t LEDMatrix::matrixTaskHandle = NULL;
|
||||
|
||||
LEDMatrix::LEDMatrix(LEDMatrix_Parameters_t* parameters)
|
||||
{
|
||||
// Take the parameters
|
||||
LEDMatrix::parameters = *parameters;
|
||||
// Calculate the total number of pixels
|
||||
LEDMatrix::numberOfPixels = parameters->width * parameters->height;
|
||||
// Create the matrix storage
|
||||
matrix = new LEDMatrix_Pixel_t[parameters->width * parameters->height];
|
||||
// Create the second matrix, that is actually transmitted to the periphery
|
||||
|
||||
// Reset the matrix to all 0 (all LEDs off)
|
||||
// memset(&matrix, 0, sizeof(matrix));
|
||||
|
||||
// Create the task Semaphore
|
||||
vSemaphoreCreateBinary(LEDMatrix::taskSemaphore);
|
||||
// Create the matrix task
|
||||
xTaskCreate(matrixTask, (const char*)"matrixTask", 4096, this, 3, &LEDMatrix::matrixTaskHandle);
|
||||
|
||||
for (int i = 0; i < numberOfPixels; i++)
|
||||
{
|
||||
matrix[i].on = false;
|
||||
matrix[i].green = 0x1F;
|
||||
}
|
||||
|
||||
LOGGER_DEBUG("---- %i -----", matrix[0].green);
|
||||
|
||||
}
|
||||
|
||||
void LEDMatrix::setPixelValue(unsigned int row, unsigned int colum, bool value)
|
||||
{
|
||||
unsigned int rowC = 0;
|
||||
unsigned int colC = 0;
|
||||
unsigned int pixelAddress = 0;
|
||||
|
||||
// Determine the actual row coordinate based on the matrix orientation
|
||||
if (parameters.columOrientation == LEDMATRIX_ORIENTATION_COLUM_UP_DOWN)
|
||||
{
|
||||
rowC = row;
|
||||
}
|
||||
else if (parameters.columOrientation == LEDMATRIX_ORIENTATION_COLUM_DOWN_UP)
|
||||
{
|
||||
rowC = parameters.height - row;
|
||||
}
|
||||
|
||||
// Determine the actual row coordinate based on the matrix orientation
|
||||
if (parameters.rowOrientation == LEDMATRIX_ORIENTATION_ROW_LEFT_RIGHT)
|
||||
{
|
||||
colC = colum;
|
||||
}
|
||||
else if (parameters.rowOrientation == LEDMATRIX_ORIENTATION_ROW_RIGHT_LEFT)
|
||||
{
|
||||
colC = parameters.width - colum;
|
||||
}
|
||||
|
||||
// Calculate the pixel address in the pixel array based on the previous information
|
||||
if (parameters.matrixOrientation == LEDMATRIX_ORIENTATION_COLUM)
|
||||
{
|
||||
pixelAddress = (rowC - 1) * (colC + 1);
|
||||
}
|
||||
|
||||
LOGGER_DEBUG("Calculated address: %i (%i:%i)", pixelAddress, rowC, colC);
|
||||
// Update the pixel value
|
||||
matrix[pixelAddress].on = value;
|
||||
|
||||
|
||||
|
||||
// Release the semaphore to trigger an matrix update
|
||||
xSemaphoreGive(LEDMatrix::taskSemaphore);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LEDMatrix::matrixTask(void* parameters)
|
||||
{
|
||||
LEDMatrix* ledmatrix = (LEDMatrix*)parameters;
|
||||
|
||||
uint8_t matrix[ledmatrix->numberOfPixels * 3];
|
||||
memset(&matrix, 0, sizeof(matrix));
|
||||
|
||||
while(1)
|
||||
{
|
||||
xSemaphoreTake(ledmatrix->taskSemaphore, 1000);
|
||||
for (int i = 0; i < ledmatrix->numberOfPixels; i++)
|
||||
{
|
||||
if (ledmatrix->matrix[i].on)
|
||||
{
|
||||
matrix[i * 3 + 0] = ledmatrix->matrix[i].red;
|
||||
matrix[i * 3 + 1] = ledmatrix->matrix[i].green;
|
||||
matrix[i * 3 + 2] = ledmatrix->matrix[i].blue;
|
||||
}
|
||||
else
|
||||
{
|
||||
matrix[i * 3 + 0] = 0;
|
||||
matrix[i * 3 + 1] = 0;
|
||||
matrix[i * 3 + 2] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
LOGGER_DEBUG("HERE AGAIN");
|
||||
// Re-draw the matrix every 10 seconds anyhow
|
||||
rmt_transmit(*ledmatrix->parameters.rmtChannel, *ledmatrix->parameters.rmtEncoder, matrix, sizeof(matrix), ledmatrix->parameters.rmtConfig);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user