Tasmota/tasmota/xdrv_52_3_berry_display.ino

78 lines
2.7 KiB
Arduino
Raw Normal View History

/*
xdrv_52_3_berry_display.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
#ifdef USE_DISPLAY
#include <berry.h>
#ifdef USE_UNIVERSAL_DISPLAY
Renderer *Init_uDisplay(const char *desc);
#endif // USE_UNIVERSAL_DISPLAY
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
* import display
* display.start(string) -> comptr or nil if failed
*
\*********************************************************************************************/
extern "C" {
2021-12-09 18:37:00 +00:00
int be_ntv_display_start(bvm *vm) {
2022-01-29 10:11:21 +00:00
#ifdef USE_UNIVERSAL_DISPLAY
int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 1 && be_isstring(vm, 1)) {
const char * desc = be_tostring(vm, 1);
// remove all objects on stack to avoid warnings in subsequent calls to Berry
be_pop(vm, argc);
Renderer * renderer = Init_uDisplay(desc);
if (renderer) {
be_pushcomptr(vm, renderer);
} else {
be_pushnil(vm);
}
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
2022-01-29 10:11:21 +00:00
#else // USE_UNIVERSAL_DISPLAY
be_raise(vm, "internal_error", "universal display driver not present");
2022-01-29 10:11:21 +00:00
#endif // USE_UNIVERSAL_DISPLAY
}
// `display.dimmer([dim:int]) -> int` sets the dimmer of display, value 0..100. If `0` then turn off display. If no arg, read the current value.
int be_ntv_display_dimmer(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
int32_t dimmer;
if (argc >= 1) {
if (!be_isint(vm, 1)) { be_raise(vm, "type_error", "arg must be int"); }
dimmer = be_toint(vm, 1);
if ((dimmer < 0) || (dimmer > 100)) { be_raise(vm, "value_error", "value must be in range 0..100"); }
be_pop(vm, argc); // clear stack to avoid ripple errors in code called later
SetDisplayDimmer(dimmer);
ApplyDisplayDimmer();
}
be_pushint(vm, GetDisplayDimmer());
be_return(vm);
}
}
#endif // USE_DISPLAY
#endif // USE_BERRY