Report Bugs Here

Moderator: Mmiscool

User avatar
By Piotr41
#49702 Thanks for example, it solved the problem.
One question:
I miss the one function that is in LUA ESP:
gpio.serout()
http://nodemcu.readthedocs.io/en/master/en/modules/gpio/#gpioserout
and sample in C
https://github.com/nodemcu/nodemcu-firmware/blob/master/app/modules/gpio.c

Code: Select all#define DELAY_TABLE_MAX_LEN 256
#define delayMicroseconds os_delay_us
// Lua: serout( pin, firstLevel, delay_table, [repeatNum] )
// -- serout( pin, firstLevel, delay_table, [repeatNum] )
// gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
// gpio.serout(1,1,{30,30,60,60,30,30})  -- serial one byte, b10110010
// gpio.serout(1,1,{30,70},8)  -- serial 30% pwm 10k, lasts 8 cycles
// gpio.serout(1,1,{3,7},8)  -- serial 30% pwm 100k, lasts 8 cycles
// gpio.serout(1,1,{0,0},8)  -- serial 50% pwm as fast as possible, lasts 8 cycles
// gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
// gpio.serout(1,0,{20,10,10,20,10,10,10,100}) -- sim uart one byte 0x5A at about 100kbps
// gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles
static int lgpio_serout( lua_State* L )
{
  unsigned clocks_per_us = system_get_cpu_freq();
  unsigned pin = luaL_checkinteger( L, 1 );
  unsigned level = luaL_checkinteger( L, 2 );
  unsigned repeats = luaL_optint( L, 4, 1 );
  unsigned table_len, i, j;

  luaL_argcheck(L, platform_gpio_exists(pin), 1, "Invalid pin");
  luaL_argcheck(L, level==HIGH || level==LOW, 2, "Wrong arg type" );
  luaL_argcheck(L, lua_istable( L, 3 ) &&
                   ((table_len = lua_objlen( L, 3 )<DELAY_TABLE_MAX_LEN)), 3, "Invalid table" );
  luaL_argcheck(L, repeats<256, 4, "repeats >= 256" );

  uint32 *delay_table = luaM_newvector(L, table_len*repeats, uint32);
  for( i = 1; i <= table_len; i++ )  {
    lua_rawgeti( L, 3, i + 1 );
    unsigned delay = (unsigned) luaL_checkinteger( L, -1 );
    if (delay > 1000000) return luaL_error( L, "delay %u must be < 1,000,000 us", i );
    delay_table[i-1] = delay;
    lua_pop( L, 1 );
  }

  for( i = 0; i <= repeats; i++ )  {
    if (!i)    // skip the first loop (presumably this is some form of icache priming??).
      continue;

    for( j = 0;j < table_len; j++ ){
      /* Direct Write is a ROM function which already disables interrupts for the atomic bit */
      GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), level);
      delayMicroseconds(delay_table[j]);
      level = level==LOW ? HIGH : LOW;
    }
  }

  luaM_freearray(L, delay_table, table_len, uint32);
  return 0;
}


Can you add this function to reference?
It makes our life easier;-)
User avatar
By RichardS
#49714 Yep any interpreted language suffers from speed issues, everything is done JIT! (just in time)

RichardS