Initial commit.

This commit is contained in:
matzman
2014-06-16 11:16:21 +02:00
commit 1768f7b41c
42 changed files with 2656 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
/*
* TfBrick.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/TfBrick.h>
namespace tfcppsimple {
TfBrick::TfBrick(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
uint16_t raw_device_identifier,
TfDeviceIdentifier device_identifier,
TfDevice* parent
)
: TfDevice(connection, uid, connected_uid, position, hardware_version, firmware_version,
raw_device_identifier, device_identifier, parent, TfDeviceType::BRICK ) {
}
TfBrick::~TfBrick() {
// TODO Auto-generated destructor stub
}
} /* namespace tfcppsimple */
+31
View File
@@ -0,0 +1,31 @@
/*
* TfBricklet.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/TfBricklet.h>
namespace tfcppsimple {
TfBricklet::TfBricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
uint16_t raw_device_identifier,
TfDeviceIdentifier device_identifier,
TfDevice* parent
)
: TfDevice(connection, uid, connected_uid, position, hardware_version, firmware_version,
raw_device_identifier, device_identifier, parent, TfDeviceType::BRICKLET ) {
}
TfBricklet::~TfBricklet() {
// TODO Auto-generated destructor stub
}
} /* namespace tfcppsimple */
+288
View File
@@ -0,0 +1,288 @@
/*
* TfConnection.cpp
*
* Created on: 10.06.2014
* Author: matzman
*/
#include <tfcppsimple/TfConnection.h>
#include <tfcppsimple/TfException.h>
#include <tfcppsimple/TfDeviceFactory.h>
#include <sstream>
#include <unistd.h>
#include <string.h>
namespace tfcppsimple {
log4cpp::Category& TfConnection::logger = log4cpp::Category::getInstance("tfcppsimple.tfconnection");
TfConnection::TfConnection() {
}
TfConnection::~TfConnection() {
logger.debug("TfConnection::~TfConnection() begin");
if (okFlag) {
ipcon_destroy(&tfIpConnection); // Calls ipcon_disconnect internally
okFlag = false;
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_ENUMERATE,
nullptr, nullptr);
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_CONNECTED,
nullptr, nullptr);
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_DISCONNECTED,
nullptr, nullptr);
}
logger.debug("TfConnection::~TfConnection() end");
}
bool TfConnection::connect(std::string host , uint16_t port, bool autoReconnect, uint32_t timeout) {
logger.debug("TfConnection::connect(\"%s\" , %d, %d, %d) begin", host.c_str(), port, autoReconnect, timeout);
if (!okFlag && state == TfConnectionState::NOTCONFIGURED) {
// Create IP Connection
ipcon_create(&tfIpConnection);
ipcon_set_auto_reconnect(&tfIpConnection, autoReconnect);
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_ENUMERATE,
(void*)TfConnection::__enumerateCallback, this);
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_CONNECTED,
(void*)TfConnection::__connectedCallback, this);
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_DISCONNECTED,
(void*)TfConnection::__disconnectedCallback, this);
// Connect to brickd
TfErrorCode errorCode;
if((errorCode = (TfErrorCode)ipcon_connect(&tfIpConnection, host.c_str(), port)) != TfErrorCode::E_OK) {
std::ostringstream msg;
msg << "Could not connect to brickd (" << host <<":" << port << "): Error " << (int)errorCode;
throw TfError(msg.str(), errorCode);
}
okFlag = true;
}
logger.debug("TfConnection::connect() end");
return okFlag;
}
bool TfConnection::disconnect() {
logger.debug("TfConnection::disconnect() begin");
if (okFlag && state != TfConnectionState::NOTCONFIGURED) {
notifyListeners([this](TfConnectionListener& l)->bool {
l.shutdownCallback(*this);
return true;
}
);
for (auto d : devices) {
d.second->signalShutdown();
delete d.second;
}
ipcon_destroy(&tfIpConnection); // Calls ipcon_disconnect internally
okFlag = false;
stateChange(TfConnectionState::NOTCONFIGURED);
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_ENUMERATE,
nullptr, nullptr);
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_CONNECTED,
nullptr, nullptr);
ipcon_register_callback(&tfIpConnection, IPCON_CALLBACK_DISCONNECTED,
nullptr, nullptr);
devices.clear();
rootDevices.clear();
}
logger.debug("TfConnection::disconnect() end");
return state == TfConnectionState::NOTCONFIGURED;
}
void TfConnection::__enumerateCallback( const char *uid,
const char *connected_uid,
char position,
uint8_t hardware_version[3],
uint8_t firmware_version[3],
uint16_t device_identifier,
uint8_t enumeration_type,
void *user_data) {
logger << log4cpp::Priority::DEBUG << "TfConnection::__enumerateCallback(\""
<< uid << "\", \"" << connected_uid <<"\", '" << (unsigned)position << "', "
<< (unsigned)hardware_version[0] << "." << (unsigned)hardware_version[1] << "." << (unsigned)hardware_version[2] << ", "
<< (unsigned)firmware_version[0] << "." << (unsigned)firmware_version[1] << "." << (unsigned)firmware_version[2] << ", "
<< device_identifier << ", " << (unsigned)enumeration_type << ", " << user_data << ") begin";
if (user_data != nullptr) {
TfConnection& conn = *(TfConnection*)user_data;
conn.enumerateCallback(uid, connected_uid, position, hardware_version, firmware_version,
device_identifier, (TfCallbackEnumerationType)enumeration_type);
}
logger.debug("TfConnection::__enumerateCallback() end");
}
void TfConnection::__connectedCallback(uint8_t connect_reason, void *user_data) {
logger << log4cpp::Priority::DEBUG << "TfConnection::__connectedCallback("
<< (unsigned)connect_reason << ", " << user_data << ") begin";
if (user_data != nullptr) {
TfConnection& conn = *(TfConnection*)user_data;
conn.connectedCallback((TfCallbackConnectReason)connect_reason);
}
logger.debug("TfConnection::__connectedCallback() end");
}
void TfConnection::__disconnectedCallback(uint8_t disconnect_reason, void *user_data) {
logger << log4cpp::Priority::DEBUG << "TfConnection::__disconnectedCallback("
<< (unsigned)disconnect_reason << ", " << user_data << ") begin";
if (user_data != nullptr) {
TfConnection& conn = *(TfConnection*)user_data;
conn.disconnectedCallback((TfCallbackDisconnectReason)disconnect_reason);
}
logger.debug("TfConnection::__disconnectedCallback() end");
}
void TfConnection::enumerateCallback(
const char *uid,
const char *connected_uid,
char position,
uint8_t hardware_version[3],
uint8_t firmware_version[3],
uint16_t device_identifier,
TfCallbackEnumerationType enumeration_type) {
logger << log4cpp::Priority::DEBUG << "TfConnection[" << this << "]::enumerateCallback(\""
<< uid << "\", \"" << connected_uid <<"\", '" << (unsigned)position << "', "
<< (unsigned)hardware_version[0] << "." << (unsigned)hardware_version[1] << "." << (unsigned)hardware_version[2] << ", "
<< (unsigned)firmware_version[0] << "." << (unsigned)firmware_version[1] << "." << (unsigned)firmware_version[2] << ", "
<< device_identifier << ", " << (unsigned)enumeration_type << ") begin";
auto dev = devices.find(uid);
if (dev != devices.end()) {
dev->second->signalEnumerationCallback(enumeration_type);
} else {
if ( enumeration_type == TfCallbackEnumerationType::CONNECTED
|| enumeration_type == TfCallbackEnumerationType::AVAILABLE ) {
TfDevice* parent = nullptr;
bool errorFlag = false;
if (strcmp(connected_uid, "0") != 0) {
auto pdev = devices.find(connected_uid);
if (pdev != devices.end()) {
parent = pdev->second;
} else {
errorFlag = true;
logger << log4cpp::Priority::ERROR << "TfConnection[" << this
<< "]::enumerateCallback: Could not find parent device \""
<< connected_uid << "\".";
}
}
if (!errorFlag) {
TfDevice* ndev = TfDeviceFactory::createNewDevice(*this, uid, connected_uid,
position, hardware_version, firmware_version, device_identifier, parent);
devices.insert({ndev->getUid(), ndev});
if (ndev->getParent() == nullptr) {
rootDevices.push_back(ndev);
} else {
ndev->getParent()->children.insert({ndev->getPosition(), ndev});
}
logger << log4cpp::Priority::INFO << "Found device \"" << ndev->getDeviceName(true) << "\".";
notifyListeners([ndev, this](TfConnectionListener& l)->bool {
l.newDeviceCallback(*this, *ndev);
return true;
}
);
}
}
}
logger.debug("TfConnection::enumerateCallback() end");
}
void TfConnection::connectedCallback(TfCallbackConnectReason connect_reason) {
logger << log4cpp::Priority::DEBUG << "TfConnection[" << this << "]::connectedCallback("
<< (unsigned)connect_reason << ") begin";
switch (connect_reason) {
case TfCallbackConnectReason::REQUEST:
logger << log4cpp::Priority::INFO << "TfConnection[" << this << "]: Connection established.";
break;
case TfCallbackConnectReason::RECONNECT:
logger << log4cpp::Priority::WARN << "TfConnection[" << this << "]: Connection reestablished.";
break;
default:
logger << log4cpp::Priority::FATAL << "TfConnection[" << this << "]: Unknown Connection Reason! What to do?";
break;
}
stateChange(TfConnectionState::CONNECTED);
try {
startDeviceQuery(0);
} catch (TfError& e) {
logger << log4cpp::Priority::FATAL << "TfConnection[" << this << "]: " << e.what();
}
notifyListeners([connect_reason, this](TfConnectionListener& l)->bool {
l.connectCallback(*this, connect_reason);
return true;
}
);
for (auto d : devices) {
d.second->signalConnectedCallback(connect_reason);
}
logger.debug("TfConnection::connectedCallback() end");
}
void TfConnection::disconnectedCallback(TfCallbackDisconnectReason disconnect_reason) {
logger << log4cpp::Priority::DEBUG << "TfConnection[" << this << "]::disconnectedCallback("
<< (unsigned)disconnect_reason << ") begin";
switch (disconnect_reason) {
case TfCallbackDisconnectReason::SHUTDOWN:
stateChange(TfConnectionState::CONNECTION_LOST);
logger << log4cpp::Priority::WARN << "TfConnection[" << this << "]: Connection shutdown by peer.";
break;
case TfCallbackDisconnectReason::REQUEST:
stateChange(TfConnectionState::DISCONNECTED);
logger << log4cpp::Priority::INFO << "TfConnection[" << this << "]: Connection shutdown by user request.";
break;
case TfCallbackDisconnectReason::ERROR:
stateChange(TfConnectionState::ERROR);
logger << log4cpp::Priority::FATAL << "TfConnection[" << this << "]: Connection Error! What to do?";
break;
default:
stateChange(TfConnectionState::ERROR);
logger << log4cpp::Priority::FATAL << "TfConnection[" << this << "]: Unknown Disconnection Reason! What to do?";
break;
}
notifyListeners([disconnect_reason, this](TfConnectionListener& l)->bool {
l.disconnectCallback(*this, disconnect_reason);
return true;
}
);
for (auto d : devices) {
d.second->signalDisconnectedCallback(disconnect_reason);
}
logger.debug("TfConnection::disconnectedCallback() end");
}
void TfConnection::startDeviceQuery(uint32_t usec) {
logger << log4cpp::Priority::DEBUG << "TfConnection::startDeviceQuery(" << usec << ") begin";
TfErrorCode errorCode;
if((errorCode = (TfErrorCode)ipcon_enumerate(&tfIpConnection)) != TfErrorCode::E_OK) {
std::ostringstream msg;
msg << "Error while trying to enumerate devices: " << (int)errorCode;
throw TfError(msg.str(), errorCode);
}
if (usec > 0) {
usleep(usec);
}
logger.debug("TfConnection::startDeviceQuery() end");
}
bool TfConnection::stateChange(TfConnectionState newstate) {
TfConnectionState oldstate = state;
state = newstate;
notifyListeners([oldstate, newstate, this](TfConnectionListener& l)->bool {
l.stateChangeCallback(*this, oldstate, newstate);
return true;
}
);
return true;
}
} /* namespace growcontrol */
+197
View File
@@ -0,0 +1,197 @@
/*
* TfAbstractDevice.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/TfDevice.h>
#include <tfcppsimple/TfException.h>
#include <sstream>
namespace tfcppsimple {
log4cpp::Category& TfDevice::logger = log4cpp::Category::getInstance("tfcppsimple.tfdevice");
TfDevice::TfDevice(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
uint16_t raw_device_identifier,
TfDeviceIdentifier device_identifier,
TfDevice* parent,
TfDeviceType deviceType
)
: connection(connection), uid(uid), connected_uid(connected_uid), position(position),
hardware_version(hardware_version), firmware_version(firmware_version),
raw_device_identifier(raw_device_identifier), device_identifier(device_identifier),
parent(parent), deviceType(deviceType) {
}
TfDevice::~TfDevice() {
}
bool TfDevice::connect() {
try {
changeDeviceState(TfDeviceState::CONNECTED);
} catch (TfException &e) {
logger << log4cpp::Priority::ERROR << getDeviceName(true) << ": exception during connect: " << e.what();
return false;
}
return true;
}
bool TfDevice::disconnect() {
try {
if (state == TfDeviceState::CONNECTED) {
changeDeviceState(TfDeviceState::AVAILABLE);
} else {
doUnconfigureDevice();
}
} catch (TfException &e) {
logger << log4cpp::Priority::ERROR << getDeviceName(true) << ": exception during disconnect: " << e.what();
return false;
}
return true;
}
bool TfDevice::changeDeviceState(TfDeviceState newstate) {
bool validFlag = false;
if (state != newstate) {
if (state == TfDeviceState::UNKNOWN && newstate == TfDeviceState::AVAILABLE) {
validFlag = true;
} else if (state == TfDeviceState::AVAILABLE && newstate == TfDeviceState::CONNECTED) {
try {
doConfigureDevice();
validFlag = true;
} catch (TfException &e) {
logger << log4cpp::Priority::ERROR << getDeviceName(true) << ": exception during state change: " << e.what();
}
} else if (state == TfDeviceState::AVAILABLE && (newstate == TfDeviceState::DISCONNECTED || newstate == TfDeviceState::CONNECTION_LOST)) {
validFlag = true;
} else if (state == TfDeviceState::CONNECTED && (newstate == TfDeviceState::DISCONNECTED || newstate == TfDeviceState::CONNECTION_LOST)) {
try {
doConnectionLost();
} catch (TfException &e) {
logger << log4cpp::Priority::ERROR << getDeviceName(true) << ": exception during state change: " << e.what();
}
validFlag = true;
} else if ((state == TfDeviceState::DISCONNECTED || state == TfDeviceState::CONNECTION_LOST) && newstate == TfDeviceState::AVAILABLE) {
validFlag = true;
} else if ((state == TfDeviceState::DISCONNECTED || state == TfDeviceState::CONNECTION_LOST) && newstate == TfDeviceState::CONNECTED) {
try {
doReconfigureDevice();
validFlag = true;
} catch (TfException &e) {
logger << log4cpp::Priority::ERROR << getDeviceName(true) << ": exception during state change: " << e.what();
}
} else {
logger << log4cpp::Priority::ERROR << getDeviceName(true) << ": invalid state changed from "
<< (int)state << " to "<< (int)newstate << ".";
}
if (validFlag) {
logger << log4cpp::Priority::DEBUG << getDeviceName(true) << ": state changed from "
<< (int)state << " to "<< (int)newstate << ".";
TfDeviceState oldstate = state;
state = newstate;
notifyListeners([oldstate, newstate, this](TfDeviceStateListener& l)->bool {
l.stateChangeCallback(*this, oldstate, newstate);
return true;
}
);
}
}
return validFlag;
}
void TfDevice::doConfigureDevice() {
configuredFlag = true;
}
void TfDevice::doReconfigureDevice() {
configuredFlag = true;
}
void TfDevice::doUnconfigureDevice() {
configuredFlag = false;
}
void TfDevice::doConnectionLost() {
}
void TfDevice::doShutdown() {
configuredFlag = false;
}
std::string TfDevice::getDeviceName(bool extended) {
std::ostringstream ss;
ss << "Unknown Device (" << raw_device_identifier << ")";
if (extended) {
ss << " [" << uid << "]";
}
return ss.str();
}
void TfDevice::signalEnumerationCallback(TfCallbackEnumerationType enumeration_type) {
if (enumeration_type == TfCallbackEnumerationType::DISCONNECTED) {
auto prio = log4cpp::Priority::INFO;
if (state == TfDeviceState::CONNECTED) {
prio = log4cpp::Priority::ERROR;
}
if (changeDeviceState(TfDeviceState::DISCONNECTED)) {
logger << prio << "Device \"" << getDeviceName(true) << "\" disconnected.";
}
} else {
switch (state) {
case TfDeviceState::AVAILABLE:
case TfDeviceState::CONNECTED:
// do nothing
break;
case TfDeviceState::DISCONNECTED:
case TfDeviceState::CONNECTION_LOST:
if (configuredFlag) {
if (changeDeviceState(TfDeviceState::CONNECTED)) {
logger << log4cpp::Priority::ERROR << "Device \"" << getDeviceName(true) << "\" reconnected.";
}
} else {
if (changeDeviceState(TfDeviceState::AVAILABLE)) {
logger << log4cpp::Priority::INFO << "Device \"" << getDeviceName(true) << "\" reconnected.";
}
}
break;
default:
break;
}
}
}
void TfDevice::signalConnectedCallback(TfCallbackConnectReason connect_reason) {}
void TfDevice::signalDisconnectedCallback(TfCallbackDisconnectReason disconnect_reason) {
auto prio = log4cpp::Priority::INFO;
if (state == TfDeviceState::CONNECTED) {
prio = log4cpp::Priority::ERROR;
}
if (changeDeviceState(TfDeviceState::CONNECTION_LOST)) {
logger << prio << "Connection to device \"" << getDeviceName(true) << "\" lost.";
}
}
void TfDevice::signalShutdown() {
notifyListeners([this](TfDeviceStateListener& l)->bool {
l.shutdownCallback(*this);
return true;
}
);
doShutdown();
}
} /* namespace tfcppsimple */
+97
View File
@@ -0,0 +1,97 @@
/*
* TfDeviceFactory.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/TfDeviceFactory.h>
#include <tfcppsimple/TfDevice.h>
#include <tfcppsimple/TfException.h>
#include <tfcppsimple/devices/TfMasterBrick.h>
#include <tfcppsimple/devices/TfHumidityBricklet.h>
#include <tfcppsimple/devices/TfIO16Bricklet.h>
#include <tfcppsimple/devices/TfLCD20x4Bricklet.h>
#include <tfcppsimple/devices/TfTemperatureBricklet.h>
#include <tfcppsimple/devices/TfUnknownBrick.h>
#include <tfcppsimple/devices/TfUnknownBricklet.h>
namespace tfcppsimple {
TfDevice* TfDeviceFactory::createNewDevice(
TfConnection& conn,
const char *uid,
const char *connected_uid,
char position,
uint8_t hardware_version[3],
uint8_t firmware_version[3],
uint16_t device_identifier,
TfDevice* parent) {
TfDevice* ret = nullptr;
switch ((TfDeviceIdentifier)device_identifier) {
case TfDeviceIdentifier::MASTER:
ret = new TfMasterBrick(conn, std::string(uid), std::string(connected_uid),
(TfDevicePosition)position,
{hardware_version[0], hardware_version[1], hardware_version[2]},
{firmware_version[0], firmware_version[1], firmware_version[2]},
parent);
ret->changeDeviceState(TfDeviceState::AVAILABLE);
break;
case TfDeviceIdentifier::TEMPERATURE:
ret = new TfTemperatureBricklet(conn, std::string(uid), std::string(connected_uid),
(TfDevicePosition)position,
{hardware_version[0], hardware_version[1], hardware_version[2]},
{firmware_version[0], firmware_version[1], firmware_version[2]},
parent);
ret->changeDeviceState(TfDeviceState::AVAILABLE);
break;
case TfDeviceIdentifier::HUMIDITY:
ret = new TfHumidityBricklet(conn, std::string(uid), std::string(connected_uid),
(TfDevicePosition)position,
{hardware_version[0], hardware_version[1], hardware_version[2]},
{firmware_version[0], firmware_version[1], firmware_version[2]},
parent);
ret->changeDeviceState(TfDeviceState::AVAILABLE);
break;
case TfDeviceIdentifier::LCD20x4:
ret = new TfLCD20x4Bricklet(conn, std::string(uid), std::string(connected_uid),
(TfDevicePosition)position,
{hardware_version[0], hardware_version[1], hardware_version[2]},
{firmware_version[0], firmware_version[1], firmware_version[2]},
parent);
ret->changeDeviceState(TfDeviceState::AVAILABLE);
break;
case TfDeviceIdentifier::IO16:
ret = new TfIO16Bricklet(conn, std::string(uid), std::string(connected_uid),
(TfDevicePosition)position,
{hardware_version[0], hardware_version[1], hardware_version[2]},
{firmware_version[0], firmware_version[1], firmware_version[2]},
parent);
ret->changeDeviceState(TfDeviceState::AVAILABLE);
break;
default:
if (position >= (uint16_t)TfDevicePosition::BRICK0 && position <= (uint16_t)TfDevicePosition::BRICK8) {
ret = new TfUnknownBrick(conn, std::string(uid), std::string(connected_uid),
(TfDevicePosition)position,
{hardware_version[0], hardware_version[1], hardware_version[2]},
{firmware_version[0], firmware_version[1], firmware_version[2]},
device_identifier, parent);
ret->changeDeviceState(TfDeviceState::AVAILABLE);
} else if (position >= (uint16_t)TfDevicePosition::BRICKLET0 && position <= (uint16_t)TfDevicePosition::BRICKLET3) {
ret = new TfUnknownBricklet(conn, std::string(uid), std::string(connected_uid),
(TfDevicePosition)position,
{hardware_version[0], hardware_version[1], hardware_version[2]},
{firmware_version[0], firmware_version[1], firmware_version[2]},
device_identifier, parent);
ret->changeDeviceState(TfDeviceState::AVAILABLE);
} else {
std::ostringstream ss;
ss << "Unknown device (" << device_identifier << ") at unknown position " << position;
throw TfException(ss.str());
}
break;
}
return ret;
}
} /* namespace tfcppsimple */
+36
View File
@@ -0,0 +1,36 @@
/*
* TfUtils.cpp
*
* Created on: 11.06.2014
* Author: matzman
*/
#include <tfcppsimple/TfUtils.h>
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/Appender.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/OstreamAppender.hh>
#include <iostream>
namespace tfcppsimple {
void TfUtils::configureLog4CPP(std::string configFile) {
try {
log4cpp::PropertyConfigurator::configure( configFile );
} catch( log4cpp::ConfigureFailure &e ) {
std::cerr << "[log4cpp::ConfigureFailure catched] while reading "
<< configFile << ": " << e.what() << std::endl;
configureLog4CPP();
}
}
void TfUtils::configureLog4CPP() {
log4cpp::Appender *appender = new log4cpp::OstreamAppender("console", &std::cerr);
appender->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.setPriority(log4cpp::Priority::INFO);
root.addAppender(appender);
}
} /* namespace tfcppsimple */
+4
View File
@@ -0,0 +1,4 @@
SRC_FILES += src/TfConnection.cpp src/TfUtils.cpp src/TfDevice.cpp src/TfBrick.cpp src/TfBricklet.cpp src/TfDeviceFactory.cpp
include src/devices/config.mk
+97
View File
@@ -0,0 +1,97 @@
/*
* TfHumidityBricklet.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/devices/TfHumidityBricklet.h>
#include <tfcppsimple/TfException.h>
namespace tfcppsimple {
TfHumidityBricklet::TfHumidityBricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
)
: TfBricklet(connection, uid, connected_uid, position, hardware_version, firmware_version,
(uint16_t)TfDeviceIdentifier::HUMIDITY, TfDeviceIdentifier::HUMIDITY, parent) {
}
TfHumidityBricklet::~TfHumidityBricklet() {
// TODO Auto-generated destructor stub
}
std::string TfHumidityBricklet::getDeviceName(bool extended) {
std::ostringstream ss;
ss << "Humidity Bricklet";
if (extended) {
ss << " [" << uid << "]";
}
return ss.str();
}
void TfHumidityBricklet::doConfigureDevice() {
humidity_create(&humidityObj, uid.c_str(), &connection.getTfIpConnection());
int ret = humidity_set_response_expected_all(&humidityObj, true);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while configuring: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
TfDevice::doConfigureDevice();
}
}
void TfHumidityBricklet::doReconfigureDevice() {
humidity_destroy(&humidityObj);
humidity_create(&humidityObj, uid.c_str(), &connection.getTfIpConnection());
int ret = humidity_set_response_expected_all(&humidityObj, true);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reconfiguring: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
TfDevice::doReconfigureDevice();
}
}
void TfHumidityBricklet::doConnectionLost() {
TfDevice::doConnectionLost();
}
void TfHumidityBricklet::doShutdown() {
humidity_destroy(&humidityObj);
TfDevice::doShutdown();
}
uint16_t TfHumidityBricklet::getHumidity() {
uint16_t retval;
int ret = humidity_get_humidity(&humidityObj, &retval);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reading humidity: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
return retval;
}
uint16_t TfHumidityBricklet::getHumidityRaw() {
uint16_t retval;
int ret = humidity_get_analog_value(&humidityObj, &retval);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reading raw humidity: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
return retval;
}
} /* namespace tfcppsimple */
+39
View File
@@ -0,0 +1,39 @@
/*
* TfIO16Bricklet.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/devices/TfIO16Bricklet.h>
namespace tfcppsimple {
TfIO16Bricklet::TfIO16Bricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
)
: TfBricklet(connection, uid, connected_uid, position, hardware_version, firmware_version,
(uint16_t)TfDeviceIdentifier::IO16, TfDeviceIdentifier::IO16, parent) {
}
TfIO16Bricklet::~TfIO16Bricklet() {
// TODO Auto-generated destructor stub
}
std::string TfIO16Bricklet::getDeviceName(bool extended) {
std::ostringstream ss;
ss << "IO16 Bricklet";
if (extended) {
ss << " [" << uid << "]";
}
return ss.str();
}
} /* namespace tfcppsimple */
+161
View File
@@ -0,0 +1,161 @@
/*
* TfLCD204Bricklet.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/devices/TfLCD20x4Bricklet.h>
#include <tfcppsimple/TfException.h>
namespace tfcppsimple {
TfLCD20x4Bricklet::TfLCD20x4Bricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
)
: TfBricklet(connection, uid, connected_uid, position, hardware_version, firmware_version,
(uint16_t)TfDeviceIdentifier::LCD20x4, TfDeviceIdentifier::LCD20x4, parent) {
}
TfLCD20x4Bricklet::~TfLCD20x4Bricklet() {
// TODO Auto-generated destructor stub
}
std::string TfLCD20x4Bricklet::getDeviceName(bool extended) {
std::ostringstream ss;
ss << "LCD 20x4 Bricklet";
if (extended) {
ss << " [" << uid << "]";
}
return ss.str();
}
void TfLCD20x4Bricklet::doConfigureDevice() {
lcd_20x4_create(&lcdObj, uid.c_str(), &connection.getTfIpConnection());
int ret = lcd_20x4_set_response_expected_all(&lcdObj, true);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while configuring: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
clearDisplay();
setBacklight(backlightFlag);
setCursor(cursorFlag, blinkingFlag);
lcd_20x4_register_callback(&lcdObj, LCD_20X4_CALLBACK_BUTTON_PRESSED, (void*)__buttonPressed, (void*)this);
lcd_20x4_register_callback(&lcdObj, LCD_20X4_CALLBACK_BUTTON_RELEASED, (void*)__buttonReleased, (void*)this);
TfDevice::doConfigureDevice();
}
}
void TfLCD20x4Bricklet::doReconfigureDevice() {
lcd_20x4_destroy(&lcdObj);
lcd_20x4_create(&lcdObj, uid.c_str(), &connection.getTfIpConnection());
int ret = lcd_20x4_set_response_expected_all(&lcdObj, true);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reconfiguring: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
setBacklight(backlightFlag);
setCursor(cursorFlag, blinkingFlag);
lcd_20x4_register_callback(&lcdObj, LCD_20X4_CALLBACK_BUTTON_PRESSED, (void*)__buttonPressed, (void*)this);
lcd_20x4_register_callback(&lcdObj, LCD_20X4_CALLBACK_BUTTON_RELEASED, (void*)__buttonReleased, (void*)this);
TfDevice::doReconfigureDevice();
}
}
void TfLCD20x4Bricklet::doConnectionLost() {
TfDevice::doConnectionLost();
}
void TfLCD20x4Bricklet::doShutdown() {
lcd_20x4_destroy(&lcdObj);
lcd_20x4_register_callback(&lcdObj, LCD_20X4_CALLBACK_BUTTON_PRESSED, nullptr, this);
lcd_20x4_register_callback(&lcdObj, LCD_20X4_CALLBACK_BUTTON_RELEASED, nullptr, this);
TfDevice::doShutdown();
}
void TfLCD20x4Bricklet::writeLine(uint8_t line, uint8_t position, std::string txt) {
int ret = lcd_20x4_write_line(&lcdObj, line, position, txt.c_str());
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while writing line: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
}
void TfLCD20x4Bricklet::clearDisplay() {
int ret = lcd_20x4_clear_display(&lcdObj);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while clearing display: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
}
void TfLCD20x4Bricklet::setBacklight(bool backlight) {
backlightFlag = backlight;
int ret;
if (backlightFlag) {
ret = lcd_20x4_backlight_on(&lcdObj);
} else {
ret = lcd_20x4_backlight_off(&lcdObj);
}
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while setting backlight: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
}
void TfLCD20x4Bricklet::setCursor(bool cursor, bool blinking) {
cursorFlag = cursor;
blinkingFlag = blinking;
int ret = lcd_20x4_set_config(&lcdObj, cursorFlag, blinkingFlag);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while setting cursor: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
}
void TfLCD20x4Bricklet::__buttonPressed(uint8_t button, void *user_data) {
if (user_data != nullptr) {
((TfLCD20x4Bricklet*)user_data)->buttonStateCallBack(button, true);
}
}
void TfLCD20x4Bricklet::__buttonReleased(uint8_t button, void *user_data) {
if (user_data != nullptr) {
((TfLCD20x4Bricklet*)user_data)->buttonStateCallBack(button, false);
}
}
void TfLCD20x4Bricklet::buttonStateCallBack(uint8_t button, bool state) {
lcdListeners.notifyListeners([button, state, this](TfLCD20x4Listener& l)->bool {
l.buttonStateCallback(*this, button, state);
return true;
}
);
}
bool TfLCD20x4Bricklet::addLcdListener(TfLCD20x4Listener& listener) {
return lcdListeners.addListener(listener);
}
bool TfLCD20x4Bricklet::removeLcdListener(TfLCD20x4Listener& listener) {
return lcdListeners.removeListener(listener);
}
} /* namespace tfcppsimple */
+121
View File
@@ -0,0 +1,121 @@
/*
* TfMasterBrick.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/devices/TfMasterBrick.h>
#include <tfcppsimple/TfException.h>
#include <sstream>
namespace tfcppsimple {
TfMasterBrick::TfMasterBrick(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
)
: TfBrick(connection, uid, connected_uid, position, hardware_version, firmware_version,
(uint16_t)TfDeviceIdentifier::MASTER, TfDeviceIdentifier::MASTER, parent) {
}
TfMasterBrick::~TfMasterBrick() {
// TODO Auto-generated destructor stub
}
std::string TfMasterBrick::getDeviceName(bool extended) {
std::ostringstream ss;
ss << "Master Brick";
if (extended) {
ss << " [" << uid << "]";
}
return ss.str();
}
void TfMasterBrick::doConfigureDevice() {
master_create(&masterObj, uid.c_str(), &connection.getTfIpConnection());
int ret = master_set_response_expected_all(&masterObj, true);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while configuring: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
TfDevice::doConfigureDevice();
}
}
void TfMasterBrick::doReconfigureDevice() {
master_destroy(&masterObj);
master_create(&masterObj, uid.c_str(), &connection.getTfIpConnection());
int ret = master_set_response_expected_all(&masterObj, true);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reconfiguring: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
TfDevice::doReconfigureDevice();
}
}
void TfMasterBrick::doConnectionLost() {
TfDevice::doConnectionLost();
}
void TfMasterBrick::doShutdown() {
master_destroy(&masterObj);
TfDevice::doShutdown();
}
uint16_t TfMasterBrick::getStackVoltage() {
uint16_t ret_voltage;
int ret = master_get_stack_voltage(&masterObj, &ret_voltage);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reading stack voltage: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
return ret_voltage;
}
uint16_t TfMasterBrick::getStackCurrent() {
uint16_t ret_current;
int ret = master_get_stack_current(&masterObj, &ret_current);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reading stack current: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
return ret_current;
}
uint16_t TfMasterBrick::getUSBVoltage() {
uint16_t ret_voltage;
int ret = master_get_usb_voltage(&masterObj, &ret_voltage);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reading usb voltage: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
return ret_voltage;
}
int16_t TfMasterBrick::getChipTemperature(){
int16_t ret_temperature;
int ret = master_get_chip_temperature(&masterObj, &ret_temperature);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reading chip temperature: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
return ret_temperature;
}
} /* namespace tfcppsimple */
+101
View File
@@ -0,0 +1,101 @@
/*
* TfTemperatureBricklet.cpp
*
* Created on: 12.06.2014
* Author: matzman
*/
#include <tfcppsimple/devices/TfTemperatureBricklet.h>
#include <tfcppsimple/TfException.h>
namespace tfcppsimple {
TfTemperatureBricklet::TfTemperatureBricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
)
: TfBricklet(connection, uid, connected_uid, position, hardware_version, firmware_version,
(uint16_t)TfDeviceIdentifier::TEMPERATURE, TfDeviceIdentifier::TEMPERATURE, parent) {
}
TfTemperatureBricklet::~TfTemperatureBricklet() {
// TODO Auto-generated destructor stub
}
std::string TfTemperatureBricklet::getDeviceName(bool extended) {
std::ostringstream ss;
ss << "Temperature Bricklet";
if (extended) {
ss << " [" << uid << "]";
}
return ss.str();
}
void TfTemperatureBricklet::doConfigureDevice() {
temperature_create(&temperatureObj, uid.c_str(), &connection.getTfIpConnection());
int ret = temperature_set_response_expected_all(&temperatureObj, true);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while configuring: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
TfDevice::doConfigureDevice();
}
}
void TfTemperatureBricklet::doReconfigureDevice() {
temperature_destroy(&temperatureObj);
temperature_create(&temperatureObj, uid.c_str(), &connection.getTfIpConnection());
int ret = temperature_set_response_expected_all(&temperatureObj, true);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reconfiguring: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
TfDevice::doReconfigureDevice();
}
}
void TfTemperatureBricklet::doConnectionLost() {
TfDevice::doConnectionLost();
}
void TfTemperatureBricklet::doShutdown() {
temperature_destroy(&temperatureObj);
TfDevice::doShutdown();
}
int16_t TfTemperatureBricklet::getTemperature() {
int16_t retval;
int ret = temperature_get_temperature(&temperatureObj, &retval);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while reading temperature: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
}
return retval;
}
void TfTemperatureBricklet::setI2Cmode(TfTemperatureI2Cmode mode) {
int ret = temperature_set_i2c_mode(&temperatureObj, (uint8_t)mode);
if (ret != E_OK) {
std::ostringstream ss;
ss << "Error while setting i2c mode: " << ret << ".";
throw TfError(ss.str(), (TfErrorCode)ret);
} else {
i2cMode = mode;
}
}
TfTemperatureI2Cmode TfTemperatureBricklet::getI2Cmode() {
return i2cMode;
}
} /* namespace tfcppsimple */
+40
View File
@@ -0,0 +1,40 @@
/*
* TfUnknownBrick.cpp
*
* Created on: 13.06.2014
* Author: matzman
*/
#include <tfcppsimple/devices/TfUnknownBrick.h>
namespace tfcppsimple {
TfUnknownBrick::TfUnknownBrick(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
uint16_t raw_device_identifier,
TfDevice* parent
)
: TfBrick(connection, uid, connected_uid, position, hardware_version, firmware_version,
raw_device_identifier, TfDeviceIdentifier::UNKNOWN_BRICK, parent) {
}
TfUnknownBrick::~TfUnknownBrick() {
// TODO Auto-generated destructor stub
}
std::string TfUnknownBrick::getDeviceName(bool extended) {
std::ostringstream ss;
ss << "Unknown Brick (" << raw_device_identifier << ")";
if (extended) {
ss << " [" << uid << "]";
}
return ss.str();
}
} /* namespace tfcppsimple */
+40
View File
@@ -0,0 +1,40 @@
/*
* TfUnknownBricklet.cpp
*
* Created on: 13.06.2014
* Author: matzman
*/
#include <tfcppsimple/devices/TfUnknownBricklet.h>
namespace tfcppsimple {
TfUnknownBricklet::TfUnknownBricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
uint16_t raw_device_identifier,
TfDevice* parent
)
: TfBricklet(connection, uid, connected_uid, position, hardware_version, firmware_version,
raw_device_identifier, TfDeviceIdentifier::UNKNOWN_BRICKLET, parent) {
}
TfUnknownBricklet::~TfUnknownBricklet() {
// TODO Auto-generated destructor stub
}
std::string TfUnknownBricklet::getDeviceName(bool extended) {
std::ostringstream ss;
ss << "Unknown Bricklet (" << raw_device_identifier << ")";
if (extended) {
ss << " [" << uid << "]";
}
return ss.str();
}
} /* namespace tfcppsimple */
+9
View File
@@ -0,0 +1,9 @@
SRC_FILES += src/devices/TfMasterBrick.cpp \
src/devices/TfHumidityBricklet.cpp \
src/devices/TfIO16Bricklet.cpp \
src/devices/TfLCD20x4Bricklet.cpp \
src/devices/TfTemperatureBricklet.cpp \
src/devices/TfUnknownBrick.cpp \
src/devices/TfUnknownBricklet.cpp