add dac module

This commit is contained in:
devsaurus 2018-10-14 12:29:30 +02:00
parent d094944b9b
commit 5e64def682
4 changed files with 143 additions and 0 deletions

View File

@ -74,6 +74,12 @@ config LUA_MODULE_CAN
help
Includes the can module.
config LUA_MODULE_DAC
bool "DAC module"
default "n"
help
Includes the dac module.
config LUA_MODULE_DHT
bool "DHT11/21/22/AM2301/AM2302 module"
default "n"

67
components/modules/dac.c Normal file
View File

@ -0,0 +1,67 @@
// Module for interfacing with dac hardware
#include <string.h>
#include "module.h"
#include "lauxlib.h"
#include "driver/gpio.h"
#include "driver/dac.h"
#define GET_CHN(idx) \
int chn = luaL_checkint( L, idx ); \
luaL_argcheck( L, chn >= DAC_CHANNEL_1 && chn <= DAC_CHANNEL_MAX, idx, "invalid channel" );
// Lua: enable( dac_channel )
static int ldac_enable( lua_State *L )
{
GET_CHN(1);
if (dac_output_enable( chn ) != ESP_OK)
return luaL_error( L, "dac failed" );
return 0;
}
// Lua: disable( dac_channel )
static int ldac_disable( lua_State *L )
{
GET_CHN(1);
if (dac_output_disable( chn ) != ESP_OK)
return luaL_error( L, "dac failed" );
return 0;
}
// Lua: write( dac_channel )
static int ldac_write( lua_State *L )
{
GET_CHN(1);
int data = luaL_checkint( L, 2 );
luaL_argcheck( L, data >= 0 && data <= 255, 2, "out of range" );
if (dac_output_voltage( chn, data ) != ESP_OK)
return luaL_error( L, "dac failed" );
return 0;
}
// Module function map
static const LUA_REG_TYPE dac_map[] =
{
{ LSTRKEY( "enable" ), LFUNCVAL( ldac_enable ) },
{ LSTRKEY( "disable" ), LFUNCVAL( ldac_disable ) },
{ LSTRKEY( "write" ), LFUNCVAL( ldac_write ) },
{ LSTRKEY( "CHANNEL_1" ), LNUMVAL( DAC_CHANNEL_1 ) },
{ LSTRKEY( "CHANNEL_2" ), LNUMVAL( DAC_CHANNEL_2 ) },
{ LNILKEY, LNILVAL }
};
NODEMCU_MODULE(DAC, "dac", dac_map, NULL);

69
docs/en/modules/dac.md Normal file
View File

@ -0,0 +1,69 @@
# DAC Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
| 2018-10-14 | [Arnim Läuger](https://github.com/devsaurus) | [Arnim Läuger](https://github.com/devsaurus) | [dac.c](../../../components/modules/dac.c)|
The DAC module provides access to the two built-in Digital to Analog Converters.
Each DAC is assigned to a dedicated GPIO:
- DAC channel 1 is attached to GPIO25
- DAC channel 2 is attached to GPIO26
The DACs are 8-bit, thus the output values are restricted to the range from 0 to 255.
## dac.disablee()
Disables DAC output on the related GPIO.
#### Syntax
```lua
dac.disable(channel)
```
#### Parameters
- `channel` DAC channel, one of
- `dac.CHANNEL_1`
- `dac.CHANNEL_2`
#### Returns
`nil`
An error is thrown in case of invalid parameters or if the dac failed.
## dac.enable()
Enables DAC output on the related GPIO.
#### Syntax
```lua
dac.enable(channel)
```
#### Parameters
- `channel` DAC channel, one of
- `dac.CHANNEL_1`
- `dac.CHANNEL_2`
#### Returns
`nil`
An error is thrown in case of invalid parameters or if the dac failed.
## dac.write()
Sets the output value of the DAC.
#### Syntax
```lua
dac.write(channel, value)
```
#### Parameters
- `channel` DAC channel, one of
- `dac.CHANNEL_1`
- `dac.CHANNEL_2`
- `value` output value
#### Returns
`nil`
An error is thrown in case of invalid parameters or if the dac failed.

View File

@ -35,6 +35,7 @@ pages:
- 'bit': 'en/modules/bit.md'
- 'bthci': 'en/modules/bthci.md'
- 'can': 'en/modules/can.md'
- 'dac': 'en/modules/dac.md'
- 'dht': 'en/modules/dht.md'
- 'encoder': 'en/modules/encoder.md'
- 'file': 'en/modules/file.md'