diff options
| author | mrfaptastic <12006953+mrfaptastic@users.noreply.github.com> | 2023-03-18 20:12:45 +0000 |
|---|---|---|
| committer | mrfaptastic <12006953+mrfaptastic@users.noreply.github.com> | 2023-03-18 20:12:45 +0000 |
| commit | 9d36abeeafbcdefda4fb9bfbf28c5af3276d297e (patch) | |
| tree | ed040e9328b810fade01a3c31510ddf951fba025 /src | |
| parent | 9308b59445e54409c8fb32f23bb7c55d656f36dc (diff) | |
Reduce #338
Diffstat (limited to 'src')
| -rw-r--r-- | src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp | 1137 | ||||
| -rw-r--r-- | src/ESP32-HUB75-MatrixPanel-I2S-DMA.h | 1389 | ||||
| -rw-r--r-- | src/platforms/esp32/esp32_i2s_parallel_dma.cpp | 110 | ||||
| -rw-r--r-- | src/platforms/esp32/esp32_i2s_parallel_dma.hpp | 7 | ||||
| -rw-r--r-- | src/platforms/esp32s3/gdma_lcd_parallel16.cpp | 6 | ||||
| -rw-r--r-- | src/platforms/esp32s3/gdma_lcd_parallel16.hpp | 2 |
6 files changed, 1309 insertions, 1342 deletions
diff --git a/src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp b/src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp index 694150e..92d7ac3 100644 --- a/src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp +++ b/src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp @@ -1,283 +1,293 @@ #include "ESP32-HUB75-MatrixPanel-I2S-DMA.h" #if defined(SPIRAM_DMA_BUFFER) - // Sprite_TM saves the day again... - // https://www.esp32.com/viewtopic.php?f=2&t=30584 - #include "rom/cache.h" -#endif +// Sprite_TM saves the day again... +// https://www.esp32.com/viewtopic.php?f=2&t=30584 +#include "rom/cache.h" +#endif -/* This replicates same function in rowBitStruct, but due to induced inlining it might be MUCH faster - * when used in tight loops while method from struct could be flushed out of instruction cache between +/* This replicates same function in rowBitStruct, but due to induced inlining it might be MUCH faster + * when used in tight loops while method from struct could be flushed out of instruction cache between * loop cycles do NOT forget about buff_id param if using this. */ -#define getRowDataPtr(row, _dpth, buff_id) &(dma_buff.rowBits[row]->data[_dpth * dma_buff.rowBits[row]->width + buff_id*(dma_buff.rowBits[row]->width * dma_buff.rowBits[row]->colour_depth)]) +// #define getRowDataPtr(row, _dpth, buff_id) &(dma_buff.rowBits[row]->data[_dpth * dma_buff.rowBits[row]->width + buff_id*(dma_buff.rowBits[row]->width * dma_buff.rowBits[row]->colour_depth)]) + +// BufferID is now ignored, seperate global pointer pointer! +#define getRowDataPtr(row, _dpth, buff_id) &(fb->rowBits[row]->data[_dpth * fb->rowBits[row]->width]) /* We need to update the correct uint16_t in the rowBitStruct array, that gets sent out in parallel * 16 bit parallel mode - Save the calculated value to the bitplane memory in reverse order to account for I2S Tx FIFO mode1 ordering * Irrelevant for ESP32-S2 the way the FIFO ordering works is different - refer to page 679 of S2 technical reference manual */ -#if defined (ESP32_THE_ORIG) - #define ESP32_TX_FIFO_POSITION_ADJUST(x_coord) (((x_coord) & 1U) ? (x_coord-1):(x_coord+1)) -#else - #define ESP32_TX_FIFO_POSITION_ADJUST(x_coord) x_coord -#endif +#if defined(ESP32_THE_ORIG) +#define ESP32_TX_FIFO_POSITION_ADJUST(x_coord) (((x_coord)&1U) ? (x_coord - 1) : (x_coord + 1)) +#else +#define ESP32_TX_FIFO_POSITION_ADJUST(x_coord) x_coord +#endif -/* This library is designed to take an 8 bit / 1 byte value (0-255) for each R G B colour sub-pixel. +/* This library is designed to take an 8 bit / 1 byte value (0-255) for each R G B colour sub-pixel. * The PIXEL_COLOR_DEPTH_BITS should always be '8' as a result. * However, if the library is to be used with lower colour depth (i.e. 6 bit colour), then we need to ensure the 8-bit value passed to the colour masking * is adjusted accordingly to ensure the LSB's are shifted left to MSB, by the difference. Otherwise the colours will be all screwed up. */ -//#if PIXEL_COLOR_DEPTH_BITS > 12 -// #error "Color depth bits cannot be greater than 12." -//#elif PIXEL_COLOR_DEPTH_BITS < 2 -// #error "Color depth bits cannot be less than 2." -//#endif +// #if PIXEL_COLOR_DEPTH_BITS > 12 +// #error "Color depth bits cannot be greater than 12." +// #elif PIXEL_COLOR_DEPTH_BITS < 2 +// #error "Color depth bits cannot be less than 2." +// #endif - //#define MASK_OFFSET (16 - PIXEL_COLOR_DEPTH_BITS) - //#define PIXEL_COLOR_MASK_BIT(color_depth_index) (1 << (color_depth_index + MASK_OFFSET)) - #define PIXEL_COLOR_MASK_BIT(color_depth_index, mask_offset) (1 << (color_depth_index + mask_offset)) - //static constexpr uint8_t const MASK_OFFSET = 8-PIXEL_COLOUR_DEPTH_BITS; +// #define MASK_OFFSET (16 - PIXEL_COLOR_DEPTH_BITS) +// #define PIXEL_COLOR_MASK_BIT(color_depth_index) (1 << (color_depth_index + MASK_OFFSET)) +#define PIXEL_COLOR_MASK_BIT(color_depth_index, mask_offset) (1 << (color_depth_index + mask_offset)) +// static constexpr uint8_t const MASK_OFFSET = 8-PIXEL_COLOUR_DEPTH_BITS; /* #if PIXEL_COLOR_DEPTH_BITS < 8 uint8_t mask = (1 << (colour_depth_idx+MASK_OFFSET)); // expect 24 bit colour (8 bits per RGB subpixel) #else uint8_t mask = (1 << (colour_depth_idx)); // expect 24 bit colour (8 bits per RGB subpixel) - #endif + #endif */ - bool MatrixPanel_I2S_DMA::allocateDMAmemory() { - ESP_LOGI("I2S-DMA", "Free heap: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL)); - ESP_LOGI("I2S-DMA", "Free SPIRAM: %d", heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); - + ESP_LOGI("I2S-DMA", "Free heap: %d", heap_caps_get_free_size(MALLOC_CAP_INTERNAL)); + ESP_LOGI("I2S-DMA", "Free SPIRAM: %d", heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); // Alright, theoretically we should be OK, so let us do this, so // lets allocate a chunk of memory for each row (a row could span multiple panels if chaining is in place) - dma_buff.rowBits.reserve(ROWS_PER_FRAME); + ESP_LOGI("I2S-DMA", "allocating rowBitStructs with pixel_color_depth_bits of %d", m_cfg.getPixelColorDepthBits()); + // iterate through number of rows, allocate memory for each + size_t allocated_fb_memory = 0; - ESP_LOGI("I2S-DMA", "allocating rowBitStructs with pixel_color_depth_bits of %d", m_cfg.getPixelColorDepthBits()); - // iterate through number of rows, allocate memory for each - size_t allocated_fb_memory = 0; - for (int malloc_num =0; malloc_num < ROWS_PER_FRAME; ++malloc_num) + int fbs_required = (m_cfg.double_buff) ? 2 : 1; + for (int fb = 0; fb < fbs_required; fb++) + { + frame_buffer[fb].rowBits.reserve(ROWS_PER_FRAME); + + for (int malloc_num = 0; malloc_num < ROWS_PER_FRAME; ++malloc_num) { - auto ptr = std::make_shared<rowBitStruct>(PIXELS_PER_ROW, m_cfg.getPixelColorDepthBits(), m_cfg.double_buff); + auto ptr = std::make_shared<rowBitStruct>(PIXELS_PER_ROW, m_cfg.getPixelColorDepthBits(), m_cfg.double_buff); - if (ptr->data == nullptr) - { - ESP_LOGE("I2S-DMA", "CRITICAL ERROR: Not enough memory for requested colour depth! Please reduce pixel_color_depth_bits value.\r\n"); - ESP_LOGE("I2S-DMA", "Could not allocate rowBitStruct %d!.\r\n", malloc_num); + if (ptr->data == nullptr) + { + ESP_LOGE("I2S-DMA", "CRITICAL ERROR: Not enough memory for requested colour depth! Please reduce pixel_color_depth_bits value.\r\n"); + ESP_LOGE("I2S-DMA", "Could not allocate rowBitStruct %d!.\r\n", malloc_num); - return false; - // TODO: should we release all previous rowBitStructs here??? - } + return false; + // TODO: should we release all previous rowBitStructs here??? + } - allocated_fb_memory += ptr->size(); - dma_buff.rowBits.emplace_back(ptr); // save new rowBitStruct into rows vector - ++dma_buff.rows; + allocated_fb_memory += ptr->size(); + frame_buffer[fb].rowBits.emplace_back(ptr); // save new rowBitStruct into rows vector + ++frame_buffer[fb].rows; } - ESP_LOGI("I2S-DMA", "Allocating %d bytes memory for DMA BCM framebuffer(s).", allocated_fb_memory); - - // calculate the lowest LSBMSB_TRANSITION_BIT value that will fit in memory that will meet or exceed the configured refresh rate -#if !defined(FORCE_COLOR_DEPTH) + } + ESP_LOGI("I2S-DMA", "Allocating %d bytes memory for DMA BCM framebuffer(s).", allocated_fb_memory); - ESP_LOGI("I2S-DMA", "Minimum visual refresh rate (scan rate from panel top to bottom) requested: %d Hz", m_cfg.min_refresh_rate); + // calculate the lowest LSBMSB_TRANSITION_BIT value that will fit in memory that will meet or exceed the configured refresh rate +#if !defined(FORCE_COLOR_DEPTH) - while(1) { - int psPerClock = 1000000000000UL/m_cfg.i2sspeed; - int nsPerLatch = ((PIXELS_PER_ROW + CLKS_DURING_LATCH) * psPerClock) / 1000; + ESP_LOGI("I2S-DMA", "Minimum visual refresh rate (scan rate from panel top to bottom) requested: %d Hz", m_cfg.min_refresh_rate); - // add time to shift out LSBs + LSB-MSB transition bit - this ignores fractions... - int nsPerRow = m_cfg.getPixelColorDepthBits() * nsPerLatch; + while (1) + { + int psPerClock = 1000000000000UL / m_cfg.i2sspeed; + int nsPerLatch = ((PIXELS_PER_ROW + CLKS_DURING_LATCH) * psPerClock) / 1000; - // add time to shift out MSBs - for(int i=lsbMsbTransitionBit + 1; i < m_cfg.getPixelColorDepthBits(); i++) - nsPerRow += (1<<(i - lsbMsbTransitionBit - 1)) * (m_cfg.getPixelColorDepthBits() - i) * nsPerLatch; + // add time to shift out LSBs + LSB-MSB transition bit - this ignores fractions... + int nsPerRow = m_cfg.getPixelColorDepthBits() * nsPerLatch; - int nsPerFrame = nsPerRow * ROWS_PER_FRAME; - int actualRefreshRate = 1000000000UL/(nsPerFrame); - calculated_refresh_rate = actualRefreshRate; + // add time to shift out MSBs + for (int i = lsbMsbTransitionBit + 1; i < m_cfg.getPixelColorDepthBits(); i++) + nsPerRow += (1 << (i - lsbMsbTransitionBit - 1)) * (m_cfg.getPixelColorDepthBits() - i) * nsPerLatch; - ESP_LOGW("I2S-DMA", "lsbMsbTransitionBit of %d gives %d Hz refresh rate.", lsbMsbTransitionBit, actualRefreshRate); + int nsPerFrame = nsPerRow * ROWS_PER_FRAME; + int actualRefreshRate = 1000000000UL / (nsPerFrame); + calculated_refresh_rate = actualRefreshRate; + + ESP_LOGW("I2S-DMA", "lsbMsbTransitionBit of %d gives %d Hz refresh rate.", lsbMsbTransitionBit, actualRefreshRate); + + if (actualRefreshRate > m_cfg.min_refresh_rate) + break; + + if (lsbMsbTransitionBit < m_cfg.getPixelColorDepthBits() - 1) + lsbMsbTransitionBit++; + else + break; + } + + if (lsbMsbTransitionBit > 0) + { + ESP_LOGW("I2S-DMA", "lsbMsbTransitionBit of %d used to achieve refresh rate of %d Hz. Percieved colour depth to the eye may be reduced.", lsbMsbTransitionBit, m_cfg.min_refresh_rate); + } - if (actualRefreshRate > m_cfg.min_refresh_rate) - break; - - if(lsbMsbTransitionBit < m_cfg.getPixelColorDepthBits() - 1) - lsbMsbTransitionBit++; - else - break; - } - - if ( lsbMsbTransitionBit > 0 ) - { - ESP_LOGW("I2S-DMA", "lsbMsbTransitionBit of %d used to achieve refresh rate of %d Hz. Percieved colour depth to the eye may be reduced.", lsbMsbTransitionBit, m_cfg.min_refresh_rate); - } - ESP_LOGI("I2S-DMA", "DMA has pixel_color_depth_bits of %d", m_cfg.getPixelColorDepthBits() - lsbMsbTransitionBit); #endif - /*** * Step 2a: lsbMsbTransition bit is now finalised - recalculate the DMA descriptor count required, which is used for * memory allocation of the DMA linked list memory structure. - */ - int numDMAdescriptorsPerRow = 1; - for(int i=lsbMsbTransitionBit + 1; i < m_cfg.getPixelColorDepthBits(); i++) { - numDMAdescriptorsPerRow += (1<<(i - lsbMsbTransitionBit - 1)); - } + */ + int numDMAdescriptorsPerRow = 1; + for (int i = lsbMsbTransitionBit + 1; i < m_cfg.getPixelColorDepthBits(); i++) + { + numDMAdescriptorsPerRow += (1 << (i - lsbMsbTransitionBit - 1)); + } - ESP_LOGI("I2S-DMA", "Recalculated number of DMA descriptors per row: %d", numDMAdescriptorsPerRow); + ESP_LOGI("I2S-DMA", "Recalculated number of DMA descriptors per row: %d", numDMAdescriptorsPerRow); - // Refer to 'DMA_LL_PAYLOAD_SPLIT' code in configureDMA() below to understand why this exists. - // numDMAdescriptorsPerRow is also used to calculate descount which is super important in i2s_parallel_config_t SoC DMA setup. - if ( dma_buff.rowBits[0]->size() > DMA_MAX ) - { - - ESP_LOGW("I2S-DMA", "rowBits struct is too large to fit in one DMA transfer payload, splitting required. Adding %d DMA descriptors\n", m_cfg.getPixelColorDepthBits()-1); + // Refer to 'DMA_LL_PAYLOAD_SPLIT' code in configureDMA() below to understand why this exists. + // numDMAdescriptorsPerRow is also used to calculate descount which is super important in i2s_parallel_config_t SoC DMA setup. + if (frame_buffer[0].rowBits[0]->size() > DMA_MAX) + { - numDMAdescriptorsPerRow += m_cfg.getPixelColorDepthBits()-1; - // Note: If numDMAdescriptorsPerRow is even just one descriptor too large, DMA linked list will not correctly loop. - } + ESP_LOGW("I2S-DMA", "rowBits struct is too large to fit in one DMA transfer payload, splitting required. Adding %d DMA descriptors\n", m_cfg.getPixelColorDepthBits() - 1); + numDMAdescriptorsPerRow += m_cfg.getPixelColorDepthBits() - 1; + // Note: If numDMAdescriptorsPerRow is even just one descriptor too large, DMA linked list will not correctly loop. + } /*** * Step 3: Allocate memory for DMA linked list, linking up each framebuffer row in sequence for GPIO output. - */ + */ - // malloc the DMA linked list descriptors that i2s_parallel will need - desccount = numDMAdescriptorsPerRow * ROWS_PER_FRAME; + // malloc the DMA linked list descriptors that i2s_parallel will need + desccount = numDMAdescriptorsPerRow * ROWS_PER_FRAME; - if (m_cfg.double_buff) - dma_bus.enable_double_dma_desc(); - - dma_bus.allocate_dma_desc_memory(desccount); + if (m_cfg.double_buff) + { + dma_bus.enable_double_dma_desc(); + } - // Just os we know - initialized = true; + dma_bus.allocate_dma_desc_memory(desccount); - return true; + // point FB we can write to, to 0 / dmadesc_a + fb = &frame_buffer[0]; -} // end allocateDMAmemory() + // Just os we know + initialized = true; + return true; +} // end allocateDMAmemory() -void MatrixPanel_I2S_DMA::configureDMA(const HUB75_I2S_CFG& _cfg) +void MatrixPanel_I2S_DMA::configureDMA(const HUB75_I2S_CFG &_cfg) { - // lldesc_t *previous_dmadesc_a = 0; - // lldesc_t *previous_dmadesc_b = 0; - int current_dmadescriptor_offset = 0; + // lldesc_t *previous_dmadesc_a = 0; + // lldesc_t *previous_dmadesc_b = 0; + int current_dmadescriptor_offset = 0; + + // HACK: If we need to split the payload in 1/2 so that it doesn't breach DMA_MAX, lets do it by the colour_depth. + int num_dma_payload_colour_depths = m_cfg.getPixelColorDepthBits(); + if (frame_buffer[0].rowBits[0]->size() > DMA_MAX) + { + num_dma_payload_colour_depths = 1; + } + + // Fill DMA linked lists for both frames (as in, halves of the HUB75 panel) and if double buffering is enabled, link it up for both buffers. + for (int row = 0; row < ROWS_PER_FRAME; row++) + { + // first set of data is LSB through MSB, single pass (IF TOTAL SIZE < DMA_MAX) - all colour bits are displayed once, which takes care of everything below and including LSBMSB_TRANSITION_BIT + // NOTE: size must be less than DMA_MAX - worst case for library: 16-bpp with 256 pixels per row would exceed this, need to break into two + // link_dma_desc(&dmadesc_a[current_dmadescriptor_offset], previous_dmadesc_a, dma_buff.rowBits[row]->getDataPtr(), dma_buff.rowBits[row]->size(num_dma_payload_colour_depths)); + // previous_dmadesc_a = &dmadesc_a[current_dmadescriptor_offset]; + + dma_bus.create_dma_desc_link(frame_buffer[0].rowBits[row]->getDataPtr(0, 0), frame_buffer[0].rowBits[row]->size(1), false); - // HACK: If we need to split the payload in 1/2 so that it doesn't breach DMA_MAX, lets do it by the colour_depth. - int num_dma_payload_colour_depths = m_cfg.getPixelColorDepthBits(); - if ( dma_buff.rowBits[0]->size() > DMA_MAX ) { - num_dma_payload_colour_depths = 1; + if (m_cfg.double_buff) + { + dma_bus.create_dma_desc_link(frame_buffer[1].rowBits[row]->getDataPtr(0, 1), frame_buffer[1].rowBits[row]->size(1), true); } - // Fill DMA linked lists for both frames (as in, halves of the HUB75 panel) and if double buffering is enabled, link it up for both buffers. - for(int row = 0; row < ROWS_PER_FRAME; row++) + current_dmadescriptor_offset++; + + // If the number of pixels per row is too great for the size of a DMA payload, so we need to split what we were going to send above. + if (frame_buffer[0].rowBits[0]->size() > DMA_MAX) { - // first set of data is LSB through MSB, single pass (IF TOTAL SIZE < DMA_MAX) - all colour bits are displayed once, which takes care of everything below and including LSBMSB_TRANSITION_BIT - // NOTE: size must be less than DMA_MAX - worst case for library: 16-bpp with 256 pixels per row would exceed this, need to break into two - //link_dma_desc(&dmadesc_a[current_dmadescriptor_offset], previous_dmadesc_a, dma_buff.rowBits[row]->getDataPtr(), dma_buff.rowBits[row]->size(num_dma_payload_colour_depths)); - // previous_dmadesc_a = &dmadesc_a[current_dmadescriptor_offset]; - dma_bus.create_dma_desc_link( dma_buff.rowBits[row]->getDataPtr(0, 0), dma_buff.rowBits[row]->size(num_dma_payload_colour_depths), false); + for (int cd = 1; cd < m_cfg.getPixelColorDepthBits(); cd++) + { + dma_bus.create_dma_desc_link(frame_buffer[0].rowBits[row]->getDataPtr(cd, 0), frame_buffer[0].rowBits[row]->size(1), false); - if (m_cfg.double_buff) + if (m_cfg.double_buff) { - dma_bus.create_dma_desc_link( dma_buff.rowBits[row]->getDataPtr(0, 1), dma_buff.rowBits[row]->size(num_dma_payload_colour_depths), true); + dma_bus.create_dma_desc_link(frame_buffer[1].rowBits[row]->getDataPtr(cd, 1), frame_buffer[1].rowBits[row]->size(1), true); } current_dmadescriptor_offset++; - // If the number of pixels per row is too great for the size of a DMA payload, so we need to split what we were going to send above. - if ( dma_buff.rowBits[0]->size() > DMA_MAX ) + } // additional linked list items + } // row depth struct + + for (int i = lsbMsbTransitionBit + 1; i < m_cfg.getPixelColorDepthBits(); i++) + { + // binary time division setup: we need 2 of bit (LSBMSB_TRANSITION_BIT + 1) four of (LSBMSB_TRANSITION_BIT + 2), etc + // because we sweep through to MSB each time, it divides the number of times we have to sweep in half (saving linked list RAM) + // we need 2^(i - LSBMSB_TRANSITION_BIT - 1) == 1 << (i - LSBMSB_TRANSITION_BIT - 1) passes from i to MSB + + for (int k = 0; k < (1 << (i - lsbMsbTransitionBit - 1)); k++) + { + dma_bus.create_dma_desc_link(frame_buffer[0].rowBits[row]->getDataPtr(i, 0), frame_buffer[0].rowBits[row]->size(1), false); + + if (m_cfg.double_buff) { - - for (int cd = 1; cd < m_cfg.getPixelColorDepthBits(); cd++) - { - dma_bus.create_dma_desc_link(dma_buff.rowBits[row]->getDataPtr(cd, 0), dma_buff.rowBits[row]->size(num_dma_payload_colour_depths), false); + dma_bus.create_dma_desc_link(frame_buffer[1].rowBits[row]->getDataPtr(i, 1), frame_buffer[1].rowBits[row]->size(1), true); + } - if (m_cfg.double_buff) { - dma_bus.create_dma_desc_link(dma_buff.rowBits[row]->getDataPtr(cd, 1), dma_buff.rowBits[row]->size(num_dma_payload_colour_depths), true); - } + current_dmadescriptor_offset++; - current_dmadescriptor_offset++; + } // end colour depth ^ 2 linked list + } // end colour depth loop + + } // end frame rows + + ESP_LOGI("I2S-DMA", "%d DMA descriptors linked to buffer data.", current_dmadescriptor_offset); + + // + // Setup DMA and Output to GPIO + // + auto bus_cfg = dma_bus.config(); // バス設定用の構造体を取得します。 + + bus_cfg.bus_freq = m_cfg.i2sspeed; + bus_cfg.pin_wr = m_cfg.gpio.clk; // WR を接続しているピン番号 + + bus_cfg.pin_d0 = m_cfg.gpio.r1; + bus_cfg.pin_d1 = m_cfg.gpio.g1; + bus_cfg.pin_d2 = m_cfg.gpio.b1; + bus_cfg.pin_d3 = m_cfg.gpio.r2; + bus_cfg.pin_d4 = m_cfg.gpio.g2; + bus_cfg.pin_d5 = m_cfg.gpio.b2; + bus_cfg.pin_d6 = m_cfg.gpio.lat; + bus_cfg.pin_d7 = m_cfg.gpio.oe; + bus_cfg.pin_d8 = m_cfg.gpio.a; + bus_cfg.pin_d9 = m_cfg.gpio.b; + bus_cfg.pin_d10 = m_cfg.gpio.c; + bus_cfg.pin_d11 = m_cfg.gpio.d; + bus_cfg.pin_d12 = m_cfg.gpio.e; + bus_cfg.pin_d13 = -1; + bus_cfg.pin_d14 = -1; + bus_cfg.pin_d15 = -1; - } // additional linked list items - } // row depth struct +#if defined(SPIRAM_DMA_BUFFER) + bus_cfg.psram_clk_override = true; +#endif + dma_bus.config(bus_cfg); - for(int i=lsbMsbTransitionBit + 1; i < m_cfg.getPixelColorDepthBits(); i++) - { - // binary time division setup: we need 2 of bit (LSBMSB_TRANSITION_BIT + 1) four of (LSBMSB_TRANSITION_BIT + 2), etc - // because we sweep through to MSB each time, it divides the number of times we have to sweep in half (saving linked list RAM) - // we need 2^(i - LSBMSB_TRANSITION_BIT - 1) == 1 << (i - LSBMSB_TRANSITION_BIT - 1) passes from i to MSB - - for(int k=0; k < (1<<(i - lsbMsbTransitionBit - 1)); k++) - { - dma_bus.create_dma_desc_link(dma_buff.rowBits[row]->getDataPtr(i, 0), dma_buff.rowBits[row]->size(m_cfg.getPixelColorDepthBits() - i), false); - - if (m_cfg.double_buff) { - dma_bus.create_dma_desc_link(dma_buff.rowBits[row]->getDataPtr(i, 1), dma_buff.rowBits[row]->size(m_cfg.getPixelColorDepthBits() - i), true ); - } - - current_dmadescriptor_offset++; - - } // end colour depth ^ 2 linked list - } // end colour depth loop - - } // end frame rows - - ESP_LOGI("I2S-DMA", "%d DMA descriptors linked to buffer data.", current_dmadescriptor_offset); - -// -// Setup DMA and Output to GPIO -// - auto bus_cfg = dma_bus.config(); // バス設定用の構造体を取得します。 - - bus_cfg.bus_freq = m_cfg.i2sspeed; - bus_cfg.pin_wr = m_cfg.gpio.clk; // WR を接続しているピン番号 - - bus_cfg.pin_d0 = m_cfg.gpio.r1; - bus_cfg.pin_d1 = m_cfg.gpio.g1; - bus_cfg.pin_d2 = m_cfg.gpio.b1; - bus_cfg.pin_d3 = m_cfg.gpio.r2; - bus_cfg.pin_d4 = m_cfg.gpio.g2; - bus_cfg.pin_d5 = m_cfg.gpio.b2; - bus_cfg.pin_d6 = m_cfg.gpio.lat; - bus_cfg.pin_d7 = m_cfg.gpio.oe; - bus_cfg.pin_d8 = m_cfg.gpio.a; - bus_cfg.pin_d9 = m_cfg.gpio.b; - bus_cfg.pin_d10 = m_cfg.gpio.c; - bus_cfg.pin_d11 = m_cfg.gpio.d; - bus_cfg.pin_d12 = m_cfg.gpio.e; - bus_cfg.pin_d13 = -1; - bus_cfg.pin_d14 = -1; - bus_cfg.pin_d15 = -1; - - #if defined(SPIRAM_DMA_BUFFER) - bus_cfg.psram_clk_override = true; - #endif - - dma_bus.config(bus_cfg); - - dma_bus.init(); - - dma_bus.dma_transfer_start(); - - flipDMABuffer(); // display back buffer 0, draw to 1, ignored if double buffering isn't enabled. - - //i2s_parallel_send_dma(ESP32_I2S_DEVICE, &dmadesc_a[0]); - ESP_LOGI("I2S-DMA", "DMA setup completed"); - -} // end initMatrixDMABuff + dma_bus.init(); + + dma_bus.dma_transfer_start(); + flipDMABuffer(); // display back buffer 0, draw to 1, ignored if double buffering isn't enabled. + + // i2s_parallel_send_dma(ESP32_I2S_DEVICE, &dmadesc_a[0]); + ESP_LOGI("I2S-DMA", "DMA setup completed"); + +} // end initMatrixDMABuff /* There are 'bits' set in the frameStruct that we simply don't need to set every single time we change a pixel / DMA buffer co-ordinate. * For example, the bits that determine the address lines, we don't need to set these every time. Once they're in place, and assuming we @@ -295,448 +305,386 @@ void MatrixPanel_I2S_DMA::configureDMA(const HUB75_I2S_CFG& _cfg) * Let's put it into IRAM to avoid situations when it could be flushed out of instruction cache * and had to be read from spi-flash over and over again. * Yes, it is always a tradeoff between memory/speed/size, but compared to DMA-buffer size is not a big deal - * + * * Note: Cannot pass a negative co-ord as it makes no sense in the DMA bit array lookup. */ void IRAM_ATTR MatrixPanel_I2S_DMA::updateMatrixDMABuffer(uint16_t x_coord, uint16_t y_coord, uint8_t red, uint8_t green, uint8_t blue) { - if ( !initialized ) return; + if (!initialized) + return; /* 1) Check that the co-ordinates are within range, or it'll break everything big time. - * Valid co-ordinates are from 0 to (MATRIX_XXXX-1) - */ - if ( x_coord >= PIXELS_PER_ROW || y_coord >= m_cfg.mx_height) { + * Valid co-ordinates are from 0 to (MATRIX_XXXX-1) + */ + if (x_coord >= PIXELS_PER_ROW || y_coord >= m_cfg.mx_height) + { return; } - /* LED Brightness Compensation. Because if we do a basic "red & mask" for example, - * we'll NEVER send the dimmest possible colour, due to binary skew. - * i.e. It's almost impossible for colour_depth_idx of 0 to be sent out to the MATRIX unless the 'value' of a colour is exactly '1' + /* LED Brightness Compensation. Because if we do a basic "red & mask" for example, + * we'll NEVER send the dimmest possible colour, due to binary skew. + * i.e. It's almost impossible for colour_depth_idx of 0 to be sent out to the MATRIX unless the 'value' of a colour is exactly '1' * https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ - */ -uint16_t red16, green16, blue16; + */ + uint16_t red16, green16, blue16; #ifndef NO_CIE1931 - red16 = lumConvTab[red]; - green16 = lumConvTab[green]; - blue16 = lumConvTab[blue]; + red16 = lumConvTab[red]; + green16 = lumConvTab[green]; + blue16 = lumConvTab[blue]; #else - red16 = red << 8; - green16 = green << 8; - blue16 = blue << 8; + red16 = red << 8; + green16 = green << 8; + blue16 = blue << 8; #endif - /* When using the drawPixel, we are obviously only changing the value of one x,y position, - * however, the two-scan panels paint TWO lines at the same time - * and this reflects the parallel in-DMA-memory data structure of uint16_t's that are getting - * pumped out at high speed. - * - * So we need to ensure we persist the bits (8 of them) of the uint16_t for the row we aren't changing. - * - * The DMA buffer order has also been reversed (refer to the last code in this function) - * so we have to check for this and check the correct position of the MATRIX_DATA_STORAGE_TYPE - * data. - */ - x_coord = ESP32_TX_FIFO_POSITION_ADJUST(x_coord); - - - uint16_t _colourbitclear = BITMASK_RGB1_CLEAR, _colourbitoffset = 0; - - if (y_coord >= ROWS_PER_FRAME){ // if we are drawing to the bottom part of the panel - _colourbitoffset = BITS_RGB2_OFFSET; - _colourbitclear = BITMASK_RGB2_CLEAR; - y_coord -= ROWS_PER_FRAME; - } + /* When using the drawPixel, we are obviously only changing the value of one x,y position, + * however, the two-scan panels paint TWO lines at the same time + * and this reflects the parallel in-DMA-memory data structure of uint16_t's that are getting + * pumped out at high speed. + * + * So we need to ensure we persist the bits (8 of them) of the uint16_t for the row we aren't changing. + * + * The DMA buffer order has also been reversed (refer to the last code in this function) + * so we have to check for this and check the correct position of the MATRIX_DATA_STORAGE_TYPE + * data. + */ + x_coord = ESP32_TX_FIFO_POSITION_ADJUST(x_coord); - // Iterating through colour depth bits, which we assume are 8 bits per RGB subpixel (24bpp) - uint8_t colour_depth_idx = m_cfg.getPixelColorDepthBits(); - do { - --colour_depth_idx; - - uint16_t mask = PIXEL_COLOR_MASK_BIT(colour_depth_idx, MASK_OFFSET); - uint16_t RGB_output_bits = 0; + uint16_t _colourbitclear = BITMASK_RGB1_CLEAR, _colourbitoffset = 0; - /* Per the .h file, the order of the output RGB bits is: - * BIT_B2, BIT_G2, BIT_R2, BIT_B1, BIT_G1, BIT_R1 */ - RGB_output_bits |= (bool)(blue16 & mask); // --B - RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(green16 & mask); // -BG - RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(red16 & mask); // BGR - RGB_output_bits <<= _colourbitoffset; // shift colour bits to the required position + if (y_coord >= ROWS_PER_FRAME) + { // if we are drawing to the bottom part of the panel + _colourbitoffset = BITS_RGB2_OFFSET; + _colourbitclear = BITMASK_RGB2_CLEAR; + y_coord -= ROWS_PER_FRAME; + } + // Iterating through colour depth bits, which we assume are 8 bits per RGB subpixel (24bpp) + uint8_t colour_depth_idx = m_cfg.getPixelColorDepthBits(); + do + { + --colour_depth_idx; + + uint16_t mask = PIXEL_COLOR_MASK_BIT(colour_depth_idx, MASK_OFFSET); + uint16_t RGB_output_bits = 0; - // Get the contents at this address, - // it would represent a vector pointing to the full row of pixels for the specified colour depth bit at Y coordinate - ESP32_I2S_DMA_STORAGE_TYPE *p = getRowDataPtr(y_coord, colour_depth_idx, back_buffer_id); + /* Per the .h file, the order of the output RGB bits is: + * BIT_B2, BIT_G2, BIT_R2, BIT_B1, BIT_G1, BIT_R1 */ + RGB_output_bits |= (bool)(blue16 & mask); // --B + RGB_output_bits <<= 1; + RGB_output_bits |= (bool)(green16 & mask); // -BG + RGB_output_bits <<= 1; + RGB_output_bits |= (bool)(red16 & mask); // BGR + RGB_output_bits <<= _colourbitoffset; // shift colour bits to the required position + // Get the contents at this address, + // it would represent a vector pointing to the full row of pixels for the specified colour depth bit at Y coordinate + ESP32_I2S_DMA_STORAGE_TYPE *p = getRowDataPtr(y_coord, colour_depth_idx, back_buffer_id); - // We need to update the correct uint16_t word in the rowBitStruct array pointing to a specific pixel at X - coordinate - p[x_coord] &= _colourbitclear; // reset RGB bits - p[x_coord] |= RGB_output_bits; // set new RGB bits + // We need to update the correct uint16_t word in the rowBitStruct array pointing to a specific pixel at X - coordinate + p[x_coord] &= _colourbitclear; // reset RGB bits + p[x_coord] |= RGB_output_bits; // set new RGB bits - #if defined(SPIRAM_DMA_BUFFER) - Cache_WriteBack_Addr((uint32_t)&p[x_coord], sizeof(ESP32_I2S_DMA_STORAGE_TYPE)) ; - #endif +#if defined(SPIRAM_DMA_BUFFER) + Cache_WriteBack_Addr((uint32_t)&p[x_coord], sizeof(ESP32_I2S_DMA_STORAGE_TYPE)); +#endif - } while(colour_depth_idx); // end of colour depth loop (8) + } while (colour_depth_idx); // end of colour depth loop (8) } // updateMatrixDMABuffer (specific co-ords change) - /* Update the entire buffer with a single specific colour - quicker */ void MatrixPanel_I2S_DMA::updateMatrixDMABuffer(uint8_t red, uint8_t green, uint8_t blue) { - if ( !initialized ) return; - - /* https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ */ -uint16_t red16, green16, blue16; + if (!initialized) + return; + + /* https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ */ + uint16_t red16, green16, blue16; #ifndef NO_CIE1931 - red16 = lumConvTab[red]; - green16 = lumConvTab[green]; - blue16 = lumConvTab[blue]; + red16 = lumConvTab[red]; + green16 = lumConvTab[green]; + blue16 = lumConvTab[blue]; #else - red16 = red << 8; - green16 = green << 8; - blue16 = blue << 8; + red16 = red << 8; + green16 = green << 8; + blue16 = blue << 8; #endif - for(uint8_t colour_depth_idx=0; colour_depth_idx < m_cfg.getPixelColorDepthBits(); colour_depth_idx++) // colour depth - 8 iterations + for (uint8_t colour_depth_idx = 0; colour_depth_idx < m_cfg.getPixelColorDepthBits(); colour_depth_idx++) // colour depth - 8 iterations { // let's precalculate RGB1 and RGB2 bits than flood it over the entire DMA buffer - uint16_t RGB_output_bits = 0; + uint16_t RGB_output_bits = 0; uint16_t mask = PIXEL_COLOR_MASK_BIT(colour_depth_idx, MASK_OFFSET); /* Per the .h file, the order of the output RGB bits is: * BIT_B2, BIT_G2, BIT_R2, BIT_B1, BIT_G1, BIT_R1 */ - RGB_output_bits |= (bool)(blue16 & mask); // --B + RGB_output_bits |= (bool)(blue16 & mask); // --B RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(green16 & mask); // -BG + RGB_output_bits |= (bool)(green16 & mask); // -BG RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(red16 & mask); // BGR - + RGB_output_bits |= (bool)(red16 & mask); // BGR + // Duplicate and shift across so we have have 6 populated bits of RGB1 and RGB2 pin values suitable for DMA buffer - RGB_output_bits |= RGB_output_bits << BITS_RGB2_OFFSET; //BGRBGR - - //Serial.printf("Fill with: 0x%#06x\n", RGB_output_bits); + RGB_output_bits |= RGB_output_bits << BITS_RGB2_OFFSET; // BGRBGR + + // Serial.printf("Fill with: 0x%#06x\n", RGB_output_bits); // iterate rows - int matrix_frame_parallel_row = dma_buff.rowBits.size(); - do { + int matrix_frame_parallel_row = fb->rowBits.size(); + do + { --matrix_frame_parallel_row; // The destination for the pixel row bitstream ESP32_I2S_DMA_STORAGE_TYPE *p = getRowDataPtr(matrix_frame_parallel_row, colour_depth_idx, back_buffer_id); // iterate pixels in a row - int x_coord=dma_buff.rowBits[matrix_frame_parallel_row]->width; - do { + int x_coord = fb->rowBits[matrix_frame_parallel_row]->width; + do + { --x_coord; - p[x_coord] &= BITMASK_RGB12_CLEAR; // reset colour bits - p[x_coord] |= RGB_output_bits; // set new colour bits + p[x_coord] &= BITMASK_RGB12_CLEAR; // reset colour bits + p[x_coord] |= RGB_output_bits; // set new colour bits +#if defined(SPIRAM_DMA_BUFFER) + Cache_WriteBack_Addr((uint32_t)&p[x_coord], sizeof(ESP32_I2S_DMA_STORAGE_TYPE)); +#endif - #if defined(SPIRAM_DMA_BUFFER) - Cache_WriteBack_Addr((uint32_t)&p[x_coord], sizeof(ESP32_I2S_DMA_STORAGE_TYPE)) ; - #endif - - } while(x_coord); + } while (x_coord); - } while(matrix_frame_parallel_row); // end row iteration - } // colour depth loop (8) + } while (matrix_frame_parallel_row); // end row iteration + } // colour depth loop (8) } // updateMatrixDMABuffer (full frame paint) /** * @brief - clears and reinitializes colour/control data in DMA buffs * When allocated, DMA buffs might be dirty, so we need to blank it and initialize ABCDE,LAT,OE control bits. * Those control bits are constants during the entire DMA sweep and never changed when updating just pixel colour data - * so we could set it once on DMA buffs initialization and forget. + * so we could set it once on DMA buffs initialization and forget. * This effectively clears buffers to blank BLACK and makes it ready to display output. * (Brightness control via OE bit manipulation is another case) - this must be done as well seperately! */ -void MatrixPanel_I2S_DMA::clearFrameBuffer(bool _buff_id){ +void MatrixPanel_I2S_DMA::clearFrameBuffer(bool _buff_id) +{ if (!initialized) return; // we start with iterating all rows in dma_buff structure - int row_idx = dma_buff.rowBits.size(); - do { + int row_idx = fb->rowBits.size(); + do + { --row_idx; - - ESP32_I2S_DMA_STORAGE_TYPE* row = dma_buff.rowBits[row_idx]->getDataPtr(0, _buff_id); // set pointer to the HEAD of a buffer holding data for the entire matrix row + + ESP32_I2S_DMA_STORAGE_TYPE *row = fb->rowBits[row_idx]->getDataPtr(0, _buff_id); // set pointer to the HEAD of a buffer holding data for the entire matrix row ESP32_I2S_DMA_STORAGE_TYPE abcde = (ESP32_I2S_DMA_STORAGE_TYPE)row_idx; - abcde <<= BITS_ADDR_OFFSET; // shift row y-coord to match ABCDE bits in vector from 8 to 12 + abcde <<= BITS_ADDR_OFFSET; // shift row y-coord to match ABCDE bits in vector from 8 to 12 // get last pixel index in a row of all colourdepths - int x_pixel = dma_buff.rowBits[row_idx]->width * dma_buff.rowBits[row_idx]->colour_depth; - //Serial.printf(" from pixel %d, ", x_pixel); + int x_pixel = fb->rowBits[row_idx]->width * fb->rowBits[row_idx]->colour_depth; + // Serial.printf(" from pixel %d, ", x_pixel); // fill all x_pixels except colour_index[0] (LSB) ones, this also clears all colour data to 0's black - do { + do + { --x_pixel; - - if ( m_cfg.driver == HUB75_I2S_CFG::SM5266P) { - // modifications here for row shift register type SM5266P + + if (m_cfg.driver == HUB75_I2S_CFG::SM5266P) + { + // modifications here for row shift register type SM5266P // https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA/issues/164 - row[x_pixel] = abcde & (0x18 << BITS_ADDR_OFFSET); // mask out the bottom 3 bits which are the clk di bk inputs - } else { + row[x_pixel] = abcde & (0x18 << BITS_ADDR_OFFSET); // mask out the bottom 3 bits which are the clk di bk inputs + } + else + { row[ESP32_TX_FIFO_POSITION_ADJUST(x_pixel)] = abcde; } - } while(x_pixel!=dma_buff.rowBits[row_idx]->width && x_pixel); + } while (x_pixel != fb->rowBits[row_idx]->width && x_pixel); // colour_index[0] (LSB) x_pixels must be "marked" with a previous's row address, 'cause it is used to display // previous row while we pump in LSB's for a new row - abcde = ((ESP32_I2S_DMA_STORAGE_TYPE)row_idx-1) << BITS_ADDR_OFFSET; - do { + abcde = ((ESP32_I2S_DMA_STORAGE_TYPE)row_idx - 1) << BITS_ADDR_OFFSET; + do + { --x_pixel; - - if ( m_cfg.driver == HUB75_I2S_CFG::SM5266P) { - // modifications here for row shift register type SM5266P + + if (m_cfg.driver == HUB75_I2S_CFG::SM5266P) + { + // modifications here for row shift register type SM5266P // https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA/issues/164 - row[x_pixel] = abcde & (0x18 << BITS_ADDR_OFFSET); // mask out the bottom 3 bits which are the clk di bk inputs - } else { + row[x_pixel] = abcde & (0x18 << BITS_ADDR_OFFSET); // mask out the bottom 3 bits which are the clk di bk inputs + } + else + { row[ESP32_TX_FIFO_POSITION_ADJUST(x_pixel)] = abcde; - } - - } while(x_pixel); - - - // modifications here for row shift register type SM5266P - // https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA/issues/164 - if ( m_cfg.driver == HUB75_I2S_CFG::SM5266P) { - uint16_t serialCount; - uint16_t latch; - x_pixel = dma_buff.rowBits[row_idx]->width - 16; // come back 8*2 pixels to allow for 8 writes - serialCount = 8; - do{ - serialCount--; - latch = row[x_pixel] | (((((ESP32_I2S_DMA_STORAGE_TYPE)row_idx) % 8) == serialCount) << 1) << BITS_ADDR_OFFSET; // data on 'B' - row[x_pixel++] = latch| (0x05<< BITS_ADDR_OFFSET); // clock high on 'A'and BK high for update - row[x_pixel++] = latch| (0x04<< BITS_ADDR_OFFSET); // clock low on 'A'and BK high for update - } while (serialCount); - } // end SM5266P - - - // let's set LAT/OE control bits for specific pixels in each colour_index subrows - // Need to consider the original ESP32's (WROOM) DMA TX FIFO reordering of bytes... - uint8_t colouridx = dma_buff.rowBits[row_idx]->colour_depth; - do { - --colouridx; - - // switch pointer to a row for a specific colour index - row = dma_buff.rowBits[row_idx]->getDataPtr(colouridx, _buff_id); - - row[ESP32_TX_FIFO_POSITION_ADJUST(dma_buff.rowBits[row_idx]->width - 1)] |= BIT_LAT; // -1 pixel to compensate array index starting at 0 - - //ESP32_TX_FIFO_POSITION_ADJUST(dma_buff.rowBits[row_idx]->width - 1) - - // need to disable OE before/after latch to hide row transition - // Should be one clock or more before latch, otherwise can get ghosting - uint8_t _blank = m_cfg.latch_blanking; - do { - --_blank; - - row[ESP32_TX_FIFO_POSITION_ADJUST(0 + _blank)] |= BIT_OE; // disable output - row[ESP32_TX_FIFO_POSITION_ADJUST(dma_buff.rowBits[row_idx]->width - 1)] |= BIT_OE; // disable output - row[ESP32_TX_FIFO_POSITION_ADJUST(dma_buff.rowBits[row_idx]->width - _blank - 1)] |= BIT_OE; // (LAT pulse is (width-2) -1 pixel to compensate array index starting at 0 - - - } while (_blank); - - } while(colouridx); - - - - #if defined(SPIRAM_DMA_BUFFER) - Cache_WriteBack_Addr((uint32_t)row, sizeof(ESP32_I2S_DMA_STORAGE_TYPE) * ((dma_buff.rowBits[row_idx]->width * dma_buff.rowBits[row_idx]->colour_depth)-1)) ; - #endif - - - } while(row_idx); -} - -/** - * @brief - reset OE bits in DMA buffer in a way to control brightness - * @param brt - brightness level from 0 to row_width - * @param _buff_id - buffer id to control - */ -/* -// Depreciated -void MatrixPanel_I2S_DMA::brtCtrlOE(int brt, const bool _buff_id){ - - if (!initialized) - return; - - if (brt > PIXELS_PER_ROW - (MAX_LAT_BLANKING + 2)) // can't control values larger than (row_width - latch_blanking) to avoid ongoing issues being raised about brightness and ghosting. - brt = PIXELS_PER_ROW - (MAX_LAT_BLANKING + 2); // +2 for a bit of buffer... + } - if (brt < 0) - brt = 0; + } while (x_pixel); - // start with iterating all rows in dma_buff structure - int row_idx = dma_buff.rowBits.size(); - do { - --row_idx; + // modifications here for row shift register type SM5266P + // https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-I2S-DMA/issues/164 + if (m_cfg.driver == HUB75_I2S_CFG::SM5266P) + { + uint16_t serialCount; + uint16_t latch; + x_pixel = fb->rowBits[row_idx]->width - 16; // come back 8*2 pixels to allow for 8 writes + serialCount = 8; + do + { + serialCount--; + latch = row[x_pixel] | (((((ESP32_I2S_DMA_STORAGE_TYPE)row_idx) % 8) == serialCount) << 1) << BITS_ADDR_OFFSET; // data on 'B' + row[x_pixel++] = latch | (0x05 << BITS_ADDR_OFFSET); // clock high on 'A'and BK high for update + row[x_pixel++] = latch | (0x04 << BITS_ADDR_OFFSET); // clock low on 'A'and BK high for update + } while (serialCount); + } // end SM5266P - // let's set OE control bits for specific pixels in each colour_index subrows - uint8_t colouridx = dma_buff.rowBits[row_idx]->colour_depth; - do { + // let's set LAT/OE control bits for specific pixels in each colour_index subrows + // Need to consider the original ESP32's (WROOM) DMA TX FIFO reordering of bytes... + uint8_t colouridx = fb->rowBits[row_idx]->colour_depth; + do + { --colouridx; // switch pointer to a row for a specific colour index - ESP32_I2S_DMA_STORAGE_TYPE* row = dma_buff.rowBits[row_idx]->getDataPtr(colouridx, _buff_id); - - int x_coord = dma_buff.rowBits[row_idx]->width; - do { - --x_coord; - - // clear OE bit for all other pixels - row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] &= BITMASK_OE_CLEAR; + row = fb->rowBits[row_idx]->getDataPtr(colouridx, _buff_id); - // Brightness control via OE toggle - disable matrix output at specified x_coord - if((colouridx > lsbMsbTransitionBit || !colouridx) && ((x_coord) >= brt)){ - row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] |= BIT_OE; // Disable output after this point. - continue; - } - // special case for the bits *after* LSB through (lsbMsbTransitionBit) - OE is output after data is shifted, so need to set OE to fractional brightness - if(colouridx && colouridx <= lsbMsbTransitionBit) { - // divide brightness in half for each bit below lsbMsbTransitionBit - int lsbBrightness = brt >> (lsbMsbTransitionBit - colouridx + 1); - if((x_coord) >= lsbBrightness) { - row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] |= BIT_OE; // Disable output after this point. - continue; - } - } + row[ESP32_TX_FIFO_POSITION_ADJUST(fb->rowBits[row_idx]->width - 1)] |= BIT_LAT; // -1 pixel to compensate array index starting at 0 - - } while(x_coord); + // ESP32_TX_FIFO_POSITION_ADJUST(dma_buff.rowBits[row_idx]->width - 1) // need to disable OE before/after latch to hide row transition // Should be one clock or more before latch, otherwise can get ghosting uint8_t _blank = m_cfg.latch_blanking; - do { + do + { --_blank; + row[ESP32_TX_FIFO_POSITION_ADJUST(0 + _blank)] |= BIT_OE; // disable output + row[ESP32_TX_FIFO_POSITION_ADJUST(fb->rowBits[row_idx]->width - 1)] |= BIT_OE; // disable output + row[ESP32_TX_FIFO_POSITION_ADJUST(fb->rowBits[row_idx]->width - _blank - 1)] |= BIT_OE; // (LAT pulse is (width-2) -1 pixel to compensate array index starting at 0 - row[ESP32_TX_FIFO_POSITION_ADJUST(0 + _blank)] |= BIT_OE; - - - - //row[0 + _blank] |= BIT_OE; - // no need, has been done already - //row[dma_buff.rowBits[row_idx]->width - _blank - 3 ] |= BIT_OE; // (LAT pulse is (width-2) -1 pixel to compensate array index starting at 0 } while (_blank); - } while(colouridx); - - // switch pointer to a row for a specific colour index - #if defined(SPIRAM_DMA_BUFFER) - ESP32_I2S_DMA_STORAGE_TYPE* row_hack = dma_buff.rowBits[row_idx]->getDataPtr(colouridx, _buff_id); - Cache_WriteBack_Addr((uint32_t)row_hack, sizeof(ESP32_I2S_DMA_STORAGE_TYPE) * ((dma_buff.rowBits[row_idx]->width * dma_buff.rowBits[row_idx]->colour_depth)-1)) ; - #endif + } while (colouridx); +#if defined(SPIRAM_DMA_BUFFER) + Cache_WriteBack_Addr((uint32_t)row, sizeof(ESP32_I2S_DMA_STORAGE_TYPE) * ((fb->rowBits[row_idx]->width * fb->rowBits[row_idx]->colour_depth) - 1)); +#endif - } while(row_idx); + } while (row_idx); } - */ - /** * @brief - reset OE bits in DMA buffer in a way to control brightness * @param brt - brightness level from 0 to 255 - NOT MATRIX_WIDTH * @param _buff_id - buffer id to control */ -void MatrixPanel_I2S_DMA::brtCtrlOEv2(uint8_t brt, const int _buff_id) { - +void MatrixPanel_I2S_DMA::brtCtrlOEv2(uint8_t brt, const int _buff_id) +{ + if (!initialized) return; - uint8_t _blank = m_cfg.latch_blanking; // don't want to inadvertantly blast over this - uint8_t _depth = dma_buff.rowBits[0]->colour_depth; - uint16_t _width = dma_buff.rowBits[0]->width; + uint8_t _blank = m_cfg.latch_blanking; // don't want to inadvertantly blast over this + uint8_t _depth = fb->rowBits[0]->colour_depth; + uint16_t _width = fb->rowBits[0]->width; // start with iterating all rows in dma_buff structure - int row_idx = dma_buff.rowBits.size(); - do { + int row_idx = fb->rowBits.size(); + do + { --row_idx; // let's set OE control bits for specific pixels in each color_index subrows uint8_t colouridx = _depth; - do { + do + { --colouridx; - - char bitplane = ( 2 * _depth - colouridx ) % _depth; - char bitshift = (_depth - lsbMsbTransitionBit - 1) >> 1; - - char rightshift = std::max( bitplane - bitshift - 2, 0 ); + + char bitplane = (2 * _depth - colouridx) % _depth; + char bitshift = (_depth - lsbMsbTransitionBit - 1) >> 1; + + char rightshift = std::max(bitplane - bitshift - 2, 0); // calculate the OE disable period by brightness, and also blanking - int brightness_in_x_pixels = ( ( _width - _blank ) * brt ) >> ( 7 + rightshift ); - brightness_in_x_pixels = ( brightness_in_x_pixels >> 1 ) | ( brightness_in_x_pixels & 1 ); - + int brightness_in_x_pixels = ((_width - _blank) * brt) >> (7 + rightshift); + brightness_in_x_pixels = (brightness_in_x_pixels >> 1) | (brightness_in_x_pixels & 1); + // switch pointer to a row for a specific color index - ESP32_I2S_DMA_STORAGE_TYPE* row = dma_buff.rowBits[row_idx]->getDataPtr(colouridx, _buff_id); + ESP32_I2S_DMA_STORAGE_TYPE *row = fb->rowBits[row_idx]->getDataPtr(colouridx, _buff_id); // define range of Output Enable on the center of the row - int x_coord_max = ( _width + brightness_in_x_pixels + 1 ) >> 1; - int x_coord_min = ( _width - brightness_in_x_pixels + 0 ) >> 1; + int x_coord_max = (_width + brightness_in_x_pixels + 1) >> 1; + int x_coord_min = (_width - brightness_in_x_pixels + 0) >> 1; int x_coord = _width; - do { + do + { --x_coord; - + // (the check is already including "blanking" ) - if (x_coord >= x_coord_min && x_coord < x_coord_max) - { - row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] &= BITMASK_OE_CLEAR; - } - else - { - row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] |= BIT_OE; // Disable output after this point. - } - - } while(x_coord); - - } while(colouridx); - - // switch pointer to a row for a specific colour index - #if defined(SPIRAM_DMA_BUFFER) - ESP32_I2S_DMA_STORAGE_TYPE* row_hack = dma_buff.rowBits[row_idx]->getDataPtr(colouridx, _buff_id); - Cache_WriteBack_Addr((uint32_t)row_hack, sizeof(ESP32_I2S_DMA_STORAGE_TYPE) * ((dma_buff.rowBits[row_idx]->width * dma_buff.rowBits[row_idx]->colour_depth)-1)) ; - #endif - } while(row_idx); - -} + if (x_coord >= x_coord_min && x_coord < x_coord_max) + { + row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] &= BITMASK_OE_CLEAR; + } + else + { + row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] |= BIT_OE; // Disable output after this point. + } + + } while (x_coord); + } while (colouridx); + +// switch pointer to a row for a specific colour index +#if defined(SPIRAM_DMA_BUFFER) + ESP32_I2S_DMA_STORAGE_TYPE *row_hack = fb->rowBits[row_idx]->getDataPtr(colouridx, _buff_id); + Cache_WriteBack_Addr((uint32_t)row_hack, sizeof(ESP32_I2S_DMA_STORAGE_TYPE) * ((fb->rowBits[row_idx]->width * fb->rowBits[row_idx]->colour_depth) - 1)); +#endif + } while (row_idx); +} /* * overload for compatibility */ - -bool MatrixPanel_I2S_DMA::begin(int r1, int g1, int b1, int r2, int g2, int b2, int a, int b, int c, int d, int e, int lat, int oe, int clk) { - if(initialized) return true; + +bool MatrixPanel_I2S_DMA::begin(int r1, int g1, int b1, int r2, int g2, int b2, int a, int b, int c, int d, int e, int lat, int oe, int clk) +{ + if (initialized) + return true; // RGB - m_cfg.gpio.r1 = r1; m_cfg.gpio.g1 = g1; m_cfg.gpio.b1 = b1; - m_cfg.gpio.r2 = r2; m_cfg.gpio.g2 = g2; m_cfg.gpio.b2 = b2; - + m_cfg.gpio.r1 = r1; + m_cfg.gpio.g1 = g1; + m_cfg.gpio.b1 = b1; + m_cfg.gpio.r2 = r2; + m_cfg.gpio.g2 = g2; + m_cfg.gpio.b2 = b2; + // Line Select - m_cfg.gpio.a = a; m_cfg.gpio.b = b; m_cfg.gpio.c = c; - m_cfg.gpio.d = d; m_cfg.gpio.e = e; - + m_cfg.gpio.a = a; + m_cfg.gpio.b = b; + m_cfg.gpio.c = c; + m_cfg.gpio.d = d; + m_cfg.gpio.e = e; + // Clock & Control - m_cfg.gpio.lat = lat; m_cfg.gpio.oe = oe; m_cfg.gpio.clk = clk; + m_cfg.gpio.lat = lat; + m_cfg.gpio.oe = oe; + m_cfg.gpio.clk = clk; return begin(); } -bool MatrixPanel_I2S_DMA::begin(const HUB75_I2S_CFG& cfg){ - if(initialized) return true; +bool MatrixPanel_I2S_DMA::begin(const HUB75_I2S_CFG &cfg) +{ + if (initialized) + return true; - if(!setCfg(cfg)) return false; + if (!setCfg(cfg)) + return false; return begin(); -} - - +} /** * @brief - Sets how many clock cycles to blank OE before/after LAT signal change @@ -745,7 +693,8 @@ bool MatrixPanel_I2S_DMA::begin(const HUB75_I2S_CFG& cfg){ * Max is MAX_LAT_BLANKING * @returns - new value for m_cfg.latch_blanking */ -uint8_t MatrixPanel_I2S_DMA::setLatBlanking(uint8_t pulses){ +uint8_t MatrixPanel_I2S_DMA::setLatBlanking(uint8_t pulses) +{ if (pulses > MAX_LAT_BLANKING) pulses = MAX_LAT_BLANKING; @@ -753,13 +702,12 @@ uint8_t MatrixPanel_I2S_DMA::setLatBlanking(uint8_t pulses){ pulses = DEFAULT_LAT_BLANKING; m_cfg.latch_blanking = pulses; - + // remove brightness var for now. - //setPanelBrightness(brightness); // set brightness to reset OE bits to the values matching new LAT blanking setting + // setPanelBrightness(brightness); // set brightness to reset OE bits to the values matching new LAT blanking setting return m_cfg.latch_blanking; } - #ifndef NO_FAST_FUNCTIONS /** * @brief - update DMA buff drawing horizontal line at specified coordinates @@ -768,94 +716,96 @@ uint8_t MatrixPanel_I2S_DMA::setLatBlanking(uint8_t pulses){ * @param l - line length * @param r,g,b, - RGB888 colour */ -void MatrixPanel_I2S_DMA::hlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, uint8_t red, uint8_t green, uint8_t blue){ - if ( !initialized ) +void MatrixPanel_I2S_DMA::hlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, uint8_t red, uint8_t green, uint8_t blue) +{ + if (!initialized) return; - if ( (x_coord + l) < 1 || y_coord < 0 || l < 1 || x_coord >= PIXELS_PER_ROW || y_coord >= m_cfg.mx_height) + if ((x_coord + l) < 1 || y_coord < 0 || l < 1 || x_coord >= PIXELS_PER_ROW || y_coord >= m_cfg.mx_height) return; - l = x_coord < 0 ? l+x_coord : l; + l = x_coord < 0 ? l + x_coord : l; x_coord = x_coord < 0 ? 0 : x_coord; + l = ((x_coord + l) >= PIXELS_PER_ROW) ? (PIXELS_PER_ROW - x_coord) : l; - l = ( (x_coord + l) >= PIXELS_PER_ROW ) ? (PIXELS_PER_ROW - x_coord):l; - - //if (x_coord+l > PIXELS_PER_ROW) -// l = PIXELS_PER_ROW - x_coord + 1; // reset width to end of row + // if (x_coord+l > PIXELS_PER_ROW) + // l = PIXELS_PER_ROW - x_coord + 1; // reset width to end of row /* LED Brightness Compensation */ -uint16_t red16, green16, blue16; + uint16_t red16, green16, blue16; #ifndef NO_CIE1931 - red16 = lumConvTab[red]; - green16 = lumConvTab[green]; - blue16 = lumConvTab[blue]; + red16 = lumConvTab[red]; + green16 = lumConvTab[green]; + blue16 = lumConvTab[blue]; #else - red16 = red << 8; - green16 = green << 8; - blue16 = blue << 8; + red16 = red << 8; + green16 = green << 8; + blue16 = blue << 8; #endif uint16_t _colourbitclear = BITMASK_RGB1_CLEAR, _colourbitoffset = 0; - if (y_coord >= ROWS_PER_FRAME){ // if we are drawing to the bottom part of the panel + if (y_coord >= ROWS_PER_FRAME) + { // if we are drawing to the bottom part of the panel _colourbitoffset = BITS_RGB2_OFFSET; - _colourbitclear = BITMASK_RGB2_CLEAR; + _colourbitclear = BITMASK_RGB2_CLEAR; y_coord -= ROWS_PER_FRAME; } // Iterating through colour depth bits (8 iterations) uint8_t colour_depth_idx = m_cfg.getPixelColorDepthBits(); - do { + do + { --colour_depth_idx; // let's precalculate RGB1 and RGB2 bits than flood it over the entire DMA buffer uint16_t RGB_output_bits = 0; -// uint8_t mask = (1 << colour_depth_idx COLOR_DEPTH_LESS_THAN_8BIT_ADJUST); + // uint8_t mask = (1 << colour_depth_idx COLOR_DEPTH_LESS_THAN_8BIT_ADJUST); // #if PIXEL_COLOR_DEPTH_BITS < 8 // uint8_t mask = (1 << (colour_depth_idx+MASK_OFFSET)); // expect 24 bit colour (8 bits per RGB subpixel) // #else // uint8_t mask = (1 << (colour_depth_idx)); // expect 24 bit colour (8 bits per RGB subpixel) - // #endif + // #endif uint16_t mask = PIXEL_COLOR_MASK_BIT(colour_depth_idx, MASK_OFFSET); /* Per the .h file, the order of the output RGB bits is: - * BIT_B2, BIT_G2, BIT_R2, BIT_B1, BIT_G1, BIT_R1 */ - RGB_output_bits |= (bool)(blue16 & mask); // --B + * BIT_B2, BIT_G2, BIT_R2, BIT_B1, BIT_G1, BIT_R1 */ + RGB_output_bits |= (bool)(blue16 & mask); // --B RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(green16 & mask); // -BG + RGB_output_bits |= (bool)(green16 & mask); // -BG RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(red16 & mask); // BGR - RGB_output_bits <<= _colourbitoffset; // shift color bits to the required position + RGB_output_bits |= (bool)(red16 & mask); // BGR + RGB_output_bits <<= _colourbitoffset; // shift color bits to the required position // Get the contents at this address, // it would represent a vector pointing to the full row of pixels for the specified colour depth bit at Y coordinate - ESP32_I2S_DMA_STORAGE_TYPE *p = dma_buff.rowBits[y_coord]->getDataPtr(colour_depth_idx, back_buffer_id); + ESP32_I2S_DMA_STORAGE_TYPE *p = fb->rowBits[y_coord]->getDataPtr(colour_depth_idx, back_buffer_id); // inlined version works slower here, dunno why :( // ESP32_I2S_DMA_STORAGE_TYPE *p = getRowDataPtr(y_coord, colour_depth_idx, back_buffer_id); int16_t _l = l; - do { // iterate pixels in a row - int16_t _x = x_coord + --_l; - - /* - #if defined(ESP32_THE_ORIG) - // Save the calculated value to the bitplane memory in reverse order to account for I2S Tx FIFO mode1 ordering - uint16_t &v = p[_x & 1U ? --_x : ++_x]; - #else - // ESP 32 doesn't need byte flipping for TX FIFO. - uint16_t &v = p[_x]; - #endif - */ - uint16_t &v = p[ESP32_TX_FIFO_POSITION_ADJUST(_x)]; - - v &= _colourbitclear; // reset colour bits - v |= RGB_output_bits; // set new colour bits - } while(_l); // iterate pixels in a row - } while(colour_depth_idx); // end of colour depth loop (8) + do + { // iterate pixels in a row + int16_t _x = x_coord + --_l; + + /* + #if defined(ESP32_THE_ORIG) + // Save the calculated value to the bitplane memory in reverse order to account for I2S Tx FIFO mode1 ordering + uint16_t &v = p[_x & 1U ? --_x : ++_x]; + #else + // ESP 32 doesn't need byte flipping for TX FIFO. + uint16_t &v = p[_x]; + #endif + */ + uint16_t &v = p[ESP32_TX_FIFO_POSITION_ADJUST(_x)]; + + v &= _colourbitclear; // reset colour bits + v |= RGB_output_bits; // set new colour bits + } while (_l); // iterate pixels in a row + } while (colour_depth_idx); // end of colour depth loop (8) } // hlineDMA() - /** * @brief - update DMA buff drawing vertical line at specified coordinates * @param x_coord - line start coordinate x @@ -863,87 +813,90 @@ uint16_t red16, green16, blue16; * @param l - line length * @param r,g,b, - RGB888 colour */ -void MatrixPanel_I2S_DMA::vlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, uint8_t red, uint8_t green, uint8_t blue){ - if ( !initialized ) +void MatrixPanel_I2S_DMA::vlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, uint8_t red, uint8_t green, uint8_t blue) +{ + if (!initialized) return; - if ( x_coord < 0 || (y_coord + l) < 1 || l < 1 || x_coord >= PIXELS_PER_ROW || y_coord >= m_cfg.mx_height) + if (x_coord < 0 || (y_coord + l) < 1 || l < 1 || x_coord >= PIXELS_PER_ROW || y_coord >= m_cfg.mx_height) return; - l = y_coord < 0 ? l+y_coord : l; + l = y_coord < 0 ? l + y_coord : l; y_coord = y_coord < 0 ? 0 : y_coord; // check for a length that goes beyond the height of the screen! Array out of bounds dma memory changes = screwed output #163 - l = ( (y_coord + l) >= m_cfg.mx_height ) ? (m_cfg.mx_height - y_coord):l; - //if (y_coord + l > m_cfg.mx_height) + l = ((y_coord + l) >= m_cfg.mx_height) ? (m_cfg.mx_height - y_coord) : l; + // if (y_coord + l > m_cfg.mx_height) /// l = m_cfg.mx_height - y_coord + 1; // reset width to end of col /* LED Brightness Compensation */ -uint16_t red16, green16, blue16; + uint16_t red16, green16, blue16; #ifndef NO_CIE1931 - red16 = lumConvTab[red]; - green16 = lumConvTab[green]; - blue16 = lumConvTab[blue]; + red16 = lumConvTab[red]; + green16 = lumConvTab[green]; + blue16 = lumConvTab[blue]; #else - red16 = red << 8; - green16 = green << 8; - blue16 = blue << 8; + red16 = red << 8; + green16 = green << 8; + blue16 = blue << 8; #endif -/* -#if defined(ESP32_THE_ORIG) - // Save the calculated value to the bitplane memory in reverse order to account for I2S Tx FIFO mode1 ordering - x_coord & 1U ? --x_coord : ++x_coord; -#endif -*/ + /* + #if defined(ESP32_THE_ORIG) + // Save the calculated value to the bitplane memory in reverse order to account for I2S Tx FIFO mode1 ordering + x_coord & 1U ? --x_coord : ++x_coord; + #endif + */ x_coord = ESP32_TX_FIFO_POSITION_ADJUST(x_coord); uint8_t colour_depth_idx = m_cfg.getPixelColorDepthBits(); - do { // Iterating through colour depth bits (8 iterations) + do + { // Iterating through colour depth bits (8 iterations) --colour_depth_idx; // let's precalculate RGB1 and RGB2 bits than flood it over the entire DMA buffer -// uint8_t mask = (1 << colour_depth_idx COLOR_DEPTH_LESS_THAN_8BIT_ADJUST); + // uint8_t mask = (1 << colour_depth_idx COLOR_DEPTH_LESS_THAN_8BIT_ADJUST); // #if PIXEL_COLOR_DEPTH_BITS < 8 // uint8_t mask = (1 << (colour_depth_idx+MASK_OFFSET)); // expect 24 bit colour (8 bits per RGB subpixel) // #else // uint8_t mask = (1 << (colour_depth_idx)); // expect 24 bit colour (8 bits per RGB subpixel) - // #endif + // #endif uint16_t mask = PIXEL_COLOR_MASK_BIT(colour_depth_idx, MASK_OFFSET); uint16_t RGB_output_bits = 0; /* Per the .h file, the order of the output RGB bits is: - * BIT_B2, BIT_G2, BIT_R2, BIT_B1, BIT_G1, BIT_R1 */ - RGB_output_bits |= (bool)(blue16 & mask); // --B + * BIT_B2, BIT_G2, BIT_R2, BIT_B1, BIT_G1, BIT_R1 */ + RGB_output_bits |= (bool)(blue16 & mask); // --B RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(green16 & mask); // -BG + RGB_output_bits |= (bool)(green16 & mask); // -BG RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(red16 & mask); // BGR + RGB_output_bits |= (bool)(red16 & mask); // BGR int16_t _l = 0, _y = y_coord; uint16_t _colourbitclear = BITMASK_RGB1_CLEAR; - do { // iterate pixels in a column + do + { // iterate pixels in a column - if (_y >= ROWS_PER_FRAME){ // if y-coord overlapped bottom-half panel + if (_y >= ROWS_PER_FRAME) + { // if y-coord overlapped bottom-half panel _y -= ROWS_PER_FRAME; - _colourbitclear = BITMASK_RGB2_CLEAR; + _colourbitclear = BITMASK_RGB2_CLEAR; RGB_output_bits <<= BITS_RGB2_OFFSET; } // Get the contents at this address, // it would represent a vector pointing to the full row of pixels for the specified colour depth bit at Y coordinate - //ESP32_I2S_DMA_STORAGE_TYPE *p = getRowDataPtr(_y, colour_depth_idx, back_buffer_id); - ESP32_I2S_DMA_STORAGE_TYPE *p = dma_buff.rowBits[_y]->getDataPtr(colour_depth_idx, back_buffer_id); + // ESP32_I2S_DMA_STORAGE_TYPE *p = getRowDataPtr(_y, colour_depth_idx, back_buffer_id); + ESP32_I2S_DMA_STORAGE_TYPE *p = fb->rowBits[_y]->getDataPtr(colour_depth_idx, back_buffer_id); - p[x_coord] &= _colourbitclear; // reset RGB bits - p[x_coord] |= RGB_output_bits; // set new RGB bits + p[x_coord] &= _colourbitclear; // reset RGB bits + p[x_coord] |= RGB_output_bits; // set new RGB bits ++_y; - } while(++_l!=l); // iterate pixels in a col - } while(colour_depth_idx); // end of colour depth loop (8) + } while (++_l != l); // iterate pixels in a col + } while (colour_depth_idx); // end of colour depth loop (8) } // vlineDMA() - /** * @brief - update DMA buff drawing a rectangular at specified coordinates * this works much faster than multiple consecutive per-pixel calls to updateMatrixDMABuffer() @@ -953,23 +906,29 @@ uint16_t red16, green16, blue16; * @param uint8_t g - RGB888 colour * @param uint8_t b - RGB888 colour */ -void MatrixPanel_I2S_DMA::fillRectDMA(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t r, uint8_t g, uint8_t b){ +void MatrixPanel_I2S_DMA::fillRectDMA(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t r, uint8_t g, uint8_t b) +{ // h-lines are >2 times faster than v-lines // so will use it only for tall rects with h >2w - if (h>2*w){ + if (h > 2 * w) + { // draw using v-lines - do { + do + { --w; - vlineDMA(x+w, y, h, r,g,b); - } while(w); - } else { + vlineDMA(x + w, y, h, r, g, b); + } while (w); + } + else + { // draw using h-lines - do { + do + { --h; - hlineDMA(x, y+h, w, r,g,b); - } while(h); + hlineDMA(x, y + h, w, r, g, b); + } while (h); } } -#endif // NO_FAST_FUNCTIONS +#endif // NO_FAST_FUNCTIONS diff --git a/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h b/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h index 4c555ec..ae3e8ee 100644 --- a/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h +++ b/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h @@ -8,14 +8,14 @@ #include <esp_log.h> #include "esp_attr.h" -//#include <Arduino.h> +// #include <Arduino.h> #include "platforms/platform_detect.hpp" #ifdef USE_GFX_ROOT - #include <FastLED.h> - #include "GFX.h" // Adafruit GFX core class -> https://github.com/mrfaptastic/GFX_Root +#include <FastLED.h> +#include "GFX.h" // Adafruit GFX core class -> https://github.com/mrfaptastic/GFX_Root #elif !defined NO_GFX - #include "Adafruit_GFX.h" // Adafruit class with all the other stuff +#include "Adafruit_GFX.h" // Adafruit class with all the other stuff #endif /******************************************************************************************* @@ -29,9 +29,9 @@ // #define NO_CIE1931 -/* Physical / Chained HUB75(s) RGB pixel WIDTH and HEIGHT. +/* Physical / Chained HUB75(s) RGB pixel WIDTH and HEIGHT. * - * This library has been tested with a 64x32 and 64x64 RGB panels. + * This library has been tested with a 64x32 and 64x64 RGB panels. * If you want to chain two or more of these horizontally to make a 128x32 panel * you can do so with the cable and then set the CHAIN_LENGTH to '2'. * @@ -41,19 +41,18 @@ * */ #ifndef MATRIX_WIDTH - #define MATRIX_WIDTH 64 // Single panel of 64 pixel width +#define MATRIX_WIDTH 64 // Single panel of 64 pixel width #endif #ifndef MATRIX_HEIGHT - #define MATRIX_HEIGHT 32 // CHANGE THIS VALUE to 64 IF USING 64px HIGH panel(s) with E PIN +#define MATRIX_HEIGHT 32 // CHANGE THIS VALUE to 64 IF USING 64px HIGH panel(s) with E PIN #endif #ifndef CHAIN_LENGTH - #define CHAIN_LENGTH 1 // Number of modules chained together, i.e. 4 panels chained result in virtualmatrix 64x4=256 px long +#define CHAIN_LENGTH 1 // Number of modules chained together, i.e. 4 panels chained result in virtualmatrix 64x4=256 px long #endif - -// Interesting Fact: We end up using a uint16_t to send data in parallel to the HUB75... but +// Interesting Fact: We end up using a uint16_t to send data in parallel to the HUB75... but // given we only map to 14 physical output wires/bits, we waste 2 bits. /***************************************************************************************/ @@ -61,163 +60,175 @@ // keeping a check sine it was possibe to set it previously #ifdef MATRIX_ROWS_IN_PARALLEL - #pragma message "You are not supposed to set MATRIX_ROWS_IN_PARALLEL. Setting it back to default." - #undef MATRIX_ROWS_IN_PARALLEL +#pragma message "You are not supposed to set MATRIX_ROWS_IN_PARALLEL. Setting it back to default." +#undef MATRIX_ROWS_IN_PARALLEL #endif -#define MATRIX_ROWS_IN_PARALLEL 2 +#define MATRIX_ROWS_IN_PARALLEL 2 // 8bit per RGB color = 24 bit/per pixel, // can be extended to offer deeper colors, or // might be reduced to save DMA RAM #ifdef PIXEL_COLOUR_DEPTH_BITS - #define PIXEL_COLOR_DEPTH_BITS PIXEL_COLOUR_DEPTH_BITS +#define PIXEL_COLOR_DEPTH_BITS PIXEL_COLOUR_DEPTH_BITS #endif -//support backwarts compatibility +// support backwarts compatibility #ifdef PIXEL_COLOR_DEPTH_BITS - #define PIXEL_COLOR_DEPTH_BITS_DEFAULT PIXEL_COLOR_DEPTH_BITS -#else - #define PIXEL_COLOR_DEPTH_BITS_DEFAULT 8 +#define PIXEL_COLOR_DEPTH_BITS_DEFAULT PIXEL_COLOR_DEPTH_BITS +#else +#define PIXEL_COLOR_DEPTH_BITS_DEFAULT 8 #endif #define PIXEL_COLOR_DEPTH_BITS_MAX 12 /***************************************************************************************/ /* Definitions below should NOT be ever changed without rewriting library logic */ -#define ESP32_I2S_DMA_STORAGE_TYPE uint16_t // DMA output of one uint16_t at a time. -#define CLKS_DURING_LATCH 0 // Not (yet) used. +#define ESP32_I2S_DMA_STORAGE_TYPE uint16_t // DMA output of one uint16_t at a time. +#define CLKS_DURING_LATCH 0 // Not (yet) used. // Panel Upper half RGB (numbering according to order in DMA gpio_bus configuration) #define BITS_RGB1_OFFSET 0 // Start point of RGB_X1 bits -#define BIT_R1 (1<<0) -#define BIT_G1 (1<<1) -#define BIT_B1 (1<<2) +#define BIT_R1 (1 << 0) +#define BIT_G1 (1 << 1) +#define BIT_B1 (1 << 2) // Panel Lower half RGB #define BITS_RGB2_OFFSET 3 // Start point of RGB_X2 bits -#define BIT_R2 (1<<3) -#define BIT_G2 (1<<4) -#define BIT_B2 (1<<5) +#define BIT_R2 (1 << 3) +#define BIT_G2 (1 << 4) +#define BIT_B2 (1 << 5) // Panel Control Signals -#define BIT_LAT (1<<6) -#define BIT_OE (1<<7) +#define BIT_LAT (1 << 6) +#define BIT_OE (1 << 7) // Panel GPIO Pin Addresses (A, B, C, D etc..) -#define BITS_ADDR_OFFSET 8 // Start point of address bits -#define BIT_A (1<<8) -#define BIT_B (1<<9) -#define BIT_C (1<<10) -#define BIT_D (1<<11) -#define BIT_E (1<<12) +#define BITS_ADDR_OFFSET 8 // Start point of address bits +#define BIT_A (1 << 8) +#define BIT_B (1 << 9) +#define BIT_C (1 << 10) +#define BIT_D (1 << 11) +#define BIT_E (1 << 12) // BitMasks are pre-computed based on the above #define's for performance. -#define BITMASK_RGB1_CLEAR (0b1111111111111000) // inverted bitmask for R1G1B1 bit in pixel vector -#define BITMASK_RGB2_CLEAR (0b1111111111000111) // inverted bitmask for R2G2B2 bit in pixel vector -#define BITMASK_RGB12_CLEAR (0b1111111111000000) // inverted bitmask for R1G1B1R2G2B2 bit in pixel vector -#define BITMASK_CTRL_CLEAR (0b1110000000111111) // inverted bitmask for control bits ABCDE,LAT,OE in pixel vector -#define BITMASK_OE_CLEAR (0b1111111101111111) // inverted bitmask for control bit OE in pixel vector +#define BITMASK_RGB1_CLEAR (0b1111111111111000) // inverted bitmask for R1G1B1 bit in pixel vector +#define BITMASK_RGB2_CLEAR (0b1111111111000111) // inverted bitmask for R2G2B2 bit in pixel vector +#define BITMASK_RGB12_CLEAR (0b1111111111000000) // inverted bitmask for R1G1B1R2G2B2 bit in pixel vector +#define BITMASK_CTRL_CLEAR (0b1110000000111111) // inverted bitmask for control bits ABCDE,LAT,OE in pixel vector +#define BITMASK_OE_CLEAR (0b1111111101111111) // inverted bitmask for control bit OE in pixel vector // How many clock cycles to blank OE before/after LAT signal change, default is 2 clocks -#define DEFAULT_LAT_BLANKING 2 +#define DEFAULT_LAT_BLANKING 2 // Max clock cycles to blank OE before/after LAT signal change -#define MAX_LAT_BLANKING 4 +#define MAX_LAT_BLANKING 4 /***************************************************************************************/ /** @brief - Structure holds raw DMA data to drive TWO full rows of pixels spanning through all chained modules * Note: sizeof(data) must be multiple of 32 bits, as ESP32 DMA linked list buffer address pointer must be word-aligned */ -struct rowBitStruct { - const size_t width; - const uint8_t colour_depth; - const bool double_buff; - ESP32_I2S_DMA_STORAGE_TYPE *data; - - /** @brief - returns size of row of data vectorfor a SINGLE buff - * size (in bytes) of a vector holding full DMA data for a row of pixels with _dpth colour bits - * a SINGLE buffer only size is accounted, when using double buffers it actually takes twice as much space - * but returned size is for a half of double-buffer - * - * default - returns full data vector size for a SINGLE buff - * - */ - size_t size(uint8_t _dpth=0 ) { if (!_dpth) _dpth = colour_depth; return width * _dpth * sizeof(ESP32_I2S_DMA_STORAGE_TYPE); }; +struct rowBitStruct +{ + const size_t width; + const uint8_t colour_depth; + const bool double_buff; + ESP32_I2S_DMA_STORAGE_TYPE *data; + + /** @brief - returns size of row of data vectorfor a SINGLE buff + * size (in bytes) of a vector holding full DMA data for a row of pixels with _dpth colour bits + * a SINGLE buffer only size is accounted, when using double buffers it actually takes twice as much space + * but returned size is for a half of double-buffer + * + * default - returns full data vector size for a SINGLE buff + * + */ + size_t size(uint8_t _dpth = 0) + { + if (!_dpth) + _dpth = colour_depth; + return width * _dpth * sizeof(ESP32_I2S_DMA_STORAGE_TYPE); + }; - /** @brief - returns pointer to the row's data vector beginning at pixel[0] for _dpth colour bit - * default - returns pointer to the data vector's head - * NOTE: this call might be very slow in loops. Due to poor instruction caching in esp32 it might be required a reread from flash - * every loop cycle, better use inlined #define instead in such cases - */ - inline ESP32_I2S_DMA_STORAGE_TYPE* getDataPtr(const uint8_t _dpth=0, const bool buff_id=0) { return &(data[_dpth*width + buff_id*(width*colour_depth)]); }; + /** @brief - returns pointer to the row's data vector beginning at pixel[0] for _dpth colour bit + * default - returns pointer to the data vector's head + * NOTE: this call might be very slow in loops. Due to poor instruction caching in esp32 it might be required a reread from flash + * every loop cycle, better use inlined #define instead in such cases + */ + // inline ESP32_I2S_DMA_STORAGE_TYPE* getDataPtr(const uint8_t _dpth=0, const bool buff_id=0) { return &(data[_dpth*width + buff_id*(width*colour_depth)]); }; + + // BUFFER ID VALUE IS NOW IGNORED!!!! + inline ESP32_I2S_DMA_STORAGE_TYPE *getDataPtr(const uint8_t _dpth = 0, const bool buff_id = 0) { return &(data[_dpth * width]); }; + + // constructor - allocates DMA-capable memory to hold the struct data + rowBitStruct(const size_t _width, const uint8_t _depth, const bool _dbuff) : width(_width), colour_depth(_depth), double_buff(_dbuff) + { - // constructor - allocates DMA-capable memory to hold the struct data - rowBitStruct(const size_t _width, const uint8_t _depth, const bool _dbuff) : width(_width), colour_depth(_depth), double_buff(_dbuff) { + // #if defined(SPIRAM_FRAMEBUFFER) && defined (CONFIG_IDF_TARGET_ESP32S3) +#if defined(SPIRAM_DMA_BUFFER) -//#if defined(SPIRAM_FRAMEBUFFER) && defined (CONFIG_IDF_TARGET_ESP32S3) - #if defined(SPIRAM_DMA_BUFFER) - // #pragma message "Enabling PSRAM / SPIRAM for frame buffer." - // ESP_LOGI("rowBitStruct", "Allocated DMA BitBuffer from PSRAM (SPIRAM)"); - //data = (ESP32_I2S_DMA_STORAGE_TYPE *)heap_caps_malloc( size()+size()*double_buff, MALLOC_CAP_SPIRAM); - data = (ESP32_I2S_DMA_STORAGE_TYPE *)heap_caps_aligned_alloc(64, size()+size()*double_buff, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + // data = (ESP32_I2S_DMA_STORAGE_TYPE *)heap_caps_aligned_alloc(64, size()+size()*double_buff, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + + // No longer have double buffer in the same struct - have a different struct + data = (ESP32_I2S_DMA_STORAGE_TYPE *)heap_caps_aligned_alloc(64, size(), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); /* - if (!psramFound()) + if (!psramFound()) { ESP_LOGE("rowBitStruct", "Requested to use PSRAM / SPIRAM for framebuffer, but it was not detected."); } */ #else - data = (ESP32_I2S_DMA_STORAGE_TYPE *)heap_caps_malloc( size()+size()*double_buff, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA); - // ESP_LOGI("rowBitStruct", "Allocated DMA BitBuffer from regular (and limited) SRAM"); -#endif + // data = (ESP32_I2S_DMA_STORAGE_TYPE *)heap_caps_malloc( size()+size()*double_buff, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA); - } - ~rowBitStruct() { delete data;} + // No longer have double buffer in the same struct - have a different struct + data = (ESP32_I2S_DMA_STORAGE_TYPE *)heap_caps_malloc(size(), MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA); + // ESP_LOGI("rowBitStruct", "Allocated DMA BitBuffer from regular (and limited) SRAM"); +#endif + } + ~rowBitStruct() { delete data; } }; - /* frameStruct * Note: A 'frameStruct' contains ALL the data for a full-frame (i.e. BOTH 2x16-row frames are - * are contained in parallel within the one uint16_t that is sent in parallel to the HUB75). - * + * are contained in parallel within the one uint16_t that is sent in parallel to the HUB75). + * * This structure isn't actually allocated in one memory block anymore, as the library now allocates * memory per row (per rowBits) instead. */ -struct frameStruct { - uint8_t rows=0; // number of rows held in current frame, not used actually, just to keep the idea of struct - std::vector<std::shared_ptr<rowBitStruct> > rowBits; +struct frameStruct +{ + uint8_t rows = 0; // number of rows held in current frame, not used actually, just to keep the idea of struct + std::vector<std::shared_ptr<rowBitStruct>> rowBits; }; -/***************************************************************************************/ -//C/p'ed from https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ -// Example calculator: https://gist.github.com/mathiasvr/19ce1d7b6caeab230934080ae1f1380e -// need to make sure this would end up in RAM for fastest access +/***************************************************************************************/ +// C/p'ed from https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ +// Example calculator: https://gist.github.com/mathiasvr/19ce1d7b6caeab230934080ae1f1380e +// need to make sure this would end up in RAM for fastest access #ifndef NO_CIE1931 /* -static const uint8_t DRAM_ATTR lumConvTab[]={ +static const uint8_t DRAM_ATTR lumConvTab[]={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16, 17, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 30, 30, 31, 31, 32, 33, 33, 34, 35, 35, 36, 37, 38, 38, 39, 40, 41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 90, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 115, 116, 118, 120, 121, 123, 124, 126, 128, 129, 131, 133, 134, 136, 138, 139, 141, 143, 145, 146, 148, 150, 152, 154, 156, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 192, 194, 196, 198, 200, 203, 205, 207, 209, 212, 214, 216, 218, 221, 223, 226, 228, 230, 233, 235, 238, 240, 243, 245, 248, 250, 253, 255, 255}; */ // This is 16-bit version of the table, // the constants taken from the example in the article above, each entries subtracted from 65535: -static const uint16_t DRAM_ATTR lumConvTab[]={ - 0, 27, 56, 84, 113, 141, 170, 198, 227, 255, 284, 312, 340, 369, 397, 426, - 454, 483, 511, 540, 568, 597, 626, 657, 688, 720, 754, 788, 824, 860, 898, 936, - 976, 1017, 1059, 1102, 1146, 1191, 1238, 1286, 1335, 1385, 1436, 1489, 1543, 1598, 1655, 1713, - 1772, 1833, 1895, 1958, 2023, 2089, 2156, 2225, 2296, 2368, 2441, 2516, 2592, 2670, 2750, 2831, - 2914, 2998, 3084, 3171, 3260, 3351, 3443, 3537, 3633, 3731, 3830, 3931, 4034, 4138, 4245, 4353, - 4463, 4574, 4688, 4803, 4921, 5040, 5161, 5284, 5409, 5536, 5665, 5796, 5929, 6064, 6201, 6340, - 6482, 6625, 6770, 6917, 7067, 7219, 7372, 7528, 7687, 7847, 8010, 8174, 8341, 8511, 8682, 8856, - 9032, 9211, 9392, 9575, 9761, 9949, 10139, 10332, 10527, 10725, 10925, 11127, 11332, 11540, 11750, 11963, - 12178, 12395, 12616, 12839, 13064, 13292, 13523, 13757, 13993, 14231, 14473, 14717, 14964, 15214, 15466, 15722, - 15980, 16240, 16504, 16771, 17040, 17312, 17587, 17865, 18146, 18430, 18717, 19006, 19299, 19595, 19894, 20195, - 20500, 20808, 21119, 21433, 21750, 22070, 22393, 22720, 23049, 23382, 23718, 24057, 24400, 24745, 25094, 25446, - 25802, 26160, 26522, 26888, 27256, 27628, 28004, 28382, 28765, 29150, 29539, 29932, 30328, 30727, 31130, 31536, - 31946, 32360, 32777, 33197, 33622, 34049, 34481, 34916, 35354, 35797, 36243, 36692, 37146, 37603, 38064, 38528, - 38996, 39469, 39945, 40424, 40908, 41395, 41886, 42382, 42881, 43383, 43890, 44401, 44916, 45434, 45957, 46484, - 47014, 47549, 48088, 48630, 49177, 49728, 50283, 50842, 51406, 51973, 52545, 53120, 53700, 54284, 54873, 55465, - 56062, 56663, 57269, 57878, 58492, 59111, 59733, 60360, 60992, 61627, 62268, 62912, 63561, 64215, 64873, 65535 -}; +static const uint16_t DRAM_ATTR lumConvTab[] = { + 0, 27, 56, 84, 113, 141, 170, 198, 227, 255, 284, 312, 340, 369, 397, 426, + 454, 483, 511, 540, 568, 597, 626, 657, 688, 720, 754, 788, 824, 860, 898, 936, + 976, 1017, 1059, 1102, 1146, 1191, 1238, 1286, 1335, 1385, 1436, 1489, 1543, 1598, 1655, 1713, + 1772, 1833, 1895, 1958, 2023, 2089, 2156, 2225, 2296, 2368, 2441, 2516, 2592, 2670, 2750, 2831, + 2914, 2998, 3084, 3171, 3260, 3351, 3443, 3537, 3633, 3731, 3830, 3931, 4034, 4138, 4245, 4353, + 4463, 4574, 4688, 4803, 4921, 5040, 5161, 5284, 5409, 5536, 5665, 5796, 5929, 6064, 6201, 6340, + 6482, 6625, 6770, 6917, 7067, 7219, 7372, 7528, 7687, 7847, 8010, 8174, 8341, 8511, 8682, 8856, + 9032, 9211, 9392, 9575, 9761, 9949, 10139, 10332, 10527, 10725, 10925, 11127, 11332, 11540, 11750, 11963, + 12178, 12395, 12616, 12839, 13064, 13292, 13523, 13757, 13993, 14231, 14473, 14717, 14964, 15214, 15466, 15722, + 15980, 16240, 16504, 16771, 17040, 17312, 17587, 17865, 18146, 18430, 18717, 19006, 19299, 19595, 19894, 20195, + 20500, 20808, 21119, 21433, 21750, 22070, 22393, 22720, 23049, 23382, 23718, 24057, 24400, 24745, 25094, 25446, + 25802, 26160, 26522, 26888, 27256, 27628, 28004, 28382, 28765, 29150, 29539, 29932, 30328, 30727, 31130, 31536, + 31946, 32360, 32777, 33197, 33622, 34049, 34481, 34916, 35354, 35797, 36243, 36692, 37146, 37603, 38064, 38528, + 38996, 39469, 39945, 40424, 40908, 41395, 41886, 42382, 42881, 43383, 43890, 44401, 44916, 45434, 45957, 46484, + 47014, 47549, 48088, 48630, 49177, 49728, 50283, 50842, 51406, 51973, 52545, 53120, 53700, 54284, 54873, 55465, + 56062, 56663, 57269, 57878, 58492, 59111, 59733, 60360, 60992, 61627, 62268, 62912, 63561, 64215, 64873, 65535}; #endif /** @brief - configuration values for HUB75_I2S driver @@ -225,19 +236,33 @@ static const uint16_t DRAM_ATTR lumConvTab[]={ * an initialization values when creating an instance of MatrixPanel_I2S_DMA object. * All params have it's default values. */ -struct HUB75_I2S_CFG { +struct HUB75_I2S_CFG +{ /** * Enumeration of hardware-specific chips * used to drive matrix modules */ - enum shift_driver {SHIFTREG=0, FM6124, FM6126A, ICN2038S, MBI5124, SM5266P}; + enum shift_driver + { + SHIFTREG = 0, + FM6124, + FM6126A, + ICN2038S, + MBI5124, + SM5266P + }; /** * I2S clock speed selector */ - enum clk_speed {HZ_8M=8000000, HZ_10M=10000000, HZ_15M=15000000, HZ_20M=20000000}; - + enum clk_speed + { + HZ_8M = 8000000, + HZ_10M = 10000000, + HZ_15M = 15000000, + HZ_20M = 20000000 + }; // // Members must be in order of declaration or it breaks Arduino compiling due to strict checking. @@ -251,9 +276,12 @@ struct HUB75_I2S_CFG { // number of chained panels regardless of the topology, default 1 - a single matrix module uint16_t chain_length; - + // GPIO Mapping - struct i2s_pins{ int8_t r1, g1, b1, r2, g2, b2, a, b, c, d, e, lat, oe, clk; } gpio; + struct i2s_pins + { + int8_t r1, g1, b1, r2, g2, b2, a, b, c, d, e, lat, oe, clk; + } gpio; // Matrix driver chip type - default is a plain shift register shift_driver driver; @@ -263,7 +291,7 @@ struct HUB75_I2S_CFG { // I2S clock speed clk_speed i2sspeed; - + // How many clock cycles to blank OE before/after LAT signal change, default is 1 clock uint8_t latch_blanking; @@ -286,347 +314,360 @@ struct HUB75_I2S_CFG { // Minimum refresh / scan rate needs to be configured on start due to LSBMSB_TRANSITION_BIT calculation in allocateDMAmemory() uint8_t min_refresh_rate; - // struct constructor - HUB75_I2S_CFG ( - uint16_t _w = MATRIX_WIDTH, - uint16_t _h = MATRIX_HEIGHT, - uint16_t _chain = CHAIN_LENGTH, - i2s_pins _pinmap = { - R1_PIN_DEFAULT, G1_PIN_DEFAULT, B1_PIN_DEFAULT, R2_PIN_DEFAULT, G2_PIN_DEFAULT, B2_PIN_DEFAULT, - A_PIN_DEFAULT, B_PIN_DEFAULT, C_PIN_DEFAULT, D_PIN_DEFAULT, E_PIN_DEFAULT, - LAT_PIN_DEFAULT, OE_PIN_DEFAULT, CLK_PIN_DEFAULT }, - shift_driver _drv = SHIFTREG, - bool _dbuff = false, - clk_speed _i2sspeed = HZ_15M, - uint8_t _latblk = DEFAULT_LAT_BLANKING, // Anything > 1 seems to cause artefacts on ICS panels - bool _clockphase = true, - uint16_t _min_refresh_rate = 60, - uint8_t _pixel_color_depth_bits = PIXEL_COLOR_DEPTH_BITS_DEFAULT - ) : mx_width(_w), - mx_height(_h), - chain_length(_chain), - gpio(_pinmap), - driver(_drv), - double_buff(_dbuff), - i2sspeed(_i2sspeed), - latch_blanking(_latblk), - clkphase(_clockphase), - min_refresh_rate(_min_refresh_rate) + HUB75_I2S_CFG( + uint16_t _w = MATRIX_WIDTH, + uint16_t _h = MATRIX_HEIGHT, + uint16_t _chain = CHAIN_LENGTH, + i2s_pins _pinmap = { + R1_PIN_DEFAULT, G1_PIN_DEFAULT, B1_PIN_DEFAULT, R2_PIN_DEFAULT, G2_PIN_DEFAULT, B2_PIN_DEFAULT, + A_PIN_DEFAULT, B_PIN_DEFAULT, C_PIN_DEFAULT, D_PIN_DEFAULT, E_PIN_DEFAULT, + LAT_PIN_DEFAULT, OE_PIN_DEFAULT, CLK_PIN_DEFAULT}, + shift_driver _drv = SHIFTREG, bool _dbuff = false, clk_speed _i2sspeed = HZ_15M, + uint8_t _latblk = DEFAULT_LAT_BLANKING, // Anything > 1 seems to cause artefacts on ICS panels + bool _clockphase = true, uint16_t _min_refresh_rate = 60, uint8_t _pixel_color_depth_bits = PIXEL_COLOR_DEPTH_BITS_DEFAULT) : mx_width(_w), mx_height(_h), chain_length(_chain), gpio(_pinmap), driver(_drv), double_buff(_dbuff), i2sspeed(_i2sspeed), latch_blanking(_latblk), clkphase(_clockphase), min_refresh_rate(_min_refresh_rate) { setPixelColorDepthBits(_pixel_color_depth_bits); } - //pixel_color_depth_bits must be between 12 and 2, and mask_offset needs to be calculated accordently - //so they have to be private with getter (and setter) - void setPixelColorDepthBits(uint8_t _pixel_color_depth_bits){ - if(_pixel_color_depth_bits > PIXEL_COLOR_DEPTH_BITS_MAX || _pixel_color_depth_bits < 2){ - - if(_pixel_color_depth_bits > PIXEL_COLOR_DEPTH_BITS_MAX){ + // pixel_color_depth_bits must be between 12 and 2, and mask_offset needs to be calculated accordently + // so they have to be private with getter (and setter) + void setPixelColorDepthBits(uint8_t _pixel_color_depth_bits) + { + if (_pixel_color_depth_bits > PIXEL_COLOR_DEPTH_BITS_MAX || _pixel_color_depth_bits < 2) + { + + if (_pixel_color_depth_bits > PIXEL_COLOR_DEPTH_BITS_MAX) + { pixel_color_depth_bits = PIXEL_COLOR_DEPTH_BITS_MAX; - }else{ + } + else + { pixel_color_depth_bits = 2; } ESP_LOGW("HUB75_I2S_CFG", "Invalid pixel_color_depth_bits (%d): 2 <= pixel_color_depth_bits <= %d, choosing nearest valid %d", _pixel_color_depth_bits, PIXEL_COLOR_DEPTH_BITS_MAX, pixel_color_depth_bits); - }else{ + } + else + { pixel_color_depth_bits = _pixel_color_depth_bits; } } - uint8_t getPixelColorDepthBits(){ + uint8_t getPixelColorDepthBits() + { return pixel_color_depth_bits; } - private: - //these were priviously handeld as defines (PIXEL_COLOR_DEPTH_BITS, MASK_OFFSET) - //to make it changable after compilation, it is now part of the config - uint8_t pixel_color_depth_bits; +private: + // these were priviously handeld as defines (PIXEL_COLOR_DEPTH_BITS, MASK_OFFSET) + // to make it changable after compilation, it is now part of the config + uint8_t pixel_color_depth_bits; }; // end of structure HUB75_I2S_CFG - - -/***************************************************************************************/ +/***************************************************************************************/ #ifdef USE_GFX_ROOT -class MatrixPanel_I2S_DMA : public GFX { +class MatrixPanel_I2S_DMA : public GFX +{ #elif !defined NO_GFX -class MatrixPanel_I2S_DMA : public Adafruit_GFX { +class MatrixPanel_I2S_DMA : public Adafruit_GFX +{ #else -class MatrixPanel_I2S_DMA { +class MatrixPanel_I2S_DMA +{ #endif // ------- PUBLIC ------- - public: - - /** - * MatrixPanel_I2S_DMA - * - * default predefined values are used for matrix configuration - * - */ - MatrixPanel_I2S_DMA() +public: + /** + * MatrixPanel_I2S_DMA + * + * default predefined values are used for matrix configuration + * + */ + MatrixPanel_I2S_DMA() #ifdef USE_GFX_ROOT : GFX(MATRIX_WIDTH, MATRIX_HEIGHT) #elif !defined NO_GFX : Adafruit_GFX(MATRIX_WIDTH, MATRIX_HEIGHT) #endif - {} + { + } - /** - * MatrixPanel_I2S_DMA - * - * @param {HUB75_I2S_CFG} opts : structure with matrix configuration - * - */ - MatrixPanel_I2S_DMA(const HUB75_I2S_CFG& opts) -#ifdef USE_GFX_ROOT - : GFX(opts.mx_width*opts.chain_length, opts.mx_height) + /** + * MatrixPanel_I2S_DMA + * + * @param {HUB75_I2S_CFG} opts : structure with matrix configuration + * + */ + MatrixPanel_I2S_DMA(const HUB75_I2S_CFG &opts) +#ifdef USE_GFX_ROOT + : GFX(opts.mx_width * opts.chain_length, opts.mx_height) #elif !defined NO_GFX - : Adafruit_GFX(opts.mx_width*opts.chain_length, opts.mx_height) -#endif - { - setCfg(opts); - } + : Adafruit_GFX(opts.mx_width * opts.chain_length, opts.mx_height) +#endif + { + setCfg(opts); + } - /* Propagate the DMA pin configuration, allocate DMA buffs and start data output, initially blank */ - bool begin(){ - - if (initialized) return true; // we don't do this twice or more! - if(!config_set) return false; - - ESP_LOGI("begin()", "Using GPIO %d for R1_PIN", m_cfg.gpio.r1); - ESP_LOGI("begin()", "Using GPIO %d for G1_PIN", m_cfg.gpio.g1); - ESP_LOGI("begin()", "Using GPIO %d for B1_PIN", m_cfg.gpio.b1); - ESP_LOGI("begin()", "Using GPIO %d for R2_PIN", m_cfg.gpio.r2); - ESP_LOGI("begin()", "Using GPIO %d for G2_PIN", m_cfg.gpio.g2); - ESP_LOGI("begin()", "Using GPIO %d for B2_PIN", m_cfg.gpio.b2); - ESP_LOGI("begin()", "Using GPIO %d for A_PIN", m_cfg.gpio.a); - ESP_LOGI("begin()", "Using GPIO %d for B_PIN", m_cfg.gpio.b); - ESP_LOGI("begin()", "Using GPIO %d for C_PIN", m_cfg.gpio.c); - ESP_LOGI("begin()", "Using GPIO %d for D_PIN", m_cfg.gpio.d); - ESP_LOGI("begin()", "Using GPIO %d for E_PIN", m_cfg.gpio.e); - ESP_LOGI("begin()", "Using GPIO %d for LAT_PIN", m_cfg.gpio.lat); - ESP_LOGI("begin()", "Using GPIO %d for OE_PIN", m_cfg.gpio.oe); - ESP_LOGI("begin()", "Using GPIO %d for CLK_PIN", m_cfg.gpio.clk); - - - // initialize some specific panel drivers - if (m_cfg.driver) - shiftDriver(m_cfg); - - #if defined(SPIRAM_DMA_BUFFER) - // Trick library into dropping colour depth slightly when using PSRAM. - // Actual output clockrate override occurs in configureDMA - m_cfg.i2sspeed = HUB75_I2S_CFG::HZ_8M; - #endif - - /* As DMA buffers are dynamically allocated, we must allocated in begin() - * Ref: https://github.com/espressif/arduino-esp32/issues/831 - */ - if ( !allocateDMAmemory() ) { return false; } // couldn't even get the basic ram required. - - - // Flush the DMA buffers prior to configuring DMA - Avoid visual artefacts on boot. - resetbuffers(); // Must fill the DMA buffer with the initial output bit sequence or the panel will display garbage - - // Setup the ESP32 DMA Engine. Sprite_TM built this stuff. - configureDMA(m_cfg); //DMA and I2S configuration and setup - - //showDMABuffer(); // show backbuf_id of 0 - - if (!initialized) { - ESP_LOGE("being()", "MatrixPanel_I2S_DMA::begin() failed!"); - } + /* Propagate the DMA pin configuration, allocate DMA buffs and start data output, initially blank */ + bool begin() + { - return initialized; + if (initialized) + return true; // we don't do this twice or more! + if (!config_set) + return false; + + ESP_LOGI("begin()", "Using GPIO %d for R1_PIN", m_cfg.gpio.r1); + ESP_LOGI("begin()", "Using GPIO %d for G1_PIN", m_cfg.gpio.g1); + ESP_LOGI("begin()", "Using GPIO %d for B1_PIN", m_cfg.gpio.b1); + ESP_LOGI("begin()", "Using GPIO %d for R2_PIN", m_cfg.gpio.r2); + ESP_LOGI("begin()", "Using GPIO %d for G2_PIN", m_cfg.gpio.g2); + ESP_LOGI("begin()", "Using GPIO %d for B2_PIN", m_cfg.gpio.b2); + ESP_LOGI("begin()", "Using GPIO %d for A_PIN", m_cfg.gpio.a); + ESP_LOGI("begin()", "Using GPIO %d for B_PIN", m_cfg.gpio.b); + ESP_LOGI("begin()", "Using GPIO %d for C_PIN", m_cfg.gpio.c); + ESP_LOGI("begin()", "Using GPIO %d for D_PIN", m_cfg.gpio.d); + ESP_LOGI("begin()", "Using GPIO %d for E_PIN", m_cfg.gpio.e); + ESP_LOGI("begin()", "Using GPIO %d for LAT_PIN", m_cfg.gpio.lat); + ESP_LOGI("begin()", "Using GPIO %d for OE_PIN", m_cfg.gpio.oe); + ESP_LOGI("begin()", "Using GPIO %d for CLK_PIN", m_cfg.gpio.clk); + + // initialize some specific panel drivers + if (m_cfg.driver) + shiftDriver(m_cfg); + +#if defined(SPIRAM_DMA_BUFFER) + // Trick library into dropping colour depth slightly when using PSRAM. + // Actual output clockrate override occurs in configureDMA + m_cfg.i2sspeed = HUB75_I2S_CFG::HZ_8M; +#endif - } + /* As DMA buffers are dynamically allocated, we must allocated in begin() + * Ref: https://github.com/espressif/arduino-esp32/issues/831 + */ + if (!allocateDMAmemory()) + { + return false; + } // couldn't even get the basic ram required. - // Obj destructor - ~MatrixPanel_I2S_DMA(){ + // Flush the DMA buffers prior to configuring DMA - Avoid visual artefacts on boot. + resetbuffers(); // Must fill the DMA buffer with the initial output bit sequence or the panel will display garbage - dma_bus.release(); - + // Setup the ESP32 DMA Engine. Sprite_TM built this stuff. + configureDMA(m_cfg); // DMA and I2S configuration and setup + + // showDMABuffer(); // show backbuf_id of 0 + + if (!initialized) + { + ESP_LOGE("being()", "MatrixPanel_I2S_DMA::begin() failed!"); } + return initialized; + } + + // Obj destructor + ~MatrixPanel_I2S_DMA() + { - /* - * overload for compatibility - */ - bool begin(int r1, int g1 = G1_PIN_DEFAULT, int b1 = B1_PIN_DEFAULT, int r2 = R2_PIN_DEFAULT, int g2 = G2_PIN_DEFAULT, int b2 = B2_PIN_DEFAULT, int a = A_PIN_DEFAULT, int b = B_PIN_DEFAULT, int c = C_PIN_DEFAULT, int d = D_PIN_DEFAULT, int e = E_PIN_DEFAULT, int lat = LAT_PIN_DEFAULT, int oe = OE_PIN_DEFAULT, int clk = CLK_PIN_DEFAULT); - bool begin(const HUB75_I2S_CFG& cfg); + dma_bus.release(); + } - // Adafruit's BASIC DRAW API (565 colour format) - virtual void drawPixel(int16_t x, int16_t y, uint16_t color); // overwrite adafruit implementation - virtual void fillScreen(uint16_t color); // overwrite adafruit implementation + /* + * overload for compatibility + */ + bool begin(int r1, int g1 = G1_PIN_DEFAULT, int b1 = B1_PIN_DEFAULT, int r2 = R2_PIN_DEFAULT, int g2 = G2_PIN_DEFAULT, int b2 = B2_PIN_DEFAULT, int a = A_PIN_DEFAULT, int b = B_PIN_DEFAULT, int c = C_PIN_DEFAULT, int d = D_PIN_DEFAULT, int e = E_PIN_DEFAULT, int lat = LAT_PIN_DEFAULT, int oe = OE_PIN_DEFAULT, int clk = CLK_PIN_DEFAULT); + bool begin(const HUB75_I2S_CFG &cfg); - /** - * A wrapper to fill whatever selected DMA buffer / screen with black - */ - inline void clearScreen() { updateMatrixDMABuffer(0,0,0); }; + // Adafruit's BASIC DRAW API (565 colour format) + virtual void drawPixel(int16_t x, int16_t y, uint16_t color); // overwrite adafruit implementation + virtual void fillScreen(uint16_t color); // overwrite adafruit implementation + + /** + * A wrapper to fill whatever selected DMA buffer / screen with black + */ + inline void clearScreen() { updateMatrixDMABuffer(0, 0, 0); }; #ifndef NO_FAST_FUNCTIONS - /** - * @brief - override Adafruit's FastVLine - * this works faster than multiple consecutive pixel by pixel drawPixel() call - */ - virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color){ - uint8_t r, g, b; - color565to888(color, r, g, b); - startWrite(); - - int16_t w = 1; - transform( x, y, w, h); - if( h > w ) - vlineDMA( x, y, h, r, g, b); - else - hlineDMA( x, y, w, r, g, b); - - endWrite(); - } - // rgb888 overload - virtual inline void drawFastVLine(int16_t x, int16_t y, int16_t h, uint8_t r, uint8_t g, uint8_t b){ - int16_t w = 1; - transform( x, y, w, h); - if( h > w ) - vlineDMA( x, y, h, r, g, b); - else - hlineDMA( x, y, w, r, g, b); - }; + /** + * @brief - override Adafruit's FastVLine + * this works faster than multiple consecutive pixel by pixel drawPixel() call + */ + virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) + { + uint8_t r, g, b; + color565to888(color, r, g, b); + startWrite(); + + int16_t w = 1; + transform(x, y, w, h); + if (h > w) + vlineDMA(x, y, h, r, g, b); + else + hlineDMA(x, y, w, r, g, b); + + endWrite(); + } + // rgb888 overload + virtual inline void drawFastVLine(int16_t x, int16_t y, int16_t h, uint8_t r, uint8_t g, uint8_t b) + { + int16_t w = 1; + transform(x, y, w, h); + if (h > w) + vlineDMA(x, y, h, r, g, b); + else + hlineDMA(x, y, w, r, g, b); + }; - /** - * @brief - override Adafruit's FastHLine - * this works faster than multiple consecutive pixel by pixel drawPixel() call - */ - virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color){ - uint8_t r, g, b; - color565to888(color, r, g, b); - startWrite(); - - int16_t h = 1; - transform( x, y, w, h); - if( h > w ) - vlineDMA( x, y, h, r, g, b); - else - hlineDMA( x, y, w, r, g, b); - - endWrite(); - } - // rgb888 overload - virtual inline void drawFastHLine(int16_t x, int16_t y, int16_t w, uint8_t r, uint8_t g, uint8_t b){ - int16_t h = 1; - transform( x, y, w, h); - if( h > w ) - vlineDMA( x, y, h, r, g, b); - else - hlineDMA( x, y, w, r, g, b); - }; + /** + * @brief - override Adafruit's FastHLine + * this works faster than multiple consecutive pixel by pixel drawPixel() call + */ + virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) + { + uint8_t r, g, b; + color565to888(color, r, g, b); + startWrite(); + + int16_t h = 1; + transform(x, y, w, h); + if (h > w) + vlineDMA(x, y, h, r, g, b); + else + hlineDMA(x, y, w, r, g, b); + + endWrite(); + } + // rgb888 overload + virtual inline void drawFastHLine(int16_t x, int16_t y, int16_t w, uint8_t r, uint8_t g, uint8_t b) + { + int16_t h = 1; + transform(x, y, w, h); + if (h > w) + vlineDMA(x, y, h, r, g, b); + else + hlineDMA(x, y, w, r, g, b); + }; - /** - * @brief - override Adafruit's fillRect - * this works much faster than multiple consecutive per-pixel drawPixel() calls - */ - virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color){ - uint8_t r, g, b; - color565to888(color, r, g, b); - startWrite(); - transform( x, y, w, h); - fillRectDMA( x, y, w, h, r, g, b); - endWrite(); - } - // rgb888 overload - virtual inline void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t r, uint8_t g, uint8_t b){ - startWrite(); - transform( x, y, w, h); - fillRectDMA( x, y, w, h, r, g, b); - endWrite(); - } + /** + * @brief - override Adafruit's fillRect + * this works much faster than multiple consecutive per-pixel drawPixel() calls + */ + virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) + { + uint8_t r, g, b; + color565to888(color, r, g, b); + startWrite(); + transform(x, y, w, h); + fillRectDMA(x, y, w, h, r, g, b); + endWrite(); + } + // rgb888 overload + virtual inline void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t r, uint8_t g, uint8_t b) + { + startWrite(); + transform(x, y, w, h); + fillRectDMA(x, y, w, h, r, g, b); + endWrite(); + } #endif - void fillScreenRGB888(uint8_t r, uint8_t g, uint8_t b); - void drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b); - + void fillScreenRGB888(uint8_t r, uint8_t g, uint8_t b); + void drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b); + #ifdef USE_GFX_ROOT - // 24bpp FASTLED CRGB colour struct support - void fillScreen(CRGB color); - void drawPixel(int16_t x, int16_t y, CRGB color); -#endif - - void drawIcon (int *ico, int16_t x, int16_t y, int16_t cols, int16_t rows); - - // Colour 444 is a 4 bit scale, so 0 to 15, colour 565 takes a 0-255 bit value, so scale up by 255/15 (i.e. 17)! - static uint16_t color444(uint8_t r, uint8_t g, uint8_t b) { return color565(r*17,g*17,b*17); } - - // Converts RGB888 to RGB565 - static uint16_t color565(uint8_t r, uint8_t g, uint8_t b); // This is what is used by Adafruit GFX! - - // Converts RGB333 to RGB565 - static uint16_t color333(uint8_t r, uint8_t g, uint8_t b); // This is what is used by Adafruit GFX! Not sure why they have a capital 'C' for this particular function. - - /** - * @brief - convert RGB565 to RGB888 - * @param uint16_t colour - RGB565 input colour - * @param uint8_t &r, &g, &b - refs to variables where converted colors would be emplaced - */ - static void color565to888(const uint16_t color, uint8_t &r, uint8_t &g, uint8_t &b); + // 24bpp FASTLED CRGB colour struct support + void fillScreen(CRGB color); + void drawPixel(int16_t x, int16_t y, CRGB color); +#endif + void drawIcon(int *ico, int16_t x, int16_t y, int16_t cols, int16_t rows); - inline void flipDMABuffer() - { - if ( !m_cfg.double_buff) { return; } + // Colour 444 is a 4 bit scale, so 0 to 15, colour 565 takes a 0-255 bit value, so scale up by 255/15 (i.e. 17)! + static uint16_t color444(uint8_t r, uint8_t g, uint8_t b) { return color565(r * 17, g * 17, b * 17); } + + // Converts RGB888 to RGB565 + static uint16_t color565(uint8_t r, uint8_t g, uint8_t b); // This is what is used by Adafruit GFX! + + // Converts RGB333 to RGB565 + static uint16_t color333(uint8_t r, uint8_t g, uint8_t b); // This is what is used by Adafruit GFX! Not sure why they have a capital 'C' for this particular function. + + /** + * @brief - convert RGB565 to RGB888 + * @param uint16_t colour - RGB565 input colour + * @param uint8_t &r, &g, &b - refs to variables where converted colors would be emplaced + */ + static void color565to888(const uint16_t color, uint8_t &r, uint8_t &g, uint8_t &b); + + inline void flipDMABuffer() + { + if (!m_cfg.double_buff) + { + return; + } + + if (back_buffer_id == 0) // back buffer is 0 (dmadesc_a) + { + fb = &frame_buffer[1]; + } + else + { + fb = &frame_buffer[0]; + } + + dma_bus.flip_dma_output_buffer(back_buffer_id); + + back_buffer_id ^= 1; - // while (active_gfx_writes) { } // wait a bit ? - // initialized = false; - dma_bus.flip_dma_output_buffer( back_buffer_id ); // initialized = true; - /* - i2s_parallel_set_previous_buffer_not_free(); - // Wait before we allow any writing to the buffer. Stop flicker. - while(i2s_parallel_is_previous_buffer_free() == false) { } - - i2s_parallel_flip_to_buffer(ESP32_I2S_DEVICE, back_buffer_id); - // Flip to other buffer as the backbuffer. - // i.e. Graphic changes happen to this buffer, but aren't displayed until flipDMABuffer() is called again. - back_buffer_id ^= 1; - - i2s_parallel_set_previous_buffer_not_free(); - // Wait before we allow any writing to the buffer. Stop flicker. - while(i2s_parallel_is_previous_buffer_free() == false) { } - */ + /* + i2s_parallel_set_previous_buffer_not_free(); + // Wait before we allow any writing to the buffer. Stop flicker. + while(i2s_parallel_is_previous_buffer_free() == false) { } + + i2s_parallel_flip_to_buffer(ESP32_I2S_DEVICE, back_buffer_id); + // Flip to other buffer as the backbuffer. + // i.e. Graphic changes happen to this buffer, but aren't displayed until flipDMABuffer() is called again. + back_buffer_id ^= 1; + + i2s_parallel_set_previous_buffer_not_free(); + // Wait before we allow any writing to the buffer. Stop flicker. + while(i2s_parallel_is_previous_buffer_free() == false) { } + */ + } + /** + * @param uint8_t b - 8-bit brightness value + */ + void setBrightness(const uint8_t b) + { + if (!initialized) + { + ESP_LOGI("setBrightness()", "Tried to set output brightness before begin()"); + return; } - - /** - * @param uint8_t b - 8-bit brightness value - */ - void setBrightness(const uint8_t b) - { - if (!initialized) - { - ESP_LOGI("setBrightness()", "Tried to set output brightness before begin()"); - return; - } - - brightness = b; - brtCtrlOEv2(b, 0); - - if (m_cfg.double_buff) { - brtCtrlOEv2(b, 1); - } - - - } - - // Takes a value that is between 0 and MATRIX_WIDTH-1 - /* + + brightness = b; + brtCtrlOEv2(b, 0); + + if (m_cfg.double_buff) + { + brtCtrlOEv2(b, 1); + } + } + + // Takes a value that is between 0 and MATRIX_WIDTH-1 + /* void setPanelBrightness(int b) { if (!initialized) - { - ESP_LOGI("setPanelBrightness()", "Tried to set output brightness before begin()"); + { + ESP_LOGI("setPanelBrightness()", "Tried to set output brightness before begin()"); return; - } - + } + // Change to set the brightness of the display, range of 1 to matrixWidth (i.e. 1 - 64) // brightness = b * PIXELS_PER_ROW / 256; @@ -634,242 +675,266 @@ class MatrixPanel_I2S_DMA { if (m_cfg.double_buff) brtCtrlOE(b, 1); } - */ - - /** - * @param uint8_t b - 8-bit brightness value - */ - void setPanelBrightness(const uint8_t b) - { - setBrightness(b); - } + */ - /** - * this is just a wrapper to control brightness - * with an 8-bit value (0-255), very popular in FastLED-based sketches :) - * @param uint8_t b - 8-bit brightness value - */ - void setBrightness8(const uint8_t b) - { - setBrightness(b); - //setPanelBrightness(b * PIXELS_PER_ROW / 256); - } - - - /** - * @brief - Sets how many clock cycles to blank OE before/after LAT signal change - * @param uint8_t pulses - clocks before/after OE - * default is DEFAULT_LAT_BLANKING - * Max is MAX_LAT_BLANKING - * @returns - new value for m_cfg.latch_blanking - */ - uint8_t setLatBlanking(uint8_t pulses); + /** + * @param uint8_t b - 8-bit brightness value + */ + void setPanelBrightness(const uint8_t b) + { + setBrightness(b); + } - /** - * Get a class configuration struct - * - */ - const HUB75_I2S_CFG& getCfg() const {return m_cfg;}; - - inline bool setCfg(const HUB75_I2S_CFG& cfg){ - if(initialized) return false; - - m_cfg = cfg; - PIXELS_PER_ROW = m_cfg.mx_width * m_cfg.chain_length; - ROWS_PER_FRAME = m_cfg.mx_height / MATRIX_ROWS_IN_PARALLEL; - MASK_OFFSET = 16 - m_cfg.getPixelColorDepthBits(); - - config_set = true; - return true; - } - - /** - * Stop the ESP32 DMA Engine. Screen will forever be black until next ESP reboot. - */ - void stopDMAoutput() { - resetbuffers(); - //i2s_parallel_stop_dma(ESP32_I2S_DEVICE); - dma_bus.dma_transfer_stop(); - } - - void startWrite() { - //ESP_LOGI("TAG", "startWrite() called"); - active_gfx_writes++; - } - + /** + * this is just a wrapper to control brightness + * with an 8-bit value (0-255), very popular in FastLED-based sketches :) + * @param uint8_t b - 8-bit brightness value + */ + void setBrightness8(const uint8_t b) + { + setBrightness(b); + // setPanelBrightness(b * PIXELS_PER_ROW / 256); + } + + /** + * @brief - Sets how many clock cycles to blank OE before/after LAT signal change + * @param uint8_t pulses - clocks before/after OE + * default is DEFAULT_LAT_BLANKING + * Max is MAX_LAT_BLANKING + * @returns - new value for m_cfg.latch_blanking + */ + uint8_t setLatBlanking(uint8_t pulses); + + /** + * Get a class configuration struct + * + */ + const HUB75_I2S_CFG &getCfg() const { return m_cfg; }; + + inline bool setCfg(const HUB75_I2S_CFG &cfg) + { + if (initialized) + return false; + + m_cfg = cfg; + PIXELS_PER_ROW = m_cfg.mx_width * m_cfg.chain_length; + ROWS_PER_FRAME = m_cfg.mx_height / MATRIX_ROWS_IN_PARALLEL; + MASK_OFFSET = 16 - m_cfg.getPixelColorDepthBits(); + + config_set = true; + return true; + } - void endWrite() { - active_gfx_writes--; - } + /** + * Stop the ESP32 DMA Engine. Screen will forever be black until next ESP reboot. + */ + void stopDMAoutput() + { + resetbuffers(); + // i2s_parallel_stop_dma(ESP32_I2S_DEVICE); + dma_bus.dma_transfer_stop(); + } + + void startWrite() + { + // ESP_LOGI("TAG", "startWrite() called"); + active_gfx_writes++; + } + + void endWrite() + { + active_gfx_writes--; + } // ------- PROTECTED ------- // those might be useful for child classes, like VirtualMatrixPanel - protected: - - /** - * @brief - clears and reinitializes colour/control data in DMA buffs - * When allocated, DMA buffs might be dirty, so we need to blank it and initialize ABCDE,LAT,OE control bits. - * Those control bits are constants during the entire DMA sweep and never changed when updating just pixel colour data - * so we could set it once on DMA buffs initialization and forget. - * This effectively clears buffers to blank BLACK and makes it ready to display output. - * (Brightness control via OE bit manipulation is another case) - */ - void clearFrameBuffer(bool _buff_id = 0); +protected: + /** + * @brief - clears and reinitializes colour/control data in DMA buffs + * When allocated, DMA buffs might be dirty, so we need to blank it and initialize ABCDE,LAT,OE control bits. + * Those control bits are constants during the entire DMA sweep and never changed when updating just pixel colour data + * so we could set it once on DMA buffs initialization and forget. + * This effectively clears buffers to blank BLACK and makes it ready to display output. + * (Brightness control via OE bit manipulation is another case) + */ + void clearFrameBuffer(bool _buff_id = 0); - /* Update a specific pixel in the DMA buffer to a colour */ - void updateMatrixDMABuffer(uint16_t x, uint16_t y, uint8_t red, uint8_t green, uint8_t blue); - - /* Update the entire DMA buffer (aka. The RGB Panel) a certain colour (wipe the screen basically) */ - void updateMatrixDMABuffer(uint8_t red, uint8_t green, uint8_t blue); + /* Update a specific pixel in the DMA buffer to a colour */ + void updateMatrixDMABuffer(uint16_t x, uint16_t y, uint8_t red, uint8_t green, uint8_t blue); - /** - * wipes DMA buffer(s) and reset all colour/service bits - */ - inline void resetbuffers(){ - - clearFrameBuffer(); - brtCtrlOEv2(brightness, 0); - - if (m_cfg.double_buff){ - clearFrameBuffer(1); - brtCtrlOEv2(brightness, 1); - } - - } + /* Update the entire DMA buffer (aka. The RGB Panel) a certain colour (wipe the screen basically) */ + void updateMatrixDMABuffer(uint8_t red, uint8_t green, uint8_t blue); + /** + * wipes DMA buffer(s) and reset all colour/service bits + */ + inline void resetbuffers() + { + + // flipDMABuffer(); + fb = &frame_buffer[0]; + + clearFrameBuffer(0); // buffer ID is not used + brtCtrlOEv2(brightness, 0); // buffer ID is not used + + fb = &frame_buffer[1]; + clearFrameBuffer(1); // buffer ID is not used + brtCtrlOEv2(brightness, 1); // buffer ID is not used + } #ifndef NO_FAST_FUNCTIONS - /** - * @brief - update DMA buff drawing horizontal line at specified coordinates - * @param x_ccord - line start coordinate x - * @param y_ccord - line start coordinate y - * @param l - line length - * @param r,g,b, - RGB888 colour - */ - void hlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, uint8_t red, uint8_t green, uint8_t blue); - - /** - * @brief - update DMA buff drawing horizontal line at specified coordinates - * @param x_ccord - line start coordinate x - * @param y_ccord - line start coordinate y - * @param l - line length - * @param r,g,b, - RGB888 colour - */ - void vlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, uint8_t red, uint8_t green, uint8_t blue); - - /** - * @brief - update DMA buff drawing a rectangular at specified coordinates - * uses Fast H/V line draw internally, works faster than multiple consecutive pixel by pixel calls to updateMatrixDMABuffer() - * @param int16_t x, int16_t y - coordinates of a top-left corner - * @param int16_t w, int16_t h - width and height of a rectangular, min is 1 px - * @param uint8_t r - RGB888 colour - * @param uint8_t g - RGB888 colour - * @param uint8_t b - RGB888 colour - */ - void fillRectDMA(int16_t x_coord, int16_t y_coord, int16_t w, int16_t h, uint8_t r, uint8_t g, uint8_t b); + /** + * @brief - update DMA buff drawing horizontal line at specified coordinates + * @param x_ccord - line start coordinate x + * @param y_ccord - line start coordinate y + * @param l - line length + * @param r,g,b, - RGB888 colour + */ + void hlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, uint8_t red, uint8_t green, uint8_t blue); + + /** + * @brief - update DMA buff drawing horizontal line at specified coordinates + * @param x_ccord - line start coordinate x + * @param y_ccord - line start coordinate y + * @param l - line length + * @param r,g,b, - RGB888 colour + */ + void vlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, uint8_t red, uint8_t green, uint8_t blue); + + /** + * @brief - update DMA buff drawing a rectangular at specified coordinates + * uses Fast H/V line draw internally, works faster than multiple consecutive pixel by pixel calls to updateMatrixDMABuffer() + * @param int16_t x, int16_t y - coordinates of a top-left corner + * @param int16_t w, int16_t h - width and height of a rectangular, min is 1 px + * @param uint8_t r - RGB888 colour + * @param uint8_t g - RGB888 colour + * @param uint8_t b - RGB888 colour + */ + void fillRectDMA(int16_t x_coord, int16_t y_coord, int16_t w, int16_t h, uint8_t r, uint8_t g, uint8_t b); #endif - // ------- PRIVATE ------- - private: + // ------- PRIVATE ------- +private: + /* Calculate the memory available for DMA use, do some other stuff, and allocate accordingly */ + bool allocateDMAmemory(); - /* Calculate the memory available for DMA use, do some other stuff, and allocate accordingly */ - bool allocateDMAmemory(); + /* Setup the DMA Link List chain and initiate the ESP32 DMA engine */ + void configureDMA(const HUB75_I2S_CFG &opts); - /* Setup the DMA Link List chain and initiate the ESP32 DMA engine */ - void configureDMA(const HUB75_I2S_CFG& opts); + /** + * pre-init procedures for specific drivers + * + */ + void shiftDriver(const HUB75_I2S_CFG &opts); - /** - * pre-init procedures for specific drivers - * - */ - void shiftDriver(const HUB75_I2S_CFG& opts); + /** + * @brief - FM6124-family chips initialization routine + */ + void fm6124init(const HUB75_I2S_CFG &_cfg); - /** - * @brief - FM6124-family chips initialization routine - */ - void fm6124init(const HUB75_I2S_CFG& _cfg); + /** + * @brief - reset OE bits in DMA buffer in a way to control brightness + * @param brt - brightness level from 0 to row_width + * @param _buff_id - buffer id to control + */ + // void brtCtrlOE(int brt, const bool _buff_id=0); - /** - * @brief - reset OE bits in DMA buffer in a way to control brightness - * @param brt - brightness level from 0 to row_width - * @param _buff_id - buffer id to control - */ - //void brtCtrlOE(int brt, const bool _buff_id=0); - - /** - * @brief - reset OE bits in DMA buffer in a way to control brightness - * @param brt - brightness level from 0 to row_width - * @param _buff_id - buffer id to control - */ - void brtCtrlOEv2(uint8_t brt, const int _buff_id=0); - - /** - * @brief - transforms coordinates according to orientation - * @param x - x position origin - * @param y - y position origin - * @param w - rectangular width - * @param h - rectangular height - */ - void transform(int16_t &x, int16_t &y, int16_t &w, int16_t &h){ - #ifndef NO_GFX - int16_t t; - switch (rotation) { - case 1: t = _height - 1 - y - ( h - 1 ); y = x; x = t; t = h; h = w; w = t; return; - case 2: x = _width - 1 - x - ( w - 1 ); y = _height - 1 - y - ( h - 1 ); return; - case 3: t = y; y = _width - 1 - x - ( w - 1 ); x = t; t = h; h = w; w = t; return; - } - #endif - }; + /** + * @brief - reset OE bits in DMA buffer in a way to control brightness + * @param brt - brightness level from 0 to row_width + * @param _buff_id - buffer id to control + */ + void brtCtrlOEv2(uint8_t brt, const int _buff_id = 0); + /** + * @brief - transforms coordinates according to orientation + * @param x - x position origin + * @param y - y position origin + * @param w - rectangular width + * @param h - rectangular height + */ + void transform(int16_t &x, int16_t &y, int16_t &w, int16_t &h) + { +#ifndef NO_GFX + int16_t t; + switch (rotation) + { + case 1: + t = _height - 1 - y - (h - 1); + y = x; + x = t; + t = h; + h = w; + w = t; + return; + case 2: + x = _width - 1 - x - (w - 1); + y = _height - 1 - y - (h - 1); + return; + case 3: + t = y; + y = _width - 1 - x - (w - 1); + x = t; + t = h; + h = w; + w = t; + return; + } +#endif + }; - public: - /** - * Contains the resulting refresh rate (scan rate) that will be achieved - * based on the i2sspeed, colour depth and min_refresh_rate requested. - */ - int calculated_refresh_rate = 0; - protected: - Bus_Parallel16 dma_bus; - private: - - // Matrix i2s settings - HUB75_I2S_CFG m_cfg; - - /* Pixel data is organized from LSB to MSB sequentially by row, from row 0 to row matrixHeight/matrixRowsInParallel - * (two rows of pixels are refreshed in parallel) - * Memory is allocated (malloc'd) by the row, and not in one massive chunk, for flexibility. - * The whole DMA framebuffer is just a vector of pointers to structs with ESP32_I2S_DMA_STORAGE_TYPE arrays - * Since it's dimensions is unknown prior to class initialization, we just declare it here as empty struct and will do all allocations later. - * Refer to rowBitStruct to get the idea of it's internal structure - */ - frameStruct dma_buff; - - // ESP 32 DMA Linked List descriptor - int desccount = 0; - // lldesc_t * dmadesc_a = {0}; - // lldesc_t * dmadesc_b = {0}; - - int active_gfx_writes = 0; // How many async routines are 'drawing' (writing) to the DMA bit buffer. Function called from Adafruit_GFX draw routines like drawCircle etc. - int back_buffer_id = 0; // If using double buffer, which one is NOT active (ie. being displayed) to write too? - int brightness = 128; // If you get ghosting... reduce brightness level. ((60/64)*255) seems to be the limit before ghosting on a 64 pixel wide physical panel for some panels. - int lsbMsbTransitionBit = 0; // For colour depth calculations - - /* ESP32-HUB75-MatrixPanel-I2S-DMA functioning constants - * we should not those once object instance initialized it's DMA structs - * they weree const, but this lead to bugs, when the default constructor was called. - * So now they could be changed, but shouldn't. Maybe put a cpp lock around it, so it can't be changed after initialisation - */ - uint16_t PIXELS_PER_ROW = m_cfg.mx_width * m_cfg.chain_length; // number of pixels in a single row of all chained matrix modules (WIDTH of a combined matrix chain) - uint8_t ROWS_PER_FRAME = m_cfg.mx_height / MATRIX_ROWS_IN_PARALLEL; // RPF - rows per frame, either 16 or 32 depending on matrix module - uint8_t MASK_OFFSET = 16 - m_cfg.getPixelColorDepthBits(); +public: + /** + * Contains the resulting refresh rate (scan rate) that will be achieved + * based on the i2sspeed, colour depth and min_refresh_rate requested. + */ + int calculated_refresh_rate = 0; + +protected: + Bus_Parallel16 dma_bus; - // Other private variables - bool initialized = false; - bool config_set = false; +private: + // Matrix i2s settings + HUB75_I2S_CFG m_cfg; + + /* Pixel data is organized from LSB to MSB sequentially by row, from row 0 to row matrixHeight/matrixRowsInParallel + * (two rows of pixels are refreshed in parallel) + * Memory is allocated (malloc'd) by the row, and not in one massive chunk, for flexibility. + * The whole DMA framebuffer is just a vector of pointers to structs with ESP32_I2S_DMA_STORAGE_TYPE arrays + * Since it's dimensions is unknown prior to class initialization, we just declare it here as empty struct and will do all allocations later. + * Refer to rowBitStruct to get the idea of it's internal structure + */ + // frameStruct dma_buff; + + frameStruct frame_buffer[2]; + frameStruct *fb; // What framebuffer we are writing pixel changes to? (pointer to either frame_buffer[0] or frame_buffer[1] basically ) + + // ESP 32 DMA Linked List descriptor + int desccount = 0; + // lldesc_t * dmadesc_a = {0}; + // lldesc_t * dmadesc_b = {0}; + + int active_gfx_writes = 0; // How many async routines are 'drawing' (writing) to the DMA bit buffer. Function called from Adafruit_GFX draw routines like drawCircle etc. + int back_buffer_id = 0; // If using double buffer, which one is NOT active (ie. being displayed) to write too? + int brightness = 128; // If you get ghosting... reduce brightness level. ((60/64)*255) seems to be the limit before ghosting on a 64 pixel wide physical panel for some panels. + int lsbMsbTransitionBit = 0; // For colour depth calculations + + /* ESP32-HUB75-MatrixPanel-I2S-DMA functioning constants + * we should not those once object instance initialized it's DMA structs + * they weree const, but this lead to bugs, when the default constructor was called. + * So now they could be changed, but shouldn't. Maybe put a cpp lock around it, so it can't be changed after initialisation + */ + uint16_t PIXELS_PER_ROW = m_cfg.mx_width * m_cfg.chain_length; // number of pixels in a single row of all chained matrix modules (WIDTH of a combined matrix chain) + uint8_t ROWS_PER_FRAME = m_cfg.mx_height / MATRIX_ROWS_IN_PARALLEL; // RPF - rows per frame, either 16 or 32 depending on matrix module + uint8_t MASK_OFFSET = 16 - m_cfg.getPixelColorDepthBits(); + + // Other private variables + bool initialized = false; + bool config_set = false; }; // end Class header -/***************************************************************************************/ +/***************************************************************************************/ // https://stackoverflow.com/questions/5057021/why-are-c-inline-functions-in-the-header /* 2. functions declared in the header must be marked inline because otherwise, every translation unit which includes the header will contain a definition of the function, and the linker will complain about multiple definitions (a violation of the One Definition Rule). The inline keyword suppresses this, allowing multiple translation units to contain (identical) definitions. */ @@ -878,7 +943,8 @@ class MatrixPanel_I2S_DMA { * @param uint16_t colour - RGB565 input colour * @param uint8_t &r, &g, &b - refs to variables where converted colours would be emplaced */ -inline void MatrixPanel_I2S_DMA::color565to888(const uint16_t color, uint8_t &r, uint8_t &g, uint8_t &b){ +inline void MatrixPanel_I2S_DMA::color565to888(const uint16_t color, uint8_t &r, uint8_t &g, uint8_t &b) +{ r = ((((color >> 11) & 0x1F) * 527) + 23) >> 6; g = ((((color >> 5) & 0x3F) * 259) + 33) >> 6; b = (((color & 0x1F) * 527) + 23) >> 6; @@ -886,91 +952,92 @@ inline void MatrixPanel_I2S_DMA::color565to888(const uint16_t color, uint8_t &r, inline void MatrixPanel_I2S_DMA::drawPixel(int16_t x, int16_t y, uint16_t color) // adafruit virtual void override { - uint8_t r,g,b; - color565to888(color,r,g,b); - + uint8_t r, g, b; + color565to888(color, r, g, b); + int16_t w = 1, h = 1; - transform( x, y, w, h); - updateMatrixDMABuffer( x, y, r, g, b); -} + transform(x, y, w, h); + updateMatrixDMABuffer(x, y, r, g, b); +} -inline void MatrixPanel_I2S_DMA::fillScreen(uint16_t color) // adafruit virtual void override +inline void MatrixPanel_I2S_DMA::fillScreen(uint16_t color) // adafruit virtual void override { - uint8_t r,g,b; - color565to888(color,r,g,b); - + uint8_t r, g, b; + color565to888(color, r, g, b); + updateMatrixDMABuffer(r, g, b); // RGB only (no pixel coordinate) version of 'updateMatrixDMABuffer' -} +} -inline void MatrixPanel_I2S_DMA::drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g,uint8_t b) +inline void MatrixPanel_I2S_DMA::drawPixelRGB888(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b) { int16_t w = 1, h = 1; - transform( x, y, w, h); - updateMatrixDMABuffer( x, y, r, g, b); + transform(x, y, w, h); + updateMatrixDMABuffer(x, y, r, g, b); } -inline void MatrixPanel_I2S_DMA::fillScreenRGB888(uint8_t r, uint8_t g,uint8_t b) +inline void MatrixPanel_I2S_DMA::fillScreenRGB888(uint8_t r, uint8_t g, uint8_t b) { updateMatrixDMABuffer(r, g, b); // RGB only (no pixel coordinate) version of 'updateMatrixDMABuffer' -} +} #ifdef USE_GFX_ROOT // Support for CRGB values provided via FastLED -inline void MatrixPanel_I2S_DMA::drawPixel(int16_t x, int16_t y, CRGB color) +inline void MatrixPanel_I2S_DMA::drawPixel(int16_t x, int16_t y, CRGB color) { int16_t w = 1, h = 1; - transform( x, y, w, h); - updateMatrixDMABuffer( x, y, color.red, color.green, color.blue); + transform(x, y, w, h); + updateMatrixDMABuffer(x, y, color.red, color.green, color.blue); } -inline void MatrixPanel_I2S_DMA::fillScreen(CRGB color) +inline void MatrixPanel_I2S_DMA::fillScreen(CRGB color) { updateMatrixDMABuffer(color.red, color.green, color.blue); } #endif - // Pass 8-bit (each) R,G,B, get back 16-bit packed colour -//https://github.com/squix78/ILI9341Buffer/blob/master/ILI9341_SPI.cpp -inline uint16_t MatrixPanel_I2S_DMA::color565(uint8_t r, uint8_t g, uint8_t b) { +// https://github.com/squix78/ILI9341Buffer/blob/master/ILI9341_SPI.cpp +inline uint16_t MatrixPanel_I2S_DMA::color565(uint8_t r, uint8_t g, uint8_t b) +{ return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); } // Promote 3/3/3 RGB to Adafruit_GFX 5/6/5 RRRrrGGGgggBBBbb -inline uint16_t MatrixPanel_I2S_DMA::color333(uint8_t r, uint8_t g, uint8_t b) { - return ((r & 0x7) << 13) | ((r & 0x6) << 10) | ((g & 0x7) << 8) | ((g & 0x7) << 5) | ((b & 0x7) << 2) | ((b & 0x6) >> 1); +inline uint16_t MatrixPanel_I2S_DMA::color333(uint8_t r, uint8_t g, uint8_t b) +{ + return ((r & 0x7) << 13) | ((r & 0x6) << 10) | ((g & 0x7) << 8) | ((g & 0x7) << 5) | ((b & 0x7) << 2) | ((b & 0x6) >> 1); } -inline void MatrixPanel_I2S_DMA::drawIcon (int *ico, int16_t x, int16_t y, int16_t cols, int16_t rows) { -/* drawIcon draws a C style bitmap. -// Example 10x5px bitmap of a yellow sun -// - int half_sun [50] = { - 0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0x0000, - 0x0000, 0xffe0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, - 0x0000, 0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000, 0x0000, - 0xffe0, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0xffe0, - 0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000, - }; - - MatrixPanel_I2S_DMA matrix; +inline void MatrixPanel_I2S_DMA::drawIcon(int *ico, int16_t x, int16_t y, int16_t cols, int16_t rows) +{ + /* drawIcon draws a C style bitmap. + // Example 10x5px bitmap of a yellow sun + // + int half_sun [50] = { + 0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0x0000, + 0x0000, 0xffe0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, + 0x0000, 0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000, 0x0000, + 0xffe0, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0xffe0, + 0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000, + }; - matrix.drawIcon (half_sun, 0,0,10,5); -*/ + MatrixPanel_I2S_DMA matrix; + + matrix.drawIcon (half_sun, 0,0,10,5); + */ int i, j; - for (i = 0; i < rows; i++) { - for (j = 0; j < cols; j++) { - drawPixel (x + j, y + i, (uint16_t) ico[i * cols + j]); + for (i = 0; i < rows; i++) + { + for (j = 0; j < cols; j++) + { + drawPixel(x + j, y + i, (uint16_t)ico[i * cols + j]); } - } + } } - #endif - - // Credits: Louis Beaudoin <https://github.com/pixelmatix/SmartMatrix/tree/teensylc> // and Sprite_TM: https://www.esp32.com/viewtopic.php?f=17&t=3188 and https://www.esp32.com/viewtopic.php?f=13&t=3256 @@ -978,13 +1045,13 @@ inline void MatrixPanel_I2S_DMA::drawIcon (int *ico, int16_t x, int16_t y, int16 This is example code to driver a p3(2121)64*32 -style RGB LED display. These types of displays do not have memory and need to be refreshed continuously. The display has 2 RGB inputs, 4 inputs to select the active line, a pixel clock input, a latch enable input and an output-enable - input. The display can be seen as 2 64x16 displays consisting of the upper half and the lower half of the display. Each half has a separate + input. The display can be seen as 2 64x16 displays consisting of the upper half and the lower half of the display. Each half has a separate RGB pixel input, the rest of the inputs are shared. Each display half can only show one line of RGB pixels at a time: to do this, the RGB data for the line is input by setting the RGB input pins to the desired value for the first pixel, giving the display a clock pulse, setting the RGB input pins to the desired value for the second pixel, - giving a clock pulse, etc. Do this 64 times to clock in an entire row. The pixels will not be displayed yet: until the latch input is made high, - the display will still send out the previously clocked in line. Pulsing the latch input high will replace the displayed data with the data just + giving a clock pulse, etc. Do this 64 times to clock in an entire row. The pixels will not be displayed yet: until the latch input is made high, + the display will still send out the previously clocked in line. Pulsing the latch input high will replace the displayed data with the data just clocked in. The 4 line select inputs select where the currently active line is displayed: when provided with a binary number (0-15), the latched pixel data @@ -1004,8 +1071,8 @@ inline void MatrixPanel_I2S_DMA::drawIcon (int *ico, int16_t x, int16_t y, int16 Binary code modulation is somewhat like PWM, but easier to implement in our case. First, we define the time we would refresh the display without binary code modulation as the 'frame time'. For, say, a four-bit binary code modulation, the frame time is divided into 15 ticks of equal length. - We also define 4 subframes (0 to 3), defining which LEDs are on and which LEDs are off during that subframe. (Subframes are the same as a - normal frame in non-binary-coded-modulation mode, but are showed faster.) From our (non-monochrome) input image, we take the (8-bit: bit 7 + We also define 4 subframes (0 to 3), defining which LEDs are on and which LEDs are off during that subframe. (Subframes are the same as a + normal frame in non-binary-coded-modulation mode, but are showed faster.) From our (non-monochrome) input image, we take the (8-bit: bit 7 to bit 0) RGB pixel values. If the pixel values have bit 7 set, we turn the corresponding LED on in subframe 3. If they have bit 6 set, we turn on the corresponding LED in subframe 2, if bit 5 is set subframe 1, if bit 4 is set in subframe 0. @@ -1020,5 +1087,5 @@ inline void MatrixPanel_I2S_DMA::drawIcon (int *ico, int16_t x, int16_t y, int16 We use a front buffer/back buffer technique here to make sure the display is refreshed in one go and drawing artefacts do not reach the display. In practice, for small displays this is not really necessarily. - + */ diff --git a/src/platforms/esp32/esp32_i2s_parallel_dma.cpp b/src/platforms/esp32/esp32_i2s_parallel_dma.cpp index 4cfd01a..d309501 100644 --- a/src/platforms/esp32/esp32_i2s_parallel_dma.cpp +++ b/src/platforms/esp32/esp32_i2s_parallel_dma.cpp @@ -28,54 +28,28 @@ Modified heavily for the ESP32 HUB75 DMA library by: #include <driver/periph_ctrl.h> #include <soc/gpio_sig_map.h> -#include <Arduino.h> // Need to make sure thi is uncommented to get ESP_LOG output on (Arduino) Serial output!!!! +#include <Arduino.h> // Need to make sure this is uncommented to get ESP_LOG output on (Arduino) Serial output!!!! #include <esp_err.h> #include <esp_log.h> // Get CPU freq function. #include <soc/rtc.h> -/* -callback shiftCompleteCallback; -void setShiftCompleteCallback(callback f) { - shiftCompleteCallback = f; -} - -volatile int previousBufferOutputLoopCount = 0; -volatile bool previousBufferFree = true; - -static void IRAM_ATTR irq_hndlr(void* arg) { // if we use I2S1 (default) - - SET_PERI_REG_BITS(I2S_INT_CLR_REG(ESP32_I2S_DEVICE), I2S_OUT_EOF_INT_CLR_V, 1, I2S_OUT_EOF_INT_CLR_S); +volatile bool previousBufferFree = true; - previousBufferFree = true; - - - -} // end irq_hndlr -*/ +// Todo: handle IS20? (this is hard coded for I2S1 only) +static void IRAM_ATTR i2s_isr(void* arg) { + REG_WRITE(I2S_INT_CLR_REG(1), (REG_READ(I2S_INT_RAW_REG(1)) & 0xffffffc0) | 0x3f); - volatile int DRAM_ATTR active_dma_buffer_output_count = 0; - - void IRAM_ATTR irq_hndlr(void* arg) { - - // Clear flag so we can get retriggered - SET_PERI_REG_BITS(I2S_INT_CLR_REG(ESP32_I2S_DEVICE), I2S_OUT_EOF_INT_CLR_V, 1, I2S_OUT_EOF_INT_CLR_S); - - active_dma_buffer_output_count++; + // at this point, the previously active buffer is free, go ahead and write to it + previousBufferFree = true; +} - /* - if ( active_dma_buffer_output_count++ ) - { - // Disable DMA chain EOF interrupt until next requested flipbuffer. - // Otherwise we're needlessly generating interrupts we don't care about. - //SET_PERI_REG_BITS(I2S_INT_ENA_REG(ESP32_I2S_DEVICE), I2S_OUT_EOF_INT_ENA_V, 0, I2S_OUT_EOF_INT_ENA_S); - active_dma_buffer_output_count = 0; - } - */ +bool DRAM_ATTR i2s_parallel_is_previous_buffer_free() { + return previousBufferFree; +} - } // end irq_hndlr // Static i2s_dev_t* getDev() @@ -221,22 +195,6 @@ static void IRAM_ATTR irq_hndlr(void* arg) { // if we use I2S1 (default) ESP_LOGD("ESP32/S2", "Requested output clock frequency: %d Mhz", (freq/1000000)); // What is the current CPU frequency? -/* - rtc_cpu_freq_config_t conf; - rtc_clk_cpu_freq_get_config(&conf); - auto source_freq = conf.source_freq_mhz; - - - ESP_LOGD("ESP32/S2", "PLL (source) frequency: %d", source_freq); - ESP_LOGD("ESP32/S2", "CPU frequency: %d", conf.freq_mhz); -*/ - - /* - if(_div_num < 2 || _div_num > 16) { - - return false; - } - */ // Calculate clock divider for ESP32-S2 #if defined (CONFIG_IDF_TARGET_ESP32S2) @@ -303,7 +261,7 @@ static void IRAM_ATTR irq_hndlr(void* arg) { // if we use I2S1 (default) #endif - output_freq = output_freq + 0; // work around arudino 'unused var' issue if debug isn't enabled. + output_freq = output_freq + 0; // work around arudino 'unused var' issue if debug isn't enabled. ESP_LOGI("ESP32/S2", "Output frequency is %ld Mhz??", (output_freq/1000000/i2s_parallel_get_memory_width(ESP32_I2S_DEVICE, 16))); @@ -415,21 +373,12 @@ static void IRAM_ATTR irq_hndlr(void* arg) { // if we use I2S1 (default) /* If we have double buffering, then allocate an interrupt service routine function * that can be used for I2S0/I2S1 created interrupts. */ - if (_double_dma_buffer) { - // Get ISR setup - esp_err_t err = esp_intr_alloc(irq_source, - (int)(ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL1), - irq_hndlr, NULL, NULL); - - if(err) { - ESP_LOGE("ESP32/S2", "init() Failed to setup interrupt request handeler."); - return false; - } + // setup I2S Interrupt + SET_PERI_REG_BITS(I2S_INT_ENA_REG(1), I2S_OUT_EOF_INT_ENA_V, 1, I2S_OUT_EOF_INT_ENA_S); + // allocate a level 1 intterupt: lowest priority, as ISR isn't urgent and may take a long time to complete + esp_intr_alloc(ETS_I2S1_INTR_SOURCE, (int)(ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL1), i2s_isr, NULL, NULL); - // Don't do this here. Don't enable just yet. - // dev->int_ena.out_eof = 1; - } #if defined (CONFIG_IDF_TARGET_ESP32S2) @@ -490,7 +439,7 @@ static void IRAM_ATTR irq_hndlr(void* arg) { // if we use I2S1 (default) ESP_LOGD("ESP32/S2", "Allocating the second buffer (double buffer enabled)."); - _dmadesc_b= (HUB75_DMA_DESCRIPTOR_T*)heap_caps_malloc(sizeof(HUB75_DMA_DESCRIPTOR_T) * len, MALLOC_CAP_DMA); + _dmadesc_b = (HUB75_DMA_DESCRIPTOR_T*)heap_caps_malloc(sizeof(HUB75_DMA_DESCRIPTOR_T) * len, MALLOC_CAP_DMA); if (_dmadesc_b == nullptr) { @@ -611,20 +560,15 @@ static void IRAM_ATTR irq_hndlr(void* arg) { // if we use I2S1 (default) } // end - void Bus_Parallel16::flip_dma_output_buffer(int ¤t_back_buffer_id) // pass by reference so we can change in main matrixpanel class + void Bus_Parallel16::flip_dma_output_buffer(int buffer_id) // pass by reference so we can change in main matrixpanel class { // Setup interrupt handler which is focussed only on the (page 322 of Tech. Ref. Manual) // "I2S_OUT_EOF_INT: Triggered when rxlink has finished sending a packet" (when dma linked list with eof = 1 is hit) - //_dev->int_ena.out_eof = 1; - _dev->int_ena.out_eof = 1; // enable interrupt - - if ( current_back_buffer_id == 1) { + + if ( buffer_id == 1) { - _dmadesc_a[_dmadesc_last].qe.stqe_next = &_dmadesc_b[0]; // Start sending out _dmadesc_b (or buffer 1) - - active_dma_buffer_output_count = 0; - while (!active_dma_buffer_output_count) {} + _dmadesc_a[_dmadesc_last].qe.stqe_next = &_dmadesc_b[0]; // Start sending out _dmadesc_b (or buffer 1) //fix _dmadesc_ loop issue #407 //need to connect the up comming _dmadesc_ not the old one @@ -633,17 +577,15 @@ static void IRAM_ATTR irq_hndlr(void* arg) { // if we use I2S1 (default) } else { _dmadesc_b[_dmadesc_last].qe.stqe_next = &_dmadesc_a[0]; - - active_dma_buffer_output_count = 0; - while (!active_dma_buffer_output_count) {} - _dmadesc_a[_dmadesc_last].qe.stqe_next = &_dmadesc_a[0]; } - current_back_buffer_id ^= 1; - // Disable intterupt - _dev->int_ena.out_eof = 0; + previousBufferFree = false; + while (i2s_parallel_is_previous_buffer_free() == false) {} + + + } // end flip diff --git a/src/platforms/esp32/esp32_i2s_parallel_dma.hpp b/src/platforms/esp32/esp32_i2s_parallel_dma.hpp index dd6c1f3..1d253c6 100644 --- a/src/platforms/esp32/esp32_i2s_parallel_dma.hpp +++ b/src/platforms/esp32/esp32_i2s_parallel_dma.hpp @@ -49,9 +49,8 @@ Contributors: #define DMA_MAX (4096-4) -#ifndef ESP32_I2S_DEVICE - #define ESP32_I2S_DEVICE I2S_NUM_0 -#endif +// DO NOT CHANGE +#define ESP32_I2S_DEVICE I2S_NUM_1 // The type used for this SoC #define HUB75_DMA_DESCRIPTOR_T lldesc_t @@ -119,7 +118,7 @@ i2s_dev_t* getDev(); void dma_transfer_start(); void dma_transfer_stop(); - void flip_dma_output_buffer(int ¤t_back_buffer_id); + void flip_dma_output_buffer(int buffer_id); private: diff --git a/src/platforms/esp32s3/gdma_lcd_parallel16.cpp b/src/platforms/esp32s3/gdma_lcd_parallel16.cpp index 8f90462..17fd218 100644 --- a/src/platforms/esp32s3/gdma_lcd_parallel16.cpp +++ b/src/platforms/esp32s3/gdma_lcd_parallel16.cpp @@ -441,12 +441,12 @@ } // end - void Bus_Parallel16::flip_dma_output_buffer(int ¤t_back_buffer_id) + void Bus_Parallel16::flip_dma_output_buffer(int back_buffer_id) { // if ( _double_dma_buffer == false) return; - if ( current_back_buffer_id == 1) // change across to everything 'b'' + if ( back_buffer_id == 1) // change across to everything 'b'' { _dmadesc_a[_dmadesc_count-1].next = (dma_descriptor_t *) &_dmadesc_b[0]; _dmadesc_b[_dmadesc_count-1].next = (dma_descriptor_t *) &_dmadesc_b[0]; @@ -457,7 +457,7 @@ _dmadesc_a[_dmadesc_count-1].next = (dma_descriptor_t *) &_dmadesc_a[0]; } - current_back_buffer_id ^= 1; + //current_back_buffer_id ^= 1; } // end flip diff --git a/src/platforms/esp32s3/gdma_lcd_parallel16.hpp b/src/platforms/esp32s3/gdma_lcd_parallel16.hpp index 3d47c22..958b65c 100644 --- a/src/platforms/esp32s3/gdma_lcd_parallel16.hpp +++ b/src/platforms/esp32s3/gdma_lcd_parallel16.hpp @@ -147,7 +147,7 @@ void dma_transfer_start(); void dma_transfer_stop(); - void flip_dma_output_buffer(int ¤t_back_buffer_id); + void flip_dma_output_buffer(int back_buffer_id); private: |
