Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

33 add hw io getset functions to lua interface #36

Merged
merged 5 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions doc/lua.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ The dataref types are listed [here](https://developer.x-plane.com/sdk/XPLMDataAc
You may know that it's a high cost call into the XPlane system to find an internal dataref by the name (more details [here](https://developer.x-plane.com/sdk/XPLMDataAccess/)) Therefore the plugin will
store the dataref pointer so the costly XPLMFindDataRef will be called only once.

## Get the value of an Input or Output HW line
You can query the value of input/output HW lines. For example you can read the position of a switch or a light state.
### To read the state of a button/switch:
```lua
hid_get_button_state(vid,pid,button_name)
```
,where vid and pid are the integer value of the USB HID device's VID and PID. Please note you can query only those devices which are in your active configuration. The button_name is a string parameter and it shall be match with the button names used in the configuration.

The return value is a string type:

"ON", "OFF", "UNKNOWN"

UNKNOWN could mean either the button_name is not valid or the button didn't changed its state.

### To read the state of a light:
```lua
hid_get_light_state(vid,pid,light_name)
```
,where vid and pid are the integer value of the USB HID device's VID and PID. Please note you can query only those devices which are in your active configuration. The light_name is a string parameter and it shall be match with the light names used in the configuration.

The return value is a string type:

"LIT", "UNLIT", "BLINK", ""UNKNOWN"

UNKNOWN could mean either the light_name is not valid or the light didn't changed its state.

## Logger command
To put a log line into the xplane's log file you can use this lua command.
The first parameter determines the log level. If the actual log level is higher than your message here (for example you call log_msg with first parameter as 'TRACE' and the log level is set to INFO by the [config file](configuration.md))
Expand Down
33 changes: 32 additions & 1 deletion src/UsbHidDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,19 @@ bool UsbHidDevice::updateLightStates()
set_bit_value(write_buffer, panel_light_it->bit, 1);
write_buffer_changed = true;
panel_light_it->blink_active = false;
stored_light_states[it.first] = light_change;
break;
case TriggerType::UNLIT:
Logger(TLogLevel::logDEBUG) << " " << it.first << " activated UNLIT" << std::endl;
set_bit_value(write_buffer, panel_light_it->bit, 0);
write_buffer_changed = true;
panel_light_it->blink_active = false;
stored_light_states[it.first] = light_change;
break;
case TriggerType::BLINK:
Logger(TLogLevel::logDEBUG) << " " << it.first << " activated BLINK" << std::endl;
panel_light_it->blink_active = true;
stored_light_states[it.first] = light_change;
break;
case TriggerType::NO_CHANGE:
//
Expand Down Expand Up @@ -259,10 +262,12 @@ void UsbHidDevice::process_selector_switch()
for (auto sel : selectors)
{
if (!is_bit_changed(read_buffer, read_buffer_old, sel.bit))
continue;
continue;

if (get_bit_value(read_buffer, sel.bit))
{
stored_button_states[sel.config_name] = 1;

for (auto button : buttons)
{
for (auto act : config.push_actions[button.config_name.c_str()])
Expand All @@ -287,6 +292,8 @@ void UsbHidDevice::process_selector_switch()
}
else
{
stored_button_states[sel.config_name] = 0;

for (auto button : buttons)
{
for (auto act : config.push_actions[button.config_name.c_str()])
Expand All @@ -308,6 +315,8 @@ void UsbHidDevice::process_and_store_button_states()
{
if (is_bit_changed(read_buffer, read_buffer_old, button.bit))
{
stored_button_states[button.config_name] = get_bit_value(read_buffer, button.bit);

Logger(TLogLevel::logTRACE) << "UsbHidDevice " << button.config_name << " button bit changed " << std::endl;
if (get_bit_value(read_buffer, button.bit) && config.push_actions.find(button.config_name.c_str()) != config.push_actions.end())
{
Expand All @@ -329,6 +338,28 @@ void UsbHidDevice::process_and_store_button_states()
}
}

int UsbHidDevice::get_stored_button_state(std::string button_name)
{
if (stored_button_states.count(button_name) == 0)
{
Logger(TLogLevel::logWARNING) << "get_stored_button_state: unknown button name: " << button_name << std::endl;
return -1;
}

return stored_button_states[button_name];
}

TriggerType UsbHidDevice::get_stored_light_state(std::string light_name)
{
if (stored_light_states.count(light_name) == 0)
{
Logger(TLogLevel::logWARNING) << "get_stored_light_state: unknown light name: " << light_name << std::endl;
return TriggerType::UNKNOWN;
}

return stored_light_states[light_name];
}

void UsbHidDevice::thread_func()
{
Logger(TLogLevel::logTRACE) << "UsbHidDevice::thread_func: started for vid=" << vid << " pid=" << pid << std::endl;
Expand Down
6 changes: 6 additions & 0 deletions src/UsbHidDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class UsbHidDevice : public Device
UsbHidDevice(DeviceConfiguration& config, int _read_buffer_size, int _write_buffer_size);
~UsbHidDevice();
virtual void thread_func();
int get_vid() { return vid; }
int get_pid() { return pid; }
int get_stored_button_state(std::string button_name);
TriggerType get_stored_light_state(std::string light_name);
protected:
int connect();
virtual void start();
Expand All @@ -66,6 +70,8 @@ class UsbHidDevice : public Device
hid_device* device_handle = NULL;
private:
std::vector<PanelButton> buttons;
std::map<std::string, int> stored_button_states;
std::map<std::string, TriggerType> stored_light_states;
std::vector<PanelButton> selectors;
std::vector<PanelLight> lights;
std::vector<PanelDisplay> panel_displays;
Expand Down
125 changes: 120 additions & 5 deletions src/lua_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ LuaHelper* LuaHelper::instance = NULL;
lua_State* lua = NULL;

extern "C" {
#define LUA_FUNC_NAME_COMMAND_ONCE "command_once"
#define LUA_FUNC_NAME_COMMAND_ONCE "command_once"
#define LUA_FUNC_NAME_COMMAND_BEGIN "command_begin"
#define LUA_FUNC_NAME_COMMAND_END "command_end"
#define LUA_FUNC_NAME_GET_DATAREF "get_dataref"
#define LUA_FUNC_NAME_SET_DATAREF "set_dataref"
#define LUA_FUNC_NAME_LOG "log_msg"
#define LUA_FUNC_NAME_COMMAND_END "command_end"
#define LUA_FUNC_NAME_GET_DATAREF "get_dataref"
#define LUA_FUNC_NAME_SET_DATAREF "set_dataref"
#define LUA_FUNC_NAME_LOG "log_msg"
#define LUA_FUNC_NAME_GET_BUTTON_STATE "hid_get_button_state"
#define LUA_FUNC_NAME_GET_LIGHT_STATE "hid_get_light"

static int LuaCommand(lua_State* L, const char* command_name)
{
Expand Down Expand Up @@ -201,6 +203,82 @@ extern "C" {
Logger(log_level) << "LUA script: " << log_msg << std::endl;
return 1;
}

// hid_get_button_state(vid,pid,button_name)
int LuaGetButtonState(lua_State* L)
{
if (!(lua_isnumber(L, 1) && lua_isnumber(L, 2) && lua_isstring(L,3)))
{
Logger(TLogLevel::logERROR) << "lua: wrong arguments to function hid_get_button_state()" << std::endl;
return 0;
}

int vid = (int)lua_tonumber(L, 1);
int pid = (int)lua_tonumber(L, 2);
std::string button_name = lua_tostring(L, 3);

int state = LuaHelper::get_instace()->get_button_state(vid, pid, button_name);

std::string button_state_str = "";

if (state == -1)
{
Logger(TLogLevel::logWARNING) << "lua: error in get_button_state(" << vid << "," << pid << "," << button_name << ")" << std::endl;
button_state_str = "UNKNOWN";
}

if (state == 1)
button_state_str = "ON";
else if (state == 0)
button_state_str = "OFF";
else
button_state_str = "UNKNOWN";

Logger(TLogLevel::logTRACE) << "lua: LuaGetButtonState "<< lua_tostring(L, 3) <<" return: " << button_state_str << std::endl;

lua_pushstring(L, button_state_str.c_str());
return 1;
}

// hid_get_light_state(vid,pid,light_name)
int LuaGetLightState(lua_State* L)
{
if (!(lua_isnumber(L, 1) && lua_isnumber(L, 2) && lua_isstring(L, 3)))
{
Logger(TLogLevel::logERROR) << "lua: wrong arguments to function hid_get_light_state()" << std::endl;
return 0;
}

int vid = (int)lua_tonumber(L, 1);
int pid = (int)lua_tonumber(L, 2);
std::string light_name = lua_tostring(L, 3);

TriggerType state = LuaHelper::get_instace()->get_light_state(vid, pid, light_name);

if (state == TriggerType::UNKNOWN)
{
Logger(TLogLevel::logWARNING) << "lua: error in get_light_state(" << vid << "," << pid << "," << light_name << ")" << std::endl;
}

std::string state_str = "";

switch (state) {
case TriggerType::BLINK: state_str = "BLINK";
break;
case TriggerType::LIT: state_str = "LIT";
break;
case TriggerType::UNLIT: state_str = "UNLIT";
break;
default:
state_str = "UNKOWN";
break;
}

Logger(TLogLevel::logTRACE) << "lua: LuaGetLightState " << lua_tostring(L, 3) << " return: " << state_str << std::endl;

lua_pushstring(L, state_str.c_str());
return 1;
}
}

LuaHelper::LuaHelper()
Expand All @@ -215,6 +293,41 @@ LuaHelper* LuaHelper::get_instace()
return instance;
}

void LuaHelper::register_hid_device(UsbHidDevice* _device)
{
hid_devices.push_back(_device);
}

int LuaHelper::get_button_state(int vid, int pid, std::string button_name)
{
for (auto dev : hid_devices)
{
if (dev->get_vid() == vid && dev->get_pid() == pid)
{
int buttonstate = dev->get_stored_button_state(button_name);
if (buttonstate != -1)
return buttonstate;
}
}

return -1; // error case
}

TriggerType LuaHelper::get_light_state(int vid, int pid, std::string light_name)
{
for (auto dev : hid_devices)
{
if (dev->get_vid() == vid && dev->get_pid() == pid)
{
TriggerType lightstate = dev->get_stored_light_state(light_name);
if (lightstate != TriggerType::UNKNOWN)
return lightstate;
}
}

return TriggerType::UNKNOWN; // error case
}

int LuaHelper::load_script_file(std::string file_name)
{
if (luaL_dofile(lua, file_name.c_str()))
Expand Down Expand Up @@ -242,6 +355,8 @@ int LuaHelper::init()
lua_register(lua, LUA_FUNC_NAME_GET_DATAREF, LuaGet);
lua_register(lua, LUA_FUNC_NAME_SET_DATAREF, LuaSet);
lua_register(lua, LUA_FUNC_NAME_LOG, LuaLogMsg);
lua_register(lua, LUA_FUNC_NAME_GET_BUTTON_STATE, LuaGetButtonState);
lua_register(lua, LUA_FUNC_NAME_GET_LIGHT_STATE, LuaGetLightState);

last_flight_loop_call = std::chrono::system_clock::now();
return EXIT_SUCCESS;
Expand Down
6 changes: 6 additions & 0 deletions src/lua_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <ctime>
#include <string>
#include <map>
#include <vector>
#include "UsbHidDevice.h"
#include "XPLMPlugin.h"
#include "XPLMUtilities.h"
#include "XPLMDataAccess.h"
Expand All @@ -21,11 +23,15 @@ class LuaHelper
XPLMCommandRef get_commandref(std::string commandref_str);
XPLMDataRef get_dataref(std::string dataref_str);
XPLMDataTypeID get_dataref_type(XPLMDataRef dataref);
void register_hid_device(UsbHidDevice* _device);
int get_button_state(int vid, int pid, std::string button_name);
TriggerType get_light_state(int vid, int pid, std::string light_name);
private:
static LuaHelper* instance;
std::map<std::string,XPLMCommandRef> command_refs;
std::map<std::string, XPLMDataRef> data_refs;
std::map<XPLMDataRef, XPLMDataTypeID> data_ref_types;
std::vector<UsbHidDevice*> hid_devices;
bool flight_loop_defined;
std::chrono::system_clock::time_point last_flight_loop_call;
LuaHelper();
Expand Down
3 changes: 2 additions & 1 deletion src/trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ typedef enum {
NO_CHANGE,
LIT,
UNLIT,
BLINK
BLINK,
UNKNOWN
} TriggerType;

class Trigger
Expand Down
3 changes: 3 additions & 0 deletions src/xpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ int init_and_start_xpanel_plugin(void)
device->connect();
device->start();
device->thread_handle = new std::thread(&SaitekMultiPanel::thread_func, (SaitekMultiPanel*)device);
LuaHelper::get_instace()->register_hid_device((UsbHidDevice*)device);
break;
case DeviceType::HOME_COCKPIT:
// set default vid & pid if it's not set in config file
Expand All @@ -276,6 +277,7 @@ int init_and_start_xpanel_plugin(void)
device->connect();
device->start();
device->thread_handle = new std::thread(&ArduinoHomeCockpit::thread_func, (ArduinoHomeCockpit*)device);
LuaHelper::get_instace()->register_hid_device((UsbHidDevice*)device);
break;
case DeviceType::SAITEK_RADIO:
// set default vid & pid if it's not set in config file
Expand All @@ -289,6 +291,7 @@ int init_and_start_xpanel_plugin(void)
device->connect();
device->start();
device->thread_handle = new std::thread(&SaitekRadioPanel::thread_func, (SaitekRadioPanel*)device);
LuaHelper::get_instace()->register_hid_device((UsbHidDevice*)device);
break;
default:
Logger(TLogLevel::logERROR) << "unknown device type" << std::endl;
Expand Down
13 changes: 13 additions & 0 deletions test/test-script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ function get_led_status()
return 1
end

function get_hid_input_status(button_name)
ret_hid_val_str = hid_get_button_state(0x12AB,0x34CD,button_name)
log_msg("TRACE","get_hid_input_status(\""..button_name.."\")=>"..tostring(ret_hid_val_str))

if (ret_hid_val_str == "ON") then
ret_hid_val = 1
else
ret_hid_val = 0
end

return ret_hid_val
end

function get_variometer()
log_msg("TRACE","get_variometer() called")
vario = get_dataref("sim/test/variometer")
Expand Down
18 changes: 18 additions & 0 deletions test/test_multi_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace test
device->connect();
device->start();
t = new std::thread(&SaitekMultiPanel::thread_func, (SaitekMultiPanel*)device);
LuaHelper::get_instace()->register_hid_device(device);
}

TEST_METHOD(Test_VID_PID)
Expand Down Expand Up @@ -241,6 +242,23 @@ namespace test
Assert::AreEqual(0, test_get_dataref_value(dataref_str.c_str()));
}

TEST_METHOD(TestMultiPanelLuaReadHid)
{
// Press REV button
unsigned char buffer[4] = { 0,0x40,0,0 };
test_hid_set_read_data(buffer, sizeof(buffer));
std::this_thread::sleep_for(150ms);
test_flight_loop(config.device_configs);
Assert::AreEqual(1, (int)LuaHelper::get_instace()->do_string("return get_hid_input_status('REV')"));

// Release REV button
buffer[1] = 0;
test_hid_set_read_data(buffer, sizeof(buffer));
std::this_thread::sleep_for(150ms);
test_flight_loop(config.device_configs);
Assert::AreEqual(0, (int)LuaHelper::get_instace()->do_string("return get_hid_input_status('REV')"));
}

TEST_METHOD_CLEANUP(TestMultiPanelCleanup)
{
device->stop(0);
Expand Down