-
-
Save TaraRed/6df528ed6c2f5287ce7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
#include <chrono> | |
#include <thread> | |
#include <string> | |
using namespace std; | |
#include <libcec/cec.h> | |
#include <libcec/cecloader.h> | |
#include <libcec/cectypes.h> | |
using namespace CEC; | |
ICECAdapter * parser = 0; | |
libcec_configuration config; | |
ICECCallbacks callbacks; | |
bool debug = false; | |
bool stop = false; | |
void Close() | |
{ | |
if (debug) | |
cout << "Closing CEC adapter..." << endl; | |
parser->Close(); | |
if (debug) | |
cout << "Destroying CEC library..." << endl; | |
CECDestroy(parser); | |
if (debug) | |
cout << "Done." << endl; | |
} | |
int CecLogMessage(void *, const cec_log_message message) | |
{ | |
if (debug) | |
cout << "MESSAGE: " << message.message << endl; | |
if (string(message.message).compare(">> AA:BB:CC:DD") == 0) | |
stop = true; | |
return 0; | |
} | |
int CecKeyPress(void *, const cec_keypress key) | |
{ | |
if (debug) | |
cout << "KEY: " << key.keycode << endl; | |
if (key.keycode == 0) | |
stop = true; | |
return 0; | |
} | |
int CecCommand(void *, const cec_command command) | |
{ | |
if (debug) | |
cout << "COMMAND: " << command.opcode << endl; | |
return 0; | |
} | |
int CecAlert(void *, const libcec_alert type, const libcec_parameter) | |
{ | |
switch (type) | |
{ | |
case CEC_ALERT_CONNECTION_LOST: | |
cout << "ERROR: Connection lost!" << endl; | |
break; | |
default: | |
break; | |
} | |
return 0; | |
} | |
unsigned int Start() | |
{ | |
if (debug) | |
cout << "Starting..." << endl; | |
if (debug) | |
cout << "Configuring..." << endl; | |
config.Clear(); | |
callbacks.Clear(); | |
snprintf(config.strDeviceName, 13, "CECTester"); | |
config.clientVersion = CEC_CLIENT_VERSION_CURRENT; | |
config.bActivateSource = 0; | |
callbacks.CBCecLogMessage = &CecLogMessage; | |
callbacks.CBCecKeyPress = &CecKeyPress; | |
callbacks.CBCecCommand = &CecCommand; | |
callbacks.CBCecAlert = &CecAlert; | |
config.callbacks = &callbacks; | |
config.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE); | |
if (debug) | |
cout << "Initializing CEC library..." << endl; | |
parser = (CEC::ICECAdapter *) CECInitialise(&config); | |
if (!parser) | |
{ | |
cout << "ERROR: Can't initialize CEC parser." << endl; | |
return 1; | |
} | |
if (debug) | |
cout << "Initializing video..." << endl; | |
parser->InitVideoStandalone(); | |
if (debug) | |
cout << "Finding adapters..." << endl; | |
cec_adapter devices[10]; | |
uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL); | |
if (iDevicesFound <= 0) | |
{ | |
cout << "ERROR: No devices detected." << endl; | |
UnloadLibCec(parser); | |
return 1; | |
} | |
if (debug) | |
cout << "Found adapter: " << devices[0].comm << endl; | |
if (debug) | |
cout << "Opening adapter..." << endl; | |
if (!parser->Open(devices[0].comm)) | |
{ | |
cout << "ERROR: Unable to open device on port " << devices[0].comm << "." << endl; | |
UnloadLibCec(parser); | |
return 1; | |
} | |
if (parser->PingAdapter()) | |
{ | |
if (debug) | |
cout << "Listening..." << endl; | |
} | |
else | |
cout << "WARNING: Pinging the adapter failed." << endl; | |
return 0; | |
} | |
void Activate() | |
{ | |
if (debug) | |
cout << "Activating source..." << endl; | |
parser->SetActiveSource(); | |
this_thread::sleep_for(chrono::milliseconds(1000)); | |
parser->SetActiveSource(); | |
this_thread::sleep_for(chrono::milliseconds(2000)); | |
if (parser->IsLibCECActiveSource()) | |
{ | |
if (debug) | |
cout << "Activated." << endl; | |
} | |
else | |
cout << "ERROR: Could not activate!" << endl; | |
} | |
int main() | |
{ | |
if (!Start()) | |
{ | |
while (!stop) | |
this_thread::sleep_for(chrono::milliseconds(1000)); | |
Close(); | |
return 0; | |
} | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC=g++ | |
CPPFLAGS=-std=c++11 -ggdb -Wall -Wextra | |
LDFLAGS=-ldl -lcec | |
main: main.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment