2014-12-22 12:35:05 +01:00
|
|
|
// Module for interfacing with PWM
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
// Lua: realfrequency = setup( id, frequency, duty )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int lpwm_setup( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
s32 freq; // signed, to error check for negative values
|
|
|
|
unsigned duty;
|
|
|
|
unsigned id;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
if(id==0)
|
|
|
|
return luaL_error( L, "no pwm for D0" );
|
|
|
|
MOD_CHECK_ID( pwm, id );
|
|
|
|
freq = luaL_checkinteger( L, 2 );
|
|
|
|
if ( freq <= 0 )
|
|
|
|
return luaL_error( L, "wrong arg range" );
|
|
|
|
duty = luaL_checkinteger( L, 3 );
|
|
|
|
if ( duty > NORMAL_PWM_DEPTH )
|
|
|
|
// Negative values will turn out > 100, so will also fail.
|
|
|
|
return luaL_error( L, "wrong arg range" );
|
|
|
|
freq = platform_pwm_setup( id, (u32)freq, duty );
|
|
|
|
if(freq==0)
|
|
|
|
return luaL_error( L, "too many pwms." );
|
|
|
|
lua_pushinteger( L, freq );
|
2019-02-17 19:26:29 +01:00
|
|
|
return 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: close( id )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int lpwm_close( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
unsigned id;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( pwm, id );
|
|
|
|
platform_pwm_close( id );
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: start( id )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int lpwm_start( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
unsigned id;
|
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( pwm, id );
|
2016-02-18 03:07:33 +01:00
|
|
|
if (!platform_pwm_start( id )) {
|
|
|
|
return luaL_error(L, "Unable to start PWM output");
|
|
|
|
}
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: stop( id )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int lpwm_stop( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
unsigned id;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( pwm, id );
|
|
|
|
platform_pwm_stop( id );
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: realclock = setclock( id, clock )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int lpwm_setclock( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
unsigned id;
|
|
|
|
s32 clk; // signed to error-check for negative values
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( pwm, id );
|
|
|
|
clk = luaL_checkinteger( L, 2 );
|
|
|
|
if ( clk <= 0 )
|
|
|
|
return luaL_error( L, "wrong arg range" );
|
|
|
|
clk = platform_pwm_set_clock( id, (u32)clk );
|
|
|
|
lua_pushinteger( L, clk );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: clock = getclock( id )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int lpwm_getclock( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
unsigned id;
|
|
|
|
u32 clk;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( pwm, id );
|
|
|
|
clk = platform_pwm_get_clock( id );
|
|
|
|
lua_pushinteger( L, clk );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: realduty = setduty( id, duty )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int lpwm_setduty( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
unsigned id;
|
|
|
|
s32 duty; // signed to error-check for negative values
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( pwm, id );
|
|
|
|
duty = luaL_checkinteger( L, 2 );
|
|
|
|
if ( duty > NORMAL_PWM_DEPTH )
|
|
|
|
return luaL_error( L, "wrong arg range" );
|
|
|
|
duty = platform_pwm_set_duty( id, (u32)duty );
|
|
|
|
lua_pushinteger( L, duty );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: duty = getduty( id )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int lpwm_getduty( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
unsigned id;
|
|
|
|
u32 duty;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( pwm, id );
|
|
|
|
duty = platform_pwm_get_duty( id );
|
|
|
|
lua_pushinteger( L, duty );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-02 03:55:21 +01:00
|
|
|
int lpwm_open( lua_State *L ) {
|
|
|
|
platform_pwm_init();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Module function map
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_BEGIN(pwm)
|
|
|
|
LROT_FUNCENTRY( setup, lpwm_setup )
|
|
|
|
LROT_FUNCENTRY( close, lpwm_close )
|
|
|
|
LROT_FUNCENTRY( start, lpwm_start )
|
|
|
|
LROT_FUNCENTRY( stop, lpwm_stop )
|
|
|
|
LROT_FUNCENTRY( setclock, lpwm_setclock )
|
|
|
|
LROT_FUNCENTRY( getclock, lpwm_getclock )
|
|
|
|
LROT_FUNCENTRY( setduty, lpwm_setduty )
|
|
|
|
LROT_FUNCENTRY( getduty, lpwm_getduty )
|
|
|
|
LROT_END( pwm, NULL, 0 )
|
|
|
|
|
|
|
|
|
|
|
|
NODEMCU_MODULE(PWM, "pwm", pwm, lpwm_open);
|