From e629ee8f400be3185dfeb7652986a908f379cda2 Mon Sep 17 00:00:00 2001 From: "Stuart P. Bentley" Date: Mon, 18 Jul 2022 13:21:04 -0700 Subject: [PATCH] Support sending strings with multi-byte-wide SPI The iteration in the loop in the prior version of spi.send assumes that the value of databits is the width of `char` when sending any string value. --- app/modules/spi.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/modules/spi.c b/app/modules/spi.c index 87d12ac0..10daa422 100644 --- a/app/modules/spi.c +++ b/app/modules/spi.c @@ -140,13 +140,21 @@ static int spi_send_recv( lua_State *L ) else { luaL_Buffer b; + size_t datachars = spi_databits[id] / 8; + if (spi_databits[id] % 8 != 0) { + return luaL_error( L, "attempt to send string with non-character-divisible databits" ); + } + if (datalen % datachars != 0) { + return luaL_error( L, "attempt to send string with non-databits-divisible length" ); + } + pdata = luaL_checklstring( L, argn, &datalen ); if (recv > 0) { luaL_buffinit( L, &b ); } - for( i = 0; i < datalen; i ++ ) + for( i = 0; i < datalen; i += datachars ) { if (recv > 0) {