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
+25
View File
@@ -0,0 +1,25 @@
/*
* TfCPPsimple.h
*
* Created on: 11.06.2014
* Author: matzman
*/
#ifndef TFCPPSIMPLE_H_
#define TFCPPSIMPLE_H_
#include <tfcppsimple/TfException.h>
#include <tfcppsimple/TfConnection.h>
#include <tfcppsimple/TfUtils.h>
#include <tfcppsimple/TfDevice.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>
#endif /* TFCPPSIMPLE_H_ */
+47
View File
@@ -0,0 +1,47 @@
/*
* ObservableTemplate.h
*
* Created on: 11.06.2014
* Author: matzman
*/
#ifndef OBSERVABLETEMPLATE_H_
#define OBSERVABLETEMPLATE_H_
#include <set>
#include <mutex>
#include <functional>
#include <cstdint>
namespace tfcppsimple {
template<class C>
class ObservableTemplate {
protected:
std::mutex observableLock;
std::set<C*> listeners;
public:
bool addListener(C& listener) {
std::lock_guard<std::mutex> lock(observableLock);
return listeners.insert(&listener).second;
}
bool removeListener(C& listener) {
std::lock_guard<std::mutex> lock(observableLock);
return listeners.erase(&listener) == 1;
}
uint32_t notifyListeners(std::function<bool(C& listener)> visitor) {
std::lock_guard<std::mutex> lock(observableLock);
uint32_t count = 0;
for (auto l : listeners) {
count++;
if (!visitor(*l)) break;
}
return count;
}
};
}
#endif /* OBSERVABLETEMPLATE_H_ */
+32
View File
@@ -0,0 +1,32 @@
/*
* TfBrick.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFBRICK_H_
#define TFBRICK_H_
#include <tfcppsimple/TfDevice.h>
namespace tfcppsimple {
class TfBrick: public TfDevice {
public:
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
);
virtual ~TfBrick();
};
} /* namespace tfcppsimple */
#endif /* TFBRICK_H_ */
+32
View File
@@ -0,0 +1,32 @@
/*
* TfBricklet.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFBRICKLET_H_
#define TFBRICKLET_H_
#include <tfcppsimple/TfDevice.h>
namespace tfcppsimple {
class TfBricklet: public TfDevice {
public:
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
);
virtual ~TfBricklet();
};
} /* namespace tfcppsimple */
#endif /* TFBRICKLET_H_ */
+127
View File
@@ -0,0 +1,127 @@
/*
* TFConnection.h
*
* Created on: 10.06.2014
* Author: matzman
*/
#ifndef TFCONNECTION_H_
#define TFCONNECTION_H_
#include <tfcppsimple/TfTypes.h>
#include <tfcppsimple/ObservableTemplate.h>
#include <tfcppsimple/TfDevice.h>
#include <log4cpp/Category.hh>
#include <ip_connection.h>
#include <string>
#include <mutex>
#include <map>
#include <vector>
namespace tfcppsimple {
class TfDevice;
class TfConnection;
enum class TfConnectionState : int {
NOTCONFIGURED = 0,
PENDING = 1,
CONNECTED = 2,
CONNECTION_LOST = 3,
DISCONNECTED = 4,
ERROR = 5
};
class TfConnectionListener {
public:
virtual ~TfConnectionListener() {}
virtual void stateChangeCallback(TfConnection& conn, TfConnectionState oldstate, TfConnectionState newstate) {}
virtual void connectCallback(TfConnection& conn, TfCallbackConnectReason reason) {}
virtual void disconnectCallback(TfConnection& conn, TfCallbackDisconnectReason reason) {}
virtual void newDeviceCallback(TfConnection& conn, TfDevice& device) {}
virtual void shutdownCallback(TfConnection& conn) {}
};
class TfConnection : public ObservableTemplate<TfConnectionListener> {
public:
private:
static log4cpp::Category& logger;
static void __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);
static void __connectedCallback(uint8_t connect_reason, void *user_data);
static void __disconnectedCallback(uint8_t disconnect_reason, void *user_data);
void 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);
void connectedCallback(TfCallbackConnectReason connect_reason);
void disconnectedCallback(TfCallbackDisconnectReason disconnect_reason);
bool stateChange(TfConnectionState newstate);
std::mutex objLock;
IPConnection tfIpConnection;
std::string host;
uint16_t port = 0;
bool okFlag = false;
TfConnectionState state = TfConnectionState::NOTCONFIGURED;
std::map<std::string, TfDevice*> devices;
std::vector<TfDevice*> rootDevices;
public:
TfConnection();
virtual ~TfConnection();
bool connect(std::string host = "localhost", uint16_t port = 4223,
bool autoReconnect = true, uint32_t timeout = 2500);
bool disconnect();
IPConnection& getTfIpConnection() { return tfIpConnection; }
const std::map<std::string, TfDevice*>& getDeviceMap() { return devices; }
const std::vector<TfDevice*>& getRootDevices() { return rootDevices; }
bool isOk() { return okFlag; }
bool isAutoReconnect() { return ipcon_get_auto_reconnect(&tfIpConnection); }
void setAutoReconnect(bool flag) { return ipcon_set_auto_reconnect(&tfIpConnection, flag); }
uint32_t getResponseTimeout() { return ipcon_get_timeout(&tfIpConnection); }
void setResponseTimeout(uint32_t timeout) { ipcon_set_timeout(&tfIpConnection, timeout); }
TfConnectionState getStatus() { return state; }
void startDeviceQuery(uint32_t usec);
void wait() { ipcon_wait(&tfIpConnection); }
void unwait() { ipcon_unwait(&tfIpConnection); }
const std::string& getHost() { return host; }
uint16_t getPort() { return port; }
};
} /* namespace growcontrol */
#endif /* TFCONNECTION_H_ */
+198
View File
@@ -0,0 +1,198 @@
/*
* TfAbstractDevice.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFABSTRACTDEVICE_H_
#define TFABSTRACTDEVICE_H_
#include <tfcppsimple/TfTypes.h>
#include <tfcppsimple/TfConnection.h>
#include <log4cpp/Category.hh>
#include <cstdint>
#include <string>
#include <map>
namespace tfcppsimple {
class TfConnection;
class TfDevice;
enum class TfDevicePosition : char {
//'0' - '8' (Position in Stapel). Für Bricklets: 'a' - 'd' (Position an Brick).
BRICK0 = '0',
BRICK1 = '1',
BRICK2 = '2',
BRICK3 = '3',
BRICK4 = '4',
BRICK5 = '5',
BRICK6 = '6',
BRICK7 = '7',
BRICK8 = '8',
BRICKLET0 = 'a',
BRICKLET1 = 'b',
BRICKLET2 = 'c',
BRICKLET3 = 'd',
};
enum class TfDeviceIdentifier : uint16_t {
UNKNOWN = 0,
UNKNOWN_BRICK = 0,
UNKNOWN_BRICKLET = 0,
MASTER = 13,
LCD20x4 = 212,
HUMIDITY = 27,
IO16 = 28,
TEMPERATURE = 216
};
enum class TfDeviceType : int {
UNKNOWN = 0,
BRICK = 1,
BRICKLET = 2
};
enum class TfDeviceState : int {
UNKNOWN,
AVAILABLE,
CONNECTED,
DISCONNECTED,
CONNECTION_LOST
};
struct TfVersion {
uint8_t _major;
uint8_t _minor;
uint8_t _release;
} __attribute__((packed));
class TfDeviceStateListener {
public:
virtual ~TfDeviceStateListener() {}
virtual void stateChangeCallback(TfDevice& device, TfDeviceState oldstate, TfDeviceState newstate) {}
virtual void shutdownCallback(TfDevice& device) {}
};
class TfDevice : public ObservableTemplate<TfDeviceStateListener> {
friend class TfDeviceFactory;
friend class TfConnection;
public:
protected:
static log4cpp::Category& logger;
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;
std::map<TfDevicePosition, TfDevice*> children;
TfDeviceType deviceType;
TfDeviceState state = TfDeviceState::UNKNOWN;
bool configuredFlag = false;
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
);
virtual bool changeDeviceState(TfDeviceState newstate);
virtual void doConfigureDevice();
virtual void doReconfigureDevice();
virtual void doUnconfigureDevice();
virtual void doConnectionLost();
virtual void doShutdown();
void signalEnumerationCallback(TfCallbackEnumerationType enumeration_type);
void signalConnectedCallback(TfCallbackConnectReason connect_reason);
void signalDisconnectedCallback(TfCallbackDisconnectReason disconnect_reason);
void signalShutdown();
public:
virtual ~TfDevice();
bool connect();
bool disconnect();
const std::string& getConnectedUid() {
return connected_uid;
}
TfDeviceIdentifier getDeviceIdentifier() {
return device_identifier;
}
const TfVersion& getFirmwareVersion() {
return firmware_version;
}
const TfVersion& getHardwareVersion() {
return hardware_version;
}
TfDevicePosition getPosition() {
return position;
}
uint16_t getRawDeviceIdentifier() {
return raw_device_identifier;
}
const std::string& getUid() {
return uid;
}
const std::map<TfDevicePosition, TfDevice*>& getChildren() {
return children;
}
TfDevice* getChild(TfDevicePosition pos) {
auto ret = children.find(pos);
return ret == children.end() ? nullptr : ret->second;
}
TfDevice* getParent() {
return parent;
}
TfDeviceType getDeviceType() {
return deviceType;
}
TfConnection& getConnection() {
return connection;
}
TfDeviceState getDeviceState() { return state; }
bool isConfigured() { return configuredFlag; }
virtual std::string getDeviceName(bool extended = true);
};
} /* namespace tfcppsimple */
#endif /* TFABSTRACTDEVICE_H_ */
+33
View File
@@ -0,0 +1,33 @@
/*
* DeviceFactory.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFDEVICEFACTORY_H_
#define TFDEVICEFACTORY_H_
#include <cstdint>
namespace tfcppsimple {
class TfDevice;
class TfConnection;
class TfDeviceFactory {
public:
static TfDevice* 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);
};
} /* namespace tfcppsimple */
#endif /* DEVICEFACTORY_H_ */
+42
View File
@@ -0,0 +1,42 @@
/*
* TfException.h
*
* Created on: 11.06.2014
* Author: matzman
*/
#ifndef TFEXCEPTION_H_
#define TFEXCEPTION_H_
#include <tfcppsimple/TfTypes.h>
#include <exception>
#include <stdexcept>
#include <string>
namespace tfcppsimple {
class TfException : public std::runtime_error {
public:
TfException(std::string message)
: std::runtime_error(message) {}
};
class TfError : public TfException {
private:
TfErrorCode errorCode;
public:
TfError(std::string message, TfErrorCode code)
: TfException(message), errorCode(code) {}
TfErrorCode getErrorCode() { return errorCode; }
};
} // end namespace tfcppsimple
#endif /* TFEXCEPTION_H_ */
+53
View File
@@ -0,0 +1,53 @@
/*
* TfTypes.h
*
* Created on: 11.06.2014
* Author: matzman
*/
#ifndef TFTYPES_H_
#define TFTYPES_H_
#include <cstdint>
namespace tfcppsimple {
enum class TfErrorCode : int {
E_OK = 0,
E_TIMEOUT = -1,
E_NO_STREAM_SOCKET = -2,
E_HOSTNAME_INVALID = -3,
E_NO_CONNECT = -4,
E_NO_THREAD = -5,
E_NOT_ADDED = -6, // (seit Bindings Version 2.0.0 nicht mehr verwendet)
E_ALREADY_CONNECTED = -7,
E_NOT_CONNECTED = -8,
E_INVALID_PARAMETER = -9,
E_NOT_SUPPORTED = -10,
E_UNKNOWN_ERROR_CODE = -11
};
enum class TfCallbackConnectReason : uint8_t {
REQUEST = 0, // Verbindung aufgebaut nach Anfrage vom Benutzer.
RECONNECT = 1 // Verbindung aufgebaut nach durch Auto-Reconnect.
};
enum class TfCallbackDisconnectReason : uint8_t {
REQUEST =0, // Trennung wurde vom Benutzer angefragt.
ERROR = 1, // Trennung aufgrund eines unlösbaren Problems.
SHUTDOWN = 2, // Trennung wurde vom Brick Daemon oder WIFI/Ethernet Extension eingeleitet.
};
enum class TfCallbackEnumerationType : uint8_t {
AVAILABLE = 0, // Gerät ist verfügbar (Enumerierung vom Benutzer ausgelöst).
CONNECTED = 1, // Gerät wurde neu verbunden (Automatisch vom Brick gesendet nachdem die Kommunikation aufgebaut wurde). Dies kann bedeuten, dass das Gerät die vorher eingestellte Konfiguration verloren hat und neu konfiguriert werden muss.
DISCONNECTED = 2, // Gerät wurde getrennt (Nur bei USB-Verbindungen möglich). In diesem Fall haben nur uid und enumeration_type einen gültigen Wert.
};
} // end namespace tfcppsimple
#endif /* TFTYPES_H_ */
+23
View File
@@ -0,0 +1,23 @@
/*
* TfUtils.h
*
* Created on: 11.06.2014
* Author: matzman
*/
#ifndef TFUTILS_H_
#define TFUTILS_H_
#include <string>
namespace tfcppsimple {
class TfUtils {
public:
static void configureLog4CPP(std::string configFile);
static void configureLog4CPP(); // Default Config
};
} /* namespace tfcppsimple */
#endif /* TFUTILS_H_ */
@@ -0,0 +1,47 @@
/*
* TfHumidityBricklet.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFHUMIDITYBRICKLET_H_
#define TFHUMIDITYBRICKLET_H_
#include <tfcppsimple/TfBricklet.h>
#include <bricklet_humidity.h>
namespace tfcppsimple {
class TfHumidityBricklet: public TfBricklet {
friend class TfDeviceFactory;
private:
Humidity humidityObj;
TfHumidityBricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
);
virtual void doConfigureDevice();
virtual void doReconfigureDevice();
virtual void doConnectionLost();
virtual void doShutdown();
public:
virtual ~TfHumidityBricklet();
virtual std::string getDeviceName(bool extended = true);
uint16_t getHumidity();
uint16_t getHumidityRaw();
};
} /* namespace tfcppsimple */
#endif /* TFHUMIDITYBRICKLET_H_ */
@@ -0,0 +1,32 @@
/*
* TfIO16Bricklet.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFIO16BRICKLET_H_
#define TFIO16BRICKLET_H_
#include <tfcppsimple/TfBricklet.h>
namespace tfcppsimple {
class TfIO16Bricklet: public TfBricklet {
public:
TfIO16Bricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
);
virtual ~TfIO16Bricklet();
virtual std::string getDeviceName(bool extended = true);
};
} /* namespace tfcppsimple */
#endif /* TFIO16BRICKLET_H_ */
@@ -0,0 +1,74 @@
/*
* TfLCD204Bricklet.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFLCD20X4BRICKLET_H_
#define TFLCD20X4BRICKLET_H_
#include <tfcppsimple/TfBricklet.h>
#include <bricklet_lcd_20x4.h>
namespace tfcppsimple {
class TfLCD20x4Bricklet;
class TfLCD20x4Listener {
public:
virtual ~TfLCD20x4Listener() {}
virtual void buttonStateCallback(TfLCD20x4Bricklet& lcd, uint8_t button, bool state) {}
};
class TfLCD20x4Bricklet: public TfBricklet {
friend class TfDeviceFactory;
private:
LCD20x4 lcdObj;
bool backlightFlag = false;
bool cursorFlag = false;
bool blinkingFlag = false;
ObservableTemplate<TfLCD20x4Listener> lcdListeners;
TfLCD20x4Bricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
);
virtual void doConfigureDevice();
virtual void doReconfigureDevice();
virtual void doConnectionLost();
virtual void doShutdown();
static void __buttonPressed(uint8_t button, void *user_data);
static void __buttonReleased(uint8_t button, void *user_data);
void buttonStateCallBack(uint8_t button, bool state);
public:
virtual ~TfLCD20x4Bricklet();
virtual std::string getDeviceName(bool extended = true);
void writeLine(uint8_t line, uint8_t position, std::string txt);
void clearDisplay();
void setBacklight(bool backlight);
bool getBacklight() { return false; }
void setCursor(bool cursor, bool blinking = false);
bool addLcdListener(TfLCD20x4Listener& listener);
bool removeLcdListener(TfLCD20x4Listener& listener);
};
} /* namespace tfcppsimple */
#endif /* TFLCD20X4BRICKLET_H_ */
@@ -0,0 +1,51 @@
/*
* TfMasterBrick.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFMASTERBRICK_H_
#define TFMASTERBRICK_H_
#include <brick_master.h>
#include <tfcppsimple/TfBrick.h>
namespace tfcppsimple {
class TfMasterBrick: public TfBrick {
friend class TfDeviceFactory;
private:
Master masterObj;
TfMasterBrick(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
);
virtual void doConfigureDevice();
virtual void doReconfigureDevice();
virtual void doConnectionLost();
virtual void doShutdown();
public:
virtual ~TfMasterBrick();
virtual std::string getDeviceName(bool extended = true);
uint16_t getStackVoltage();
uint16_t getStackCurrent();
uint16_t getUSBVoltage();
int16_t getChipTemperature();
};
} /* namespace tfcppsimple */
#endif /* TFMASTERBRICK_H_ */
@@ -0,0 +1,54 @@
/*
* TfTemperatureBricklet.h
*
* Created on: 12.06.2014
* Author: matzman
*/
#ifndef TFTEMPERATUREBRICKLET_H_
#define TFTEMPERATUREBRICKLET_H_
#include <tfcppsimple/TfBricklet.h>
#include <bricklet_temperature.h>
namespace tfcppsimple {
enum class TfTemperatureI2Cmode : uint8_t {
FAST = 0, // Fast (400kHz, default)
SLOW = 1 // Slow (100kHz)
};
class TfTemperatureBricklet: public TfBricklet {
friend class TfDeviceFactory;
private:
Temperature temperatureObj;
TfTemperatureI2Cmode i2cMode = TfTemperatureI2Cmode::FAST;
TfTemperatureBricklet(TfConnection& connection,
std::string uid,
std::string connected_uid,
TfDevicePosition position,
TfVersion hardware_version,
TfVersion firmware_version,
TfDevice* parent
);
virtual void doConfigureDevice();
virtual void doReconfigureDevice();
virtual void doConnectionLost();
virtual void doShutdown();
public:
virtual ~TfTemperatureBricklet();
virtual std::string getDeviceName(bool extended = true);
int16_t getTemperature();
void setI2Cmode(TfTemperatureI2Cmode mode);
TfTemperatureI2Cmode getI2Cmode();
};
} /* namespace tfcppsimple */
#endif /* TFTEMPERATUREBRICKLET_H_ */
@@ -0,0 +1,36 @@
/*
* TfUnknownBrick.h
*
* Created on: 13.06.2014
* Author: matzman
*/
#ifndef TFUNKNOWNBRICK_H_
#define TFUNKNOWNBRICK_H_
#include <tfcppsimple/TfBrick.h>
namespace tfcppsimple {
class TfUnknownBrick: public TfBrick {
friend class TfDeviceFactory;
private:
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
);
public:
virtual ~TfUnknownBrick();
virtual std::string getDeviceName(bool extended = true);
};
} /* namespace tfcppsimple */
#endif /* TFUNKNOWNBRICK_H_ */
@@ -0,0 +1,36 @@
/*
* TfUnknownBricklet.h
*
* Created on: 13.06.2014
* Author: matzman
*/
#ifndef TFUNKNOWNBRICKLET_H_
#define TFUNKNOWNBRICKLET_H_
#include <tfcppsimple/TfBricklet.h>
namespace tfcppsimple {
class TfUnknownBricklet: public TfBricklet {
friend class TfDeviceFactory;
private:
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
);
public:
virtual ~TfUnknownBricklet();
virtual std::string getDeviceName(bool extended = true);
};
} /* namespace tfcppsimple */
#endif /* TFUNKNOWNBRICKLET_H_ */