As the title says... Chat on...

User avatar
By Patriko
#11966 Ok, if it'll be useful for somebody, I implemented in C in nodemcu firmware, here is the code:

Code: Select all#include "lualib.h"
#include "lauxlib.h"
#include "platform.h"
#include "auxmods.h"
#include "lrotable.h"
#define Baudrate        2400
#define OneBitDelay     (1000000/Baudrate)
#define DataBitCount    8

static int ICACHE_FLASH_ATTR rgbw_write(lua_State* L) {
  const uint8_t pin = luaL_checkinteger(L, 1);
  size_t length;
  const char *buffer = luaL_checklstring(L, 2, &length);
  char c = *buffer;


  platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);

  platform_gpio_write(pin, 0);
  os_intr_lock();

  os_delay_us(OneBitDelay);
  uint8_t mask = 0;

  unsigned char i = 0;
  for(i = 0; i < DataBitCount; i++)
  {
     if((c>>i)&0x01)
     {
        platform_gpio_write(pin, 1);
     }
     else
     {
        platform_gpio_write(pin, 0);
     }
       os_delay_us(OneBitDelay);
  }
  //stop bit
  platform_gpio_write(pin, 1);
  os_delay_us(OneBitDelay);
  os_intr_unlock();

  return 0;
}

#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE rgbw_map[] =
{
  { LSTRKEY( "write" ), LFUNCVAL( rgbw_write )},
  { LNILKEY, LNILVAL}
};

LUALIB_API int luaopen_rgbw(lua_State *L) {
  LREGISTER(L, "rgbw", rgbw_map);
  return 1;
}

// ----------------------------------------------------------------------------

User avatar
By alon24
#11968 So just for me to learn
If I wanted to use this code? is it in NodeMCU?
Should I compile something for it?

How do I use c code, in nodemcu, can this code be compiled, and then added externally? so I can have the current NodeMcu and add to it?
Again, this is for knowing and understanding