diff options
| author | Kouzerumatsu / Bananafox <46141631+Kouzeru@users.noreply.github.com> | 2023-02-08 22:33:14 +0800 |
|---|---|---|
| committer | Kouzerumatsu / Bananafox <46141631+Kouzeru@users.noreply.github.com> | 2023-02-08 22:33:14 +0800 |
| commit | 79e1897aed951e37ee14665b2e13d9c172c8cfaf (patch) | |
| tree | 2aee395dac145c7120d4eb71cc23150c41c599cb /src | |
| parent | 9d4d12a43bd135de343853f4e7d1b2f2d59ee727 (diff) | |
Deeper color depth
Diffstat (limited to 'src')
| -rw-r--r-- | src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp | 130 | ||||
| -rw-r--r-- | src/ESP32-HUB75-MatrixPanel-I2S-DMA.h | 23 |
2 files changed, 91 insertions, 62 deletions
diff --git a/src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp b/src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp index 4778c5b..c1e7dd6 100644 --- a/src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp +++ b/src/ESP32-HUB75-MatrixPanel-I2S-DMA.cpp @@ -27,19 +27,15 @@ * 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_COLOUR_DEPTH_BITS > 8 - #error "Color depth bits cannot be greater than 8." +#if PIXEL_COLOUR_DEPTH_BITS > 12 + #error "Color depth bits cannot be greater than 12." #elif PIXEL_COLOUR_DEPTH_BITS < 2 #error "Colour depth bits cannot be less than 2." #endif -#if PIXEL_COLOUR_DEPTH_BITS != 8 - #define MASK_OFFSET (8 - PIXEL_COLOUR_DEPTH_BITS) + #define MASK_OFFSET (16 - PIXEL_COLOUR_DEPTH_BITS) #define PIXEL_COLOUR_MASK_BIT(colour_depth_index) (1 << (colour_depth_index + MASK_OFFSET)) //static constexpr uint8_t const MASK_OFFSET = 8-PIXEL_COLOUR_DEPTH_BITS; -#else - #define PIXEL_COLOUR_MASK_BIT(colour_depth_index) (1 << (colour_depth_index)) -#endif /* #if PIXEL_COLOUR_DEPTH_BITS < 8 @@ -314,10 +310,15 @@ void IRAM_ATTR MatrixPanel_I2S_DMA::updateMatrixDMABuffer(uint16_t x_coord, uint * i.e. It's almost impossible for colour_depth_idx of 0 to be sent out to the MATRIX unless the 'value' of a color is exactly '1' * https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ */ +uint16_t red16, green16, blue16; #ifndef NO_CIE1931 - red = lumConvTab[red]; - green = lumConvTab[green]; - blue = lumConvTab[blue]; + red16 = lumConvTab[red]; + green16 = lumConvTab[green]; + blue16 = lumConvTab[blue]; +#else + 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, @@ -362,16 +363,16 @@ void IRAM_ATTR MatrixPanel_I2S_DMA::updateMatrixDMABuffer(uint16_t x_coord, uint uint8_t mask = (1 << (colour_depth_idx)); // expect 24 bit color (8 bits per RGB subpixel) #endif */ - uint8_t mask = PIXEL_COLOUR_MASK_BIT(colour_depth_idx); + uint16_t mask = PIXEL_COLOUR_MASK_BIT(colour_depth_idx); 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)(blue & mask); // --B + RGB_output_bits |= (bool)(blue16 & mask); // --B RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(green & mask); // -BG + RGB_output_bits |= (bool)(green16 & mask); // -BG RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(red & mask); // BGR + RGB_output_bits |= (bool)(red16 & mask); // BGR RGB_output_bits <<= _colourbitoffset; // shift colour bits to the required position @@ -398,10 +399,15 @@ void MatrixPanel_I2S_DMA::updateMatrixDMABuffer(uint8_t red, uint8_t green, uint 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 - red = lumConvTab[red]; - green = lumConvTab[green]; - blue = lumConvTab[blue]; + red16 = lumConvTab[red]; + green16 = lumConvTab[green]; + blue16 = lumConvTab[blue]; +#else + red16 = red << 8; + green16 = green << 8; + blue16 = blue << 8; #endif for(uint8_t colour_depth_idx=0; colour_depth_idx<PIXEL_COLOUR_DEPTH_BITS; colour_depth_idx++) // color depth - 8 iterations @@ -415,15 +421,15 @@ void MatrixPanel_I2S_DMA::updateMatrixDMABuffer(uint8_t red, uint8_t green, uint // uint8_t mask = (1 << (colour_depth_idx)); // expect 24 bit colour (8 bits per RGB subpixel) // #endif - uint8_t mask = PIXEL_COLOUR_MASK_BIT(colour_depth_idx); + uint16_t mask = PIXEL_COLOUR_MASK_BIT(colour_depth_idx); /* 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)(blue & mask); // --B + RGB_output_bits |= (bool)(blue16 & mask); // --B RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(green & mask); // -BG + RGB_output_bits |= (bool)(green16 & mask); // -BG RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(red & 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 @@ -680,7 +686,6 @@ void MatrixPanel_I2S_DMA::brtCtrlOE(int brt, const bool _buff_id){ */ - /** * @brief - reset OE bits in DMA buffer in a way to control brightness * @param brt - brightness level from 0 to 255 - NOT MATRIX_WIDTH @@ -692,6 +697,8 @@ void MatrixPanel_I2S_DMA::brtCtrlOEv2(uint8_t brt, const int _buff_id) { 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; // start with iterating all rows in dma_buff structure int row_idx = dma_buff.rowBits.size(); @@ -699,40 +706,29 @@ void MatrixPanel_I2S_DMA::brtCtrlOEv2(uint8_t brt, const int _buff_id) { --row_idx; // let's set OE control bits for specific pixels in each color_index subrows - uint8_t colouridx = dma_buff.rowBits[row_idx]->colour_depth; + uint8_t colouridx = _depth; do { - // Multiply brightness according to index of bitplane (color index subrow) - // in respect of accumulating LED intensities with Binary-Coded Modulation: - // bitplane 0 is 1/1 of total brightness; bitplane 1 is 1/2; bitplane 2 is 1/4, etc - // accumulating all of them together means we will get only ~1/4 of the total brightness. - - // During the DMA, assume bitplane 0 shown for 4 subrows; bitplane 1 shown for 2 subrows; - // bitplane 2 and the rest shown for one subrow, would give us ~2/3 of the total brightness, - // which is good balance for the depth, brightness, while the flickers still less noticeable: - // row 0 (blanking)\ row 1 (blanking)\ row 2 (blanking)\ .. - // bitplane : ... 0 0 0 0 1 1 2 3 4 5 6 7 \- 0 0 0 0 1 1 2 3 4 5 6 7 \- 0 0 0 0 1 1 2 3 4 5 6 7 \- .. - // rightshift: ... 0 0 0 0 0 0 0 1 2 3 4 5 -\ 0 0 0 0 0 0 0 1 2 3 4 5 -\ 0 0 0 0 0 0 0 1 2 3 4 5 -\ .. - char bitplane = dma_buff.rowBits[row_idx]->colour_depth-colouridx; - char rightshift = std::max(bitplane-2,0); - int brightness_in_x_pixels = (PIXELS_PER_ROW * brt) >> (8 + rightshift); --colouridx; - + + 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) >> (8 + rightshift); + // 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); - int x_coord = dma_buff.rowBits[row_idx]->width; + // 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 = _width; do { --x_coord; - if (x_coord <= _blank) // Can't touch blanking. They need to stay blank. - { - row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] |= BIT_OE; // Disable output after this point. - } - else if(x_coord >= (PIXELS_PER_ROW - _blank - 1) ) // Can't touch blanking. They need to stay blank. - { - row[ESP32_TX_FIFO_POSITION_ADJUST(x_coord)] |= BIT_OE; // Disable output after this point. - } - else if (x_coord < brightness_in_x_pixels) + // (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; } @@ -844,10 +840,15 @@ void MatrixPanel_I2S_DMA::hlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, // l = PIXELS_PER_ROW - x_coord + 1; // reset width to end of row /* LED Brightness Compensation */ +uint16_t red16, green16, blue16; #ifndef NO_CIE1931 - red = lumConvTab[red]; - green = lumConvTab[green]; - blue = lumConvTab[blue]; + red16 = lumConvTab[red]; + green16 = lumConvTab[green]; + blue16 = lumConvTab[blue]; +#else + red16 = red << 8; + green16 = green << 8; + blue16 = blue << 8; #endif uint16_t _colourbitclear = BITMASK_RGB1_CLEAR, _colourbitoffset = 0; @@ -871,15 +872,15 @@ void MatrixPanel_I2S_DMA::hlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, // #else // uint8_t mask = (1 << (colour_depth_idx)); // expect 24 bit color (8 bits per RGB subpixel) // #endif - uint8_t mask = PIXEL_COLOUR_MASK_BIT(colour_depth_idx); + uint16_t mask = PIXEL_COLOUR_MASK_BIT(colour_depth_idx); /* 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)(blue & mask); // --B + RGB_output_bits |= (bool)(blue16 & mask); // --B RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(green & mask); // -BG + RGB_output_bits |= (bool)(green16 & mask); // -BG RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(red & mask); // BGR + RGB_output_bits |= (bool)(red16 & mask); // BGR RGB_output_bits <<= _colourbitoffset; // shift color bits to the required position // Get the contents at this address, @@ -930,10 +931,15 @@ void MatrixPanel_I2S_DMA::vlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, /// l = m_cfg.mx_height - y_coord + 1; // reset width to end of col /* LED Brightness Compensation */ +uint16_t red16, green16, blue16; #ifndef NO_CIE1931 - red = lumConvTab[red]; - green = lumConvTab[green]; - blue = lumConvTab[blue]; + red16 = lumConvTab[red]; + green16 = lumConvTab[green]; + blue16 = lumConvTab[blue]; +#else + red16 = red << 8; + green16 = green << 8; + blue16 = blue << 8; #endif /* @@ -956,16 +962,16 @@ void MatrixPanel_I2S_DMA::vlineDMA(int16_t x_coord, int16_t y_coord, int16_t l, // uint8_t mask = (1 << (colour_depth_idx)); // expect 24 bit color (8 bits per RGB subpixel) // #endif - uint8_t mask = PIXEL_COLOUR_MASK_BIT(colour_depth_idx); + uint16_t mask = PIXEL_COLOUR_MASK_BIT(colour_depth_idx); 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)(blue & mask); // --B + RGB_output_bits |= (bool)(blue16 & mask); // --B RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(green & mask); // -BG + RGB_output_bits |= (bool)(green16 & mask); // -BG RGB_output_bits <<= 1; - RGB_output_bits |= (bool)(red & mask); // BGR + RGB_output_bits |= (bool)(red16 & mask); // BGR int16_t _l = 0, _y = y_coord; uint16_t _colourbitclear = BITMASK_RGB1_CLEAR; diff --git a/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h b/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h index 2b76152..200530e 100644 --- a/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h +++ b/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h @@ -65,6 +65,7 @@ #endif // 8bit per RGB color = 24 bit/per pixel, +// can be extended to offer deeper colors, or // might be reduced to save DMA RAM #ifndef PIXEL_COLOUR_DEPTH_BITS #define PIXEL_COLOUR_DEPTH_BITS 8 @@ -182,8 +183,30 @@ struct frameStruct { // 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[]={ 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 +}; #endif /** @brief - configuration values for HUB75_I2S driver |
