commit 1768f7b41cf535509160fbeb92ba7fc6c11d8945 Author: matzman Date: Mon Jun 16 11:16:21 2014 +0200 Initial commit. diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..b1d5b49 --- /dev/null +++ b/.cproject @@ -0,0 +1,203 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + make + + all + true + true + true + + + make + + clean + true + true + true + + + make + + distclean + true + true + true + + + make + + log4cpp-dist + true + true + true + + + make + + tinkerforge-dist + true + true + true + + + make + + log4cpp-distclean + true + true + true + + + make + + tinkerforge-distclean + true + true + true + + + make + + all + true + true + true + + + make + + clean + true + true + true + + + make + tinkerforge-dist + true + true + true + + + make + tinkerforge-distclean + true + true + true + + + make + log4cpp-dist + true + true + true + + + make + log4cpp-distclean + true + true + true + + + make + all + true + true + true + + + make + clean + true + true + true + + + + diff --git a/.hgignore b/.hgignore new file mode 100644 index 0000000..fc56b47 --- /dev/null +++ b/.hgignore @@ -0,0 +1,7 @@ +syntax: regexp + +^bin/ +^build/ +^lib/ +^third-party/log4cpp/ +^third-party/tinkerforge/ diff --git a/.project b/.project new file mode 100644 index 0000000..c3eb2c4 --- /dev/null +++ b/.project @@ -0,0 +1,27 @@ + + + TinkerforgeCPPsimple + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml new file mode 100644 index 0000000..b53e2e1 --- /dev/null +++ b/.settings/language.settings.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..485b6c0 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ + +include Makefile.local + +SRC_FILES = + +include src/config.mk + +OBJS = $(SRC_FILES:src/%.cpp=build/%.o) + +all : lib/libtfcppsimple.a + +lib/libtfcppsimple.a : lib $(OBJS) + mkdir -p lib + $(AR) rc $@ $(OBJS) + $(RANLIB) $@ + +$(OBJS) : build/%.o : src/%.cpp + mkdir -p $(dir $@) + $(CXX) $(CXX_FLAGS) -I include -I third-party/tinkerforge/include -I third-party/log4cpp/include -c $< -o $@ + +clean : + rm -rf build + +distclean : clean + rm -rf lib/libtfcppsimple.a diff --git a/Makefile.local b/Makefile.local new file mode 100644 index 0000000..05ffb0d --- /dev/null +++ b/Makefile.local @@ -0,0 +1,28 @@ + + + +CXX_FLAGS = -O0 -g3 -Wall -std=c++11 + +TINKERFORGE_CXX_FLAGS = + + + +GCC_HOME = +GCC_PREFIX = + +GCC = ${GCC_PREFIX}-gcc +CXX = ${GCC_PREFIX}-g++ +CPP = ${GCC_PREFIX}-cpp +AR = ${GCC_PREFIX}-ar +OBJDUMP = ${GCC_PREFIX}-objdump +RANLIB = ${GCC_PREFIX}-ranlib +SIZE = ${GCC_PREFIX}-size + +#GCC = ${GCC_HOME}/bin/${GCC_PREFIX}-gcc +#CXX = ${GCC_HOME}/bin/${GCC_PREFIX}-g++ +#CPP = ${GCC_HOME}/bin/${GCC_PREFIX}-cpp +#AR = ${GCC_HOME}/bin/${GCC_PREFIX}-ar +#OBJDUMP = ${GCC_HOME}/bin/${GCC_PREFIX}-objdump +#RANLIB = ${GCC_HOME}/bin/${GCC_PREFIX}-ranlib +#SIZE = ${GCC_HOME}/bin/${GCC_PREFIX}-size + diff --git a/include/TfCPPsimple.h b/include/TfCPPsimple.h new file mode 100644 index 0000000..5bdbcbf --- /dev/null +++ b/include/TfCPPsimple.h @@ -0,0 +1,25 @@ +/* + * TfCPPsimple.h + * + * Created on: 11.06.2014 + * Author: matzman + */ + +#ifndef TFCPPSIMPLE_H_ +#define TFCPPSIMPLE_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +#endif /* TFCPPSIMPLE_H_ */ diff --git a/include/tfcppsimple/ObservableTemplate.h b/include/tfcppsimple/ObservableTemplate.h new file mode 100644 index 0000000..6f22e60 --- /dev/null +++ b/include/tfcppsimple/ObservableTemplate.h @@ -0,0 +1,47 @@ +/* + * ObservableTemplate.h + * + * Created on: 11.06.2014 + * Author: matzman + */ + +#ifndef OBSERVABLETEMPLATE_H_ +#define OBSERVABLETEMPLATE_H_ + +#include +#include +#include +#include + +namespace tfcppsimple { + +template +class ObservableTemplate { +protected: + std::mutex observableLock; + std::set listeners; +public: + bool addListener(C& listener) { + std::lock_guard lock(observableLock); + return listeners.insert(&listener).second; + } + bool removeListener(C& listener) { + std::lock_guard lock(observableLock); + return listeners.erase(&listener) == 1; + } + uint32_t notifyListeners(std::function visitor) { + std::lock_guard lock(observableLock); + uint32_t count = 0; + for (auto l : listeners) { + count++; + if (!visitor(*l)) break; + } + return count; + } +}; + +} + + + +#endif /* OBSERVABLETEMPLATE_H_ */ diff --git a/include/tfcppsimple/TfBrick.h b/include/tfcppsimple/TfBrick.h new file mode 100644 index 0000000..d28abf8 --- /dev/null +++ b/include/tfcppsimple/TfBrick.h @@ -0,0 +1,32 @@ +/* + * TfBrick.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFBRICK_H_ +#define TFBRICK_H_ + +#include + +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_ */ diff --git a/include/tfcppsimple/TfBricklet.h b/include/tfcppsimple/TfBricklet.h new file mode 100644 index 0000000..fc82fb5 --- /dev/null +++ b/include/tfcppsimple/TfBricklet.h @@ -0,0 +1,32 @@ +/* + * TfBricklet.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFBRICKLET_H_ +#define TFBRICKLET_H_ + +#include + +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_ */ diff --git a/include/tfcppsimple/TfConnection.h b/include/tfcppsimple/TfConnection.h new file mode 100644 index 0000000..7eef44e --- /dev/null +++ b/include/tfcppsimple/TfConnection.h @@ -0,0 +1,127 @@ +/* + * TFConnection.h + * + * Created on: 10.06.2014 + * Author: matzman + */ + +#ifndef TFCONNECTION_H_ +#define TFCONNECTION_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +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 { +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 devices; + std::vector 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& getDeviceMap() { return devices; } + const std::vector& 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_ */ diff --git a/include/tfcppsimple/TfDevice.h b/include/tfcppsimple/TfDevice.h new file mode 100644 index 0000000..08cca40 --- /dev/null +++ b/include/tfcppsimple/TfDevice.h @@ -0,0 +1,198 @@ +/* + * TfAbstractDevice.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFABSTRACTDEVICE_H_ +#define TFABSTRACTDEVICE_H_ + +#include +#include +#include +#include +#include +#include + + +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 { +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 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& 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_ */ diff --git a/include/tfcppsimple/TfDeviceFactory.h b/include/tfcppsimple/TfDeviceFactory.h new file mode 100644 index 0000000..806db01 --- /dev/null +++ b/include/tfcppsimple/TfDeviceFactory.h @@ -0,0 +1,33 @@ +/* + * DeviceFactory.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFDEVICEFACTORY_H_ +#define TFDEVICEFACTORY_H_ + +#include + +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_ */ diff --git a/include/tfcppsimple/TfException.h b/include/tfcppsimple/TfException.h new file mode 100644 index 0000000..a9713b9 --- /dev/null +++ b/include/tfcppsimple/TfException.h @@ -0,0 +1,42 @@ +/* + * TfException.h + * + * Created on: 11.06.2014 + * Author: matzman + */ + +#ifndef TFEXCEPTION_H_ +#define TFEXCEPTION_H_ + + +#include +#include +#include +#include + + +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_ */ diff --git a/include/tfcppsimple/TfTypes.h b/include/tfcppsimple/TfTypes.h new file mode 100644 index 0000000..3c2a60f --- /dev/null +++ b/include/tfcppsimple/TfTypes.h @@ -0,0 +1,53 @@ +/* + * TfTypes.h + * + * Created on: 11.06.2014 + * Author: matzman + */ + +#ifndef TFTYPES_H_ +#define TFTYPES_H_ + +#include + +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_ */ diff --git a/include/tfcppsimple/TfUtils.h b/include/tfcppsimple/TfUtils.h new file mode 100644 index 0000000..a903616 --- /dev/null +++ b/include/tfcppsimple/TfUtils.h @@ -0,0 +1,23 @@ +/* + * TfUtils.h + * + * Created on: 11.06.2014 + * Author: matzman + */ + +#ifndef TFUTILS_H_ +#define TFUTILS_H_ + +#include + +namespace tfcppsimple { + +class TfUtils { +public: + static void configureLog4CPP(std::string configFile); + static void configureLog4CPP(); // Default Config +}; + +} /* namespace tfcppsimple */ + +#endif /* TFUTILS_H_ */ diff --git a/include/tfcppsimple/devices/TfHumidityBricklet.h b/include/tfcppsimple/devices/TfHumidityBricklet.h new file mode 100644 index 0000000..dc13777 --- /dev/null +++ b/include/tfcppsimple/devices/TfHumidityBricklet.h @@ -0,0 +1,47 @@ +/* + * TfHumidityBricklet.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFHUMIDITYBRICKLET_H_ +#define TFHUMIDITYBRICKLET_H_ + +#include +#include + +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_ */ diff --git a/include/tfcppsimple/devices/TfIO16Bricklet.h b/include/tfcppsimple/devices/TfIO16Bricklet.h new file mode 100644 index 0000000..b5e2603 --- /dev/null +++ b/include/tfcppsimple/devices/TfIO16Bricklet.h @@ -0,0 +1,32 @@ +/* + * TfIO16Bricklet.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFIO16BRICKLET_H_ +#define TFIO16BRICKLET_H_ + +#include + +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_ */ diff --git a/include/tfcppsimple/devices/TfLCD20x4Bricklet.h b/include/tfcppsimple/devices/TfLCD20x4Bricklet.h new file mode 100644 index 0000000..24eeed2 --- /dev/null +++ b/include/tfcppsimple/devices/TfLCD20x4Bricklet.h @@ -0,0 +1,74 @@ +/* + * TfLCD204Bricklet.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFLCD20X4BRICKLET_H_ +#define TFLCD20X4BRICKLET_H_ + +#include +#include + +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 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_ */ diff --git a/include/tfcppsimple/devices/TfMasterBrick.h b/include/tfcppsimple/devices/TfMasterBrick.h new file mode 100644 index 0000000..94883b4 --- /dev/null +++ b/include/tfcppsimple/devices/TfMasterBrick.h @@ -0,0 +1,51 @@ +/* + * TfMasterBrick.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFMASTERBRICK_H_ +#define TFMASTERBRICK_H_ + +#include +#include + +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_ */ diff --git a/include/tfcppsimple/devices/TfTemperatureBricklet.h b/include/tfcppsimple/devices/TfTemperatureBricklet.h new file mode 100644 index 0000000..5ee6780 --- /dev/null +++ b/include/tfcppsimple/devices/TfTemperatureBricklet.h @@ -0,0 +1,54 @@ +/* + * TfTemperatureBricklet.h + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#ifndef TFTEMPERATUREBRICKLET_H_ +#define TFTEMPERATUREBRICKLET_H_ + +#include +#include + +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_ */ diff --git a/include/tfcppsimple/devices/TfUnknownBrick.h b/include/tfcppsimple/devices/TfUnknownBrick.h new file mode 100644 index 0000000..c612884 --- /dev/null +++ b/include/tfcppsimple/devices/TfUnknownBrick.h @@ -0,0 +1,36 @@ +/* + * TfUnknownBrick.h + * + * Created on: 13.06.2014 + * Author: matzman + */ + +#ifndef TFUNKNOWNBRICK_H_ +#define TFUNKNOWNBRICK_H_ + +#include + +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_ */ diff --git a/include/tfcppsimple/devices/TfUnknownBricklet.h b/include/tfcppsimple/devices/TfUnknownBricklet.h new file mode 100644 index 0000000..9e3d843 --- /dev/null +++ b/include/tfcppsimple/devices/TfUnknownBricklet.h @@ -0,0 +1,36 @@ +/* + * TfUnknownBricklet.h + * + * Created on: 13.06.2014 + * Author: matzman + */ + +#ifndef TFUNKNOWNBRICKLET_H_ +#define TFUNKNOWNBRICKLET_H_ + +#include + +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_ */ diff --git a/src/TfBrick.cpp b/src/TfBrick.cpp new file mode 100644 index 0000000..340effe --- /dev/null +++ b/src/TfBrick.cpp @@ -0,0 +1,31 @@ +/* + * TfBrick.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include + +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 */ diff --git a/src/TfBricklet.cpp b/src/TfBricklet.cpp new file mode 100644 index 0000000..cdd6dc2 --- /dev/null +++ b/src/TfBricklet.cpp @@ -0,0 +1,31 @@ +/* + * TfBricklet.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include + +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 */ diff --git a/src/TfConnection.cpp b/src/TfConnection.cpp new file mode 100644 index 0000000..3f3a8d1 --- /dev/null +++ b/src/TfConnection.cpp @@ -0,0 +1,288 @@ +/* + * TfConnection.cpp + * + * Created on: 10.06.2014 + * Author: matzman + */ + +#include +#include +#include +#include +#include +#include + + +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 */ diff --git a/src/TfDevice.cpp b/src/TfDevice.cpp new file mode 100644 index 0000000..dc5a9bb --- /dev/null +++ b/src/TfDevice.cpp @@ -0,0 +1,197 @@ +/* + * TfAbstractDevice.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include +#include +#include + +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 */ diff --git a/src/TfDeviceFactory.cpp b/src/TfDeviceFactory.cpp new file mode 100644 index 0000000..dda9d8f --- /dev/null +++ b/src/TfDeviceFactory.cpp @@ -0,0 +1,97 @@ +/* + * TfDeviceFactory.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 */ diff --git a/src/TfUtils.cpp b/src/TfUtils.cpp new file mode 100644 index 0000000..3ad6db7 --- /dev/null +++ b/src/TfUtils.cpp @@ -0,0 +1,36 @@ +/* + * TfUtils.cpp + * + * Created on: 11.06.2014 + * Author: matzman + */ + +#include +#include +#include +#include +#include +#include +#include + +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 */ diff --git a/src/config.mk b/src/config.mk new file mode 100644 index 0000000..08520e8 --- /dev/null +++ b/src/config.mk @@ -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 \ No newline at end of file diff --git a/src/devices/TfHumidityBricklet.cpp b/src/devices/TfHumidityBricklet.cpp new file mode 100644 index 0000000..75956a7 --- /dev/null +++ b/src/devices/TfHumidityBricklet.cpp @@ -0,0 +1,97 @@ +/* + * TfHumidityBricklet.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include +#include + +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 */ diff --git a/src/devices/TfIO16Bricklet.cpp b/src/devices/TfIO16Bricklet.cpp new file mode 100644 index 0000000..05415f4 --- /dev/null +++ b/src/devices/TfIO16Bricklet.cpp @@ -0,0 +1,39 @@ +/* + * TfIO16Bricklet.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include + +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 */ diff --git a/src/devices/TfLCD20x4Bricklet.cpp b/src/devices/TfLCD20x4Bricklet.cpp new file mode 100644 index 0000000..ba7822d --- /dev/null +++ b/src/devices/TfLCD20x4Bricklet.cpp @@ -0,0 +1,161 @@ +/* + * TfLCD204Bricklet.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include +#include + +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 */ diff --git a/src/devices/TfMasterBrick.cpp b/src/devices/TfMasterBrick.cpp new file mode 100644 index 0000000..22f70cc --- /dev/null +++ b/src/devices/TfMasterBrick.cpp @@ -0,0 +1,121 @@ +/* + * TfMasterBrick.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include +#include +#include + +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 */ diff --git a/src/devices/TfTemperatureBricklet.cpp b/src/devices/TfTemperatureBricklet.cpp new file mode 100644 index 0000000..117223e --- /dev/null +++ b/src/devices/TfTemperatureBricklet.cpp @@ -0,0 +1,101 @@ +/* + * TfTemperatureBricklet.cpp + * + * Created on: 12.06.2014 + * Author: matzman + */ + +#include +#include + +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 */ diff --git a/src/devices/TfUnknownBrick.cpp b/src/devices/TfUnknownBrick.cpp new file mode 100644 index 0000000..8c5d2a4 --- /dev/null +++ b/src/devices/TfUnknownBrick.cpp @@ -0,0 +1,40 @@ +/* + * TfUnknownBrick.cpp + * + * Created on: 13.06.2014 + * Author: matzman + */ + +#include + +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 */ diff --git a/src/devices/TfUnknownBricklet.cpp b/src/devices/TfUnknownBricklet.cpp new file mode 100644 index 0000000..d1a1aa2 --- /dev/null +++ b/src/devices/TfUnknownBricklet.cpp @@ -0,0 +1,40 @@ +/* + * TfUnknownBricklet.cpp + * + * Created on: 13.06.2014 + * Author: matzman + */ + +#include + +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 */ diff --git a/src/devices/config.mk b/src/devices/config.mk new file mode 100644 index 0000000..dc1f7ec --- /dev/null +++ b/src/devices/config.mk @@ -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 + diff --git a/third-party/Makefile b/third-party/Makefile new file mode 100644 index 0000000..da21eaf --- /dev/null +++ b/third-party/Makefile @@ -0,0 +1,70 @@ + +include ../Makefile.local + +TINKERFORGE_FILES=$(shell cat tinkerforge-filelist) +TINKERFORGE_OBJS=$(TINKERFORGE_FILES:%.cpp=tinkerforge/build/%.o) + + +all: tinkerforge-dist log4cpp-dist + +clean: log4cpp-distclean tinkerforge-distclean + +tinkerforge-dist : tinkerforge-unzip tinkerforge/lib/libtinkerforge.a tinkerforge-copy-libs tinkerforge-clean + +tinkerforge-unzip : tinkerforge-distclean + unzip tinkerforge_c_bindings_*.zip -d tinkerforge + cd tinkerforge/bindings; for i in *.c; do mv $$i $${i%.c}.cpp; done + mkdir tinkerforge/include + cd tinkerforge/bindings; for i in *.h; do mv $$i ../include/$$i; done + +tinkerforge-copy-libs : + mkdir -p ../lib + cp tinkerforge/lib/libtinkerforge.a ../lib + +tinkerforge-generate-filelist : + cd tinkerforge/bindings; ls -1 *.cpp > ../../tinkerforge-filelist + +tinkerforge-distclean : + rm -rf tinkerforge tinkerforge/lib/libtinkerforge.a + +tinkerforge-clean : + rm -rf tinkerforge/build + +tinkerforge/lib : + mkdir -p $@ + +tinkerforge/build : + mkdir -p $@ + +tinkerforge/lib/libtinkerforge.a : tinkerforge/lib $(TINKERFORGE_OBJS) + $(AR) rc $@ tinkerforge/build/*.o + $(RANLIB) $@ + +$(TINKERFORGE_OBJS) : tinkerforge/build/%.o : tinkerforge/bindings/%.cpp tinkerforge/build + $(CXX) $(CXX_FLAGS) $(TINKERFORGE_CXX_FLAGS) -I tinkerforge/include -c $< -o $@ + + +log4cpp-dist : log4cpp-unzip log4cpp-build log4cpp-copy-libs log4cpp-clean + +log4cpp-unzip : log4cpp-distclean + tar xvfz log4cpp-*.tar.gz + mv log4cpp log4cpp-src + mkdir log4cpp + mv log4cpp-src log4cpp/src + +log4cpp-build : + cd log4cpp/src; \ + ./configure --prefix $(realpath log4cpp) --libdir $(realpath log4cpp)/lib + make -C log4cpp/src + make -C log4cpp/src install + +log4cpp-copy-libs : + mkdir -p ../lib + cp log4cpp/lib/liblog4cpp.a ../lib + +log4cpp-distclean : + rm -rf log4cpp log4cpp/lib/liblog4cpp.a + +log4cpp-clean : + rm -rf log4cpp/src + diff --git a/third-party/log4cpp-1.1.1.tar.gz b/third-party/log4cpp-1.1.1.tar.gz new file mode 100644 index 0000000..c7acdcc Binary files /dev/null and b/third-party/log4cpp-1.1.1.tar.gz differ diff --git a/third-party/tinkerforge-filelist b/third-party/tinkerforge-filelist new file mode 100644 index 0000000..1a0371f --- /dev/null +++ b/third-party/tinkerforge-filelist @@ -0,0 +1,47 @@ +brick_dc.cpp +brick_imu.cpp +bricklet_ambient_light.cpp +bricklet_analog_in.cpp +bricklet_analog_out.cpp +bricklet_barometer.cpp +bricklet_current12.cpp +bricklet_current25.cpp +bricklet_distance_ir.cpp +bricklet_distance_us.cpp +bricklet_dual_button.cpp +bricklet_dual_relay.cpp +bricklet_gps.cpp +bricklet_hall_effect.cpp +bricklet_humidity.cpp +bricklet_industrial_digital_in_4.cpp +bricklet_industrial_digital_out_4.cpp +bricklet_industrial_dual_0_20ma.cpp +bricklet_industrial_quad_relay.cpp +bricklet_io16.cpp +bricklet_io4.cpp +bricklet_joystick.cpp +bricklet_lcd_16x2.cpp +bricklet_lcd_20x4.cpp +bricklet_led_strip.cpp +bricklet_linear_poti.cpp +bricklet_line.cpp +bricklet_moisture.cpp +bricklet_motion_detector.cpp +bricklet_multi_touch.cpp +bricklet_piezo_buzzer.cpp +bricklet_piezo_speaker.cpp +bricklet_ptc.cpp +bricklet_remote_switch.cpp +bricklet_rotary_encoder.cpp +bricklet_rotary_poti.cpp +bricklet_segment_display_4x7.cpp +bricklet_sound_intensity.cpp +bricklet_temperature.cpp +bricklet_temperature_ir.cpp +bricklet_tilt.cpp +bricklet_voltage.cpp +bricklet_voltage_current.cpp +brick_master.cpp +brick_servo.cpp +brick_stepper.cpp +ip_connection.cpp diff --git a/third-party/tinkerforge_c_bindings_latest.zip b/third-party/tinkerforge_c_bindings_latest.zip new file mode 100644 index 0000000..21fcd35 Binary files /dev/null and b/third-party/tinkerforge_c_bindings_latest.zip differ