Tasmota/tasmota/xdrv_52_3_berry_gpio.ino

209 lines
6.6 KiB
Arduino
Raw Normal View History

2021-04-03 17:06:40 +01:00
/*
xdrv_52_3_berry_native.ino - Berry scripting language, native fucnctions
Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_BERRY
#include <berry.h>
2021-09-13 13:12:41 +01:00
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2)
#include <driver/dac.h>
2021-09-13 13:12:41 +01:00
#endif
2021-04-03 17:06:40 +01:00
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
* import gpio
*
*
\*********************************************************************************************/
extern "C" {
2021-11-05 18:25:50 +00:00
#include "berry/include/be_gpio_defines.h"
// virtual member
int gp_member(bvm *vm);
int gp_member(bvm *vm) {
2022-01-20 21:02:21 +00:00
if (be_const_module_member(vm, lv_gpio_constants, lv_gpio_constants_size)) {
2021-11-05 18:25:50 +00:00
be_return(vm);
} else {
be_return_nil(vm);
}
}
2021-04-03 17:06:40 +01:00
int gp_pin_mode(bvm *vm);
int gp_pin_mode(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc == 2 && be_isint(vm, 1) && be_isint(vm, 2)) {
int32_t pin = be_toint(vm, 1);
int32_t mode = be_toint(vm, 2);
2021-04-04 08:25:42 +01:00
if (pin >= 0) {
if (mode > 0) {
// standard ESP mode
pinMode(pin, mode);
} else {
// synthetic mode
if (-1 == mode) {
// DAC
#if defined(CONFIG_IDF_TARGET_ESP32)
if (25 == pin || 26 == pin) {
2022-01-11 17:40:47 +00:00
dac_channel_t channel = (25 == pin) ? DAC_CHANNEL_1 : DAC_CHANNEL_2;
esp_err_t err = dac_output_enable(channel);
if (err) {
be_raisef(vm, "value_error", "Error: dac_output_enable(%i) -> %i", channel, err);
}
} else {
be_raise(vm, "value_error", "DAC only supported on GPIO25-26");
}
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
if (17 == pin || 18 == pin) {
2022-01-11 17:40:47 +00:00
dac_channel_t channel = (17 == pin) ? DAC_CHANNEL_1 : DAC_CHANNEL_2;
esp_err_t err = dac_output_enable(channel);
if (err) {
be_raisef(vm, "value_error", "Error: dac_output_enable(%i) -> %i", channel, err);
}
} else {
be_raise(vm, "value_error", "DAC only supported on GPIO17-18");
}
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
be_raise(vm, "value_error", "DAC unsupported in this chip");
#else
be_raise(vm, "value_error", "DAC unsupported in this chip");
#endif
}
}
2021-04-04 08:25:42 +01:00
}
2021-04-03 17:06:40 +01:00
be_return_nil(vm);
}
be_raise(vm, kTypeError, nullptr);
}
int gp_digital_write(bvm *vm);
int gp_digital_write(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc == 2 && be_isint(vm, 1) && be_isint(vm, 2)) {
int32_t pin = be_toint(vm, 1);
int32_t val = be_toint(vm, 2);
2021-04-04 08:25:42 +01:00
if (pin >= 0) {
digitalWrite(pin, val);
}
2021-04-03 17:06:40 +01:00
be_return_nil(vm);
}
be_raise(vm, kTypeError, nullptr);
}
int gp_digital_read(bvm *vm);
int gp_digital_read(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc == 1 && be_isint(vm, 1)) {
int32_t pin = be_toint(vm, 1);
2021-04-04 08:25:42 +01:00
if (pin >= 0) {
int32_t ret = digitalRead(pin);
be_pushint(vm, ret);
} else {
be_pushnil(vm);
}
2021-04-03 17:06:40 +01:00
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
int gp_dac_voltage(bvm *vm);
int gp_dac_voltage(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc == 2 && be_isint(vm, 1) && be_isint(vm, 2)) {
int32_t pin = be_toint(vm, 1);
int32_t mV = be_toint(vm, 2);
if (mV < 0) { mV = 0; }
uint32_t dac_value = changeUIntScale(mV, 0, 3300, 0, 255); // convert from 0..3300 ms to 0..255
#if defined(CONFIG_IDF_TARGET_ESP32)
if (25 == pin || 26 == pin) {
2022-01-11 17:40:47 +00:00
dac_channel_t channel = (25 == pin) ? DAC_CHANNEL_1 : DAC_CHANNEL_2;
esp_err_t err = dac_output_voltage(channel, dac_value);
if (err) {
be_raisef(vm, "internal_error", "Error: esp_err_tdac_output_voltage(%i, %i) -> %i", channel, dac_value, err);
}
} else {
be_raise(vm, "value_error", "DAC only supported on GPIO25-26");
}
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
if (17 == pin || 18 == pin) {
2022-01-11 17:40:47 +00:00
dac_channel_t channel = (17 == pin) ? DAC_CHANNEL_1 : DAC_CHANNEL_2;
esp_err_t err = dac_output_voltage(channel, dac_value);
if (err) {
be_raisef(vm, "internal_error", "Error: esp_err_tdac_output_voltage(%i, %i) -> %i", channel, dac_value, err);
}
} else {
be_raise(vm, "value_error", "DAC only supported on GPIO17-18");
}
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
be_raise(vm, "value_error", "DAC unsupported in this chip");
#else
be_raise(vm, "value_error", "DAC unsupported in this chip");
#endif
be_pushint(vm, changeUIntScale(dac_value, 0, 255, 0, 3300));
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
2021-04-03 17:06:40 +01:00
// Tasmota specific
int gp_pin_used(bvm *vm);
int gp_pin_used(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 1 && argc <= 2 && be_isint(vm, 1)) {
int32_t pin = be_toint(vm, 1);
int32_t index = 0;
if (argc == 2 && be_isint(vm, 2)) {
index = be_toint(vm, 2);
}
2021-11-09 22:11:44 +00:00
bool ret;
if (pin == GPIO_OPTION_A) {
ret = bitRead(TasmotaGlobal.gpio_optiona.data, index);
} else {
ret = PinUsed(pin, index);
}
2021-04-03 17:06:40 +01:00
be_pushbool(vm, ret);
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
int gp_pin(bvm *vm);
int gp_pin(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 1 && argc <= 2 && be_isint(vm, 1)) {
int32_t pin = be_toint(vm, 1);
int32_t index = 0;
if (argc == 2 && be_isint(vm, 2)) {
index = be_toint(vm, 2);
}
int32_t ret = Pin(pin, index);
be_pushint(vm, ret);
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
}
#endif // USE_BERRY