hub75_framebuffer/
plain.rs

1//! DMA-friendly framebuffer implementation for HUB75 LED panels.
2//!
3//! This module provides a framebuffer implementation with memory
4//! layout optimized for efficient transfer to HUB75 LED panels. The data is
5//! structured for direct signal mapping, making it ideal for DMA transfers but
6//! also suitable for programmatic transfer. It supports RGB color and brightness
7//! control through multiple frames using Binary Code Modulation (BCM).
8//!
9//! # Hardware Requirements
10//! This implementation can be used by any microcontroller that has a peripheral
11//! capable of outputting a clock signal and 16 bits (though only 14 bits are
12//! actually needed) in parallel. The data is structured to directly match the
13//! HUB75 connector signals, making it suitable for various parallel output
14//! peripherals.
15//!
16//! # Features
17//! - Memory layout optimized for efficient data transfer
18//! - Direct signal mapping to HUB75 connector signals
19//! - Support for RGB color with brightness control
20//! - Multiple frame buffers for Binary Code Modulation (BCM)
21//! - Integration with embedded-graphics for easy drawing
22//!
23//! # Brightness Control
24//! Brightness is controlled through Binary Code Modulation (BCM):
25//! - The number of brightness levels is determined by the `BITS` parameter
26//! - Each additional bit doubles the number of brightness levels
27//! - More bits provide better brightness resolution but require more memory
28//! - Memory usage grows exponentially with the number of bits: `(2^BITS)-1`
29//!   frames
30//! - Example: 8 bits = 256 levels, 4 bits = 16 levels
31//!
32//! # Memory Usage
33//! The framebuffer's memory usage is determined by:
34//! - Panel size (ROWS × COLS)
35//! - Number of brightness bits (BITS)
36//! - Memory grows exponentially with bits: `(2^BITS)-1` frames
37//! - 16-bit entries provide direct signal mapping but use more memory
38//!
39//! # Example
40//! ```rust,no_run
41//! use embedded_graphics::pixelcolor::RgbColor;
42//! use embedded_graphics::prelude::*;
43//! use embedded_graphics::primitives::Circle;
44//! use embedded_graphics::primitives::Rectangle;
45//! use hub75_framebuffer::compute_frame_count;
46//! use hub75_framebuffer::compute_rows;
47//! use hub75_framebuffer::Color;
48//! use hub75_framebuffer::plain::DmaFrameBuffer;
49//! use embedded_graphics::primitives::PrimitiveStyle;
50//!
51//! // Create a framebuffer for a 64x32 panel with 3-bit color depth
52//! const ROWS: usize = 32;
53//! const COLS: usize = 64;
54//! const BITS: u8 = 3; // Color depth (8 brightness levels, 7 frames)
55//! const NROWS: usize = compute_rows(ROWS); // Number of rows per scan
56//! const FRAME_COUNT: usize = compute_frame_count(BITS); // Number of frames for BCM
57//!
58//! let mut framebuffer = DmaFrameBuffer::<ROWS, COLS, NROWS, BITS, FRAME_COUNT>::new();
59//!
60//! // Draw a red rectangle
61//! Rectangle::new(Point::new(10, 10), Size::new(20, 20))
62//!     .into_styled(PrimitiveStyle::with_fill(Color::RED))
63//!     .draw(&mut framebuffer)
64//!     .unwrap();
65//!
66//! // Draw a blue circle
67//! Circle::new(Point::new(40, 20), 10)
68//!     .into_styled(PrimitiveStyle::with_fill(Color::BLUE))
69//!     .draw(&mut framebuffer)
70//!     .unwrap();
71//! ```
72//!
73//! # Implementation Details
74//! The framebuffer is organized to directly match the HUB75 connector signals:
75//! - Each 16-bit word maps directly to the HUB75 control signals
76//! - Color data (R, G, B) for two sub-pixels is stored in dedicated bits
77//! - Control signals (output enable, latch, address) are mapped to specific
78//!   bits
79//! - Multiple frames are used to achieve Binary Code Modulation (BCM)
80//! - DMA transfers the data directly to the panel without any transformation
81//!
82//! # HUB75 Signal Bit Mapping
83//! Each 16-bit `Entry` represents the logic levels that will be driven onto the HUB75
84//! bus during a single pixel-clock cycle. Every panel signal—apart from the clock
85//! (`CLK`) itself—occupies a dedicated bit, allowing the word to be streamed via DMA
86//! with zero run-time manipulation.
87//!
88//! ```text
89//! 15 ─ Dummy2   (spare)
90//! 14 ─ B2       Blue  – lower half of the panel
91//! 13 ─ G2       Green – lower half of the panel
92//! 12 ─ R2       Red   – lower half of the panel
93//! 11 ─ B1       Blue  – upper half of the panel
94//! 10 ─ G1       Green – upper half of the panel
95//!  9 ─ R1       Red   – upper half of the panel
96//!  8 ─ OE       Output-Enable / Blank
97//!  7 ─ Dummy1   (spare)
98//!  6 ─ Dummy0   (spare)
99//!  5 ─ LAT      Latch / STB
100//! 4-0 ─ A..E    Row address lines
101//! ```
102//!
103//! The pixel clock is generated by the peripheral that owns the DMA stream and
104//! is therefore **not** part of the 16-bit word stored in the framebuffer.
105//!
106//! # Binary Code Modulation (BCM) Frames
107//! Brightness is achieved with Binary-Code-Modulation as outlined in
108//! <https://www.batsocks.co.uk/readme/art_bcm_1.htm>. For a colour depth of
109//! `BITS`, the driver allocates `FRAME_COUNT = 2^BITS - 1` frames. Frame *n*
110//! (0-based) is displayed for a duration proportional to `2^n`, mirroring the
111//! weight of that bit in a binary number.
112//!
113//! When a pixel is written its 8-bit RGB components are compared against a
114//! per-frame threshold:
115//!
116//! ```text
117//! brightness_step = 256 / 2^BITS
118//! threshold_n     = (n + 1) * brightness_step
119//! ```
120//!
121//! If a channel's value is greater than or equal to `threshold_n` the
122//! corresponding colour bit is set in frame *n*. Streaming the frames from the
123//! least-significant (shortest) to the most-significant (longest) time-slot
124//! produces the correct 8-bit brightness while keeping the refresh routine
125//! trivial.
126//!
127//! # Safety
128//! This implementation uses unsafe code for DMA operations. The framebuffer
129//! must be properly aligned in memory and the DMA configuration must match the
130//! buffer layout.
131
132use core::convert::Infallible;
133
134use crate::FrameBufferOperations;
135use bitfield::bitfield;
136#[cfg(not(feature = "esp-hal-dma"))]
137use embedded_dma::ReadBuffer;
138use embedded_graphics::pixelcolor::RgbColor;
139use embedded_graphics::prelude::Point;
140#[cfg(feature = "esp-hal-dma")]
141use esp_hal::dma::ReadBuffer;
142
143use super::Color;
144use super::FrameBuffer;
145use super::WordSize;
146
147const BLANKING_DELAY: usize = 1;
148
149/// Creates a pre-computed data template for a row with the specified addresses.
150/// This template contains all the timing and control signals but no pixel data.
151#[inline]
152const fn make_data_template<const COLS: usize>(addr: u8, prev_addr: u8) -> [Entry; COLS] {
153    let mut data = [Entry::new(); COLS];
154    let mut i = 0;
155
156    while i < COLS {
157        let mut entry = Entry::new();
158        entry.0 = prev_addr as u16;
159
160        // Apply timing control based on position
161        if i == 1 {
162            entry.0 |= 0b1_0000_0000; // set output_enable bit
163        } else if i == COLS - BLANKING_DELAY - 1 {
164            // output_enable already false from initialization
165        } else if i == COLS - 1 {
166            entry.0 |= 0b0010_0000; // set latch bit
167            entry.0 = (entry.0 & !0b0001_1111) | (addr as u16); // set new address
168        } else if i > 1 && i < COLS - BLANKING_DELAY - 1 {
169            entry.0 |= 0b1_0000_0000; // set output_enable bit
170        }
171
172        data[map_index(i)] = entry;
173        i += 1;
174    }
175
176    data
177}
178
179bitfield! {
180    /// A 16-bit word representing the HUB75 control signals for a single pixel.
181    ///
182    /// This structure directly maps to the HUB75 connector signals:
183    /// - RGB color data for two sub-pixels (color0 and color1)
184    /// - Panel control signals (output enable, latch, address)
185    /// - Dummy bits for timing alignment
186    ///
187    /// The bit layout matches the HUB75 connector signals:
188    /// - Bit 15: Dummy bit 2
189    /// - Bit 14: Blue channel for color1
190    /// - Bit 13: Green channel for color1
191    /// - Bit 12: Red channel for color1
192    /// - Bit 11: Blue channel for color0
193    /// - Bit 10: Green channel for color0
194    /// - Bit 9: Red channel for color0
195    /// - Bit 8: Output enable
196    /// - Bit 7: Dummy bit 1
197    /// - Bit 6: Dummy bit 0
198    /// - Bit 5: Latch signal
199    /// - Bits 4-0: Row address
200    #[derive(Clone, Copy, Default, PartialEq)]
201    #[repr(transparent)]
202    struct Entry(u16);
203    dummy2, set_dummy2: 15;
204    blu2, set_blu2: 14;
205    grn2, set_grn2: 13;
206    red2, set_red2: 12;
207    blu1, set_blu1: 11;
208    grn1, set_grn1: 10;
209    red1, set_red1: 9;
210    output_enable, set_output_enable: 8;
211    dummy1, set_dummy1: 7;
212    dummy0, set_dummy0: 6;
213    latch, set_latch: 5;
214    addr, set_addr: 4, 0;
215}
216
217impl core::fmt::Debug for Entry {
218    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
219        f.debug_tuple("Entry")
220            .field(&format_args!("{:#x}", self.0))
221            .finish()
222    }
223}
224
225#[cfg(feature = "defmt")]
226impl defmt::Format for Entry {
227    fn format(&self, f: defmt::Formatter) {
228        defmt::write!(f, "Entry({=u16:#x})", self.0);
229    }
230}
231
232impl Entry {
233    const fn new() -> Self {
234        Self(0)
235    }
236
237    // Optimized color bit manipulation constants and methods
238    const COLOR0_MASK: u16 = 0b0000_1110_0000_0000; // bits 9-11: R1, G1, B1
239    const COLOR1_MASK: u16 = 0b0111_0000_0000_0000; // bits 12-14: R2, G2, B2
240
241    #[inline]
242    fn set_color0_bits(&mut self, bits: u8) {
243        let bits16 = u16::from(bits) << 9;
244        self.0 = (self.0 & !Self::COLOR0_MASK) | (bits16 & Self::COLOR0_MASK);
245    }
246
247    #[inline]
248    fn set_color1_bits(&mut self, bits: u8) {
249        let bits16 = u16::from(bits) << 12;
250        self.0 = (self.0 & !Self::COLOR1_MASK) | (bits16 & Self::COLOR1_MASK);
251    }
252}
253
254/// Represents a single row of pixels in the framebuffer.
255///
256/// Each row contains a fixed number of columns (`COLS`) and manages the timing
257/// and control signals for the HUB75 panel. The row handles:
258/// - Output enable timing to prevent ghosting
259/// - Latch signal generation for row updates
260/// - Row address management
261/// - Color data for both sub-pixels
262#[derive(Clone, Copy, PartialEq, Debug)]
263#[repr(C)]
264struct Row<const COLS: usize> {
265    data: [Entry; COLS],
266}
267
268const fn map_index(i: usize) -> usize {
269    #[cfg(feature = "esp32-ordering")]
270    {
271        i ^ 1
272    }
273    #[cfg(not(feature = "esp32-ordering"))]
274    {
275        i
276    }
277}
278
279impl<const COLS: usize> Default for Row<COLS> {
280    fn default() -> Self {
281        Self::new()
282    }
283}
284
285impl<const COLS: usize> Row<COLS> {
286    pub const fn new() -> Self {
287        Self {
288            data: [Entry::new(); COLS],
289        }
290    }
291
292    pub fn format(&mut self, addr: u8, prev_addr: u8) {
293        // Use pre-computed template and bulk copy for maximum performance
294        let template = make_data_template::<COLS>(addr, prev_addr);
295        self.data.copy_from_slice(&template);
296    }
297
298    /// Fast clear method that preserves timing/control bits while clearing pixel data.
299    /// Uses bulk memory operations for maximum performance.
300    #[inline]
301    pub fn clear_colors(&mut self) {
302        // Clear color bits while preserving timing and control bits
303        const COLOR_CLEAR_MASK: u16 = !0b0111_1110_0000_0000; // Clear bits 9-14 (R1,G1,B1,R2,G2,B2)
304
305        for entry in &mut self.data {
306            entry.0 &= COLOR_CLEAR_MASK;
307        }
308    }
309
310    #[inline]
311    pub fn set_color0(&mut self, col: usize, r: bool, g: bool, b: bool) {
312        let bits = (u8::from(b) << 2) | (u8::from(g) << 1) | u8::from(r);
313        let col = map_index(col);
314        self.data[col].set_color0_bits(bits);
315    }
316
317    #[inline]
318    pub fn set_color1(&mut self, col: usize, r: bool, g: bool, b: bool) {
319        let bits = (u8::from(b) << 2) | (u8::from(g) << 1) | u8::from(r);
320        let col = map_index(col);
321        self.data[col].set_color1_bits(bits);
322    }
323}
324
325#[derive(Copy, Clone, Debug)]
326#[repr(C)]
327struct Frame<const ROWS: usize, const COLS: usize, const NROWS: usize> {
328    rows: [Row<COLS>; NROWS],
329}
330
331impl<const ROWS: usize, const COLS: usize, const NROWS: usize> Frame<ROWS, COLS, NROWS> {
332    pub const fn new() -> Self {
333        Self {
334            rows: [Row::new(); NROWS],
335        }
336    }
337
338    pub fn format(&mut self) {
339        for (addr, row) in self.rows.iter_mut().enumerate() {
340            let prev_addr = if addr == 0 {
341                NROWS as u8 - 1
342            } else {
343                addr as u8 - 1
344            };
345            row.format(addr as u8, prev_addr);
346        }
347    }
348
349    /// Fast clear method that preserves timing/control bits while clearing pixel data.
350    #[inline]
351    pub fn clear_colors(&mut self) {
352        for row in &mut self.rows {
353            row.clear_colors();
354        }
355    }
356
357    #[inline]
358    pub fn set_pixel(&mut self, y: usize, x: usize, red: bool, green: bool, blue: bool) {
359        let row = &mut self.rows[if y < NROWS { y } else { y - NROWS }];
360        if y < NROWS {
361            row.set_color0(x, red, green, blue);
362        } else {
363            row.set_color1(x, red, green, blue);
364        }
365    }
366}
367
368impl<const ROWS: usize, const COLS: usize, const NROWS: usize> Default
369    for Frame<ROWS, COLS, NROWS>
370{
371    fn default() -> Self {
372        Self::new()
373    }
374}
375
376/// DMA-compatible framebuffer for HUB75 LED panels.
377///
378/// This is a framebuffer implementation that:
379/// - Manages multiple frames for Binary Code Modulation (BCM)
380/// - Provides DMA-compatible memory layout
381/// - Implements the embedded-graphics `DrawTarget` trait
382///
383/// # Type Parameters
384/// - `ROWS`: Total number of rows in the panel
385/// - `COLS`: Number of columns in the panel
386/// - `NROWS`: Number of rows per scan (typically half of ROWS)
387/// - `BITS`: Color depth (1-8 bits)
388/// - `FRAME_COUNT`: Number of frames used for Binary Code Modulation
389///
390/// # Helper Functions
391/// Use these functions to compute the correct values:
392/// - `esp_hub75::compute_frame_count(BITS)`: Computes the required number of
393///   frames
394/// - `esp_hub75::compute_rows(ROWS)`: Computes the number of rows per scan
395///
396/// # Memory Layout
397/// The buffer is aligned to ensure efficient DMA transfers and contains:
398/// - A 64-bit alignment field
399/// - An array of frames, each containing the full panel data
400#[derive(Copy, Clone)]
401#[repr(C)]
402pub struct DmaFrameBuffer<
403    const ROWS: usize,
404    const COLS: usize,
405    const NROWS: usize,
406    const BITS: u8,
407    const FRAME_COUNT: usize,
408> {
409    _align: u64,
410    frames: [Frame<ROWS, COLS, NROWS>; FRAME_COUNT],
411}
412
413impl<
414        const ROWS: usize,
415        const COLS: usize,
416        const NROWS: usize,
417        const BITS: u8,
418        const FRAME_COUNT: usize,
419    > Default for DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
420{
421    fn default() -> Self {
422        Self::new()
423    }
424}
425
426impl<
427        const ROWS: usize,
428        const COLS: usize,
429        const NROWS: usize,
430        const BITS: u8,
431        const FRAME_COUNT: usize,
432    > DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
433{
434    /// Create a new, ready-to-use framebuffer.
435    ///
436    /// This creates a new framebuffer and automatically formats it with proper timing signals.
437    /// The framebuffer is immediately ready for pixel operations and DMA transfers.
438    ///
439    /// # Panics
440    ///
441    /// Panics if `BITS` is greater than 8, as only 1-8 bit color depths are supported.
442    ///
443    /// # Example
444    /// ```rust,no_run
445    /// use hub75_framebuffer::{Color,plain::DmaFrameBuffer,compute_rows,compute_frame_count};
446    ///
447    /// const ROWS: usize = 32;
448    /// const COLS: usize = 64;
449    /// const BITS: u8 = 3; // Color depth (8 brightness levels, 7 frames)
450    /// const NROWS: usize = compute_rows(ROWS); // Number of rows per scan
451    /// const FRAME_COUNT: usize = compute_frame_count(BITS); // Number of frames for BCM
452    ///
453    /// let mut framebuffer = DmaFrameBuffer::<ROWS, COLS, NROWS, BITS, FRAME_COUNT>::new();
454    /// // No need to call format() - framebuffer is ready to use!
455    /// ```
456    #[must_use]
457    pub fn new() -> Self {
458        debug_assert!(BITS <= 8);
459
460        let mut instance = Self {
461            _align: 0,
462            frames: [Frame::new(); FRAME_COUNT],
463        };
464
465        // Pre-format the framebuffer so it's immediately ready for use
466        instance.format();
467        instance
468    }
469
470    /// This returns the size of the DMA buffer in bytes.  Its used to calculate
471    /// the number of DMA descriptors needed.
472    /// # Example
473    /// ```rust
474    /// use hub75_framebuffer::{Color,plain::DmaFrameBuffer,compute_rows,compute_frame_count};
475    ///
476    /// const ROWS: usize = 32;
477    /// const COLS: usize = 64;
478    /// const BITS: u8 = 3; // Color depth (8 brightness levels, 7 frames)
479    /// const NROWS: usize = compute_rows(ROWS); // Number of rows per scan
480    /// const FRAME_COUNT: usize = compute_frame_count(BITS);
481    ///
482    /// type FBType = DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>;
483    /// let (_, tx_descriptors) = esp_hal::dma_descriptors!(0, FBType::dma_buffer_size_bytes());
484    /// ```
485    #[cfg(feature = "esp-hal-dma")]
486    pub const fn dma_buffer_size_bytes() -> usize {
487        core::mem::size_of::<[Frame<ROWS, COLS, NROWS>; FRAME_COUNT]>()
488    }
489
490    /// Perform full formatting of the framebuffer with timing and control signals.
491    ///
492    /// This sets up all the timing and control signals needed for proper HUB75 operation.
493    /// This is automatically called by `new()`, so you typically don't need to call this
494    /// unless you want to completely reinitialize the framebuffer.
495    ///
496    /// # Example
497    /// ```rust,no_run
498    /// use hub75_framebuffer::{Color,plain::DmaFrameBuffer,compute_rows,compute_frame_count};
499    ///
500    /// const ROWS: usize = 32;
501    /// const COLS: usize = 64;
502    /// const BITS: u8 = 3; // Color depth (8 brightness levels, 7 frames)
503    /// const NROWS: usize = compute_rows(ROWS); // Number of rows per scan
504    /// const FRAME_COUNT: usize = compute_frame_count(BITS); // Number of frames for BCM
505    ///
506    /// let mut framebuffer = DmaFrameBuffer::<ROWS, COLS, NROWS, BITS, FRAME_COUNT>::new();
507    /// framebuffer.format(); // Reinitialize if needed
508    /// ```
509    #[inline]
510    pub fn format(&mut self) {
511        for frame in &mut self.frames {
512            frame.format();
513        }
514    }
515
516    /// Fast erase operation that clears all pixel data while preserving timing signals.
517    ///
518    /// This is much faster than `format()` when you just want to clear the display
519    /// since it preserves all the timing and control signals that are already set up.
520    /// Use this for clearing between frames or when you want to start drawing fresh content.
521    ///
522    /// # Example
523    /// ```rust,no_run
524    /// use hub75_framebuffer::{Color,plain::DmaFrameBuffer,compute_rows,compute_frame_count};
525    ///
526    /// const ROWS: usize = 32;
527    /// const COLS: usize = 64;
528    /// const BITS: u8 = 3; // Color depth (8 brightness levels, 7 frames)
529    /// const NROWS: usize = compute_rows(ROWS); // Number of rows per scan
530    /// const FRAME_COUNT: usize = compute_frame_count(BITS); // Number of frames for BCM
531    ///
532    /// let mut framebuffer = DmaFrameBuffer::<ROWS, COLS, NROWS, BITS, FRAME_COUNT>::new();
533    /// framebuffer.erase();
534    /// ```
535    #[inline]
536    pub fn erase(&mut self) {
537        for frame in &mut self.frames {
538            frame.clear_colors();
539        }
540    }
541
542    /// Set a pixel in the framebuffer.
543    /// # Example
544    /// ```rust,no_run
545    /// use hub75_framebuffer::{Color,plain::DmaFrameBuffer,compute_rows,compute_frame_count};
546    /// use embedded_graphics::prelude::*;
547    ///
548    /// const ROWS: usize = 32;
549    /// const COLS: usize = 64;
550    /// const BITS: u8 = 3; // Color depth (8 brightness levels, 7 frames)
551    /// const NROWS: usize = compute_rows(ROWS); // Number of rows per scan
552    /// const FRAME_COUNT: usize = compute_frame_count(BITS); // Number of frames for BCM
553    ///
554    /// let mut framebuffer = DmaFrameBuffer::<ROWS, COLS, NROWS, BITS, FRAME_COUNT>::new();
555    /// framebuffer.set_pixel(Point::new(10, 10), Color::RED);
556    /// ```
557    pub fn set_pixel(&mut self, p: Point, color: Color) {
558        if p.x < 0 || p.y < 0 {
559            return;
560        }
561        self.set_pixel_internal(p.x as usize, p.y as usize, color);
562    }
563
564    #[inline]
565    fn frames_on(v: u8) -> usize {
566        // v / brightness_step but the compiler resolves the shift at build-time
567        (v as usize) >> (8 - BITS)
568    }
569
570    #[inline]
571    fn set_pixel_internal(&mut self, x: usize, y: usize, color: Color) {
572        if x >= COLS || y >= ROWS {
573            return;
574        }
575
576        // Early exit for black pixels - common in UI backgrounds
577        // Only enabled when skip-black-pixels feature is active
578        #[cfg(feature = "skip-black-pixels")]
579        if color == Color::BLACK {
580            return;
581        }
582
583        // Pre-compute how many frames each channel should be on
584        let red_frames = Self::frames_on(color.r());
585        let green_frames = Self::frames_on(color.g());
586        let blue_frames = Self::frames_on(color.b());
587
588        // Set the pixel in all frames based on pre-computed frame counts
589        for (frame_idx, frame) in self.frames.iter_mut().enumerate() {
590            frame.set_pixel(
591                y,
592                x,
593                frame_idx < red_frames,
594                frame_idx < green_frames,
595                frame_idx < blue_frames,
596            );
597        }
598    }
599}
600
601impl<
602        const ROWS: usize,
603        const COLS: usize,
604        const NROWS: usize,
605        const BITS: u8,
606        const FRAME_COUNT: usize,
607    > FrameBufferOperations<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
608    for DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
609{
610    #[inline]
611    fn erase(&mut self) {
612        DmaFrameBuffer::<ROWS, COLS, NROWS, BITS, FRAME_COUNT>::erase(self);
613    }
614
615    #[inline]
616    fn set_pixel(&mut self, p: Point, color: Color) {
617        DmaFrameBuffer::<ROWS, COLS, NROWS, BITS, FRAME_COUNT>::set_pixel(self, p, color);
618    }
619}
620
621impl<
622        const ROWS: usize,
623        const COLS: usize,
624        const NROWS: usize,
625        const BITS: u8,
626        const FRAME_COUNT: usize,
627    > embedded_graphics::prelude::OriginDimensions
628    for DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
629{
630    fn size(&self) -> embedded_graphics::prelude::Size {
631        embedded_graphics::prelude::Size::new(COLS as u32, ROWS as u32)
632    }
633}
634
635impl<
636        const ROWS: usize,
637        const COLS: usize,
638        const NROWS: usize,
639        const BITS: u8,
640        const FRAME_COUNT: usize,
641    > embedded_graphics::prelude::OriginDimensions
642    for &mut DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
643{
644    fn size(&self) -> embedded_graphics::prelude::Size {
645        embedded_graphics::prelude::Size::new(COLS as u32, ROWS as u32)
646    }
647}
648
649impl<
650        const ROWS: usize,
651        const COLS: usize,
652        const NROWS: usize,
653        const BITS: u8,
654        const FRAME_COUNT: usize,
655    > embedded_graphics::draw_target::DrawTarget
656    for DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
657{
658    type Color = Color;
659
660    type Error = Infallible;
661
662    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
663    where
664        I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
665    {
666        for pixel in pixels {
667            self.set_pixel_internal(pixel.0.x as usize, pixel.0.y as usize, pixel.1);
668        }
669        Ok(())
670    }
671}
672
673unsafe impl<
674        const ROWS: usize,
675        const COLS: usize,
676        const NROWS: usize,
677        const BITS: u8,
678        const FRAME_COUNT: usize,
679    > ReadBuffer for DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
680{
681    #[cfg(not(feature = "esp-hal-dma"))]
682    type Word = u8;
683
684    unsafe fn read_buffer(&self) -> (*const u8, usize) {
685        let ptr = (&raw const self.frames).cast::<u8>();
686        let len = core::mem::size_of_val(&self.frames);
687        (ptr, len)
688    }
689}
690
691unsafe impl<
692        const ROWS: usize,
693        const COLS: usize,
694        const NROWS: usize,
695        const BITS: u8,
696        const FRAME_COUNT: usize,
697    > ReadBuffer for &mut DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
698{
699    #[cfg(not(feature = "esp-hal-dma"))]
700    type Word = u8;
701
702    unsafe fn read_buffer(&self) -> (*const u8, usize) {
703        let ptr = (&raw const self.frames).cast::<u8>();
704        let len = core::mem::size_of_val(&self.frames);
705        (ptr, len)
706    }
707}
708
709impl<
710        const ROWS: usize,
711        const COLS: usize,
712        const NROWS: usize,
713        const BITS: u8,
714        const FRAME_COUNT: usize,
715    > core::fmt::Debug for DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
716{
717    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
718        let brightness_step = 1 << (8 - BITS);
719        f.debug_struct("DmaFrameBuffer")
720            .field("size", &core::mem::size_of_val(&self.frames))
721            .field("frame_count", &self.frames.len())
722            .field("frame_size", &core::mem::size_of_val(&self.frames[0]))
723            .field("brightness_step", &&brightness_step)
724            .finish_non_exhaustive()
725    }
726}
727
728#[cfg(feature = "defmt")]
729impl<
730        const ROWS: usize,
731        const COLS: usize,
732        const NROWS: usize,
733        const BITS: u8,
734        const FRAME_COUNT: usize,
735    > defmt::Format for DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
736{
737    fn format(&self, f: defmt::Formatter) {
738        let brightness_step = 1 << (8 - BITS);
739        defmt::write!(
740            f,
741            "DmaFrameBuffer<{}, {}, {}, {}, {}>",
742            ROWS,
743            COLS,
744            NROWS,
745            BITS,
746            FRAME_COUNT
747        );
748        defmt::write!(f, " size: {}", core::mem::size_of_val(&self.frames));
749        defmt::write!(
750            f,
751            " frame_size: {}",
752            core::mem::size_of_val(&self.frames[0])
753        );
754        defmt::write!(f, " brightness_step: {}", brightness_step);
755    }
756}
757
758impl<
759        const ROWS: usize,
760        const COLS: usize,
761        const NROWS: usize,
762        const BITS: u8,
763        const FRAME_COUNT: usize,
764    > FrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
765    for DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
766{
767    fn get_word_size(&self) -> WordSize {
768        WordSize::Sixteen
769    }
770}
771
772impl<
773        const ROWS: usize,
774        const COLS: usize,
775        const NROWS: usize,
776        const BITS: u8,
777        const FRAME_COUNT: usize,
778    > FrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
779    for &mut DmaFrameBuffer<ROWS, COLS, NROWS, BITS, FRAME_COUNT>
780{
781    fn get_word_size(&self) -> WordSize {
782        WordSize::Sixteen
783    }
784}
785
786#[cfg(test)]
787mod tests {
788    extern crate std;
789
790    use std::format;
791    use std::vec;
792
793    use super::*;
794    use crate::{FrameBuffer, WordSize};
795    use embedded_graphics::pixelcolor::RgbColor;
796    use embedded_graphics::prelude::*;
797    use embedded_graphics::primitives::{Circle, PrimitiveStyle, Rectangle};
798
799    const TEST_ROWS: usize = 32;
800    const TEST_COLS: usize = 64;
801    const TEST_NROWS: usize = TEST_ROWS / 2;
802    const TEST_BITS: u8 = 3;
803    const TEST_FRAME_COUNT: usize = (1 << TEST_BITS) - 1; // 7 frames for 3-bit depth
804
805    type TestFrameBuffer =
806        DmaFrameBuffer<TEST_ROWS, TEST_COLS, TEST_NROWS, TEST_BITS, TEST_FRAME_COUNT>;
807
808    // Helper function to get mapped index for ESP32
809    fn get_mapped_index(index: usize) -> usize {
810        map_index(index)
811    }
812
813    #[test]
814    fn test_entry_construction() {
815        let entry = Entry::new();
816        assert_eq!(entry.0, 0);
817        assert_eq!(entry.dummy2(), false);
818        assert_eq!(entry.blu2(), false);
819        assert_eq!(entry.grn2(), false);
820        assert_eq!(entry.red2(), false);
821        assert_eq!(entry.blu1(), false);
822        assert_eq!(entry.grn1(), false);
823        assert_eq!(entry.red1(), false);
824        assert_eq!(entry.output_enable(), false);
825        assert_eq!(entry.dummy1(), false);
826        assert_eq!(entry.dummy0(), false);
827        assert_eq!(entry.latch(), false);
828        assert_eq!(entry.addr(), 0);
829    }
830
831    #[test]
832    fn test_entry_setters() {
833        let mut entry = Entry::new();
834
835        entry.set_dummy2(true);
836        assert_eq!(entry.dummy2(), true);
837        assert_eq!(entry.0 & 0b1000000000000000, 0b1000000000000000);
838
839        entry.set_blu2(true);
840        assert_eq!(entry.blu2(), true);
841        assert_eq!(entry.0 & 0b0100000000000000, 0b0100000000000000);
842
843        entry.set_grn2(true);
844        assert_eq!(entry.grn2(), true);
845        assert_eq!(entry.0 & 0b0010000000000000, 0b0010000000000000);
846
847        entry.set_red2(true);
848        assert_eq!(entry.red2(), true);
849        assert_eq!(entry.0 & 0b0001000000000000, 0b0001000000000000);
850
851        entry.set_blu1(true);
852        assert_eq!(entry.blu1(), true);
853        assert_eq!(entry.0 & 0b0000100000000000, 0b0000100000000000);
854
855        entry.set_grn1(true);
856        assert_eq!(entry.grn1(), true);
857        assert_eq!(entry.0 & 0b0000010000000000, 0b0000010000000000);
858
859        entry.set_red1(true);
860        assert_eq!(entry.red1(), true);
861        assert_eq!(entry.0 & 0b0000001000000000, 0b0000001000000000);
862
863        entry.set_output_enable(true);
864        assert_eq!(entry.output_enable(), true);
865        assert_eq!(entry.0 & 0b0000000100000000, 0b0000000100000000);
866
867        entry.set_dummy1(true);
868        assert_eq!(entry.dummy1(), true);
869        assert_eq!(entry.0 & 0b0000000010000000, 0b0000000010000000);
870
871        entry.set_dummy0(true);
872        assert_eq!(entry.dummy0(), true);
873        assert_eq!(entry.0 & 0b0000000001000000, 0b0000000001000000);
874
875        entry.set_latch(true);
876        assert_eq!(entry.latch(), true);
877        assert_eq!(entry.0 & 0b0000000000100000, 0b0000000000100000);
878
879        entry.set_addr(0b11111);
880        assert_eq!(entry.addr(), 0b11111);
881        assert_eq!(entry.0 & 0b0000000000011111, 0b0000000000011111);
882    }
883
884    #[test]
885    fn test_entry_bit_isolation() {
886        let mut entry = Entry::new();
887
888        // Test that setting one field doesn't affect others
889        entry.set_addr(0b11111);
890        entry.set_latch(true);
891        assert_eq!(entry.addr(), 0b11111);
892        assert_eq!(entry.latch(), true);
893        assert_eq!(entry.output_enable(), false);
894        assert_eq!(entry.red1(), false);
895
896        entry.set_red1(true);
897        entry.set_grn2(true);
898        assert_eq!(entry.addr(), 0b11111);
899        assert_eq!(entry.latch(), true);
900        assert_eq!(entry.red1(), true);
901        assert_eq!(entry.grn2(), true);
902        assert_eq!(entry.blu1(), false);
903        assert_eq!(entry.red2(), false);
904    }
905
906    #[test]
907    fn test_entry_set_color0() {
908        let mut entry = Entry::new();
909
910        let bits = (u8::from(true) << 2) | (u8::from(false) << 1) | u8::from(true); // b=1, g=0, r=1 = 0b101
911        entry.set_color0_bits(bits);
912        assert_eq!(entry.red1(), true);
913        assert_eq!(entry.grn1(), false);
914        assert_eq!(entry.blu1(), true);
915        // Check that only the expected bits are set
916        assert_eq!(entry.0 & 0b0000101000000000, 0b0000101000000000); // Red1 and Blue1 bits
917    }
918
919    #[test]
920    fn test_entry_set_color1() {
921        let mut entry = Entry::new();
922
923        let bits = (u8::from(true) << 2) | (u8::from(true) << 1) | u8::from(false); // b=1, g=1, r=0 = 0b110
924        entry.set_color1_bits(bits);
925        assert_eq!(entry.red2(), false);
926        assert_eq!(entry.grn2(), true);
927        assert_eq!(entry.blu2(), true);
928        // Check that only the expected bits are set
929        assert_eq!(entry.0 & 0b0110000000000000, 0b0110000000000000); // Green2 and Blue2 bits
930    }
931
932    #[test]
933    fn test_entry_debug_formatting() {
934        let entry = Entry(0x1234);
935        let debug_str = format!("{:?}", entry);
936        assert_eq!(debug_str, "Entry(0x1234)");
937
938        let entry = Entry(0xabcd);
939        let debug_str = format!("{:?}", entry);
940        assert_eq!(debug_str, "Entry(0xabcd)");
941    }
942
943    #[test]
944    fn test_row_construction() {
945        let row: Row<TEST_COLS> = Row::new();
946        assert_eq!(row.data.len(), TEST_COLS);
947
948        // Check that all entries are initialized to zero
949        for entry in &row.data {
950            assert_eq!(entry.0, 0);
951        }
952    }
953
954    #[test]
955    fn test_row_format() {
956        let mut row: Row<TEST_COLS> = Row::new();
957        let test_addr = 5;
958        let prev_addr = 4;
959
960        row.format(test_addr, prev_addr);
961
962        // Check data entries configuration
963        for (physical_i, entry) in row.data.iter().enumerate() {
964            let logical_i = get_mapped_index(physical_i);
965
966            match logical_i {
967                i if i == TEST_COLS - BLANKING_DELAY - 1 => {
968                    // Second to last pixel should have output_enable false
969                    assert_eq!(entry.output_enable(), false);
970                    assert_eq!(entry.addr(), prev_addr as u16);
971                    assert_eq!(entry.latch(), false);
972                }
973                i if i == TEST_COLS - 1 => {
974                    // Last pixel should have latch true and new address
975                    assert_eq!(entry.latch(), true);
976                    assert_eq!(entry.addr(), test_addr as u16);
977                    assert_eq!(entry.output_enable(), false);
978                }
979                1 => {
980                    // First pixel after start should have output_enable true
981                    assert_eq!(entry.output_enable(), true);
982                    assert_eq!(entry.addr(), prev_addr as u16);
983                    assert_eq!(entry.latch(), false);
984                }
985                _ => {
986                    // Other pixels should have the previous address and no latch
987                    assert_eq!(entry.addr(), prev_addr as u16);
988                    assert_eq!(entry.latch(), false);
989                    if logical_i > 1 && logical_i < TEST_COLS - BLANKING_DELAY - 1 {
990                        assert_eq!(entry.output_enable(), true);
991                    }
992                }
993            }
994        }
995    }
996
997    #[test]
998    fn test_row_set_color0() {
999        let mut row: Row<TEST_COLS> = Row::new();
1000
1001        row.set_color0(0, true, false, true);
1002
1003        let mapped_col_0 = get_mapped_index(0);
1004        assert_eq!(row.data[mapped_col_0].red1(), true);
1005        assert_eq!(row.data[mapped_col_0].grn1(), false);
1006        assert_eq!(row.data[mapped_col_0].blu1(), true);
1007
1008        // Test another column
1009        row.set_color0(1, false, true, false);
1010
1011        let mapped_col_1 = get_mapped_index(1);
1012        assert_eq!(row.data[mapped_col_1].red1(), false);
1013        assert_eq!(row.data[mapped_col_1].grn1(), true);
1014        assert_eq!(row.data[mapped_col_1].blu1(), false);
1015    }
1016
1017    #[test]
1018    fn test_row_set_color1() {
1019        let mut row: Row<TEST_COLS> = Row::new();
1020
1021        row.set_color1(0, true, true, false);
1022
1023        let mapped_col_0 = get_mapped_index(0);
1024        assert_eq!(row.data[mapped_col_0].red2(), true);
1025        assert_eq!(row.data[mapped_col_0].grn2(), true);
1026        assert_eq!(row.data[mapped_col_0].blu2(), false);
1027    }
1028
1029    #[test]
1030    fn test_row_default() {
1031        let row1: Row<TEST_COLS> = Row::new();
1032        let row2: Row<TEST_COLS> = Row::default();
1033
1034        // Both should be equivalent
1035        assert_eq!(row1, row2);
1036        assert_eq!(row1.data.len(), row2.data.len());
1037
1038        // Check that all entries are initialized to zero
1039        for (entry1, entry2) in row1.data.iter().zip(row2.data.iter()) {
1040            assert_eq!(entry1.0, entry2.0);
1041            assert_eq!(entry1.0, 0);
1042        }
1043    }
1044
1045    #[test]
1046    fn test_frame_construction() {
1047        let frame: Frame<TEST_ROWS, TEST_COLS, TEST_NROWS> = Frame::new();
1048        assert_eq!(frame.rows.len(), TEST_NROWS);
1049    }
1050
1051    #[test]
1052    fn test_frame_format() {
1053        let mut frame: Frame<TEST_ROWS, TEST_COLS, TEST_NROWS> = Frame::new();
1054
1055        frame.format();
1056
1057        // Check that each row was formatted with correct address parameters
1058        for addr in 0..TEST_NROWS {
1059            let prev_addr = if addr == 0 { TEST_NROWS - 1 } else { addr - 1 };
1060
1061            // Check some key pixels in each row
1062            let row = &frame.rows[addr];
1063
1064            // Check last pixel has correct new address
1065            let last_pixel_idx = get_mapped_index(TEST_COLS - 1);
1066            assert_eq!(row.data[last_pixel_idx].addr(), addr as u16);
1067            assert_eq!(row.data[last_pixel_idx].latch(), true);
1068
1069            // Check non-last pixels have previous address
1070            let first_pixel_idx = get_mapped_index(0);
1071            assert_eq!(row.data[first_pixel_idx].addr(), prev_addr as u16);
1072            assert_eq!(row.data[first_pixel_idx].latch(), false);
1073        }
1074    }
1075
1076    #[test]
1077    fn test_frame_set_pixel() {
1078        let mut frame: Frame<TEST_ROWS, TEST_COLS, TEST_NROWS> = Frame::new();
1079
1080        // Test setting pixel in upper half (y < NROWS)
1081        frame.set_pixel(5, 10, true, false, true);
1082
1083        let mapped_col_10 = get_mapped_index(10);
1084        assert_eq!(frame.rows[5].data[mapped_col_10].red1(), true);
1085        assert_eq!(frame.rows[5].data[mapped_col_10].grn1(), false);
1086        assert_eq!(frame.rows[5].data[mapped_col_10].blu1(), true);
1087
1088        // Test setting pixel in lower half (y >= NROWS)
1089        frame.set_pixel(TEST_NROWS + 5, 15, false, true, false);
1090
1091        let mapped_col_15 = get_mapped_index(15);
1092        assert_eq!(frame.rows[5].data[mapped_col_15].red2(), false);
1093        assert_eq!(frame.rows[5].data[mapped_col_15].grn2(), true);
1094        assert_eq!(frame.rows[5].data[mapped_col_15].blu2(), false);
1095    }
1096
1097    #[test]
1098    fn test_frame_default() {
1099        let frame1: Frame<TEST_ROWS, TEST_COLS, TEST_NROWS> = Frame::new();
1100        let frame2: Frame<TEST_ROWS, TEST_COLS, TEST_NROWS> = Frame::default();
1101
1102        // Both should be equivalent
1103        assert_eq!(frame1.rows.len(), frame2.rows.len());
1104
1105        // Check that all rows are equivalent
1106        for (row1, row2) in frame1.rows.iter().zip(frame2.rows.iter()) {
1107            assert_eq!(row1, row2);
1108
1109            // Verify all entries are zero-initialized
1110            for (entry1, entry2) in row1.data.iter().zip(row2.data.iter()) {
1111                assert_eq!(entry1.0, entry2.0);
1112                assert_eq!(entry1.0, 0);
1113            }
1114        }
1115    }
1116
1117    #[test]
1118    fn test_dma_framebuffer_construction() {
1119        let fb = TestFrameBuffer::new();
1120        assert_eq!(fb.frames.len(), TEST_FRAME_COUNT);
1121        assert_eq!(fb._align, 0);
1122    }
1123
1124    #[test]
1125    #[cfg(feature = "esp-hal-dma")]
1126    fn test_dma_framebuffer_dma_buffer_size() {
1127        let expected_size =
1128            core::mem::size_of::<[Frame<TEST_ROWS, TEST_COLS, TEST_NROWS>; TEST_FRAME_COUNT]>();
1129        assert_eq!(TestFrameBuffer::dma_buffer_size_bytes(), expected_size);
1130    }
1131
1132    #[test]
1133    fn test_dma_framebuffer_erase() {
1134        let fb = TestFrameBuffer::new();
1135
1136        // After erasing, all frames should be formatted
1137        for frame in &fb.frames {
1138            for addr in 0..TEST_NROWS {
1139                let prev_addr = if addr == 0 { TEST_NROWS - 1 } else { addr - 1 };
1140
1141                // Check some key pixels in each row
1142                let row = &frame.rows[addr];
1143
1144                // Check last pixel has correct new address
1145                let last_pixel_idx = get_mapped_index(TEST_COLS - 1);
1146                assert_eq!(row.data[last_pixel_idx].addr(), addr as u16);
1147                assert_eq!(row.data[last_pixel_idx].latch(), true);
1148
1149                // Check non-last pixels have previous address
1150                let first_pixel_idx = get_mapped_index(0);
1151                assert_eq!(row.data[first_pixel_idx].addr(), prev_addr as u16);
1152                assert_eq!(row.data[first_pixel_idx].latch(), false);
1153            }
1154        }
1155    }
1156
1157    #[test]
1158    fn test_dma_framebuffer_set_pixel_bounds() {
1159        let mut fb = TestFrameBuffer::new();
1160
1161        // Test negative coordinates
1162        fb.set_pixel(Point::new(-1, 5), Color::RED);
1163        fb.set_pixel(Point::new(5, -1), Color::RED);
1164
1165        // Test coordinates out of bounds (should not panic)
1166        fb.set_pixel(Point::new(TEST_COLS as i32, 5), Color::RED);
1167        fb.set_pixel(Point::new(5, TEST_ROWS as i32), Color::RED);
1168    }
1169
1170    #[test]
1171    fn test_dma_framebuffer_set_pixel_internal() {
1172        let mut fb = TestFrameBuffer::new();
1173
1174        let red_color = Color::RED;
1175        fb.set_pixel_internal(10, 5, red_color);
1176
1177        // With 3-bit depth, brightness steps are 32 (256/8)
1178        // Frames represent thresholds: 32, 64, 96, 128, 160, 192, 224
1179        // Red value 255 should activate all frames
1180        for frame in &fb.frames {
1181            // Check upper half pixel
1182            let mapped_col_10 = get_mapped_index(10);
1183            assert_eq!(frame.rows[5].data[mapped_col_10].red1(), true);
1184            assert_eq!(frame.rows[5].data[mapped_col_10].grn1(), false);
1185            assert_eq!(frame.rows[5].data[mapped_col_10].blu1(), false);
1186        }
1187    }
1188
1189    #[test]
1190    fn test_dma_framebuffer_brightness_modulation() {
1191        let mut fb = TestFrameBuffer::new();
1192
1193        // Test with a medium brightness value
1194        let brightness_step = 1 << (8 - TEST_BITS); // 32 for 3-bit
1195        let test_brightness = brightness_step * 3; // 96
1196        let color = Color::from(embedded_graphics::pixelcolor::Rgb888::new(
1197            test_brightness,
1198            0,
1199            0,
1200        ));
1201
1202        fb.set_pixel_internal(0, 0, color);
1203
1204        // Should activate frames 0, 1, 2 (thresholds 32, 64, 96)
1205        // but not frames 3, 4, 5, 6 (thresholds 128, 160, 192, 224)
1206        for (frame_idx, frame) in fb.frames.iter().enumerate() {
1207            let frame_threshold = (frame_idx as u8 + 1) * brightness_step;
1208            let should_be_active = test_brightness >= frame_threshold;
1209
1210            let mapped_col_0 = get_mapped_index(0);
1211            assert_eq!(frame.rows[0].data[mapped_col_0].red1(), should_be_active);
1212        }
1213    }
1214
1215    #[test]
1216    fn test_origin_dimensions() {
1217        let fb = TestFrameBuffer::new();
1218        let size = fb.size();
1219        assert_eq!(size.width, TEST_COLS as u32);
1220        assert_eq!(size.height, TEST_ROWS as u32);
1221
1222        // Test reference
1223        let size = (&fb).size();
1224        assert_eq!(size.width, TEST_COLS as u32);
1225        assert_eq!(size.height, TEST_ROWS as u32);
1226
1227        // Test mutable reference
1228        let mut fb = TestFrameBuffer::new();
1229        let fb_ref = &mut fb;
1230        let size = fb_ref.size();
1231        assert_eq!(size.width, TEST_COLS as u32);
1232        assert_eq!(size.height, TEST_ROWS as u32);
1233    }
1234
1235    #[test]
1236    fn test_draw_target() {
1237        let mut fb = TestFrameBuffer::new();
1238
1239        let pixels = vec![
1240            embedded_graphics::Pixel(Point::new(0, 0), Color::RED),
1241            embedded_graphics::Pixel(Point::new(1, 1), Color::GREEN),
1242            embedded_graphics::Pixel(Point::new(2, 2), Color::BLUE),
1243        ];
1244
1245        let result = fb.draw_iter(pixels);
1246        assert!(result.is_ok());
1247
1248        // Test mutable reference
1249        let result = (&mut fb).draw_iter(vec![embedded_graphics::Pixel(
1250            Point::new(3, 3),
1251            Color::WHITE,
1252        )]);
1253        assert!(result.is_ok());
1254    }
1255
1256    #[test]
1257    fn test_draw_iter_pixel_verification() {
1258        let mut fb = TestFrameBuffer::new();
1259
1260        // Create test pixels with specific colors and positions
1261        let pixels = vec![
1262            // Upper half pixels (y < NROWS) - should set color0
1263            embedded_graphics::Pixel(Point::new(5, 2), Color::RED), // (5, 2) -> red
1264            embedded_graphics::Pixel(Point::new(10, 5), Color::GREEN), // (10, 5) -> green
1265            embedded_graphics::Pixel(Point::new(15, 8), Color::BLUE), // (15, 8) -> blue
1266            embedded_graphics::Pixel(Point::new(20, 10), Color::WHITE), // (20, 10) -> white
1267            // Lower half pixels (y >= NROWS) - should set color1
1268            embedded_graphics::Pixel(Point::new(25, (TEST_NROWS + 3) as i32), Color::RED), // (25, 19) -> red
1269            embedded_graphics::Pixel(Point::new(30, (TEST_NROWS + 7) as i32), Color::GREEN), // (30, 23) -> green
1270            embedded_graphics::Pixel(Point::new(35, (TEST_NROWS + 12) as i32), Color::BLUE), // (35, 28) -> blue
1271            // Edge case: black pixel (should not be visible in first frame)
1272            embedded_graphics::Pixel(Point::new(40, 1), Color::BLACK), // (40, 1) -> black
1273        ];
1274
1275        let result = fb.draw_iter(pixels);
1276        assert!(result.is_ok());
1277
1278        // Check the first frame only
1279        let first_frame = &fb.frames[0];
1280        let brightness_step = 1 << (8 - TEST_BITS); // 32 for 3-bit
1281        let first_frame_threshold = brightness_step; // 32
1282
1283        // Test upper half pixels (color0)
1284        // Red pixel at (5, 2) - should be red in first frame
1285        let col_idx = get_mapped_index(5);
1286        assert_eq!(
1287            first_frame.rows[2].data[col_idx].red1(),
1288            Color::RED.r() >= first_frame_threshold
1289        );
1290        assert_eq!(
1291            first_frame.rows[2].data[col_idx].grn1(),
1292            Color::RED.g() >= first_frame_threshold
1293        );
1294        assert_eq!(
1295            first_frame.rows[2].data[col_idx].blu1(),
1296            Color::RED.b() >= first_frame_threshold
1297        );
1298
1299        // Green pixel at (10, 5) - should be green in first frame
1300        let col_idx = get_mapped_index(10);
1301        assert_eq!(
1302            first_frame.rows[5].data[col_idx].red1(),
1303            Color::GREEN.r() >= first_frame_threshold
1304        );
1305        assert_eq!(
1306            first_frame.rows[5].data[col_idx].grn1(),
1307            Color::GREEN.g() >= first_frame_threshold
1308        );
1309        assert_eq!(
1310            first_frame.rows[5].data[col_idx].blu1(),
1311            Color::GREEN.b() >= first_frame_threshold
1312        );
1313
1314        // Blue pixel at (15, 8) - should be blue in first frame
1315        let col_idx = get_mapped_index(15);
1316        assert_eq!(
1317            first_frame.rows[8].data[col_idx].red1(),
1318            Color::BLUE.r() >= first_frame_threshold
1319        );
1320        assert_eq!(
1321            first_frame.rows[8].data[col_idx].grn1(),
1322            Color::BLUE.g() >= first_frame_threshold
1323        );
1324        assert_eq!(
1325            first_frame.rows[8].data[col_idx].blu1(),
1326            Color::BLUE.b() >= first_frame_threshold
1327        );
1328
1329        // White pixel at (20, 10) - should be white in first frame
1330        let col_idx = get_mapped_index(20);
1331        assert_eq!(
1332            first_frame.rows[10].data[col_idx].red1(),
1333            Color::WHITE.r() >= first_frame_threshold
1334        );
1335        assert_eq!(
1336            first_frame.rows[10].data[col_idx].grn1(),
1337            Color::WHITE.g() >= first_frame_threshold
1338        );
1339        assert_eq!(
1340            first_frame.rows[10].data[col_idx].blu1(),
1341            Color::WHITE.b() >= first_frame_threshold
1342        );
1343
1344        // Test lower half pixels (color1)
1345        // Red pixel at (25, TEST_NROWS + 3) -> row 3, color1
1346        let col_idx = get_mapped_index(25);
1347        assert_eq!(
1348            first_frame.rows[3].data[col_idx].red2(),
1349            Color::RED.r() >= first_frame_threshold
1350        );
1351        assert_eq!(
1352            first_frame.rows[3].data[col_idx].grn2(),
1353            Color::RED.g() >= first_frame_threshold
1354        );
1355        assert_eq!(
1356            first_frame.rows[3].data[col_idx].blu2(),
1357            Color::RED.b() >= first_frame_threshold
1358        );
1359
1360        // Green pixel at (30, TEST_NROWS + 7) -> row 7, color1
1361        let col_idx = get_mapped_index(30);
1362        assert_eq!(
1363            first_frame.rows[7].data[col_idx].red2(),
1364            Color::GREEN.r() >= first_frame_threshold
1365        );
1366        assert_eq!(
1367            first_frame.rows[7].data[col_idx].grn2(),
1368            Color::GREEN.g() >= first_frame_threshold
1369        );
1370        assert_eq!(
1371            first_frame.rows[7].data[col_idx].blu2(),
1372            Color::GREEN.b() >= first_frame_threshold
1373        );
1374
1375        // Blue pixel at (35, TEST_NROWS + 12) -> row 12, color1
1376        let col_idx = get_mapped_index(35);
1377        assert_eq!(
1378            first_frame.rows[12].data[col_idx].red2(),
1379            Color::BLUE.r() >= first_frame_threshold
1380        );
1381        assert_eq!(
1382            first_frame.rows[12].data[col_idx].grn2(),
1383            Color::BLUE.g() >= first_frame_threshold
1384        );
1385        assert_eq!(
1386            first_frame.rows[12].data[col_idx].blu2(),
1387            Color::BLUE.b() >= first_frame_threshold
1388        );
1389
1390        // Test black pixel - should not be visible in any frame
1391        let col_idx = get_mapped_index(40);
1392        assert_eq!(first_frame.rows[1].data[col_idx].red1(), false);
1393        assert_eq!(first_frame.rows[1].data[col_idx].grn1(), false);
1394        assert_eq!(first_frame.rows[1].data[col_idx].blu1(), false);
1395    }
1396
1397    #[test]
1398    fn test_embedded_graphics_integration() {
1399        let mut fb = TestFrameBuffer::new();
1400
1401        // Draw a rectangle
1402        let result = Rectangle::new(Point::new(5, 5), Size::new(10, 8))
1403            .into_styled(PrimitiveStyle::with_fill(Color::RED))
1404            .draw(&mut fb);
1405        assert!(result.is_ok());
1406
1407        // Draw a circle
1408        let result = Circle::new(Point::new(30, 15), 8)
1409            .into_styled(PrimitiveStyle::with_fill(Color::BLUE))
1410            .draw(&mut fb);
1411        assert!(result.is_ok());
1412    }
1413
1414    #[test]
1415    #[cfg(feature = "skip-black-pixels")]
1416    fn test_skip_black_pixels_enabled() {
1417        let mut fb = TestFrameBuffer::new();
1418
1419        // Set a red pixel first
1420        fb.set_pixel_internal(10, 5, Color::RED);
1421
1422        // Verify it's red in the first frame
1423        let mapped_col_10 = get_mapped_index(10);
1424        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].red1(), true);
1425        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].grn1(), false);
1426        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].blu1(), false);
1427
1428        // Now set it to black - with skip-black-pixels enabled, this should be ignored
1429        fb.set_pixel_internal(10, 5, Color::BLACK);
1430
1431        // The pixel should still be red (black write was skipped)
1432        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].red1(), true);
1433        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].grn1(), false);
1434        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].blu1(), false);
1435    }
1436
1437    #[test]
1438    #[cfg(not(feature = "skip-black-pixels"))]
1439    fn test_skip_black_pixels_disabled() {
1440        let mut fb = TestFrameBuffer::new();
1441
1442        // Set a red pixel first
1443        fb.set_pixel_internal(10, 5, Color::RED);
1444
1445        // Verify it's red in the first frame
1446        let mapped_col_10 = get_mapped_index(10);
1447        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].red1(), true);
1448        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].grn1(), false);
1449        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].blu1(), false);
1450
1451        // Now set it to black - with skip-black-pixels disabled, this should overwrite
1452        fb.set_pixel_internal(10, 5, Color::BLACK);
1453
1454        // The pixel should now be black (all bits false)
1455        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].red1(), false);
1456        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].grn1(), false);
1457        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].blu1(), false);
1458    }
1459
1460    #[test]
1461    fn test_bcm_frame_overwrite() {
1462        let mut fb = TestFrameBuffer::new();
1463
1464        // First write a white pixel (255, 255, 255)
1465        fb.set_pixel_internal(10, 5, Color::WHITE);
1466
1467        let mapped_col_10 = get_mapped_index(10);
1468
1469        // Verify white pixel is lit in all frames (255 >= all thresholds)
1470        for frame in fb.frames.iter() {
1471            // White (255) should be active in all frames since it's >= all thresholds
1472            assert_eq!(frame.rows[5].data[mapped_col_10].red1(), true);
1473            assert_eq!(frame.rows[5].data[mapped_col_10].grn1(), true);
1474            assert_eq!(frame.rows[5].data[mapped_col_10].blu1(), true);
1475        }
1476
1477        // Now overwrite with 50% white (128, 128, 128)
1478        let half_white = Color::from(embedded_graphics::pixelcolor::Rgb888::new(128, 128, 128));
1479        fb.set_pixel_internal(10, 5, half_white);
1480
1481        // Verify only the correct frames are lit for 50% white
1482        // With 3-bit depth: thresholds are 32, 64, 96, 128, 160, 192, 224
1483        // 128 should activate frames 0, 1, 2, 3 (thresholds 32, 64, 96, 128)
1484        // but not frames 4, 5, 6 (thresholds 160, 192, 224)
1485        let brightness_step = 1 << (8 - TEST_BITS); // 32 for 3-bit
1486        for (frame_idx, frame) in fb.frames.iter().enumerate() {
1487            let frame_threshold = (frame_idx as u8 + 1) * brightness_step;
1488            let should_be_active = 128 >= frame_threshold;
1489
1490            assert_eq!(frame.rows[5].data[mapped_col_10].red1(), should_be_active);
1491            assert_eq!(frame.rows[5].data[mapped_col_10].grn1(), should_be_active);
1492            assert_eq!(frame.rows[5].data[mapped_col_10].blu1(), should_be_active);
1493        }
1494
1495        // Specifically verify the expected pattern for 3-bit depth
1496        // Frames 0-3 should be active (thresholds 32, 64, 96, 128)
1497        for frame_idx in 0..4 {
1498            assert_eq!(
1499                fb.frames[frame_idx].rows[5].data[mapped_col_10].red1(),
1500                true
1501            );
1502        }
1503        // Frames 4-6 should be inactive (thresholds 160, 192, 224)
1504        for frame_idx in 4..TEST_FRAME_COUNT {
1505            assert_eq!(
1506                fb.frames[frame_idx].rows[5].data[mapped_col_10].red1(),
1507                false
1508            );
1509        }
1510    }
1511
1512    #[test]
1513    fn test_read_buffer_implementation() {
1514        // Test owned implementation - explicitly move the framebuffer to ensure we're testing the owned impl
1515        let fb = TestFrameBuffer::new();
1516        let expected_size = core::mem::size_of_val(&fb.frames);
1517
1518        // Test owned ReadBuffer implementation by calling ReadBuffer::read_buffer explicitly
1519        unsafe {
1520            let (ptr, len) = <TestFrameBuffer as ReadBuffer>::read_buffer(&fb);
1521            assert!(!ptr.is_null());
1522            assert_eq!(len, expected_size);
1523        }
1524
1525        // Test direct method call on owned value
1526        unsafe {
1527            let (ptr, len) = fb.read_buffer();
1528            assert!(!ptr.is_null());
1529            assert_eq!(len, expected_size);
1530        }
1531
1532        // Test reference implementation
1533        let fb = TestFrameBuffer::new();
1534        let fb_ref = &fb;
1535        unsafe {
1536            let (ptr, len) = fb_ref.read_buffer();
1537            assert!(!ptr.is_null());
1538            assert_eq!(len, core::mem::size_of_val(&fb.frames));
1539        }
1540
1541        // Test mutable reference implementation
1542        let mut fb = TestFrameBuffer::new();
1543        let fb_ref = &mut fb;
1544        unsafe {
1545            let (ptr, len) = fb_ref.read_buffer();
1546            assert!(!ptr.is_null());
1547            assert_eq!(len, core::mem::size_of_val(&fb.frames));
1548        }
1549    }
1550
1551    #[test]
1552    fn test_read_buffer_owned_implementation() {
1553        // This test specifically ensures the owned ReadBuffer implementation is tested
1554        // by consuming the framebuffer and testing the pointer validity
1555        fn test_owned_read_buffer(fb: TestFrameBuffer) -> (bool, usize) {
1556            unsafe {
1557                let (ptr, len) = fb.read_buffer();
1558                (!ptr.is_null(), len)
1559            }
1560        }
1561
1562        let fb = TestFrameBuffer::new();
1563        let expected_len = core::mem::size_of_val(&fb.frames);
1564
1565        let (ptr_valid, actual_len) = test_owned_read_buffer(fb);
1566        assert!(ptr_valid);
1567        assert_eq!(actual_len, expected_len);
1568    }
1569
1570    #[test]
1571    fn test_framebuffer_trait() {
1572        let fb = TestFrameBuffer::new();
1573        assert_eq!(fb.get_word_size(), WordSize::Sixteen);
1574
1575        let fb_ref = &fb;
1576        assert_eq!(fb_ref.get_word_size(), WordSize::Sixteen);
1577
1578        let mut fb = TestFrameBuffer::new();
1579        let fb_ref = &mut fb;
1580        assert_eq!(fb_ref.get_word_size(), WordSize::Sixteen);
1581    }
1582
1583    #[test]
1584    fn test_debug_formatting() {
1585        let fb = TestFrameBuffer::new();
1586        let debug_string = format!("{:?}", fb);
1587        assert!(debug_string.contains("DmaFrameBuffer"));
1588        assert!(debug_string.contains("frame_count"));
1589        assert!(debug_string.contains("frame_size"));
1590        assert!(debug_string.contains("brightness_step"));
1591    }
1592
1593    #[test]
1594    fn test_default_implementation() {
1595        let fb1 = TestFrameBuffer::new();
1596        let fb2 = TestFrameBuffer::default();
1597
1598        // Both should be equivalent in size, but may differ in content since new() calls format()
1599        assert_eq!(fb1.frames.len(), fb2.frames.len());
1600        assert_eq!(fb1._align, fb2._align);
1601    }
1602
1603    #[test]
1604    fn test_memory_alignment() {
1605        let fb = TestFrameBuffer::new();
1606        let ptr = &fb as *const _ as usize;
1607
1608        // Should be aligned to 64-bit boundary due to the _align field
1609        assert_eq!(ptr % 8, 0);
1610    }
1611
1612    #[test]
1613    fn test_color_values() {
1614        let mut fb = TestFrameBuffer::new();
1615
1616        // Test different color values
1617        let colors = [
1618            (Color::RED, (255, 0, 0)),
1619            (Color::GREEN, (0, 255, 0)),
1620            (Color::BLUE, (0, 0, 255)),
1621            (Color::WHITE, (255, 255, 255)),
1622            (Color::BLACK, (0, 0, 0)),
1623        ];
1624
1625        for (i, (color, (r, g, b))) in colors.iter().enumerate() {
1626            fb.set_pixel(Point::new(i as i32, 0), *color);
1627            assert_eq!(color.r(), *r);
1628            assert_eq!(color.g(), *g);
1629            assert_eq!(color.b(), *b);
1630        }
1631    }
1632
1633    #[test]
1634    fn test_blanking_delay() {
1635        let mut row: Row<TEST_COLS> = Row::new();
1636        let test_addr = 5;
1637        let prev_addr = 4;
1638
1639        row.format(test_addr, prev_addr);
1640
1641        // Test that the blanking delay is respected
1642        let blanking_pixel_idx = get_mapped_index(TEST_COLS - BLANKING_DELAY - 1);
1643        assert_eq!(row.data[blanking_pixel_idx].output_enable(), false);
1644
1645        // Test that pixels before blanking delay have output enabled (if after pixel 1)
1646        let before_blanking_idx = get_mapped_index(TEST_COLS - BLANKING_DELAY - 2);
1647        assert_eq!(row.data[before_blanking_idx].output_enable(), true);
1648    }
1649
1650    #[test]
1651    fn test_esp32_mapping() {
1652        // Test the ESP32-specific index mapping
1653        #[cfg(feature = "esp32-ordering")]
1654        {
1655            assert_eq!(map_index(0), 1);
1656            assert_eq!(map_index(1), 0);
1657            assert_eq!(map_index(2), 3);
1658            assert_eq!(map_index(3), 2);
1659            assert_eq!(map_index(4), 5);
1660            assert_eq!(map_index(5), 4);
1661        }
1662        #[cfg(not(feature = "esp32-ordering"))]
1663        {
1664            assert_eq!(map_index(0), 0);
1665            assert_eq!(map_index(1), 1);
1666            assert_eq!(map_index(2), 2);
1667            assert_eq!(map_index(3), 3);
1668        }
1669    }
1670
1671    #[test]
1672    fn test_bits_assertion() {
1673        // Test that BITS <= 8 assertion is enforced at compile time
1674        // This test mainly documents the constraint
1675        assert!(TEST_BITS <= 8);
1676    }
1677
1678    #[test]
1679    fn test_fast_clear_method() {
1680        let mut fb = TestFrameBuffer::new();
1681
1682        // Set some pixels
1683        fb.set_pixel_internal(10, 5, Color::RED);
1684        fb.set_pixel_internal(20, 10, Color::GREEN);
1685
1686        // Verify pixels are set
1687        let mapped_col_10 = get_mapped_index(10);
1688        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].red1(), true);
1689
1690        // Clear using fast method
1691        fb.erase();
1692
1693        // Verify pixels are cleared but timing signals remain
1694        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].red1(), false);
1695        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].grn1(), false);
1696        assert_eq!(fb.frames[0].rows[5].data[mapped_col_10].blu1(), false);
1697
1698        // Verify timing signals are still present (check last pixel has latch)
1699        let last_col = get_mapped_index(TEST_COLS - 1);
1700        assert_eq!(fb.frames[0].rows[5].data[last_col].latch(), true);
1701    }
1702
1703    // Remove the old `test_draw_char_bottom_right` and replace with a helper + combined test.
1704
1705    // Constants for FONT_6X10 glyph size
1706    const CHAR_W: i32 = 6;
1707    const CHAR_H: i32 = 10;
1708
1709    /// Draws glyph 'A' at `origin` and validates framebuffer pixels against a reference.
1710    fn verify_glyph_at(fb: &mut TestFrameBuffer, origin: Point) {
1711        use embedded_graphics::mock_display::MockDisplay;
1712        use embedded_graphics::mono_font::ascii::FONT_6X10;
1713        use embedded_graphics::mono_font::MonoTextStyle;
1714        use embedded_graphics::text::{Baseline, Text};
1715
1716        // Draw the glyph
1717        let style = MonoTextStyle::new(&FONT_6X10, Color::WHITE);
1718        Text::with_baseline("A", origin, style, Baseline::Top)
1719            .draw(fb)
1720            .unwrap();
1721
1722        // Reference glyph at (0,0)
1723        let mut reference: MockDisplay<Color> = MockDisplay::new();
1724        Text::with_baseline("A", Point::zero(), style, Baseline::Top)
1725            .draw(&mut reference)
1726            .unwrap();
1727
1728        for dy in 0..CHAR_H {
1729            for dx in 0..CHAR_W {
1730                let expected_on = reference
1731                    .get_pixel(Point::new(dx, dy))
1732                    .unwrap_or(Color::BLACK)
1733                    != Color::BLACK;
1734
1735                let gx = (origin.x + dx) as usize;
1736                let gy = (origin.y + dy) as usize;
1737
1738                // we have computed the origin to be within the panel, so we don't need to check for bounds
1739                // if gx >= TEST_COLS || gy >= TEST_ROWS {
1740                //     continue;
1741                // }
1742
1743                // Fetch Entry from frame 0
1744                let frame0 = &fb.frames[0];
1745                let e = if gy < TEST_NROWS {
1746                    &frame0.rows[gy].data[get_mapped_index(gx)]
1747                } else {
1748                    &frame0.rows[gy - TEST_NROWS].data[get_mapped_index(gx)]
1749                };
1750
1751                let (r, g, b) = if gy >= TEST_NROWS {
1752                    (e.red2(), e.grn2(), e.blu2())
1753                } else {
1754                    (e.red1(), e.grn1(), e.blu1())
1755                };
1756
1757                if expected_on {
1758                    assert!(r && g && b,);
1759                } else {
1760                    assert!(!r && !g && !b);
1761                }
1762            }
1763        }
1764    }
1765
1766    #[test]
1767    fn test_draw_char_corners() {
1768        let upper_left = Point::new(0, 0);
1769        let lower_right = Point::new(TEST_COLS as i32 - CHAR_W, TEST_ROWS as i32 - CHAR_H);
1770
1771        let mut fb = TestFrameBuffer::new();
1772
1773        verify_glyph_at(&mut fb, upper_left);
1774        verify_glyph_at(&mut fb, lower_right);
1775    }
1776
1777    #[test]
1778    fn test_framebuffer_operations_trait_erase() {
1779        let mut fb = TestFrameBuffer::new();
1780
1781        // Set pixels so erase has work to do
1782        fb.set_pixel_internal(10, 5, Color::RED);
1783        fb.set_pixel_internal(20, 10, Color::GREEN);
1784
1785        // Explicitly call trait method to hit the FrameBufferOperations impl
1786        <TestFrameBuffer as FrameBufferOperations<
1787            TEST_ROWS,
1788            TEST_COLS,
1789            TEST_NROWS,
1790            TEST_BITS,
1791            TEST_FRAME_COUNT,
1792        >>::erase(&mut fb);
1793
1794        // Verify colors cleared on frame 0
1795        let mc10 = get_mapped_index(10);
1796        let mc20 = get_mapped_index(20);
1797        assert_eq!(fb.frames[0].rows[5].data[mc10].red1(), false);
1798        assert_eq!(fb.frames[0].rows[10].data[mc20].grn1(), false);
1799
1800        // Timing signals preserved: last pixel should have latch
1801        let last_col = get_mapped_index(TEST_COLS - 1);
1802        assert!(fb.frames[0].rows[0].data[last_col].latch());
1803    }
1804
1805    #[test]
1806    fn test_framebuffer_operations_trait_set_pixel() {
1807        let mut fb = TestFrameBuffer::new();
1808
1809        // Explicitly call trait method to hit the FrameBufferOperations impl
1810        <TestFrameBuffer as FrameBufferOperations<
1811            TEST_ROWS,
1812            TEST_COLS,
1813            TEST_NROWS,
1814            TEST_BITS,
1815            TEST_FRAME_COUNT,
1816        >>::set_pixel(&mut fb, Point::new(8, 3), Color::BLUE);
1817
1818        let idx = get_mapped_index(8);
1819        assert_eq!(fb.frames[0].rows[3].data[idx].blu1(), true);
1820        assert_eq!(fb.frames[0].rows[3].data[idx].red1(), false);
1821        assert_eq!(fb.frames[0].rows[3].data[idx].grn1(), false);
1822    }
1823}