2929#include "nrfx_systick.h"
3030#include "spi.h"
3131#include "system_font.h"
32+ #include "nrfx_log.h"
33+
34+ typedef struct colors_t
35+ {
36+ const char * name ;
37+ uint8_t initial_y : 4 ;
38+ uint8_t initial_cb : 3 ;
39+ uint8_t initial_cr : 3 ;
40+ } colors_t ;
41+
42+ static colors_t colors [16 ] = {
43+ {"VOID" , 0 , 4 , 4 },
44+ {"WHITE" , 15 , 4 , 4 },
45+ {"GREY" , 7 , 4 , 4 },
46+ {"RED" , 5 , 3 , 6 },
47+ {"PINK" , 9 , 3 , 5 },
48+ {"DARKBROWN" , 2 , 2 , 5 },
49+ {"BROWN" , 4 , 2 , 5 },
50+ {"ORANGE" , 9 , 2 , 5 },
51+ {"YELLOW" , 13 , 2 , 4 },
52+ {"DARKGREEN" , 4 , 4 , 3 },
53+ {"GREEN" , 6 , 2 , 3 },
54+ {"LIGHTGREEN" , 10 , 1 , 3 },
55+ {"NIGHTBLUE" , 1 , 5 , 2 },
56+ {"SEABLUE" , 4 , 5 , 2 },
57+ {"SKYBLUE" , 8 , 5 , 2 },
58+ {"CLOUDBLUE" , 13 , 4 , 3 },
59+ };
3260
3361static uint32_t utf8_decode (const char * string , size_t * index )
3462{
@@ -70,18 +98,38 @@ static uint32_t utf8_decode(const char *string, size_t *index)
7098 return codepoint ;
7199}
72100
101+ static void assign_color_to_palette (uint8_t palette_index ,
102+ uint8_t y ,
103+ uint8_t cb ,
104+ uint8_t cr )
105+ {
106+ uint8_t data [4 ] = {palette_index , y , cb , cr };
107+
108+ spi_write (FPGA , 0x11 , (uint8_t * )data , sizeof (data ));
109+ }
110+
73111static int lua_display_assign_color (lua_State * L )
74112{
75- lua_Integer pallet_index = luaL_checkinteger (L , 1 ) - 1 ;
76- lua_Integer red = luaL_checkinteger (L , 2 );
77- lua_Integer green = luaL_checkinteger (L , 3 );
78- lua_Integer blue = luaL_checkinteger (L , 4 );
113+ uint8_t color_palette_index ;
79114
80- if ( pallet_index < 0 || pallet_index > 15 )
115+ for ( uint8_t i = 0 ; i <= 16 ; i ++ )
81116 {
82- luaL_error (L , "pallet_index must be between 1 and 16" );
117+ if (i == 16 )
118+ {
119+ luaL_error (L , "Invalid color name" );
120+ }
121+
122+ if (strcmp (luaL_checkstring (L , 1 ), colors [i ].name ) == 0 )
123+ {
124+ color_palette_index = i ;
125+ break ;
126+ }
83127 }
84128
129+ lua_Integer red = luaL_checkinteger (L , 2 );
130+ lua_Integer green = luaL_checkinteger (L , 3 );
131+ lua_Integer blue = luaL_checkinteger (L , 4 );
132+
85133 if (red < 0 || red > 255 )
86134 {
87135 luaL_error (L , "red component must be between 0 and 255" );
@@ -101,49 +149,55 @@ static int lua_display_assign_color(lua_State *L)
101149 double cb = floor (-0.169 * red - 0.331 * green + 0.5 * blue + 128 );
102150 double cr = floor (0.5 * red - 0.419 * green - 0.081 * blue + 128 );
103151
104- uint8_t data [4 ] = {(uint8_t )pallet_index ,
105- (uint8_t )y ,
106- (uint8_t )cb ,
107- (uint8_t )cr };
108-
109- spi_write (FPGA , 0x11 , (uint8_t * )data , sizeof (data ));
152+ assign_color_to_palette (color_palette_index ,
153+ ((uint8_t )y ) >> 4 ,
154+ ((uint8_t )cb ) >> 5 ,
155+ ((uint8_t )cr ) >> 5 );
110156
111157 return 0 ;
112158}
113159
114160static int lua_display_assign_color_ycbcr (lua_State * L )
115161{
116- lua_Integer pallet_index = luaL_checkinteger (L , 1 ) - 1 ;
117- lua_Integer y = luaL_checkinteger (L , 2 );
118- lua_Integer cb = luaL_checkinteger (L , 3 );
119- lua_Integer cr = luaL_checkinteger (L , 4 );
162+ uint8_t color_palette_index ;
120163
121- if ( pallet_index < 0 || pallet_index > 15 )
164+ for ( uint8_t i = 0 ; i <= 16 ; i ++ )
122165 {
123- luaL_error (L , "pallet_index must be between 1 and 16" );
166+ if (i == 16 )
167+ {
168+ luaL_error (L , "Invalid color name" );
169+ }
170+
171+ if (strcmp (luaL_checkstring (L , 1 ), colors [i ].name ) == 0 )
172+ {
173+ color_palette_index = i ;
174+ break ;
175+ }
124176 }
125177
126- if (y < 0 || y > 255 )
178+ lua_Integer y = luaL_checkinteger (L , 2 );
179+ lua_Integer cb = luaL_checkinteger (L , 3 );
180+ lua_Integer cr = luaL_checkinteger (L , 4 );
181+
182+ if (y < 0 || y > 15 )
127183 {
128- luaL_error (L , "Y component must be between 0 and 255 " );
184+ luaL_error (L , "Y component must be between 0 and 15 " );
129185 }
130186
131- if (cb < 0 || cb > 255 )
187+ if (cb < 0 || cb > 7 )
132188 {
133- luaL_error (L , "Cb component must be between 0 and 255 " );
189+ luaL_error (L , "Cb component must be between 0 and 7 " );
134190 }
135191
136- if (cr < 0 || cr > 255 )
192+ if (cr < 0 || cr > 7 )
137193 {
138- luaL_error (L , "Cr component must be between 0 and 255 " );
194+ luaL_error (L , "Cr component must be between 0 and 7 " );
139195 }
140196
141- uint8_t data [4 ] = {(uint8_t )pallet_index ,
142- (uint8_t )y ,
143- (uint8_t )cb ,
144- (uint8_t )cr };
145-
146- spi_write (FPGA , 0x11 , (uint8_t * )data , sizeof (data ));
197+ assign_color_to_palette (color_palette_index ,
198+ (uint8_t )y ,
199+ (uint8_t )cb ,
200+ (uint8_t )cr );
147201
148202 return 0 ;
149203}
@@ -228,14 +282,40 @@ static int lua_display_bitmap(lua_State *L)
228282
229283static int lua_display_text (lua_State * L )
230284{
231- // TODO color options
232- // TODO justification options
233- // TODO character spacing
234-
235285 const char * string = luaL_checkstring (L , 1 );
236286 lua_Integer x_position = luaL_checkinteger (L , 2 );
237287 lua_Integer y_position = luaL_checkinteger (L , 3 );
288+ lua_Integer color_palette_offset = 0 ;
238289 lua_Integer character_spacing = 4 ;
290+ // TODO justification options
291+
292+ if (lua_istable (L , 4 ))
293+ {
294+ if (lua_getfield (L , 4 , "color" ) != LUA_TNIL )
295+ {
296+ for (size_t i = 1 ; i <= 16 ; i ++ )
297+ {
298+ if (i == 16 )
299+ {
300+ luaL_error (L , "Invalid color name" );
301+ }
302+
303+ if (strcmp (luaL_checkstring (L , -1 ), colors [i ].name ) == 0 )
304+ {
305+ color_palette_offset = i - 1 ;
306+ break ;
307+ }
308+ }
309+
310+ lua_pop (L , 1 );
311+ }
312+
313+ if (lua_getfield (L , 4 , "spacing" ) != LUA_TNIL )
314+ {
315+ character_spacing = luaL_checkinteger (L , -1 );
316+ lua_pop (L , 1 );
317+ }
318+ }
239319
240320 for (size_t index = 0 ; index < strlen (string );)
241321 {
@@ -276,7 +356,7 @@ static int lua_display_text(lua_State *L)
276356 y_position ,
277357 sprite_metadata [entry ].width ,
278358 sprite_metadata [entry ].colors ,
279- 0 , // TODO
359+ color_palette_offset ,
280360 sprite_data + data_offset ,
281361 data_length );
282362
@@ -378,4 +458,13 @@ void lua_open_display_library(lua_State *L)
378458 lua_setfield (L , -2 , "display" );
379459
380460 lua_pop (L , 1 );
461+
462+ // Assign the initial colors
463+ for (uint8_t i = 0 ; i < 16 ; i ++ )
464+ {
465+ assign_color_to_palette (i ,
466+ colors [i ].initial_y ,
467+ colors [i ].initial_cb ,
468+ colors [i ].initial_cr );
469+ }
381470}
0 commit comments