1
0
Fork 0

Extend event-input with report-setting.

Can generate feature report data via Nasal callbacks. This is used to
drive the GoFlight LCD / LEDs.
This commit is contained in:
James Turner 2016-03-19 14:44:38 +00:00
parent eb1c6adbc7
commit 347a89c1f2
4 changed files with 113 additions and 1 deletions

View file

@ -251,6 +251,12 @@ void FGInputDevice::Configure( SGPropertyNode_ptr aDeviceNode )
debugEvents = deviceNode->getBoolValue("debug-events", debugEvents );
grab = deviceNode->getBoolValue("grab", grab );
PropertyList reportNodes = deviceNode->getChildren("report");
for( PropertyList::iterator it = reportNodes.begin(); it != reportNodes.end(); ++it ) {
FGReportSetting_ptr r = new FGReportSetting(*it);
reportSettings.push_back(r);
}
// TODO:
// add nodes for the last event:
// last-event/name [string]
@ -273,6 +279,14 @@ void FGInputDevice::update( double dt )
{
for( map<string,FGInputEvent_ptr>::iterator it = handledEvents.begin(); it != handledEvents.end(); it++ )
(*it).second->update( dt );
report_setting_list_t::const_iterator it;
for (it = reportSettings.begin(); it != reportSettings.end(); ++it) {
if ((*it)->Test()) {
std::string reportData = (*it)->reportBytes(nasalModule);
SendFeatureReport((*it)->getReportId(), reportData);
}
}
}
void FGInputDevice::HandleEvent( FGEventData & eventData )
@ -292,6 +306,12 @@ void FGInputDevice::SetName( string name )
this->name = name;
}
void FGInputDevice::SendFeatureReport(unsigned int reportId, const std::string& data)
{
SG_LOG(SG_INPUT, SG_WARN, "SendFeatureReport not implemented");
}
const char * FGEventInput::PROPERTY_ROOT = "/input/event";
FGEventInput::FGEventInput() :
@ -389,3 +409,52 @@ void FGEventInput::RemoveDevice( unsigned index )
deviceNode = baseNode->removeChild("device", index);
}
FGReportSetting::FGReportSetting( SGPropertyNode_ptr base )
{
reportId = base->getIntValue("report-id");
nasalFunction = base->getStringValue("nasal-function");
PropertyList watchNodes = base->getChildren( "watch" );
for (PropertyList::iterator it = watchNodes.begin(); it != watchNodes.end(); ++it ) {
std::string path = (*it)->getStringValue();
SGPropertyNode_ptr n = globals->get_props()->getNode(path, true);
n->addChangeListener(this);
}
dirty = true;
}
bool FGReportSetting::Test()
{
bool d = dirty;
dirty = false;
return d;
}
std::string FGReportSetting::reportBytes(const std::string& moduleName) const
{
FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
if (!nas) {
return std::string();
}
naRef module = nas->getModule(moduleName.c_str());
naRef func = naHash_cget(module, (char*) nasalFunction.c_str());
if (!naIsFunc(func)) {
return std::string();
}
naRef result = nas->call(func, 0, 0, naNil());
if (!naIsString(result)) {
return std::string();
}
size_t len = naStr_len(result);
char* bytes = naStr_data(result);
return std::string(bytes, len);
}
void FGReportSetting::valueChanged(SGPropertyNode * node)
{
dirty = true;
}

View file

@ -62,6 +62,37 @@ protected:
typedef SGSharedPtr<FGEventSetting> FGEventSetting_ptr;
typedef std::vector<FGEventSetting_ptr> setting_list_t;
class FGReportSetting : public SGReferenced,
public SGPropertyChangeListener
{
public:
FGReportSetting( SGPropertyNode_ptr base );
unsigned int getReportId() const
{
return reportId;
}
std::string getNasalFunctionName() const
{
return nasalFunction;
}
bool Test();
std::string reportBytes(const std::string& moduleName) const;
virtual void valueChanged(SGPropertyNode * node);
protected:
unsigned int reportId;
std::string nasalFunction;
bool dirty;
};
typedef SGSharedPtr<FGReportSetting> FGReportSetting_ptr;
typedef std::vector<FGReportSetting_ptr> report_setting_list_t;
/*
* A wrapper class for a configured event.
*
@ -200,6 +231,8 @@ public:
Send( eventName.c_str(), value );
}
virtual void SendFeatureReport(unsigned int reportId, const std::string& data);
virtual const char * TranslateEventName( FGEventData & eventData ) = 0;
@ -208,7 +241,7 @@ public:
void HandleEvent( FGEventData & eventData );
void AddHandledEvent( FGInputEvent_ptr handledEvent ) {
virtual void AddHandledEvent( FGInputEvent_ptr handledEvent ) {
if( handledEvents.count( handledEvent->GetName() ) == 0 )
handledEvents[handledEvent->GetName()] = handledEvent;
}
@ -240,6 +273,8 @@ private:
SGPropertyNode_ptr deviceNode;
std::string nasalModule;
report_setting_list_t reportSettings;
};
typedef SGSharedPtr<FGInputDevice> FGInputDevice_ptr;

View file

@ -1223,6 +1223,12 @@ void FGNasalSys::deleteModule(const char* moduleName)
naFreeContext(ctx);
}
naRef FGNasalSys::getModule(const char* moduleName)
{
naRef mod = naHash_cget(_globals, (char*) moduleName);
return mod;
}
naRef FGNasalSys::parse(naContext ctx, const char* filename, const char* buf, int len)
{
int errLine = -1;

View file

@ -89,6 +89,8 @@ public:
void deleteModule(const char* moduleName);
naRef getModule(const char* moduleName);
void addCommand(naRef func, const std::string& name);
void removeCommand(const std::string& name);