From 5e64def6820325dd9333cb543e8ba47538803ef3 Mon Sep 17 00:00:00 2001 From: devsaurus Date: Sun, 14 Oct 2018 12:29:30 +0200 Subject: [PATCH] add dac module --- components/modules/Kconfig | 6 ++++ components/modules/dac.c | 67 ++++++++++++++++++++++++++++++++++++ docs/en/modules/dac.md | 69 ++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + 4 files changed, 143 insertions(+) create mode 100644 components/modules/dac.c create mode 100644 docs/en/modules/dac.md diff --git a/components/modules/Kconfig b/components/modules/Kconfig index a952cac3..ffc9fa7e 100644 --- a/components/modules/Kconfig +++ b/components/modules/Kconfig @@ -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" diff --git a/components/modules/dac.c b/components/modules/dac.c new file mode 100644 index 00000000..f39c97e9 --- /dev/null +++ b/components/modules/dac.c @@ -0,0 +1,67 @@ +// Module for interfacing with dac hardware + +#include + +#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); diff --git a/docs/en/modules/dac.md b/docs/en/modules/dac.md new file mode 100644 index 00000000..c4122c0e --- /dev/null +++ b/docs/en/modules/dac.md @@ -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. diff --git a/mkdocs.yml b/mkdocs.yml index 7d3c12ba..af0bf086 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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'