Преглед изворни кода

ensure backwards compatibility of exising api functions

devsaurus пре 8 година
родитељ
комит
26f4bc7957

+ 67 - 27
app/modules/spi.c

@@ -7,14 +7,17 @@
 #include "auxmods.h"
 #include "lrotable.h"
 
-// Lua: = spi.setup( id, mode, cpol, cpha, clock_div )
+static u8 spi_databits[NUM_SPI] = {0, 0};
+
+// Lua: = spi.setup( id, mode, cpol, cpha, databits, clock_div )
 static int spi_setup( lua_State *L )
 {
   int id        = luaL_checkinteger( L, 1 );
   int mode      = luaL_checkinteger( L, 2 );
   int cpol      = luaL_checkinteger( L, 3 );
   int cpha      = luaL_checkinteger( L, 4 );
-  u32 clock_div = luaL_checkinteger( L, 5 );
+  int databits  = luaL_checkinteger( L, 5 );
+  u32 clock_div = luaL_checkinteger( L, 6 );
 
   MOD_CHECK_ID( spi, id );
 
@@ -30,66 +33,103 @@ static int spi_setup( lua_State *L )
     return luaL_error( L, "wrong arg type" );
   }
 
+  if (databits < 0 || databits > 32) {
+    return luaL_error( L, "out of range" );
+  }
+
   if (clock_div < 4) {
     return luaL_error( L, "invalid clock divider" );
   }
 
+  spi_databits[id] = databits;
+
   u32 res = platform_spi_setup(id, mode, cpol, cpha, clock_div);
   lua_pushinteger( L, res );
   return 1;
 }
 
