29
29
#include "nrfx_systick.h"
30
30
#include "spi.h"
31
31
#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
+ };
32
60
33
61
static uint32_t utf8_decode (const char * string , size_t * index )
34
62
{
@@ -70,18 +98,38 @@ static uint32_t utf8_decode(const char *string, size_t *index)
70
98
return codepoint ;
71
99
}
72
100
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
+
73
111
static int lua_display_assign_color (lua_State * L )
74
112
{
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 ;
79
114
80
- if ( pallet_index < 0 || pallet_index > 15 )
115
+ for ( uint8_t i = 0 ; i <= 16 ; i ++ )
81
116
{
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
+ }
83
127
}
84
128
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
+
85
133
if (red < 0 || red > 255 )
86
134
{
87
135
luaL_error (L , "red component must be between 0 and 255" );
@@ -101,49 +149,55 @@ static int lua_display_assign_color(lua_State *L)
101
149
double cb = floor (-0.169 * red - 0.331 * green + 0.5 * blue + 128 );
102
150
double cr = floor (0.5 * red - 0.419 * green - 0.081 * blue + 128 );
103
151
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 );
110
156
111
157
return 0 ;
112
158
}
113
159
114
160
static int lua_display_assign_color_ycbcr (lua_State * L )
115
161
{
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 ;
120
163
121
- if ( pallet_index < 0 || pallet_index > 15 )
164
+ for ( uint8_t i = 0 ; i <= 16 ; i ++ )
122
165
{
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
+ }
124
176
}
125
177
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 )
127
183
{
128
- luaL_error (L , "Y component must be between 0 and 255 " );
184
+ luaL_error (L , "Y component must be between 0 and 15 " );
129
185
}
130
186
131
- if (cb < 0 || cb > 255 )
187
+ if (cb < 0 || cb > 7 )
132
188
{
133
- luaL_error (L , "Cb component must be between 0 and 255 " );
189
+ luaL_error (L , "Cb component must be between 0 and 7 " );
134
190
}
135
191
136
- if (cr < 0 || cr > 255 )
192
+ if (cr < 0 || cr > 7 )
137
193
{
138
- luaL_error (L , "Cr component must be between 0 and 255 " );
194
+ luaL_error (L , "Cr component must be between 0 and 7 " );
139
195
}
140
196
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 );
147
201
148
202
return 0 ;
149
203
}
@@ -228,14 +282,40 @@ static int lua_display_bitmap(lua_State *L)
228
282
229
283
static int lua_display_text (lua_State * L )
230
284
{
231
- // TODO color options
232
- // TODO justification options
233
- // TODO character spacing
234
-
235
285
const char * string = luaL_checkstring (L , 1 );
236
286
lua_Integer x_position = luaL_checkinteger (L , 2 );
237
287
lua_Integer y_position = luaL_checkinteger (L , 3 );
288
+ lua_Integer color_palette_offset = 0 ;
238
289
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
+ }
239
319
240
320
for (size_t index = 0 ; index < strlen (string );)
241
321
{
@@ -276,7 +356,7 @@ static int lua_display_text(lua_State *L)
276
356
y_position ,
277
357
sprite_metadata [entry ].width ,
278
358
sprite_metadata [entry ].colors ,
279
- 0 , // TODO
359
+ color_palette_offset ,
280
360
sprite_data + data_offset ,
281
361
data_length );
282
362
@@ -378,4 +458,13 @@ void lua_open_display_library(lua_State *L)
378
458
lua_setfield (L , -2 , "display" );
379
459
380
460
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
+ }
381
470
}
0 commit comments