Skip to content

Commit a00391e

Browse files
committed
Added camera_read_raw function to Lua API
1 parent 03cb5e3 commit a00391e

File tree

2 files changed

+708
-4
lines changed

2 files changed

+708
-4
lines changed

source/application/lua_libraries/camera.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,79 @@ static int lua_camera_read(lua_State *L)
231231
return 1;
232232
}
233233

234+
static int lua_camera_read_raw(lua_State *L)
235+
{
236+
lua_Integer bytes_requested = luaL_checkinteger(L, 1);
237+
if (bytes_requested <= 0)
238+
{
239+
luaL_error(L, "bytes must be greater than 0");
240+
}
241+
242+
size_t bytes_remaining = bytes_requested;
243+
244+
uint8_t *payload = malloc(bytes_requested);
245+
if (payload == NULL)
246+
{
247+
luaL_error(L, "bytes requested is too large");
248+
}
249+
250+
uint16_t image_bytes_available = get_bytes_available();
251+
252+
// Append image data
253+
if (image_bytes_available > 0)
254+
{
255+
if (bytes_remaining > 0)
256+
{
257+
258+
// append image data
259+
size_t length = bytes_remaining < image_bytes_available
260+
? bytes_remaining
261+
: image_bytes_available;
262+
263+
spi_read(FPGA,
264+
0x22,
265+
payload + bytes_requested - bytes_remaining,
266+
length);
267+
268+
bytes_remaining -= length;
269+
}
270+
}
271+
272+
else
273+
{
274+
// append footer 0xFF
275+
if (bytes_remaining > 0 && jpeg_footer_bytes_sent_out == 0)
276+
{
277+
payload[bytes_requested - bytes_remaining] = 0xFF;
278+
jpeg_footer_bytes_sent_out++;
279+
bytes_remaining--;
280+
}
281+
282+
// append footer 0xD9
283+
if (bytes_remaining > 0 && jpeg_footer_bytes_sent_out == 1)
284+
{
285+
payload[bytes_requested - bytes_remaining] = 0xD9;
286+
jpeg_footer_bytes_sent_out++;
287+
bytes_remaining--;
288+
}
289+
}
290+
291+
// Return nill if nothing was written to payload
292+
if (bytes_remaining == bytes_requested)
293+
{
294+
lua_pushnil(L);
295+
}
296+
297+
// Otherwise return payload
298+
else
299+
{
300+
lua_pushlstring(L, (char *)payload, bytes_requested - bytes_remaining);
301+
}
302+
303+
free(payload);
304+
return 1;
305+
}
306+
234307
static int lua_camera_auto(lua_State *L)
235308
{
236309
if (nrf_gpio_pin_out_read(CAMERA_SLEEP_PIN) == false)
@@ -625,6 +698,9 @@ void lua_open_camera_library(lua_State *L)
625698
lua_pushcfunction(L, lua_camera_read);
626699
lua_setfield(L, -2, "read");
627700

701+
lua_pushcfunction(L, lua_camera_read_raw);
702+
lua_setfield(L, -2, "read_raw");
703+
628704
lua_pushcfunction(L, lua_camera_auto);
629705
lua_setfield(L, -2, "auto");
630706

0 commit comments

Comments
 (0)