Skip to content

Commit

Permalink
Merge pull request #15 from whitecatboard/i2c
Browse files Browse the repository at this point in the history
Merge i2c into master
  • Loading branch information
jolivepetrus committed Jan 11, 2017
2 parents 1adaf6f + bb07887 commit bb446ad
Show file tree
Hide file tree
Showing 35 changed files with 935 additions and 783 deletions.
21 changes: 14 additions & 7 deletions Makefile
Expand Up @@ -5,16 +5,23 @@

PROJECT_NAME := lua_rtos

all_binaries: restore-idf configure-idf-lua-rtos
all_binaries: configure-idf-lua-rtos configure-idf-lua-rtos-tests

clean: restore-idf

configure-idf-lua-rtos:
@echo "Configure esp-idf for Lua RTOS ..."
configure-idf-lua-rtos-tests:
@echo "Configure esp-idf for Lua RTOS tests ..."
@touch $(PROJECT_PATH)/components/lua_rtos/sys/sys_init.c
ifeq ("$(wildcard $(IDF_PATH)/components/lua_rtos)","")
@ln -s $(PROJECT_PATH)/main/test/lua_rtos $(IDF_PATH)/components/lua_rtos
endif
@touch $(PROJECT_PATH)/components/lua_rtos/sys/sys_init.c

configure-idf-lua-rtos:
@echo "Configure esp-idf ..."
@cd $(IDF_PATH)/components/esp32/ld && git checkout esp32.common.ld
@cd $(IDF_PATH)/components/lwip/api && git checkout api_msg.c
@cd $(IDF_PATH)/components/vfs/include/sys && git checkout dirent.h
@echo "Configure esp-idf for Lua RTOS ..."
@touch $(PROJECT_PATH)/components/lua_rtos/lwip/socket.c
@cd $(IDF_PATH)/components/esp32/ld && git checkout esp32.common.ld
@cd $(IDF_PATH)/components/lwip/api && git checkout api_msg.c
Expand All @@ -24,11 +31,11 @@ endif

restore-idf:
@echo "Restoring esp-idf ..."
ifeq ("$(wildcard $(IDF_PATH)/components/lua_rtos)","$(IDF_PATH)/components/lua_rtos")
@rm $(IDF_PATH)/components/lua_rtos
endif
@cd $(IDF_PATH)/components/esp32/ld && git checkout esp32.common.ld
@cd $(IDF_PATH)/components/lwip/api && git checkout api_msg.c
@cd $(IDF_PATH)/components/vfs/include/sys && git checkout dirent.h
#ifeq ("$(wildcard $(IDF_PATH)/components/lua_rtos)","$(IDF_PATH)/components/lua_rtos")
# @rm $(IDF_PATH)/components/lua_rtos
#endif

include $(IDF_PATH)/make/project.mk
2 changes: 2 additions & 0 deletions components/lua_rtos/Lua/adds/lrotable.h
Expand Up @@ -90,5 +90,7 @@ int luaH_getn_ro (void *t);
void luaR_next(lua_State *L, void *data, TValue *key, TValue *val);
int luaH_next_ro (lua_State *L, void *t, StkId key);

int luaR_index(lua_State *L, const void *funcs, const void *consts);

#endif

2 changes: 1 addition & 1 deletion components/lua_rtos/Lua/adds/luaconf.h
Expand Up @@ -165,7 +165,7 @@
#endif

#if LUA_USE_I2C
#define LINIT_REG_I2C {AUXLIB_I2C, luaopen_i2c},
// #define LINIT_REG_I2C {AUXLIB_I2C, luaopen_i2c},
#else
#define LINIT_REG_I2C
#endif
Expand Down
11 changes: 1 addition & 10 deletions components/lua_rtos/Lua/adds/modules.h
Expand Up @@ -15,7 +15,7 @@
#define LIB_TOSTRING_EVALUATOR(x) LIB_TOSTRING_PASTER(x)
#define LIB_TOSTRING(x) LIB_TOSTRING_EVALUATOR(x)

// Macros for register a library init function
// Macros for register a library
#define LIB_USED(fname) LIB_CONCAT(LUA_USE_,fname)
#define LIB_SECTION(fname, section) LIB_CONCAT(section,LIB_USED(fname))

Expand All @@ -29,13 +29,4 @@ const PUT_IN_SECTION(LIB_TOSTRING(LIB_SECTION(fname,.lua_rotable))) luaR_entry L
#define LUA_OS_MODULE(fname, lname, map)
#endif

/*
#define NODEMCU_MODULE(cfgname, luaname, map, initfunc) \
const LOCK_IN_SECTION(".lua_libs") \
luaL_Reg MODULE_PASTE_(lua_lib_,cfgname) = { luaname, initfunc }; \
const LOCK_IN_SECTION(".lua_rotable") \
luaR_table MODULE_EXPAND_PASTE_(cfgname,MODULE_EXPAND_PASTE_(_module_selected,MODULE_PASTE_(LUA_USE_MODULES_,cfgname))) \
= { luaname, map }
*/

#endif
22 changes: 22 additions & 0 deletions components/lua_rtos/Lua/common/lrotable.c
Expand Up @@ -201,4 +201,26 @@ int luaH_next_ro (lua_State *L, void *t, StkId key) {
return ttisnil(key) ? 0 : 1;
}

int luaR_index(lua_State *L, const void *funcs, const void *consts) {
int res;

if ((res = luaR_findfunction(L, funcs)) != 0)
return res;

if (consts) {
const char *key = luaL_checkstring(L, 2);
const TValue *val = luaR_findentry(consts, key, 0, NULL);
if (val != luaO_nilobject) {
if (ttnov(val) == LUA_TROTABLE) {
lua_pushrotable( L, val->value_.p);
} else {
lua_pushinteger(L, val->value_.i);
}
return 1;
}
}

return (int)luaO_nilobject;
}

