// Lua: realfrequency = setup( idR,idG,idB,frequency) static int lpwmled_setup( lua_State* L ) { s32 freq; // signed, to error check for negative values int err; if((err=checkid(L,&(led.pwmidr),1))!=1) return err; if((err=checkid(L,&(led.pwmidg),2))!=1) return err; if((err=checkid(L,&(led.pwmidb),3))!=1) return err; freq = luaL_checkinteger( L, 4 ); if ( freq <= 0 ) return luaL_error( L, "wrong arg range" ); led.frequency=freq; if ( led.frequency > 0 ) led.frequency = platform_pwm_setup( led.pwmidr,led.frequency, 0 ); if ( led.frequency > 0 ) led.frequency = platform_pwm_setup( led.pwmidg,led.frequency, 0 ); if ( led.frequency > 0 ) led.frequency = platform_pwm_setup( led.pwmidb,led.frequency, 0 ); if(led.frequency==0) return luaL_error( L, "too many pwms." ); lua_pushinteger( L, led.frequency ); platform_pwm_start( led.pwmidr ); platform_pwm_start( led.pwmidg ); platform_pwm_start( led.pwmidb ); led.maxduty_r=1023; led.maxduty_g=1023; led.maxduty_b=1023; return 1; }
/* picoc: realfrequency = setup(id, frequency, duty) */ static void pwm_setup(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { u32 freq; unsigned duty, id; id = Param[0]->Val->UnsignedInteger; MOD_CHECK_ID(pwm, id); freq = Param[1]->Val->UnsignedInteger; duty = Param[2]->Val->UnsignedInteger; if (duty > 100) duty = 100; freq = platform_pwm_setup(id, freq, duty); ReturnValue->Val->Integer = freq; }
// Lua: realfrequency = setup( id, frequency, duty ) static int pwm_setup( lua_State *L ) { s32 freq; // signed, to error check for negative values unsigned duty; unsigned id; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( pwm, id ); freq = luaL_checkinteger( L, 2 ); if ( freq <= 0 ) return luaL_error( L, "frequency must be > 0" ); duty = luaL_checkinteger( L, 3 ); if ( duty > 100 ) // Negative values will turn out > 100, so will also fail. return luaL_error( L, "duty cycle must be from 0 to 100" ); freq = platform_pwm_setup( id, (u32)freq, duty ); lua_pushinteger( L, freq ); return 1; }
// Lua: realfrequency = setup( id, frequency, duty ) static int ICACHE_FLASH_ATTR lpwm_setup( lua_State* L ) { s32 freq; // signed, to error check for negative values unsigned duty; unsigned id; 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 ); return 1; }