From cd1b0462c53739887b418e949ec968d4961c6d05 Mon Sep 17 00:00:00 2001 From: Adam Preble Date: Fri, 22 May 2009 01:51:52 -0400 Subject: [PATCH] Added excruciatingly rough implementation of PRLoadDefaultsFromYAML() and related items. --- .gitignore | 1 + Makefile | 3 +- README.markdown | 8 +++ examples/pinproctest/Example.yaml | 7 +++ examples/pinproctest/Makefile | 4 +- examples/pinproctest/pinproctest.cpp | 1 + include/pinproc.h | 2 + libpinproc.xcodeproj/project.pbxproj | 22 +++++-- src/PRDevice.cpp | 87 ++++++++++++++++++++++++++++ src/PRDevice.h | 1 + src/pinproc.cpp | 4 ++ 11 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 examples/pinproctest/Example.yaml diff --git a/.gitignore b/.gitignore index 8248aa2..83e29ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/ */*.pbxuser */*.perspectivev3 +*.o diff --git a/Makefile b/Makefile index ceabe44..c03269e 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,8 @@ LIBSRC=src/pinproc.cpp src/PRDevice.cpp src/PRHardware.cpp LIBOBJ=$(LIBSRC:.cpp=.o) -#CXXFLAGS=-I/usr/local/lib -lusb -lftdi +#CXXFLAGS=-I/usr/local/lib -lusb -lftdi +CXXFLAGS=-I../../yaml-cpp/include $(LIB): $(LIBOBJ) @echo lib Makefile - archiving $(LIB) diff --git a/README.markdown b/README.markdown index d27d016..7e5b248 100644 --- a/README.markdown +++ b/README.markdown @@ -2,6 +2,14 @@ Library for Gerry Stellenberg's [P-ROC](http://pinballcontrollers.com/) (Pinball Remote Operations Controller). +### Compiling + +libpinproc requires: + +- [libusb-0.1.12](http://libusb.wiki.sourceforge.net/): Install with the default /usr/local prefix. +- [libftdi-0.16](http://www.intra2net.com/en/developer/libftdi/): Install with the default /usr/local prefix. +- [yaml-cpp](http://code.google.com/p/yaml-cpp/): Should be checked out in the directory two levels below libpinproc in the full source tree (at the same level as ./P-ROC) in a directory named yaml-cpp. Follow the build instructions, creating the build subdirectory. The Makefiles and other project files expect to find libyaml-cpp.a in the yaml-cpp/build/bin directory. + ### License #### The MIT License diff --git a/examples/pinproctest/Example.yaml b/examples/pinproctest/Example.yaml new file mode 100644 index 0000000..f99a700 --- /dev/null +++ b/examples/pinproctest/Example.yaml @@ -0,0 +1,7 @@ +PRGameName: My Great Pin +PRDrivers: + 1: driver one + 2: driver two +PRSwitches: + 1: switch one + 2: switch two diff --git a/examples/pinproctest/Makefile b/examples/pinproctest/Makefile index db31cb6..99244bc 100644 --- a/examples/pinproctest/Makefile +++ b/examples/pinproctest/Makefile @@ -2,8 +2,8 @@ # File: Makefile for application # CC=g++ -LDFLAGS=-L../.. -L/usr/local/lib -LIBS=-lpinproc -lusb -lftdi +LDFLAGS=-L../.. -L/usr/local/lib -L../../../../yaml-cpp/build/bin +LIBS=-lpinproc -lusb -lftdi -lyaml-cpp SRC=pinproctest.cpp diff --git a/examples/pinproctest/pinproctest.cpp b/examples/pinproctest/pinproctest.cpp index 1a489b2..ce7b117 100644 --- a/examples/pinproctest/pinproctest.cpp +++ b/examples/pinproctest/pinproctest.cpp @@ -162,6 +162,7 @@ int main(const char **argv, int argc) return 1; printf("Configuring P-ROC...\n"); + PRLoadDefaultsFromYAML(proc, "../../examples/pinproctest/Example.yaml"); ConfigureSwitches(proc); // Make Drivers the last thing to configure so watchdog doesn't expire diff --git a/include/pinproc.h b/include/pinproc.h index 17b7879..b9b45eb 100644 --- a/include/pinproc.h +++ b/include/pinproc.h @@ -91,6 +91,8 @@ typedef enum PRMachineType { PR_EXPORT PRHandle PRCreate(PRMachineType machineType); /**< Create a new P-ROC device handle. Only one handle per device may be created. This handle must be destroyed with PRDelete() when it is no longer needed. Returns #kPRHandleInvalid if an error occurred. */ PR_EXPORT void PRDelete(PRHandle handle); /**< Destroys an existing P-ROC device handle. */ +PR_EXPORT PRResult PRLoadDefaultsFromYAML(PRHandle handle, const char *yamlFilePath); + /** @} */ // End of Device Creation & Deletion // Drivers diff --git a/libpinproc.xcodeproj/project.pbxproj b/libpinproc.xcodeproj/project.pbxproj index 98001c9..65642bf 100644 --- a/libpinproc.xcodeproj/project.pbxproj +++ b/libpinproc.xcodeproj/project.pbxproj @@ -221,7 +221,10 @@ GCC_ENABLE_FIX_AND_CONTINUE = YES; GCC_MODEL_TUNING = G5; GCC_OPTIMIZATION_LEVEL = 0; - HEADER_SEARCH_PATHS = /usr/local/include; + HEADER_SEARCH_PATHS = ( + /usr/local/include, + "../../yaml-cpp/include", + ); INSTALL_PATH = /usr/local/lib; PRODUCT_NAME = pinproc; }; @@ -233,7 +236,10 @@ ALWAYS_SEARCH_USER_PATHS = NO; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; GCC_MODEL_TUNING = G5; - HEADER_SEARCH_PATHS = /usr/local/include; + HEADER_SEARCH_PATHS = ( + /usr/local/include, + "../../yaml-cpp/include", + ); INSTALL_PATH = /usr/local/lib; PRODUCT_NAME = pinproc; }; @@ -275,7 +281,11 @@ GCC_MODEL_TUNING = G5; GCC_OPTIMIZATION_LEVEL = 0; INSTALL_PATH = /usr/local/bin; - OTHER_LDFLAGS = "-lftdi"; + OTHER_LDFLAGS = ( + "-L../../yaml-cpp/build/bin", + "-lyaml-cpp", + "-lftdi", + ); PREBINDING = NO; PRODUCT_NAME = pinproctest; }; @@ -290,7 +300,11 @@ GCC_ENABLE_FIX_AND_CONTINUE = NO; GCC_MODEL_TUNING = G5; INSTALL_PATH = /usr/local/bin; - OTHER_LDFLAGS = "-lftdi"; + OTHER_LDFLAGS = ( + "-L../../yaml-cpp/build/bin", + "-lyaml-cpp", + "-lftdi", + ); PREBINDING = NO; PRODUCT_NAME = pinproctest; ZERO_LINK = NO; diff --git a/src/PRDevice.cpp b/src/PRDevice.cpp index db14858..5b70b7b 100644 --- a/src/PRDevice.cpp +++ b/src/PRDevice.cpp @@ -99,6 +99,93 @@ void PRDevice::Reset() // TODO: Assign defaults based on machineType. Some may have already been done above. } +#include +#include "yaml.h" + +PRResult PRDevice::LoadDefaultsFromYAML(const char *yamlFilePath) +{ + try + { + std::ifstream fin(yamlFilePath); + if (fin.is_open() == false) + { + DEBUG(PRLog("YAML file not found: %s\n", yamlFilePath)); + return kPRFailure; + } + YAML::Parser parser(fin); + + while(parser) { + YAML::Node doc; + parser.GetNextDocument(doc); + + for(YAML::Iterator it=doc.begin();it!=doc.end();++it) { + std::string key; + it.first() >> key; + DEBUG(PRLog("Parsing key %s...\n", key.c_str())); + if (key.compare("PRGameName") == 0) + { + std::string name; + it.second() >> name; + DEBUG(PRLog(" Machine name: %s\n", name.c_str())); + } + else if (key.compare("PRDriverGlobalConfig") == 0) + { + //const YAML::Node& dict = it.second(); + } + else if (key.compare("PRDriverGroupConfigs") == 0) + { + const YAML::Node& groups = it.second(); + for(YAML::Iterator it=groups.begin();it!=groups.end();++it) + { + std::string key; + it.first() >> key; + } + } + else if (key.compare("PRDrivers") == 0) + { + const YAML::Node& dict = it.second(); + for(YAML::Iterator dictIt=dict.begin(); dictIt != dict.end(); ++dictIt) + { + std::string driverKey; + std::string driverName; + dictIt.first() >> driverKey; + dictIt.second() >> driverName; + DEBUG(PRLog(" Driver: %s -> %s\n", driverKey.c_str(), driverName.c_str())); + } + } + else if (key.compare("PRSwitches") == 0) + { + const YAML::Node& dict = it.second(); + for(YAML::Iterator dictIt=dict.begin(); dictIt != dict.end(); ++dictIt) + { + std::string driverKey; + std::string driverName; + dictIt.first() >> driverKey; + dictIt.second() >> driverName; + DEBUG(PRLog(" Switch: %s -> %s\n", driverKey.c_str(), driverName.c_str())); + } + } + } + } + } + catch (YAML::ParserException& ex) + { + DEBUG(PRLog("YAML parse error at line=%d col=%d: %s\n", ex.line, ex.column, ex.msg.c_str())); + return kPRFailure; + } + catch (YAML::RepresentationException& ex) + { + DEBUG(PRLog("YAML representation error at line=%d col=%d: %s\n", ex.line, ex.column, ex.msg.c_str())); + return kPRFailure; + } + catch (...) + { + DEBUG(PRLog("Unexpected exception while parsing YAML config.\n")); + return kPRFailure; + } + return kPRSuccess; +} + int PRDevice::GetEvents(PREvent *events, int maxEvents) { diff --git a/src/PRDevice.h b/src/PRDevice.h index a181458..04b9018 100644 --- a/src/PRDevice.h +++ b/src/PRDevice.h @@ -56,6 +56,7 @@ protected: public: // public libpinproc API: + PRResult LoadDefaultsFromYAML(const char *yamlFilePath); int GetEvents(PREvent *events, int maxEvents); PRResult DriverUpdateGlobalConfig(PRDriverGlobalConfig *driverGlobalConfig); diff --git a/src/pinproc.cpp b/src/pinproc.cpp index e223f6f..a078067 100644 --- a/src/pinproc.cpp +++ b/src/pinproc.cpp @@ -72,6 +72,10 @@ PR_EXPORT void PRDelete(PRHandle handle) delete (PRDevice*)handle; } +PR_EXPORT PRResult PRLoadDefaultsFromYAML(PRHandle handle, const char *yamlFilePath) +{ + return handleAsDevice->LoadDefaultsFromYAML(yamlFilePath); +} // Events