@@ -56,8 +56,48 @@ const LCD_H_RES: usize = 206;
5656const LCD_V_RES : usize = 320 ;
5757const LCD_BUFFER_SIZE : usize = LCD_H_RES * LCD_V_RES ;
5858
59+ use embedded_graphics:: pixelcolor:: PixelColor ;
60+ use embedded_graphics_framebuf:: backends:: FrameBufferBackend ;
61+
62+ /// A wrapper around a boxed array that implements FrameBufferBackend.
63+ /// This allows the framebuffer to be allocated on the heap.
64+ pub struct HeapBuffer < C : PixelColor , const N : usize > ( Box < [ C ; N ] > ) ;
65+
66+ impl < C : PixelColor , const N : usize > HeapBuffer < C , N > {
67+ pub fn new ( data : Box < [ C ; N ] > ) -> Self {
68+ Self ( data)
69+ }
70+ }
71+
72+ impl < C : PixelColor , const N : usize > core:: ops:: Deref for HeapBuffer < C , N > {
73+ type Target = [ C ; N ] ;
74+ fn deref ( & self ) -> & Self :: Target {
75+ & * self . 0
76+ }
77+ }
78+
79+ impl < C : PixelColor , const N : usize > core:: ops:: DerefMut for HeapBuffer < C , N > {
80+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
81+ & mut * self . 0
82+ }
83+ }
84+
85+ impl < C : PixelColor , const N : usize > FrameBufferBackend for HeapBuffer < C , N > {
86+ type Color = C ;
87+ fn set ( & mut self , index : usize , color : Self :: Color ) {
88+ self . 0 [ index] = color;
89+ }
90+ fn get ( & self , index : usize ) -> Self :: Color {
91+ self . 0 [ index]
92+ }
93+ fn nr_elements ( & self ) -> usize {
94+ N
95+ }
96+ }
97+
98+
5999// We want our pixels stored as Rgb565.
60- type FbBuffer = [ Rgb565 ; LCD_BUFFER_SIZE ] ;
100+ type FbBuffer = HeapBuffer < Rgb565 , LCD_BUFFER_SIZE > ;
61101// Define a type alias for the complete FrameBuf.
62102type MyFrameBuf = FrameBuf < Rgb565 , FbBuffer > ;
63103
@@ -68,9 +108,10 @@ struct FrameBufferResource {
68108
69109impl FrameBufferResource {
70110 fn new ( ) -> Self {
71- // Allocate the framebuffer data as an owned array of Rgb565.
72- let fb_data: FbBuffer = * Box :: new ( [ Rgb565 :: BLACK ; LCD_BUFFER_SIZE ] ) ;
73- let frame_buf = MyFrameBuf :: new ( fb_data, LCD_H_RES , LCD_V_RES ) ;
111+ // Allocate the framebuffer data on the heap.
112+ let fb_data: Box < [ Rgb565 ; LCD_BUFFER_SIZE ] > = Box :: new ( [ Rgb565 :: BLACK ; LCD_BUFFER_SIZE ] ) ;
113+ let heap_buffer = HeapBuffer :: new ( fb_data) ;
114+ let frame_buf = MyFrameBuf :: new ( heap_buffer, LCD_H_RES , LCD_V_RES ) ;
74115 Self { frame_buf }
75116 }
76117}
0 commit comments