#endif
116 changes: 35 additions & 81 deletions components/lua_rtos/Lua/modules/adc.c
Expand Up @@ -37,6 +37,7 @@
#include "auxmods.h"
#include "error.h"
#include "adc.h"
#include "modules.h"

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -124,18 +125,24 @@ static int ladc_read( lua_State* L ) {
}
}

#include "modules.h"
static int ladc_index(lua_State *L);
static int ladc_chan_index(lua_State *L);

static const LUA_REG_TYPE ladc_error_map[] = {
};

static const LUA_REG_TYPE adc_method_map[] = {
{ LSTRKEY( "read" ), LFUNCVAL( ladc_read ) },
{ LSTRKEY( "setup" ), LFUNCVAL( ladc_setup ) },
{ LSTRKEY( "setupchan" ), LFUNCVAL( ladc_setup_channel ) },
{ LNILKEY, LNILVAL }
static const LUA_REG_TYPE ladc_map[] = {
{ LSTRKEY( "setup" ), LFUNCVAL( ladc_setup ) },
{ LNILKEY, LNILVAL }
};

#if LUA_USE_ROTABLE
static const LUA_REG_TYPE ladc_chan_map[] = {
{ LSTRKEY( "read" ), LFUNCVAL( ladc_read ) },
{ LSTRKEY( "setupchan" ), LFUNCVAL( ladc_setup_channel ) },
{ LNILKEY, LNILVAL }
};

static const LUA_REG_TYPE adc_constants_map[] = {
static const LUA_REG_TYPE ladc_constants_map[] = {
ADC_ADC0
ADC_ADC1
ADC_ADC_CH0
Expand All @@ -146,95 +153,42 @@ static const LUA_REG_TYPE adc_constants_map[] = {
ADC_ADC_CH5
ADC_ADC_CH6
ADC_ADC_CH7
{ LNILKEY, LNILVAL }
};

static int luaL_adc_index(lua_State *L) {
int res;

if ((res = luaR_findfunction(L, adc_method_map)) != 0)
return res;
// Error definitions
{LSTRKEY("error"), LROVAL( ladc_error_map )},

const char *key = luaL_checkstring(L, 2);
const TValue *val = luaR_findentry(adc_constants_map, key, 0, NULL);
if (val != luaO_nilobject) {
lua_pushinteger(L, val->value_.i);
return 1;
}

return (int)luaO_nilobject;
}
{ LNILKEY, LNILVAL }
};

static const luaL_Reg adc_load_funcs[] = {
{ "__index" , luaL_adc_index },
static const luaL_Reg ladc_func[] = {
{ "__index", ladc_index },
{ NULL, NULL }
};

static int luaL_madc_index(lua_State *L) {
int fres;
if ((fres = luaR_findfunction(L, adc_method_map)) != 0)
return fres;

return (int)luaO_nilobject;
}

static const luaL_Reg madc_load_funcs[] = {
{ "__index" , luaL_madc_index },
static const luaL_Reg ladc_chan_func[] = {
{ "__index", ladc_chan_index },
{ NULL, NULL }
};

#else

static const luaL_Reg adc_map[] = {
{ NULL, NULL }
};
static int ladc_index(lua_State *L) {
return luaR_index(L, ladc_map, ladc_constants_map);
}

#endif
static int ladc_chan_index(lua_State *L) {
return luaR_index(L, ladc_chan_map, NULL);
}

LUALIB_API int luaopen_adc( lua_State *L ) {
#if !LUA_USE_ROTABLE
int i;
char buff[5];

luaL_register( L, AUXLIB_ADC, adc_map );

// Set it as its own metatable
lua_pushvalue( L, -1 );
lua_setmetatable( L, -2 );

// create metatable for adc module
luaL_newmetatable(L, "adc");

// Module constants
for(i=CPU_FIRST_ADC;i<=CPU_FIRST_ADC;i++) {
sprintf(buff,"ADC%d",i);
MOD_REG_INTEGER( L, buff, i );
}

for(i=CPU_FIRST_ADC_CH;i<=CPU_FIRST_ADC_CH;i++) {
sprintf(buff,"ADC_CH%d",i);
MOD_REG_INTEGER( L, buff, i );
}

// metatable.__index = metatable
lua_pushliteral(L, "__index");
lua_pushvalue(L,-2);
lua_rawset(L,-3);

// Setup the methods inside metatable
luaL_register( L, NULL, adc_method_map );
#else
luaL_newlib(L, adc_load_funcs); /* new module */
luaL_newlib(L, ladc_func);
lua_pushvalue(L, -1);
lua_setmetatable(L, -2);

luaL_newmetatable(L, "adc"); /* create metatable */
lua_pushvalue(L, -1); /* push metatable */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
luaL_newmetatable(L, "adc");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");

luaL_setfuncs(L, madc_load_funcs, 0); /* add file methods to new metatable */
lua_pop(L, 1); /* pop new metatable */
#endif
luaL_setfuncs(L, ladc_chan_func, 0);
lua_pop(L, 1);

return 1;
}
Expand Down
1 change: 1 addition & 0 deletions components/lua_rtos/Lua/modules/error.h
Expand Up @@ -34,6 +34,7 @@

#include <sys/driver.h>

#define LUA_I2C_ID I2C_DRIVER_ID
#define LUA_MQTT_ID 30

#define LUA_EXCEPTION_BASE(n) (n << 24)
Expand Down

0 comments on commit bb446ad

Please sign in to comment.