-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
pwm.c
143 lines (123 loc) · 3.22 KB
/
pwm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Module for interfacing with PWM
#include "module.h"
#include "lauxlib.h"
#include "platform.h"
#include <stdint.h>
// Lua: realfrequency = setup( id, frequency, duty )
static int 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;
}
// Lua: close( id )
static int lpwm_close( lua_State* L )
{
unsigned id;
id = luaL_checkinteger( L, 1 );
MOD_CHECK_ID( pwm, id );
platform_pwm_close( id );
return 0;
}
// Lua: start( id )
static int lpwm_start( lua_State* L )
{
unsigned id;
id = luaL_checkinteger( L, 1 );
MOD_CHECK_ID( pwm, id );
if (!platform_pwm_start( id )) {
return luaL_error(L, "Unable to start PWM output");
}
return 0;
}
// Lua: stop( id )
static int lpwm_stop( lua_State* L )
{
unsigned id;
id = luaL_checkinteger( L, 1 );
MOD_CHECK_ID( pwm, id );
platform_pwm_stop( id );
return 0;
}
// Lua: realclock = setclock( id, clock )
static int lpwm_setclock( lua_State* L )
{
unsigned id;
s32 clk; // signed to error-check for negative values
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 )
static int lpwm_getclock( lua_State* L )
{
unsigned id;
u32 clk;
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 )
static int lpwm_setduty( lua_State* L )
{
unsigned id;
s32 duty; // signed to error-check for negative values
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 )
static int lpwm_getduty( lua_State* L )
{
unsigned id;
u32 duty;
id = luaL_checkinteger( L, 1 );
MOD_CHECK_ID( pwm, id );
duty = platform_pwm_get_duty( id );
lua_pushinteger( L, duty );
return 1;
}
int lpwm_open( lua_State *L ) {
platform_pwm_init();
return 0;
}
// Module function map
LROT_BEGIN(pwm, NULL, 0)
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);