#include "../helpers.h" namespace esp32lua { namespace bindings { namespace { int getButtons(lua_State* state) { pushStrings(state, Runtime::from(state)->buttons().buttons()); return 1; } int isAnyPressed(lua_State* state) { lua_pushboolean(state, Runtime::from(state)->buttons().isAnyPressed()); return 1; } int isPressed(lua_State* state) { const std::string button = checkString(state, 1); lua_pushboolean(state, Runtime::from(state)->buttons().isPressed(button)); return 1; } int wasPressed(lua_State* state) { const std::string button = checkString(state, 1); lua_pushboolean(state, Runtime::from(state)->buttons().wasPressed(button)); return 1; } int wasReleased(lua_State* state) { const std::string button = checkString(state, 1); lua_pushboolean(state, Runtime::from(state)->buttons().wasReleased(button)); return 1; } // @lua-module buttons ButtonsLib // @lua-preamble ---@alias Button "up"|"down"|"left"|"right"|"confirm"|"back" // @lua-preamble // @lua-preamble -- Roles, not physical buttons: a device maps whatever hardware // it has onto them, and // @lua-preamble -- up/down/left/right are the directions tree.moveFocus already // takes. const luaL_Reg FUNCTIONS[] = { // ---Returns the roles this device reports, so an app can label only the // actions it has. // @return Button[] {"getAll", getButtons}, // ---Whether any button is held. // @return boolean {"isAnyPressed", isAnyPressed}, // ---Whether a button is held. // @param button Button // @return boolean {"isPressed", isPressed}, // ---Whether a button went down since the last poll. // @param button Button // @return boolean {"wasPressed", wasPressed}, // ---Whether a button came up since the last poll. // @param button Button // @return boolean {"wasReleased", wasReleased}, {nullptr, nullptr}, }; } // namespace void registerButtons(lua_State* state) { luaL_newlib(state, FUNCTIONS); lua_setglobal(state, "buttons"); } } // namespace bindings } // namespace esp32lua // @lua-app ButtonHandlers // @lua-preamble -- What an app implements to see buttons, composed into its own // class: // @lua-preamble -- // @lua-preamble -- ---@class MenuApp : App, ButtonHandlers // ---Fired when a button goes down. // @param button Button // @lua-fn onButtonDown? // ---Fired when a button comes up. // @param button Button // @lua-fn onButtonUp? // ---Tap alias, fired on release like a click, after onButtonUp. // @param button Button // @lua-fn onButton?