-// Lua: wrote = spi.send( id, bitlen, data1, [data2], ..., [datan] )
+// Lua: wrote = spi.send( id, data1, [data2], ..., [datan] )
 // data can be either a string, a table or an 8-bit number
 static int spi_send( lua_State *L )
 {
-  int id     = luaL_checkinteger( L, 1 );
-  int bitlen = luaL_checkinteger( L, 2 );
-  size_t i;
-  u32 numdata;
+  unsigned id = luaL_checkinteger( L, 1 );
+  const char *pdata;
+  size_t datalen, i;
+  int numdata;
   u32 wrote = 0;
-  int argn;
+  unsigned argn;
 
   MOD_CHECK_ID( spi, id );
-  if( lua_gettop( L ) < 3 ) {
-    return luaL_error( L, "too few args" );
-  }
+  if( lua_gettop( L ) < 2 )
+    return luaL_error( L, "wrong arg type" );
 
-  for( argn = 3; argn <= lua_gettop( L ); argn ++ )
+  for( argn = 2; argn <= lua_gettop( L ); argn ++ )
   {
-    numdata = ( u32 )luaL_checkinteger( L, argn );
-    if (PLATFORM_OK != platform_spi_send( id, bitlen, numdata )) {
-      return luaL_error( L, "failed" );
+    // lua_isnumber() would silently convert a string of digits to an integer
+    // whereas here strings are handled separately.
+    if( lua_type( L, argn ) == LUA_TNUMBER )
+    {
+      numdata = ( int )luaL_checkinteger( L, argn );
+      if( numdata < 0 )
+        return luaL_error( L, "wrong arg range" );
+      platform_spi_send( id, spi_databits[id], numdata );
+      wrote ++;
+    }
+    else if( lua_istable( L, argn ) )
+    {
+      datalen = lua_objlen( L, argn );
+      for( i = 0; i < datalen; i ++ )
+      {
+        lua_rawgeti( L, argn, i + 1 );
+        numdata = ( int )luaL_checkinteger( L, -1 );
+        lua_pop( L, 1 );
+        if( numdata < 0 )
+          return luaL_error( L, "wrong arg range" );
+        platform_spi_send( id, spi_databits[id], numdata );
+      }
+      wrote += i;
+      if( i < datalen )
+        break;
+    }
+    else
+    {
+      pdata = luaL_checklstring( L, argn, &datalen );
+      for( i = 0; i < datalen; i ++ )
+        platform_spi_send( id, spi_databits[id], pdata[ i ] );
+      wrote += i;
+      if( i < datalen )
+        break;
     }
-    wrote ++;
   }
 
   lua_pushinteger( L, wrote );
   return 1;
 }
 
-// Lua: read = spi.recv( id, bitlen, num )
+// Lua: read = spi.recv( id, size )
 static int spi_recv( lua_State *L )
 {
-  int id     = luaL_checkinteger( L, 1 );
-  int bitlen = luaL_checkinteger( L, 2 );
-  int num    = luaL_checkinteger( L, 3 ), i;
+  int id   = luaL_checkinteger( L, 1 );
+  int size = luaL_checkinteger( L, 2 ), i;
+
+  luaL_Buffer b;
 
   MOD_CHECK_ID( spi, id );
-  if (bitlen < 1 || bitlen > 32) {
-    return luaL_error( L, "bitlen out of range" );
+  if (size == 0) {
+    return 0;
   }
-  if (num < 0) num = 0;
 
-  for (i=0; i<num; i++)
+  luaL_buffinit( L, &b );
+  for (i=0; i<size; i++)
   {
-    if (PLATFORM_OK != platform_spi_transaction( id, 0, 0, 0, 0, 0, 0, bitlen )) {
+    if (PLATFORM_OK != platform_spi_transaction( id, 0, 0, 0, 0, 0, 0, spi_databits[id] )) {
       return luaL_error( L, "failed" );
     }
-    lua_pushinteger( L, platform_spi_get_miso( id, 0, bitlen ) );
+    luaL_addchar( &b, ( char )platform_spi_get_miso( id, 0, spi_databits[id] ) );
   }
 
-  return num;
+  luaL_pushresult( &b );
+  return 1;
 }
 
 // Lua: spi.set_mosi( id, offset, bitlen, data1, [data2], ..., [datan] )

+ 1 - 1
lua_examples/u8glib/u8g_bitmaps.lua

@@ -30,7 +30,7 @@ function init_spi_display()
      local dc  = 4 -- GPIO2
      local res = 0 -- GPIO16
 
-     spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8)
+     spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
      disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
 end
 

+ 1 - 1
lua_examples/u8glib/u8g_graphics_test.lua

@@ -29,7 +29,7 @@ function init_spi_display()
     local dc  = 4 -- GPIO2
     local res = 0 -- GPIO16
 
-    spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8)
+    spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
     disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
 end
 

+ 1 - 1
lua_examples/u8glib/u8g_rotation.lua

@@ -30,7 +30,7 @@ function init_spi_display()
      local dc  = 4 -- GPIO2
      local res = 0 -- GPIO16
 
-     spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8)
+     spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
      disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
 end
 

+ 1 - 1
lua_examples/ucglib/GraphicsTest.lua

@@ -8,7 +8,7 @@ function init_spi_display()
     local dc  = 4 -- GPIO2
     local res = 0 -- GPIO16
 
-    spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8)
+    spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
 
     -- initialize the matching driver for your display
     -- see app/include/ucg_config.h

+ 1 - 1
lua_examples/ucglib/HelloWorld.lua

@@ -8,7 +8,7 @@ function init_spi_display()
    local dc  = 4 -- GPIO2
    local res = 0 -- GPIO16
 
-   spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8)
+   spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
    disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
 end
 

+ 2 - 2
lua_examples/ucglib/UcgLogo.lua

@@ -8,8 +8,8 @@ function init_spi_display()
     local dc  = 4 -- GPIO2
     local res = 0 -- GPIO16
 
-    spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8)
-    disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
+    spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
+    disp = ucg.st7735_18x128x160_hw_spi(cs, dc, res)
 end