mirror of
https://github.com/preble/libpinproc
synced 2026-02-24 18:25:23 +01:00
Updated pinproctest to load switches and coils from command-line specified YAML file.
Removed Makefiles. Added instructions to install yaml-cpp in /usr/local so CMake can find it (relative paths == bad).
This commit is contained in:
@@ -32,29 +32,14 @@
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <cmath>
|
||||
#include "pinproc.h" // Include libpinproc's header.
|
||||
#include "../../include/pinproc.h" // Include libpinproc's header.
|
||||
#include <fstream>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
|
||||
#define kFlipperLwRightSw (1)
|
||||
#define kFlipperLwLeftSw (3)
|
||||
#define kFlipperUpRightSw (5)
|
||||
#define kFlipperUpLeftSw (7)
|
||||
|
||||
#define kFlipperLwRightMain (32)
|
||||
#define kFlipperLwLeftMain (34)
|
||||
#define kFlipperUpRightMain (36)
|
||||
#define kFlipperUpLeftMain (38)
|
||||
|
||||
#define kFlipperLwRightHold (33)
|
||||
#define kFlipperLwLeftHold (35)
|
||||
#define kFlipperUpRightHold (37)
|
||||
#define kFlipperUpLeftHold (39)
|
||||
|
||||
#define kSlingLeftSw (96)
|
||||
#define kSlingRightSw (97)
|
||||
|
||||
#define kSlingLeftCoil (70)
|
||||
#define kSlingRightCoil (71)
|
||||
#define kFlippersSection "PRFlippers"
|
||||
#define kBumpersSection "PRBumpers"
|
||||
#define kCoilsSection "PRCoils"
|
||||
#define kSwitchesSection "PRSwitches"
|
||||
|
||||
#define kFlipperPulseTime (34) // 34 ms
|
||||
#define kBumperPulseTime (25) // 25 ms
|
||||
@@ -69,7 +54,42 @@ void TestLogger(const char *text)
|
||||
fprintf(stderr, "TEST: %s", text);
|
||||
}
|
||||
|
||||
void ConfigureDrivers(PRHandle proc)
|
||||
PRResult LoadConfiguration(YAML::Node& yamlDoc, const char *yamlFilePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::ifstream fin(yamlFilePath);
|
||||
if (fin.is_open() == false)
|
||||
{
|
||||
fprintf(stderr, "YAML file not found: %s\n", yamlFilePath);
|
||||
return kPRFailure;
|
||||
}
|
||||
YAML::Parser parser(fin);
|
||||
|
||||
while(parser)
|
||||
{
|
||||
parser.GetNextDocument(yamlDoc);
|
||||
}
|
||||
}
|
||||
catch (YAML::ParserException& ex)
|
||||
{
|
||||
fprintf(stderr, "YAML parse error at line=%d col=%d: %s\n", ex.line, ex.column, ex.msg.c_str());
|
||||
return kPRFailure;
|
||||
}
|
||||
catch (YAML::RepresentationException& ex)
|
||||
{
|
||||
fprintf(stderr, "YAML representation error at line=%d col=%d: %s\n", ex.line, ex.column, ex.msg.c_str());
|
||||
return kPRFailure;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
fprintf(stderr, "Unexpected exception while parsing YAML config.\n");
|
||||
return kPRFailure;
|
||||
}
|
||||
return kPRSuccess;
|
||||
}
|
||||
|
||||
void ConfigureDrivers(PRHandle proc, YAML::Node& yamlDoc)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -131,7 +151,7 @@ void ConfigureDrivers(PRHandle proc)
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigureSwitches(PRHandle proc)
|
||||
void ConfigureSwitches(PRHandle proc, YAML::Node& yamlDoc)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -191,17 +211,32 @@ void ConfigureBumperRule (PRHandle proc, int swNum, int coilNum, int pulseTime)
|
||||
PRSwitchUpdateRule(proc,swNum, kPREventTypeSwitchClosedNondebounced, &sw, drivers, numDriverRules);
|
||||
}
|
||||
|
||||
void ConfigureSwitchRules(PRHandle proc)
|
||||
void ConfigureSwitchRules(PRHandle proc, YAML::Node& yamlDoc)
|
||||
{
|
||||
// WPC Flippers
|
||||
ConfigureWPCFlipperSwitchRule (proc, kFlipperLwRightSw, kFlipperLwRightMain, kFlipperLwRightHold, kFlipperPulseTime); // Lower Right WPC Flipper
|
||||
ConfigureWPCFlipperSwitchRule (proc, kFlipperLwLeftSw, kFlipperLwLeftMain, kFlipperLwLeftHold, kFlipperPulseTime); // Lower Left WPC Flipper
|
||||
ConfigureWPCFlipperSwitchRule (proc, kFlipperUpRightSw, kFlipperUpRightMain, kFlipperUpRightHold, kFlipperPulseTime); // Upper Right WPC Flipper
|
||||
ConfigureWPCFlipperSwitchRule (proc, kFlipperUpLeftSw, kFlipperUpLeftMain, kFlipperUpLeftHold, kFlipperPulseTime); // Upper Left WPC Flipper
|
||||
const YAML::Node& flippers = yamlDoc[kFlippersSection];
|
||||
for (YAML::Iterator flippersIt = flippers.begin(); flippersIt != flippers.end(); ++flippersIt)
|
||||
{
|
||||
int swNum, coilMain, coilHold;
|
||||
std::string flipperName;
|
||||
*flippersIt >> flipperName;
|
||||
yamlDoc[kSwitchesSection][flipperName] >> swNum;
|
||||
yamlDoc[kCoilsSection][flipperName + "Main"] >> coilMain;
|
||||
yamlDoc[kCoilsSection][flipperName + "Hold"] >> coilHold;
|
||||
ConfigureWPCFlipperSwitchRule (proc, swNum, coilMain, coilHold, kFlipperPulseTime);
|
||||
}
|
||||
|
||||
// WPC Slingshots
|
||||
ConfigureBumperRule (proc, kSlingRightSw, kSlingRightCoil, kBumperPulseTime); // WPC Right Slingshot
|
||||
ConfigureBumperRule (proc, kSlingLeftSw, kSlingLeftCoil, kBumperPulseTime); // WPC Left Slingshot
|
||||
const YAML::Node& bumpers = yamlDoc[kBumpersSection];
|
||||
for (YAML::Iterator bumpersIt = bumpers.begin(); bumpersIt != bumpers.end(); ++bumpersIt)
|
||||
{
|
||||
int swNum, coilNum;
|
||||
// WPC Slingshots
|
||||
std::string bumperName;
|
||||
*bumpersIt >> bumperName;
|
||||
yamlDoc[kSwitchesSection][bumperName] >> swNum;
|
||||
yamlDoc[kCoilsSection][bumperName] >> coilNum;
|
||||
ConfigureBumperRule (proc, swNum, coilNum, kBumperPulseTime);
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigureDMD(PRHandle proc)
|
||||
@@ -325,26 +360,49 @@ void sigint(int)
|
||||
printf("Exiting...\n");
|
||||
}
|
||||
|
||||
int main(const char **argv, int argc)
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
// Set a signal handler so that we can exit gracefully on Ctrl-C:
|
||||
signal(SIGINT, sigint);
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <yaml machine description>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
const char *yamlFilename = argv[1];
|
||||
|
||||
// Assign a custom logging callback to demonstrate capturing log information from P-ROC:
|
||||
PRLogSetCallback(TestLogger);
|
||||
|
||||
YAML::Node yamlDoc;
|
||||
LoadConfiguration(yamlDoc, yamlFilename);
|
||||
|
||||
PRMachineType machineType = kPRMachineInvalid;
|
||||
std::string machineTypeString;
|
||||
yamlDoc["PRGame"]["machineType"] >> machineTypeString;
|
||||
if (machineTypeString == "wpc")
|
||||
machineType = kPRMachineWPC;
|
||||
else if(machineTypeString == "stern")
|
||||
machineType = kPRMachineStern;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unknown machine type: %s\n", machineTypeString.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Finally instantiate the P-ROC device:
|
||||
PRHandle proc = PRCreate(kPRMachineWPC);
|
||||
PRHandle proc = PRCreate(machineType);
|
||||
if (proc == kPRHandleInvalid)
|
||||
return 1;
|
||||
|
||||
|
||||
ConfigureDMD(proc);
|
||||
ConfigureSwitches(proc); // Notify host for all debounced switch events.
|
||||
ConfigureSwitchRules(proc); // Flippers, slingshots
|
||||
ConfigureSwitches(proc, yamlDoc); // Notify host for all debounced switch events.
|
||||
ConfigureSwitchRules(proc, yamlDoc); // Flippers, slingshots
|
||||
|
||||
// Make Drivers the last thing to configure so watchdog doesn't expire
|
||||
// before the RunLoop begins.
|
||||
ConfigureDrivers(proc);
|
||||
ConfigureDrivers(proc, yamlDoc);
|
||||
|
||||
printf("Running. Hit Ctrl-C to exit.\n");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user