diff --git a/CMakeLists.txt b/CMakeLists.txt index eecb7f3..df6f80b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ if (WIN32) set(PIP_DLL_DIR "${CMAKE_CURRENT_BINARY_DIR}/pip") endif() -set(LIST_LIBS pip qad_widgets qad_utils qad_application qad_blockview qad_graphic qad_sql_table piqt mbricks cd_utils kx_utils piqt_utils touch_widgets qglview) +set(LIST_LIBS pip qad_widgets qad_utils qad_application qad_blockview qad_graphic qad_sql_table piqt cd_utils kx_utils piqt_utils touch_widgets qglview) foreach(L ${LIST_LIBS}) add_subdirectory(${L}) include_directories(${L}) diff --git a/mbricks/CMakeLists.txt b/mbricks/CMakeLists.txt deleted file mode 100644 index 7548be0..0000000 --- a/mbricks/CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ -project(mbricks) -cmake_minimum_required(VERSION 2.6) -if (NOT LIBPROJECT) - find_package(PIP REQUIRED) -endif () -if (MINGW) - find_package(MinGW REQUIRED) -endif() -include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${PIP_INCLUDES}) -option(LIB "System install" 1) -option(DEBUG "Build with -g3" 0) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall") -if (DEBUG) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3") -endif () -file(GLOB HDRS "*.h") -file(GLOB CPPS "*.cpp") -add_library(${PROJECT_NAME} SHARED ${CPPS}) -target_link_libraries(${PROJECT_NAME} ${PIP_LIBRARY}) -if (LIB) - if (WIN32) - set(CMAKE_INSTALL_PREFIX ${MINGW_DIR}) - install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}) - install(TARGETS ${PROJECT_NAME} DESTINATION ${MINGW_LIB}) - install(TARGETS ${PROJECT_NAME} DESTINATION ${MINGW_BIN}) - else () - set(CMAKE_INSTALL_PREFIX /usr) - install(FILES ${HDRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include) - install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) - endif () - message(STATUS "Install to system \"${CMAKE_INSTALL_PREFIX}\"") -else () - install(TARGETS ${PROJECT_NAME} DESTINATION bin) - message(STATUS "Install to local \"bin\"") -endif () diff --git a/mbricks/brick_base.cpp b/mbricks/brick_base.cpp deleted file mode 100644 index 310a630..0000000 --- a/mbricks/brick_base.cpp +++ /dev/null @@ -1,140 +0,0 @@ -#include "brick_base.h" - -/* -double randomn(double dv, double sv) { - double s = 2., v0, v1; - while (s > 1. || s == 0.) { - v0 = randomd(); - v1 = randomd(); - s = v0*v0 + v1*v1; - } - v0 = v0 * sqrt(-2 * log(s) / s); - return v0 * sv + dv; -} -*/ - -void BrickBase::Parameter::setValue(const PIVariant & v, bool withType) { - value = v; - if (withType) { - PIString tn = v.typeName(); - if (tn == "bool") {type = BrickBase::Bool; return;} - if (tn == "char") {type = BrickBase::Integer; return;} - if (tn == "uchar") {type = BrickBase::Integer; return;} - if (tn == "short") {type = BrickBase::Integer; return;} - if (tn == "ushort") {type = BrickBase::Integer; return;} - if (tn == "int") {type = BrickBase::Integer; return;} - if (tn == "uint") {type = BrickBase::Integer; return;} - if (tn == "llong") {type = BrickBase::Integer; return;} - if (tn == "ullong") {type = BrickBase::Integer; return;} - if (tn == "float") {type = BrickBase::Float; return;} - if (tn == "double") {type = BrickBase::Float; return;} - if (tn == "ldouble") {type = BrickBase::Float; return;} - if (tn == "PIString") {type = BrickBase::String; return;} - if (tn == "PIStringList") {type = BrickBase::String; return;} - }/* else { - switch (v.type()) { - case PIVariant::Bool: value.vBool = v.vBool; return; - case PIVariant::Char: value.vInt = v.vChar; return; - case PIVariant::Short: value.vInt = v.vShort; return; - case PIVariant::Int: value.vInt = v.vInt; return; - case PIVariant::Long: value.vInt = v.vLong; return; - case PIVariant::LLong: value.vInt = v.vLLong; return; - case PIVariant::UChar: value.vInt = v.vUChar; return; - case PIVariant::UShort: value.vInt = v.vUShort; return; - case PIVariant::UInt: value.vInt = v.vUInt; return; - case PIVariant::ULong: value.vInt = v.vULong; return; - case PIVariant::ULLong: value.vInt = v.vULLong; return; - case PIVariant::Float: value.vDouble = v.vFloat; return; - case PIVariant::Double: value.vDouble = v.vDouble; return; - case PIVariant::LDouble: value.vDouble = v.vLDouble; return; - case PIVariant::String: value.vString = v.vString; return; - case PIVariant::StringList: value.vString = v.vStringList.join(", "); return; - }; - }*/ -} - - -BrickBase::BrickBase(int inputs_num, int outputs_num, int parameters_num) { - inputs_count = inputs_num; - outputs_count = outputs_num; - parameters_count = parameters_num; - inputs.resize(inputs_num, 0.); - outputs.resize(outputs_num, 0.); - defInputs.resize(inputs_num, 0.); - parameters.resize(parameters_num, 0.); - inNames.resize(inputs_num); - outNames.resize(outputs_num); - paramNames.resize(parameters_num); - if (inputs_num > 0) inNames[0] = "Input"; - if (outputs_num > 0) outNames[0] = "Output"; - parametersName_ = "Parameters"; - pt = dt = 0; - freqDivider_ = freqCurrent_ = 1; - time_ = 0.; - level_ = -666; - io_Type = Fixed; - initial = composite = buffered = interactive = rtOnly = false; -} - - -bool BrickBase::tick(double time) { - if (freqDivider_ > 1) { - if (freqCurrent_ < freqDivider_) { - ++freqCurrent_; - lastTick = false; - return false; - } - freqCurrent_ = 1; - } - time_ = time; - dt = time - pt; - lastTick = tick_body(time); - pt = time; - return lastTick; -} - - -void BrickBase::proceedConnections(bool) { - for (uint i = 0; i < connections.size(); ++i) - connections[i].brick_to->setInputValue(connections[i].in_num, output(connections[i].out_num)); -} - - -bool BrickBase::connect(BrickBase * b_from, int out_num, BrickBase * b_to, int in_num) { - if (out_num < 0 || out_num >= b_from->outputsCount()) { - piCout << "Connect error: no such output" << out_num << "in \"" << b_from->name() << "\"!"; - return false; - } - if (in_num < 0 || in_num >= b_to->inputsCount()) { - piCout << "Connect error: no such input" << in_num << "in \"" << b_to->name() << "\"!"; - return false; - } - b_from->addConnection(BrickBase::Connection(out_num, b_to, in_num)); - //string fn = b_from->name().stdString(), tn = b_to->name().stdString(); - //cout << "Connect \"" << fn << "\": " << out_num << " to \"" << tn << "\": " << in_num << endl; - return true; -} - - -bool BrickBase::disconnect(BrickBase * b_from, int out_num, BrickBase * b_to, int in_num) { - /*if (out_num < 0 || out_num >= b_from->outCount()) { - cout << "Disconnect error: no such output " << out_num << " in \"" << b_from->name() << "\"!" << endl; - return false; - } - if (in_num < 0 || in_num >= b_to->inCount()) { - cout << "Disconnect error: no such input " << in_num << " in \"" << b_to->name() << "\"!" << endl; - return false; - }*/ - BrickBase::Connection conn; - for (int i = 0; i < b_from->connectionsCount(); ++i) { - conn = b_from->connection(i); - if (out_num != conn.out_num || b_to != conn.brick_to || in_num != conn.in_num) - continue; - b_from->removeConnection(i); - //string fn = b_from->name().stdString(), tn = b_to->name().stdString(); - //cout << "Disconnect \"" << fn << "\": " << out_num << " from \"" << tn << "\": " << in_num << endl; - return true; - } - return false; -} - diff --git a/mbricks/brick_base.h b/mbricks/brick_base.h deleted file mode 100644 index ee083bc..0000000 --- a/mbricks/brick_base.h +++ /dev/null @@ -1,179 +0,0 @@ -#ifndef BRICK_BASE_H -#define BRICK_BASE_H - -#include "pievaluator.h" -#include "piconfig.h" -#include "pivariant.h" -#include "picollection.h" -#include "pimathmodule.h" - -#define MBRICK(brick) PIOBJECT_SUBCLASS(brick, BrickBase) friend class BrickManager; public: virtual BrickBase * copy() {brick * t = new brick(*this); t->setName(name()); t->type = type; t->copied(); return t;} - -inline double residue(double f, double s) {return f - floor(f / s) * s;} -inline double dbool(double d) {return d > 0 ? 1. : 0;} - -class BrickComposite; -class BrickManager; - -class BrickBase: public PIObject -{ - PIOBJECT(BrickBase) - friend class BrickManager; - friend void buildTree(PIVector & bricks, PIVector > & tree); - friend PIVector > buildSubtree(PIVector & bricks, PIVector > & tree); -public: - BrickBase(int inputs_num = 1, int outputs_num = 1, int parameters_num = 0); - virtual ~BrickBase() {;} - virtual BrickBase * copy() {return new BrickBase(*this);} - - enum IOType { - Fixed = 0x0, - VariableInputs = 0x01, - VariableOutputs = 0x02, - VariableInOuts = VariableInputs | VariableOutputs - }; - enum Mode {Synchronous, Asynchronous}; - enum ParameterType {Bool, Integer, Float, String, File}; - - struct Parameter { - Parameter() {type = BrickBase::Float;} - Parameter(const char * v) {setValue(v);} - Parameter(const bool & v) {setValue(v);} - Parameter(const int & v) {setValue(v);} - Parameter(const double & v) {setValue(v);} - Parameter(const PIString & v) {setValue(v);} - void setType(const ParameterType & t) {type = t;} - void setValue(const char * v, bool withType = true) {setValue(PIString(v), withType);} - void setValue(const bool & v, bool withType = true) {if (withType) type = BrickBase::Bool; value = v;} - void setValue(const int & v, bool withType = true) {if (withType) type = BrickBase::Integer; value = v;} - void setValue(const double & v, bool withType = true) {if (withType) type = BrickBase::Float; value = v;} - void setValue(const PIString & v, bool withType = true) {if (withType) type = BrickBase::String; value = v;} - void setValue(const PIVariant & v, bool withType = true); - void setValueOnly(const PIString & v) {value = v;} - bool toBool() const {return value.toBool();} - int toInt() const {return value.toInt();} - uint toUInt() const {return value.toInt();} - double toFloat() const {return value.toDouble();} - PIString toString() const {return value.toString();} - ParameterType type; - PIVariant value; - }; - - virtual void reset_specified() {;} - virtual void inputsCountChanged(int count) {;} - virtual void outputsCountChanged(int count) {;} - virtual void started() {;} - virtual void finished() {;} - virtual void parameterChanged(int index) {;} - virtual void saveInputsToDefault() {defInputs = inputs;} - virtual void resetOutputs() {outputs.fill(0.);} - virtual void reset() {finished(); inputs = defInputs; dt = pt = time_ = 0.; reset_specified(); resetOutputs(); started();} - virtual void proceedConnections(bool compositeToo = false); - bool tick(double time); - int frequencyDivider() const {return freqDivider_;} - void setFrequencyDivider(const int divider) {freqDivider_ = divider;} - int inputsCount() const {return inputs_count;} - int outputsCount() const {return outputs_count;} - int parametersCount() const {return parameters_count;} - double input(const int index = 0) const {return inputs[index];} - double output(const int index = 0) const {return outputs[index];} - const PIVariant & parameter(const int index = 0) const {return parameters[index].value;} - const BrickBase::ParameterType & parameterType(const int index = 0) const {return parameters[index].type;} - double * input_ptr(const int index = 0) {return &inputs[index];} - double * output_ptr(const int index = 0) {return &outputs[index];} - //double * parameter_ptr(const int index = 0) {return &(parameters[index].value.toDouble());} - void setInputValue(const int index, double value) {inputs[index] = value;} - void setOutputValue(const int index, double value) {outputs[index] = value;} - void setDefaultInputValue(const int index, double value) {defInputs[index] = value;} - void setParameterValue(const int index, const char * value, bool withType = false) {setParameterValue(index, PIString(value), withType);} - void setParameterValue(const int index, const bool & value, bool withType = false) {parameters[index].setValue(value, withType); parameterChanged(index);} - void setParameterValue(const int index, const int & value, bool withType = false) {parameters[index].setValue(value, withType); parameterChanged(index);} - void setParameterValue(const int index, const double & value, bool withType = false) {parameters[index].setValue(value, withType); parameterChanged(index);} - void setParameterValue(const int index, const PIString & value, bool withType = false) {parameters[index].setValue(value, withType); parameterChanged(index);} - void setParameterValue(const int index, const PIVariant & value, bool withType = false) {parameters[index].setValue(value, withType); parameterChanged(index);} - void setParameterValue(const int index, const BrickBase::Parameter & value) {parameters[index] = value; parameterChanged(index);} - void setParameterValueOnly(const int index, const PIString & value) {parameters[index].setValueOnly(value); parameterChanged(index);} - void setInputName(const int index, const PIString & name) {inNames[index] = name;} - void setOutputName(const int index, const PIString & name) {outNames[index] = name;} - void setParameterName(const int index, const PIString & name) {paramNames[index] = name;} - void setInputsCount(const int count) {inputs.resize(count); inputs_count = inputs.size(); inNames.resize(count); inputsCountChanged(count);} - void setOutputsCount(const int count) {outputs.resize(count); outputs_count = outputs.size(); outNames.resize(count); outputsCountChanged(count);} - void setParametersCount(const int count, const char * val) {parameters.resize(count, BrickBase::Parameter(PIString(val))); paramNames.resize(count); parameters_count = parameters.size();} - void setParametersCount(const int count, const bool & val) {parameters.resize(count, BrickBase::Parameter(val)); paramNames.resize(count); parameters_count = parameters.size();} - void setParametersCount(const int count, const int val) {parameters.resize(count, BrickBase::Parameter(val)); paramNames.resize(count); parameters_count = parameters.size();} - void setParametersCount(const int count, const double & val = 0.) {parameters.resize(count, BrickBase::Parameter(val)); paramNames.resize(count); parameters_count = parameters.size();} - void setParametersCount(const int count, const PIString & val) {parameters.resize(count, BrickBase::Parameter(val)); paramNames.resize(count); parameters_count = parameters.size();} - const PIVector & getParameters() const {return parameters;} - void setInOutCount(const int count) {setInputsCount(count); setOutputsCount(count);} - int ioType() const {return io_Type;} - PIString libName() const {return lib;} - PIString typeName() const {return type;} - PIString codeName() const {return className();} - PIString parametersName() const {return parametersName_;} - PIString inputName(const int index) const {return inNames[index];} - PIString outputName(const int index) const {return outNames[index];} - PIString parameterName(const int index) const {return paramNames[index];} - PIString note() const {return note_;} - void setNote(const PIString & str) {note_ = str;} - PIString tag() const {return tag_;} - void setTag(const PIString & str) {tag_ = str;} - BrickManager * manager() const {return manager_;} - void setManager(BrickManager * m) {manager_ = m;} - bool isInitial() const {return initial;} - void setInitial(const bool yes = true) {initial = yes;} - bool isComposite() const {return composite;} - void setComposite(const bool yes = true) {composite = yes;} - bool isBuffered() const {return buffered;} - void setBuffered(const bool yes = true) {buffered = yes;} - bool isInteractive() const {return interactive;} - void setInteractive(const bool yes = true) {interactive = yes;} - bool isRealTimeOnly() const {return rtOnly;} - void setRealTimeOnly(const bool yes = true) {rtOnly = yes;} - BrickComposite * toComposite() {return reinterpret_cast(this);} - - struct Connection { - int out_num; - BrickBase * brick_to; - int in_num; - Connection() {;} - Connection(int out_n, BrickBase * b_to, int in_n) {brick_to = b_to; out_num = out_n; in_num = in_n;} - }; - - void addConnection(Connection conn) {connections.push_back(conn);} - void clearConnections() {connections.clear();} - BrickBase::Connection connection(int index) const {return connections[index];} - int connectionsCount() const {return connections.size();} - void removeConnection(int index) {connections.remove(index);} - - bool lastTick, userBool; - double time_, userDouble; - int level_, userInt; - PIVector buffer; - - static bool connect(BrickBase * b_from, int out_num, BrickBase * b_to, int in_num); - static bool connect(BrickBase * b_from, int out_num, BrickBase & b_to, int in_num) {return connect(b_from, out_num, &b_to, in_num);} - static bool connect(BrickBase & b_from, int out_num, BrickBase * b_to, int in_num) {return connect(&b_from, out_num, b_to, in_num);} - static bool connect(BrickBase & b_from, int out_num, BrickBase & b_to, int in_num) {return connect(&b_from, out_num, &b_to, in_num);} - - static bool disconnect(BrickBase * b_from, int out_num, BrickBase * b_to, int in_num); - static bool disconnect(BrickBase * b_from, int out_num, BrickBase & b_to, int in_num) {return disconnect(b_from, out_num, &b_to, in_num);} - static bool disconnect(BrickBase & b_from, int out_num, BrickBase * b_to, int in_num) {return disconnect(&b_from, out_num, b_to, in_num);} - static bool disconnect(BrickBase & b_from, int out_num, BrickBase & b_to, int in_num) {return disconnect(&b_from, out_num, &b_to, in_num);} - -protected: - virtual void copied() {;} - virtual bool tick_body(double time) {return true;} - - PIVector inputs, outputs, defInputs; - PIVector connections; - PIVector parameters; - PIVector inNames, outNames, paramNames; - PIString parametersName_, lib, type, note_, tag_; - int inputs_count, outputs_count, parameters_count, io_Type, tmp_, freqDivider_, freqCurrent_; - double dt, pt; - bool initial, ticked, composite, buffered, interactive, rtOnly; - BrickManager * manager_; -}; - - -#endif // BRICK_BASE_H diff --git a/mbricks/brick_composite.cpp b/mbricks/brick_composite.cpp deleted file mode 100644 index 58b386e..0000000 --- a/mbricks/brick_composite.cpp +++ /dev/null @@ -1,533 +0,0 @@ -#include "brick_composite.h" - -PIVector BrickComposite::baseBricks; - - -BrickComposite::BrickComposite(): BrickCompositeBase(0, 0, 1) { - type = "Composite"; - setName(type); - paramNames[0] = "Scheme file"; - note_ = "\"Scheme\" is path to *.mbs file, described this brick"; - parameters[0].setValue(""); - parameters[0].setType(BrickBase::File); - setComposite(true); - mode_ = Asynchronous; - bi = 0; bo = 0; bp = 0; inf = 0; -} - - -void BrickComposite::clear() { - for (uint i = 0; i < bricks.size(); ++i) - delete bricks[i]; - bricks.clear(); -} - - -void BrickComposite::proceedConnections(bool compositeToo) { - if (compositeToo) { - if (bi != 0) { - for (int i = 0; i < inputsCount(); ++i) - bi->setOutputValue(i, inputs[i]); - bi->proceedConnections(); - } - if (bp != 0) { - for (int i = 1; i < parametersCount(); ++i) - bp->setOutputValue(i - 1, parameters[i].toFloat()); - bp->proceedConnections(); - } - switch (mode_) { - case BrickBase::Synchronous: - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->proceedConnections(); - break; - case BrickBase::Asynchronous: default: - for (uint i = 0; i < tree.size(); ++i) { - tl = &tree[i]; - for (uint j = 0; j < tl->size(); ++j) - (*tl)[j]->proceedConnections(); - } - break; - } - if (bo != 0) - for (int i = 0; i < outputsCount(); ++i) - outputs[i] = bo->input(i); - } - BrickBase::proceedConnections(compositeToo); -} - - -void BrickComposite::parameterChanged(int index) { - fillBaseBricks(); - if (index != 0) return; - PIConfig sr(parameters[0].toString()); - PIString prefix, cname; - BrickBase * b; - int pcnt; - clear(); - setMode((Mode)(int)sr.getValue("Mode", 1)); - PIStringList names = sr.getValue("Bricks", PIStringList()); - for (uint i = 0; i < names.size(); ++i) { - prefix = names[i] + " "; - cname = sr.getValue(prefix + "codeName", ""); - b = findBaseBrick(cname)->copy(); - BrickComposite::loadBrick(sr, prefix, b); - bricks.push_back(b); - } - uint cnt = sr.getValue("Connections count", 0); - //cout << endl; - for (uint i = 0; i < cnt; ++i) { - prefix = "Connection " + PIString::fromNumber(i) + " "; - BrickBase::connect(findBrick(sr.getValue(prefix + "from Name", "")), - sr.getValue(prefix + "from Port", 0), - findBrick(sr.getValue(prefix + "to Name", "")), - sr.getValue(prefix + "to Port", 0)); - } - - bi = 0; - for (uint i = 0; i < bricks.size(); ++i) - if (bricks[i]->codeName() == "BrickCompositeInput") - {bi = (BrickCompositeInput * )bricks[i]; break;} - if (bi != 0) { - setInputsCount(bi->outputsCount()); - for (int i = 0; i < inputsCount(); ++i) - inNames[i] = bi->parameter(i).toString(); - } else setInputsCount(0); - - bo = 0; - for (uint i = 0; i < bricks.size(); ++i) - if (bricks[i]->codeName() == "BrickCompositeOutput") - {bo = (BrickCompositeOutput * )bricks[i]; break;} - if (bo != 0) { - setOutputsCount(bo->inputsCount()); - for (int i = 0; i < outputsCount(); ++i) - outNames[i] = bo->parameter(i).toString(); - } else setOutputsCount(0); - - bp = 0; - for (uint i = 0; i < bricks.size(); ++i) - if (bricks[i]->codeName() == "BrickCompositeParameters") - {bp = (BrickCompositeParameters * )bricks[i]; break;} - if (bp != 0) { - pcnt = parametersCount(); - setParametersCount(bp->parametersCount() + 1); - for (int i = 1; i < parametersCount(); ++i) { - cname = bp->parameter(i - 1).toString(); - int j = cname.toLowerCase().find(':'); - if (j < 0) paramNames[i] = cname; - else { - paramNames[i] = cname.left(j).trim(); - if (i >= pcnt) parameters[i].setValue(cname.cutLeft(j + 1).toDouble()); - } - } - } else setParametersCount(1); - - inf = 0; - for (uint i = 0; i < bricks.size(); ++i) - if (bricks[i]->codeName() == "BrickCompositeInformation") - {inf = (BrickCompositeInformation * )bricks[i]; break;} - if (inf != 0) { - type = inf->parameter(0).toString(); - note_ = inf->parameter(1).toString(); - } else { - type = "Composite"; - note_ = "\"Scheme\" is path to *.mbs file, described this brick"; - } - - buildBrickTree(); - //cout << name_ << " load " << bricks.size() << " bricks" << endl; -} - - -void BrickComposite::loadBrick(PIConfig & sr, const PIString & prefix, BrickBase * b, BrickManager * m) { - b->setName(sr.getValue(prefix + "name", b->name())); - b->setInitial(sr.getValue(prefix + "initial", false)); - b->setFrequencyDivider(sr.getValue(prefix + "freqDiv", 1)); - b->setOutputsCount(sr.getValue(prefix + "outCount", b->outputsCount())); - b->setInputsCount(sr.getValue(prefix + "inCount", b->inputsCount())); - for (int i = 0; i < b->inputsCount(); ++i) - b->setInputValue(i, sr.getValue(prefix + "in " + PIString::fromNumber(i), b->input(i))); - b->setParametersCount(sr.getValue(prefix + "paramCount", b->parametersCount())); - for (int i = 0; i < b->parametersCount(); ++i) - b->setParameterValue(i, PIVariant::fromValue(sr.getValue(prefix + "param " + PIString::fromNumber(i), b->parameter(i).toString())), false); - b->setManager(m); -} - - -BrickBase * BrickComposite::findBrick(const PIString & name) { - for (uint i = 0; i < bricks.size(); ++i) - if (bricks[i]->name() == name) - return bricks[i]; - return 0; -} - - -BrickBase * BrickComposite::findBaseBrick(const PIString & code_name) { - for (uint i = 0; i < baseBricks.size(); ++i) - if (baseBricks[i]->codeName() == code_name) - return baseBricks[i]; - return 0; -} - - -bool BrickComposite::tick_body(double time) { - if (bi != 0) { - for (int i = 0; i < inputsCount(); ++i) - bi->setOutputValue(i, inputs[i]); - bi->proceedConnections(); - } - if (bp != 0) { - for (int i = 1; i < parametersCount(); ++i) - bp->setOutputValue(i - 1, parameters[i].toFloat()); - bp->proceedConnections(); - } - switch (mode_) { - case BrickBase::Synchronous: - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->tick(time); - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->proceedConnections(); - break; - case BrickBase::Asynchronous: default: - for (uint i = 0; i < tree.size(); ++i) { - tl = &tree[i]; - for (uint j = 0; j < tl->size(); ++j) - (*tl)[j]->tick(time); - for (uint j = 0; j < tl->size(); ++j) - (*tl)[j]->proceedConnections(); - } - break; - } - if (bo != 0) - for (int i = 0; i < outputsCount(); ++i) - outputs[i] = bo->input(i); - return true; -} - - -void BrickComposite::fillBaseBricks() { - if (!baseBricks.isEmpty()) return; - PIStringList libs = PICollection::groups(); - piForeachC (PIString & l, libs) { - PIVector group = PICollection::groupElements(l); - piForeachC (PIObject * o, group) - baseBricks << (BrickBase * )o; - } - /*baseBricks.push_back(new BrickEmitConst()); - baseBricks.push_back(new BrickEmitLinear()); - baseBricks.push_back(new BrickEmitSin()); - baseBricks.push_back(new BrickEmitDelta()); - baseBricks.push_back(new BrickEmitStep()); - baseBricks.push_back(new BrickEmitPulse()); - baseBricks.push_back(new BrickEmitStrobe()); - baseBricks.push_back(new BrickEmitTime()); - baseBricks.push_back(new BrickEmitFrequency()); - baseBricks.push_back(new BrickEmitNoiseRandom()); - baseBricks.push_back(new BrickEmitNoiseNormal()); - baseBricks.push_back(new BrickMathAbsolute()); - baseBricks.push_back(new BrickMathSign()); - baseBricks.push_back(new BrickMathAmplifier()); - baseBricks.push_back(new BrickMathSum()); - baseBricks.push_back(new BrickMathMultiply()); - baseBricks.push_back(new BrickMathDivide()); - baseBricks.push_back(new BrickMathPower()); - baseBricks.push_back(new BrickMathDeadZone()); - baseBricks.push_back(new BrickMathSaturation()); - baseBricks.push_back(new BrickMathDelayTicks()); - baseBricks.push_back(new BrickMathDelaySeconds()); - baseBricks.push_back(new BrickMathRelay()); - baseBricks.push_back(new BrickMathDerivate()); - baseBricks.push_back(new BrickMathIntegral()); - baseBricks.push_back(new BrickMathIntegralSaturated()); - baseBricks.push_back(new BrickMathQuantize()); - baseBricks.push_back(new BrickMathThreshold()); - baseBricks.push_back(new BrickMathFunction()); - baseBricks.push_back(new BrickMathFFT()); - baseBricks.push_back(new BrickMathCopy()); - baseBricks.push_back(new BrickMathBessel()); - baseBricks.push_back(new BrickLogicNot()); - baseBricks.push_back(new BrickLogicAnd()); - baseBricks.push_back(new BrickLogicOr()); - baseBricks.push_back(new BrickLogicXor()); - baseBricks.push_back(new BrickLogicNAnd()); - baseBricks.push_back(new BrickLogicNOr()); - baseBricks.push_back(new BrickLogicNXor()); - baseBricks.push_back(new BrickLogicMemory()); - baseBricks.push_back(new BrickLogicCompare()); - baseBricks.push_back(new BrickDigitalTriggerRS()); - baseBricks.push_back(new BrickDigitalTriggerD()); - baseBricks.push_back(new BrickDigitalTriggerJK()); - baseBricks.push_back(new BrickDigitalCounter2()); - baseBricks.push_back(new BrickDigitalCounter4()); - baseBricks.push_back(new BrickDigitalCounter8()); - baseBricks.push_back(new BrickDigitalCoder1()); - baseBricks.push_back(new BrickDigitalCoder2()); - baseBricks.push_back(new BrickDigitalCoder3()); - baseBricks.push_back(new BrickDigitalCoder4()); - baseBricks.push_back(new BrickDigitalDecoder1()); - baseBricks.push_back(new BrickDigitalDecoder2()); - baseBricks.push_back(new BrickDigitalDecoder3()); - baseBricks.push_back(new BrickDigitalDecoder4()); - baseBricks.push_back(new BrickDigitalMux1()); - baseBricks.push_back(new BrickDigitalMux2()); - baseBricks.push_back(new BrickDigitalMux3()); - baseBricks.push_back(new BrickDigitalMux4()); - baseBricks.push_back(new BrickDigitalDemux1()); - baseBricks.push_back(new BrickDigitalDemux2()); - baseBricks.push_back(new BrickDigitalDemux3()); - baseBricks.push_back(new BrickDigitalDemux4()); - baseBricks.push_back(new BrickDigitalDigitalToAnalog2()); - baseBricks.push_back(new BrickDigitalDigitalToAnalog4()); - baseBricks.push_back(new BrickDigitalDigitalToAnalog8()); - baseBricks.push_back(new BrickDigitalAnalogToDigital2()); - baseBricks.push_back(new BrickDigitalAnalogToDigital4()); - baseBricks.push_back(new BrickDigitalAnalogToDigital8()); - baseBricks.push_back(new BrickLinkTransferFunction()); - baseBricks.push_back(new BrickLinkFilter1Degree()); - baseBricks.push_back(new BrickLinkFilterBandpass()); - baseBricks.push_back(new BrickInterfaceSerialIn()); - baseBricks.push_back(new BrickInterfaceSerialOut()); - baseBricks.push_back(new BrickInterfaceUDPIn()); - baseBricks.push_back(new BrickInterfaceUDPOut()); - baseBricks.push_back(new BrickInterfaceBinFileOut()); - baseBricks.push_back(new BrickStatisticMinMaxI()); - baseBricks.push_back(new BrickStatisticMinMaxF()); - baseBricks.push_back(new BrickStatisticExpectation()); - baseBricks.push_back(new BrickStatisticVariance()); - baseBricks.push_back(new BrickComposite()); - baseBricks.push_back(new BrickCompositeInput()); - baseBricks.push_back(new BrickCompositeOutput()); - baseBricks.push_back(new BrickCompositeParameters()); - baseBricks.push_back(new BrickCompositeInformation());*/ -} - - -void sumTrees(PIVector > & ft, PIVector > & st) { - uint mi = piMin(ft.size(), st.size()); - for (uint i = 0; i < mi; ++i) - for (uint j = 0; j < st[i].size(); ++j) - ft[i].push_back(st[i][j]); - if (st.size() > ft.size()) - for (uint i = mi; i < st.size(); ++i) - ft.push_back(st[i]); -} - - -void buildTree(PIVector & bricks, PIVector > & tree) { - PIVector > ct; - BrickBase * b; - tree.clear(); - if (bricks.size() == 0) return; - for (uint i = 0; i < bricks.size(); ++i) { - b = bricks[i]; - b->ticked = false; - b->level_ = -666; - b->tmp_ = 0; - } - ct = buildSubtree(bricks, tree); - //cout << "--------" << endl; - while (ct.size() > 0) { - //cout << "subtree " << ct.size() << endl; - sumTrees(tree, ct); - ct = buildSubtree(bricks, tree); - } - //cout << "subtrees built" << endl; - uint tcnt = 0; - for (uint i = 0; i < tree.size(); ++i) - tcnt += tree[i].size(); - //cout << "built for " << tcnt << " from " << bricks.size() << endl; - if (tcnt < bricks.size()) { - for (uint i = 0; i < bricks.size(); ++i) { - b = bricks[i]; - for (uint j = 0; j < tree.size(); ++j) - for (uint k = 0; k < tree[j].size(); ++k) - if (tree[j][k] == b) goto next; - //cout << "add " << b->name().stdString() << " in level (" << b->level_ << ") ... " << flush; - if (b->level_ < 0) b->level_ = 0; - if ((uint)b->level_ < tree.size() && b->level_ >= 0) tree[b->level_].push_back(b); - else { - tree.resize(b->level_ + 1); - tree[b->level_].push_back(b); - } - //cout << "ok" << endl; - next: ; - } - } - int mlev = bricks[0]->level_; - for (uint i = 1; i < bricks.size(); ++i) - if (mlev > bricks[i]->level_) - mlev = bricks[i]->level_; - if (mlev >= 0) return; - piCout << "minimum level =" << mlev << ", so rebuild tree manually"; - tree.clear(); - int clev = 0; - uint cnt = 0; - PIVector level; - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->level_ -= mlev; - mlev = 0; - while (bricks.size() > cnt) { - level.clear(); - for (uint i = 0; i < bricks.size(); ++i) { - b = bricks[i]; - if (b->level_ != clev) continue; - level.push_back(b); - ++cnt; - } - if (level.size() > 0) tree.push_back(level); - ++clev; - } -} - - -PIVector > buildSubtree(PIVector & bricks, PIVector > & tree) { - PIVector bbs, tbs, level; - PIVector > ttree; - BrickBase * b, * sb; - int minlev, clev; - if (bricks.size() == 0) return ttree; - //cout << "initial stage" << endl; - /// Initial stage: first level - initialize bricks - for (uint i = 0; i < bricks.size(); ++i) { - b = bricks[i]; - if (b->level_ >= 0) continue; - bbs.push_back(b); - if (!b->isInitial()) { - tbs.push_back(b); - continue; - } - b->level_ = 0; - level.push_back(b); - } - if (level.size() == 0 && tbs.size() == 0) return ttree; - if (level.size() == 0) { - level.push_back(tbs[0]); - level.back()->level_ = 0; - tbs.pop_front(); - } - ttree.clear(); - ttree.push_back(level); - //cout << "first stage" << endl; - /// First pass: direct bricks bypass - while (tbs.size() > 0) { - level.clear(); - for (uint i = 0; i < ttree.back().size(); ++i) { - b = ttree.back()[i]; - b->tmp_++; - for (uint j = 0; j < b->connections.size(); ++j) { - sb = b->connections[j].brick_to; - if (sb->ticked) continue; - level.push_back(sb); - sb->level_ = ttree.size(); - for (uint k = 0; k < tbs.size(); ++k) { - if (tbs[k] == sb) { - tbs.remove(k); - break; - } - } - } - if (b->tmp_ >= b->inputs_count) b->ticked = true; - } - if (level.size() == 0) break; - ttree.push_back(level); - } - minlev = ttree[0][0]->level_; - if (tbs.size() == 0) goto third; - for (uint i = 0; i < ttree.size(); ++i) - for (uint j = 0; j < ttree[i].size(); ++j) - ttree[i][j]->ticked = true; - //cout << "second stage" << endl; - /// Second pass: ttree complement with invert bypass - while (tbs.size() > 0) { - level.clear(); - for (uint i = 0; i < tbs.size(); ++i) { - b = tbs[i]; - for (uint j = 0; j < b->connections.size(); ++j) { - sb = b->connections[j].brick_to; - if (!sb->ticked) continue; - level.push_back(b); - b->level_ = sb->level_ - 1; - if (minlev > b->level_) minlev = b->level_; - b->ticked = true; - for (uint k = 0; k < tbs.size(); ++k) { - if (tbs[k] == b) { - tbs.remove(k); - break; - } - } - i = 0; - } - } - if (level.size() == 0) break; - } - //cout << "third stage" << endl; - third: - /// Third pass: rebuild ttree - ttree.clear(); - clev = minlev; - level.push_back(0); - while (level.size() > 0) { - level.clear(); - for (uint i = 0; i < bbs.size(); ++i) { - b = bbs[i]; - if (b->level_ == clev) level.push_back(b); - } - if (level.size() > 0) ttree.push_back(level); - ++clev; - } - //cout << "final stage" << endl << endl; - /// Final stage: normalize levels - for (uint i = 0; i < ttree.size(); ++i) - for (uint j = 0; j < ttree[i].size(); ++j) - ttree[i][j]->level_ = i; - return ttree; -} - - -BrickCompositeInput::BrickCompositeInput(int inputs_num): BrickCompositeBase(0, inputs_num, inputs_num) { - type = "Input"; - setName(type); - io_Type = BrickBase::VariableOutputs; - outNames[0] = "0"; - note_ += "\"Parameters\" are labels for inputs that will be shown in parent composite brick"; - parameters[0].setValue(""); -} - - -BrickCompositeOutput::BrickCompositeOutput(int outputs_num): BrickCompositeBase(outputs_num, 0, outputs_num) { - type = "Output"; - setName(type); - io_Type = BrickBase::VariableInputs; - inNames[0] = "0"; - note_ += "\"Parameters\" are labels for outputs that will be shown in parent composite brick"; - parameters[0].setValue(""); -} - - -BrickCompositeParameters::BrickCompositeParameters(): BrickCompositeBase(0, 1, 1) { - type = "Parameters"; - setName(type); - io_Type = BrickBase::VariableOutputs; - note_ = "There are parameters you can set from parent brick\nEach parameter is float and should be typed in format: \"name[:default]\""; - parameters[0].setValue("p"); - outNames[0] = "p"; -} - - -void BrickCompositeParameters::parameterChanged(int index) { - PIString s = parameters[index].toString(); - int i = s.toLowerCase().find(':'); - if (i < 0) outNames[index] = s; - else outNames[index] = s.left(i).trim(); -} - - -BrickCompositeInformation::BrickCompositeInformation(): BrickCompositeBase(0, 0, 2) { - type = "Information"; - setName(type); - parametersName_ = "Information"; - paramNames[0] = "Type name"; - paramNames[1] = "Promt"; - note_ = "There are information fields about parent composite brick"; - parameters[0].setValue(""); - parameters[1].setValue(""); -} diff --git a/mbricks/brick_composite.h b/mbricks/brick_composite.h deleted file mode 100644 index 7c3511d..0000000 --- a/mbricks/brick_composite.h +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef BRICK_COMPOSITE_H -#define BRICK_COMPOSITE_H - -#include "brick_emits.h" -#include "brick_math.h" -#include "brick_logic.h" -#include "brick_digital.h" -#include "brick_link.h" -#include "brick_interface.h" -#include "brick_statistic.h" - -void buildTree(PIVector & bricks, PIVector > & tree); -PIVector > buildSubtree(PIVector & bricks, PIVector > & tree); -void sumTrees(PIVector > & ft, PIVector > & st); - - -class BrickCompositeBase: public BrickBase { - public: BrickCompositeBase(int inputs_num = 0, int outputs_num = 0, int parameters_num = 1): BrickBase(inputs_num, outputs_num, parameters_num) {lib = "Composite";} -}; - - -class BrickCompositeInput; -class BrickCompositeOutput; -class BrickCompositeParameters; -class BrickCompositeInformation; - - -class BrickComposite: public BrickCompositeBase { - MBRICK(BrickComposite) - BrickComposite(); - virtual bool tick_body(double time); - Mode mode() const {return mode_;} - void setMode(Mode m) {mode_ = m;} - void buildBrickTree() {buildTree(bricks, tree);} - void clear(); - void loadScheme(const PIString & file) {setParameterValue(0, file);} - static BrickBase * findBaseBrick(const PIString & code_name); - -protected: - virtual void parameterChanged(int index); - virtual void started() {parameterChanged(0); for (uint i = 0; i < bricks.size(); ++i) {bricks[i]->setManager(manager_); bricks[i]->started();}} - virtual void finished() {for (uint i = 0; i < bricks.size(); ++i) bricks[i]->finished();} - virtual void saveInputsToDefault() {defInputs = inputs; for (uint i = 0; i < bricks.size(); ++i) bricks[i]->saveInputsToDefault();} - virtual void resetOutputs() {for (uint i = 0; i < bricks.size(); ++i) bricks[i]->resetOutputs();} - virtual void reset_specified() {for (uint i = 0; i < bricks.size(); ++i) bricks[i]->reset_specified();} - virtual void reset() {BrickBase::reset(); for (uint i = 0; i < bricks.size(); ++i) bricks[i]->reset();} - virtual void proceedConnections(bool compositeToo = false); - - BrickBase * findBrick(const PIString & name); - static void loadBrick(PIConfig & sr, const PIString & prefix, BrickBase * b, BrickManager * m = 0); - static void fillBaseBricks(); - - static PIVector baseBricks; - PIVector bricks; - PIVector > tree; - PIVector * tl; - BrickCompositeInput * bi; - BrickCompositeOutput * bo; - BrickCompositeParameters * bp; - BrickCompositeInformation * inf; - Mode mode_; - int header_; - -}; -ADD_NEW_TO_COLLECTION(Composite, BrickComposite) - - -class BrickCompositeInput: public BrickCompositeBase { - MBRICK(BrickCompositeInput) - BrickCompositeInput(int inputs_num = 1); - virtual bool tick_body(double time) {return true;} - virtual void parameterChanged(int index) {if (outNames.size_s() > index) outNames[index] = parameters[index].toString();} - virtual void outputsCountChanged(int count) {setParametersCount(count, "");} -}; -ADD_NEW_TO_COLLECTION(Composite, BrickCompositeInput) - - -class BrickCompositeOutput: public BrickCompositeBase { - MBRICK(BrickCompositeOutput) - BrickCompositeOutput(int outputs_num = 1); - virtual bool tick_body(double time) {return true;} - virtual void parameterChanged(int index) {if (inNames.size_s() > index) inNames[index] = parameters[index].toString();} - virtual void inputsCountChanged(int count) {setParametersCount(count, "");} -}; -ADD_NEW_TO_COLLECTION(Composite, BrickCompositeOutput) - - -class BrickCompositeParameters: public BrickCompositeBase { - MBRICK(BrickCompositeParameters) - BrickCompositeParameters(); - virtual void outputsCountChanged(int count) {setParametersCount(count, "p");} - virtual void parameterChanged(int index); -}; -ADD_NEW_TO_COLLECTION(Composite, BrickCompositeParameters) - - -class BrickCompositeInformation: public BrickCompositeBase { - MBRICK(BrickCompositeInformation) - BrickCompositeInformation(); -}; -ADD_NEW_TO_COLLECTION(Composite, BrickCompositeInformation) - -#endif // BRICK_COMPOSITE_H diff --git a/mbricks/brick_digital.cpp b/mbricks/brick_digital.cpp deleted file mode 100644 index 3b25593..0000000 --- a/mbricks/brick_digital.cpp +++ /dev/null @@ -1,307 +0,0 @@ -#include "brick_digital.h" - - -bool BrickDigitalTriggerRS::tick_body(double time) { - if (inputs[R] > 0.) outputs[T] = 0.; - else if (inputs[S] > 0.) outputs[T] = 1.; - outputs[To] = 1. - outputs[T]; - return true; -} - - -bool BrickDigitalTriggerD::tick_body(double time) { - if (inputs[C] <= 0.) return true; - outputs[T] = dbool(inputs[D]); - outputs[To] = 1. - outputs[T]; - return true; -} - - -bool BrickDigitalTriggerJK::tick_body(double time) { - if (inputs[C] - pc <= 0. || inputs[C] <= 0.) { - pc = inputs[C]; - return true; - } - outputs[T] = dbool((1. - outputs[T]) * inputs[J] + outputs[T] * (1. - inputs[K])); - outputs[To] = 1. - outputs[T]; - pc = inputs[C]; - return true; -} - - -bool BrickDigitalCounter2::tick_body(double time) { - dv = inputs[C] - v; - v = inputs[C]; - if (inputs[R] > 0) { - outputs[0] = outputs[1] = 0.; - return true; - } - if (dv > 0.) { - outputs[0] = 1. - outputs[0]; - if (outputs[0] == 0.) { - outputs[1] = 1. - outputs[1]; - if (outputs[1] == 0.) - outputs[0] = 0.; - } - } - return true; -} - - -bool BrickDigitalCounter4::tick_body(double time) { - dv = inputs[C] - v; - v = inputs[C]; - if (inputs[R] > 0) { - outputs[0] = outputs[1] = outputs[2] = outputs[3] = 0.; - return true; - } - if (dv > 0.) { - outputs[0] = 1. - outputs[0]; - if (outputs[0] == 0.) { - outputs[1] = 1. - outputs[1]; - if (outputs[1] == 0.) { - outputs[2] = 1. - outputs[2]; - if (outputs[2] == 0.) { - outputs[3] = 1. - outputs[3]; - if (outputs[3] == 0.) - outputs[0] = outputs[1] = outputs[2] = 0.; - } - } - } - } - return true; -} - - -bool BrickDigitalCounter8::tick_body(double time) { - dv = inputs[C] - v; - v = inputs[C]; - if (inputs[R] > 0) { - for (int i = 0; i < 8; ++i) - outputs[i] = 0.; - return true; - } - if (dv > 0.) { - outputs[0] = 1. - outputs[0]; - if (outputs[0] == 0.) { - outputs[1] = 1. - outputs[1]; - if (outputs[1] == 0.) { - outputs[2] = 1. - outputs[2]; - if (outputs[2] == 0.) { - outputs[3] = 1. - outputs[3]; - if (outputs[3] == 0.) { - outputs[4] = 1. - outputs[4]; - if (outputs[4] == 0.) { - outputs[5] = 1. - outputs[5]; - if (outputs[5] == 0.) { - outputs[6] = 1. - outputs[6]; - if (outputs[6] == 0.) { - outputs[7] = 1. - outputs[7]; - if (outputs[7] == 0.) - for (int i = 0; i < 7; ++i) - outputs[i] = 0.; - } - } - } - } - } - } - } - } - return true; -} - - -bool BrickDigitalCoder2::tick_body(double time) { - int cv = 0; - for (int i = 3; i >= 0; --i) - if (inputs[i] > 0.) {cv = i; break;} - outputs[1] = cv / 2; - outputs[0] = cv % 2; - return true; -} - - -bool BrickDigitalCoder3::tick_body(double time) { - int cv = 0; - for (int i = 7; i >= 0; --i) - if (inputs[i] > 0.) {cv = i; break;} - outputs[2] = cv / 4; - outputs[1] = (cv % 4) / 2; - outputs[0] = cv % 2; - return true; -} - - -bool BrickDigitalCoder4::tick_body(double time) { - int cv = 0; - for (int i = 15; i >= 0; --i) - if (inputs[i] > 0.) {cv = i; break;} - outputs[3] = cv / 8; - outputs[2] = (cv % 8) / 4; - outputs[1] = (cv % 4) / 2; - outputs[0] = cv % 2; - return true; -} - - -bool BrickDigitalDecoder1::tick_body(double time) { - outputs[0] = outputs[1] = 0.; - if (inputs[0] > 0.) - outputs[1] = 1.; - else - outputs[0] = 1.; - return true; -} - - -bool BrickDigitalDecoder2::tick_body(double time) { - int cv; - for (int i = 0; i < 4; ++i) - outputs[i] = 0.; - cv = dbool(inputs[0]) + dbool(inputs[1]) * 2; - outputs[cv] = 1.; - return true; -} - - -bool BrickDigitalDecoder3::tick_body(double time) { - int cv; - for (int i = 0; i < 8; ++i) - outputs[i] = 0.; - cv = dbool(inputs[0]) + dbool(inputs[1]) * 2 + dbool(inputs[2]) * 4; - outputs[cv] = 1.; - return true; -} - - -bool BrickDigitalDecoder4::tick_body(double time) { - int cv; - for (int i = 0; i < 16; ++i) - outputs[i] = 0.; - cv = dbool(inputs[0]) + dbool(inputs[1]) * 2 + dbool(inputs[2]) * 4 + dbool(inputs[3]) * 8; - outputs[cv] = 1.; - return true; -} - - -bool BrickDigitalMux1::tick_body(double time) { - int cv; - cv = dbool(inputs[2]); - outputs[0] = inputs[cv]; - return true; -} - - -bool BrickDigitalMux2::tick_body(double time) { - int cv; - cv = dbool(inputs[4]) + dbool(inputs[5]) * 2; - outputs[0] = inputs[cv]; - return true; -} - - -bool BrickDigitalMux3::tick_body(double time) { - int cv; - cv = dbool(inputs[8]) + dbool(inputs[9]) * 2 + dbool(inputs[10]) * 4; - outputs[0] = inputs[cv]; - return true; -} - - -bool BrickDigitalMux4::tick_body(double time) { - int cv; - cv = dbool(inputs[16]) + dbool(inputs[17]) * 2 + dbool(inputs[18]) * 4 + dbool(inputs[19]) * 8; - outputs[0] = inputs[cv]; - return true; -} - - -bool BrickDigitalDemux1::tick_body(double time) { - outputs[0] = outputs[1] = 0.; - if (inputs[1] > 0.) - outputs[1] = inputs[0]; - else - outputs[0] = inputs[0]; - return true; -} - - -bool BrickDigitalDemux2::tick_body(double time) { - int cv; - for (int i = 0; i < 4; ++i) - outputs[i] = 0.; - cv = dbool(inputs[1]) + dbool(inputs[2]) * 2; - outputs[cv] = inputs[0]; - return true; -} - - -bool BrickDigitalDemux3::tick_body(double time) { - int cv; - for (int i = 0; i < 8; ++i) - outputs[i] = 0.; - cv = dbool(inputs[1]) + dbool(inputs[2]) * 2 + dbool(inputs[3]) * 4; - outputs[cv] = inputs[0]; - return true; -} - - -bool BrickDigitalDemux4::tick_body(double time) { - int cv; - for (int i = 0; i < 16; ++i) - outputs[i] = 0.; - cv = dbool(inputs[1]) + dbool(inputs[2]) * 2 + dbool(inputs[3]) * 4 + dbool(inputs[4]) * 8; - outputs[cv] = inputs[0]; - return true; -} - - -bool BrickDigitalDigitalToAnalog4::tick_body(double time) { - double v = 0; - for (int i = 0; i < 4; ++i) - v += pow2(i) * dbool(inputs[i]); - outputs[0] = inputs[4] * v / 15.; - return true; -} - - -bool BrickDigitalDigitalToAnalog8::tick_body(double time) { - double v = 0; - for (int i = 0; i < 8; ++i) - v += pow2(i) * dbool(inputs[i]); - outputs[0] = inputs[8] * v / 255.; - return true; -} - - -bool BrickDigitalAnalogToDigital2::tick_body(double time) { - double v = inputs[Input], t = inputs[Max] / 2.; - outputs[1] = (v >= t ? 1. : 0.); - if (outputs[1] > 0.) v -= t; - t /= 2.; - outputs[0] = (v >= t ? 1. : 0.); - return true; -} - - -bool BrickDigitalAnalogToDigital4::tick_body(double time) { - double v = inputs[Input], t = inputs[Max] / 2.; - for (int i = 3; i >= 0; --i) { - outputs[i] = (v >= t ? 1. : 0.); - if (outputs[i] > 0.) v -= t; - t /= 2.; - } - return true; -} - - -bool BrickDigitalAnalogToDigital8::tick_body(double time) { - double v = inputs[Input], t = inputs[Max] / 2.; - for (int i = 7; i >= 0; --i) { - outputs[i] = (v >= t ? 1. : 0.); - if (outputs[i] > 0.) v -= t; - t /= 2.; - } - return true; -} diff --git a/mbricks/brick_digital.h b/mbricks/brick_digital.h deleted file mode 100644 index 13f316f..0000000 --- a/mbricks/brick_digital.h +++ /dev/null @@ -1,279 +0,0 @@ -#ifndef BRICK_DIGITAL_H -#define BRICK_DIGITAL_H - -#include "brick_base.h" - - -class BrickDigitalBase: public BrickBase { -public: BrickDigitalBase(int inputs_num = 1, int outputs_num = 1, int parameters_num = 0): BrickBase(inputs_num, outputs_num, parameters_num) {lib = "Digital"; inNames[0] = outNames[0] = "0";} -}; - - -class BrickDigitalTriggerRS: public BrickDigitalBase { - MBRICK(BrickDigitalTriggerRS) - BrickDigitalTriggerRS(): BrickDigitalBase(2, 2) { - type = "RStrigger"; - setName(type); - inNames[0] = "S"; - inNames[1] = "R"; - outNames[0] = "T"; - outNames[1] = "To"; - } - enum Inputs {S, R}; - enum Outputs {T, To}; - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalTriggerRS) - - -class BrickDigitalTriggerD: public BrickDigitalBase { - MBRICK(BrickDigitalTriggerD) - BrickDigitalTriggerD(): BrickDigitalBase(2, 2) { - type = "Dtrigger"; - setName(type); - inNames[0] = "D"; - inNames[1] = "C"; - outNames[0] = "T"; - outNames[1] = "To"; - } - enum Inputs {D, C}; - enum Outputs {T, To}; - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalTriggerD) - - -class BrickDigitalTriggerJK: public BrickDigitalBase { - MBRICK(BrickDigitalTriggerJK) - BrickDigitalTriggerJK(): BrickDigitalBase(3, 2) { - type = "JKtrigger"; - setName(type); - inNames[0] = "J"; - inNames[1] = "C"; - inNames[2] = "K"; - outNames[0] = "T"; - outNames[1] = "To"; - } - enum Inputs {J, C, K}; - enum Outputs {T, To}; - virtual void reset_specified() {pc = 0.;} - virtual bool tick_body(double time); -private: - double pc; -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalTriggerJK) - - -class BrickDigitalCounter2: public BrickDigitalBase { - MBRICK(BrickDigitalCounter2) - BrickDigitalCounter2(): BrickDigitalBase(2, 2) {type = "Counter2"; setName(type); inNames[0] = "/ C"; inNames[1] = "R"; v = 0.;} - enum Inputs {C, R}; - virtual void reset_specified() {v = dv = 0.;} - virtual bool tick_body(double time); -private: - double v, dv; -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalCounter2) - - -class BrickDigitalCounter4: public BrickDigitalBase { - MBRICK(BrickDigitalCounter4) - BrickDigitalCounter4(): BrickDigitalBase(2, 4) {type = "Counter4"; setName(type); inNames[0] = "/ C"; inNames[1] = "R"; v = 0.;} - enum Inputs {C, R}; - virtual bool tick_body(double time); -private: - double v, dv; -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalCounter4) - - -class BrickDigitalCounter8: public BrickDigitalBase { - MBRICK(BrickDigitalCounter8) - BrickDigitalCounter8(): BrickDigitalBase(2, 8) {type = "Counter8"; setName(type); inNames[0] = "/ C"; inNames[1] = "R"; v = 0.;} - enum Inputs {C, R}; - virtual bool tick_body(double time); -private: - double v, dv; -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalCounter8) - - -class BrickDigitalCoder1: public BrickDigitalBase { - MBRICK(BrickDigitalCoder1) - BrickDigitalCoder1(): BrickDigitalBase(2, 1) {type = "Coder1"; setName(type);} - virtual bool tick_body(double time) {if (inputs[1] > 0.) outputs[0] = 1.; else outputs[0] = 0.; return true;} -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalCoder1) - - -class BrickDigitalCoder2: public BrickDigitalBase { - MBRICK(BrickDigitalCoder2) - BrickDigitalCoder2(): BrickDigitalBase(4, 2) {type = "Coder2"; setName(type);} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalCoder2) - - -class BrickDigitalCoder3: public BrickDigitalBase { - MBRICK(BrickDigitalCoder3) - BrickDigitalCoder3(): BrickDigitalBase(8, 3) {type = "Coder3"; setName(type);} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalCoder3) - - -class BrickDigitalCoder4: public BrickDigitalBase { - MBRICK(BrickDigitalCoder4) - BrickDigitalCoder4(): BrickDigitalBase(16, 4) {type = "Coder4"; setName(type);} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalCoder4) - - -class BrickDigitalDecoder1: public BrickDigitalBase { - MBRICK(BrickDigitalDecoder1) - BrickDigitalDecoder1(): BrickDigitalBase(1, 2) {type = "Decoder1"; setName(type);} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDecoder1) - - -class BrickDigitalDecoder2: public BrickDigitalBase { - MBRICK(BrickDigitalDecoder2) - BrickDigitalDecoder2(): BrickDigitalBase(2, 4) {type = "Decoder2"; setName(type);} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDecoder2) - - -class BrickDigitalDecoder3: public BrickDigitalBase { - MBRICK(BrickDigitalDecoder3) - BrickDigitalDecoder3(): BrickDigitalBase(3, 8) {type = "Decoder3"; setName(type);} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDecoder3) - - -class BrickDigitalDecoder4: public BrickDigitalBase { - MBRICK(BrickDigitalDecoder4) - BrickDigitalDecoder4(): BrickDigitalBase(4, 16) {type = "Decoder4"; setName(type);} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDecoder4) - - -class BrickDigitalMux1: public BrickDigitalBase { - MBRICK(BrickDigitalMux1) - BrickDigitalMux1(): BrickDigitalBase(3, 1) {type = "Mux1"; setName(type); inNames[2] = "A0";} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalMux1) - - -class BrickDigitalMux2: public BrickDigitalBase { - MBRICK(BrickDigitalMux2) - BrickDigitalMux2(): BrickDigitalBase(6, 1) {type = "Mux2"; setName(type); inNames[4] = "A0"; inNames[5] = "A1";} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalMux2) - - -class BrickDigitalMux3: public BrickDigitalBase { - MBRICK(BrickDigitalMux3) - BrickDigitalMux3(): BrickDigitalBase(11, 1) {type = "Mux3"; setName(type); inNames[8] = "A0"; inNames[9] = "A1"; inNames[10] = "A2";} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalMux3) - - -class BrickDigitalMux4: public BrickDigitalBase { - MBRICK(BrickDigitalMux4) - BrickDigitalMux4(): BrickDigitalBase(20, 1) {type = "Mux4"; setName(type); inNames[16] = "A0"; inNames[17] = "A1"; inNames[18] = "A2"; inNames[19] = "A3";} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalMux4) - - -class BrickDigitalDemux1: public BrickDigitalBase { - MBRICK(BrickDigitalDemux1) - BrickDigitalDemux1(): BrickDigitalBase(2, 2) {type = "Demux1"; setName(type); inNames[1] = "A0";} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDemux1) - - -class BrickDigitalDemux2: public BrickDigitalBase { - MBRICK(BrickDigitalDemux2) - BrickDigitalDemux2(): BrickDigitalBase(3, 4) {type = "Demux2"; setName(type); inNames[1] = "A0"; inNames[2] = "A1";} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDemux2) - - -class BrickDigitalDemux3: public BrickDigitalBase { - MBRICK(BrickDigitalDemux3) - BrickDigitalDemux3(): BrickDigitalBase(4, 8) {type = "Demux3"; setName(type); inNames[1] = "A0"; inNames[2] = "A1"; inNames[3] = "A2";} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDemux3) - - -class BrickDigitalDemux4: public BrickDigitalBase { - MBRICK(BrickDigitalDemux4) - BrickDigitalDemux4(): BrickDigitalBase(5, 16) {type = "Demux4"; setName(type); inNames[1] = "A0"; inNames[2] = "A1"; inNames[3] = "A2"; inNames[4] = "A3";} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDemux4) - - -class BrickDigitalDigitalToAnalog2: public BrickDigitalBase { - MBRICK(BrickDigitalDigitalToAnalog2) - BrickDigitalDigitalToAnalog2(): BrickDigitalBase(3, 1) {type = "DigitalToAnalog2"; setName(type); inNames[2] = "Max"; outNames[0] = "Output"; inputs[2] = 1.;} - virtual bool tick_body(double time) {outputs[0] = inputs[2] * (dbool(inputs[0]) + 2. * dbool(inputs[1])) / 3.; return true;} -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDigitalToAnalog2) - - -class BrickDigitalDigitalToAnalog4: public BrickDigitalBase { - MBRICK(BrickDigitalDigitalToAnalog4) - BrickDigitalDigitalToAnalog4(): BrickDigitalBase(5, 1) {type = "DigitalToAnalog4"; setName(type); inNames[4] = "Max"; outNames[0] = "Output"; inputs[4] = 1.;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDigitalToAnalog4) - - -class BrickDigitalDigitalToAnalog8: public BrickDigitalBase { - MBRICK(BrickDigitalDigitalToAnalog8) - BrickDigitalDigitalToAnalog8(): BrickDigitalBase(9, 1) { type = "DigitalToAnalog8"; setName(type); inNames[8] = "Max"; outNames[0] = "Output"; inputs[8] = 1.;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalDigitalToAnalog8) - - -class BrickDigitalAnalogToDigital2: public BrickDigitalBase { - MBRICK(BrickDigitalAnalogToDigital2) - BrickDigitalAnalogToDigital2(): BrickDigitalBase(2, 2) {type = "AnalogToDigital2"; setName(type); inNames[0] = "Input"; inNames[1] = "Max"; inputs[1] = 1.;} - enum Inputs {Input, Max}; - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalAnalogToDigital2) - - -class BrickDigitalAnalogToDigital4: public BrickDigitalBase { - MBRICK(BrickDigitalAnalogToDigital4) - BrickDigitalAnalogToDigital4(): BrickDigitalBase(2, 4) {type = "AnalogToDigital4"; setName(type); inNames[0] = "Input"; inNames[1] = "Max"; inputs[1] = 1.;} - enum Inputs {Input, Max}; - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalAnalogToDigital4) - - -class BrickDigitalAnalogToDigital8: public BrickDigitalBase { - MBRICK(BrickDigitalAnalogToDigital8) - BrickDigitalAnalogToDigital8(): BrickDigitalBase(2, 8) {type = "AnalogToDigital8"; setName(type); inNames[0] = "Input"; inNames[1] = "Max"; inputs[1] = 1.;} - enum Inputs {Input, Max}; - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Digital, BrickDigitalAnalogToDigital8) - -#endif // BRICK_DIGITAL_H diff --git a/mbricks/brick_emits.cpp b/mbricks/brick_emits.cpp deleted file mode 100644 index 6116414..0000000 --- a/mbricks/brick_emits.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "brick_emits.h" - - -bool BrickEmitLinear::tick_body(double time) { - outputs[0] = time * inputs[Slope] + inputs[StartValue]; - return true; -} - - -bool BrickEmitSin::tick_body(double time) { - outputs[0] = inputs[Amplitude] * sin(inputs[Frequency] * time * (M_PI + M_PI) + inputs[PhaseShift]); - //cout << time << "\t: " << outputs[0] << endl; - return true; -} - - -bool BrickEmitDelta::tick_body(double time) { - outputs[0] = (pt < inputs[Time] && time >= inputs[Time]) ? 1. / dt : 0.; - //cout << time << "\t: " << outputs[0] << endl; - return true; -} - - -bool BrickEmitStep::tick_body(double time) { - if (time == inputs[Time]) outputs[0] = (inputs[StartValue] + inputs[FinishValue]) / 2.; - else outputs[0] = (time > inputs[Time]) ? inputs[FinishValue] : inputs[StartValue]; - //cout << time << "\t: " << outputs[0] << endl; - return true; -} - - -bool BrickEmitPulse::tick_body(double time) { - outputs[0] = time < inputs[StartDelay] ? 0. : (residue(time - inputs[StartDelay], inputs[Period]) <= inputs[Duration]) ? inputs[Amplitude] : 0.; - //cout << time << "\t: " << outputs[0] << endl; - return true; -} diff --git a/mbricks/brick_emits.h b/mbricks/brick_emits.h deleted file mode 100644 index e0578d8..0000000 --- a/mbricks/brick_emits.h +++ /dev/null @@ -1,115 +0,0 @@ -#ifndef BRICK_EMITS_H -#define BRICK_EMITS_H - -#include "brick_base.h" -#include "brick_manager.h" - - -class BrickEmitBase: public BrickBase { -public: BrickEmitBase(int inputs_num = 1, int outputs_num = 1, int parameters_num = 0): BrickBase(inputs_num, outputs_num, parameters_num) {lib = "Emitters";} -}; - - -class BrickEmitConst: public BrickEmitBase { - MBRICK(BrickEmitConst) - BrickEmitConst(double value = 1.): BrickEmitBase(0, 1, 1) {parameters[0].setValue(value); type = "Const"; setName(type); paramNames[0] = "Const";} - void setValue(double value) {parameters[0].setValue(value);} - virtual bool tick_body(double time) {outputs[0] = parameters[0].toFloat(); return true;} -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitConst) - - -class BrickEmitLinear: public BrickEmitBase { - MBRICK(BrickEmitLinear) - BrickEmitLinear(double start_value = 0., double slope = 1.): BrickEmitBase(2, 1) {setParameters(start_value, slope); type = "Linear"; setName(type); inNames[0] = "Start"; inNames[1] = "Slope";} - enum Inputs {StartValue, Slope}; - void setParameters(double start_value, double slope) {inputs[StartValue] = start_value; inputs[Slope] = slope;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitLinear) - - -class BrickEmitSin: public BrickEmitBase { - MBRICK(BrickEmitSin) - BrickEmitSin(double amplitude = 1., double frequency = 1., double phase_shift = 0.): BrickEmitBase(3, 1) {setParameters(amplitude, frequency, phase_shift); type = "Sin"; setName(type); inNames[0] = "Amplitude"; inNames[1] = "Frequency, Hz"; inNames[2] = "Phase Shift, rad";} - enum Inputs {Amplitude, Frequency, PhaseShift}; - void setParameters(double amplitude, double frequency, double phase_shift) {inputs[Amplitude] = amplitude; inputs[Frequency] = frequency; inputs[PhaseShift] = phase_shift;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitSin) - - -class BrickEmitDelta: public BrickEmitBase { - MBRICK(BrickEmitDelta) - BrickEmitDelta(double time = 0.): BrickEmitBase(1, 1) {setTime(time); type = "Delta"; setName(type); inNames[0] = "Time, s";} - enum Inputs {Time}; - void setTime(double time) {inputs[Time] = time;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitDelta) - - -class BrickEmitStep: public BrickEmitBase { - MBRICK(BrickEmitStep) - BrickEmitStep(double time = 0., double start = 0., double finish = 1.): BrickEmitBase(3, 1) {setParameters(time, start, finish); type = "Step"; setName(type); inNames[0] = "Time, s"; inNames[1] = "Start Value"; inNames[2] = "Finish Value";} - enum Inputs {Time, StartValue, FinishValue}; - void setParameters(double time, double start, double finish) {inputs[Time] = time; inputs[StartValue] = start; inputs[FinishValue] = finish;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitStep) - - -class BrickEmitPulse: public BrickEmitBase { - MBRICK(BrickEmitPulse) - BrickEmitPulse(double ampl = 1., double period = 1., double duration = 0.5, double offset = 0.): BrickEmitBase(4, 1) {setParameters(ampl, period, duration, offset); type = "Pulse"; setName(type); inNames[0] = "Amplitude"; inNames[1] = "Period, s"; inNames[2] = "Duration, s"; inNames[3] = "Start Delay, s";} - enum Inputs {Amplitude, Period, Duration, StartDelay}; - void setParameters(double ampl, double period, double duration, double offset) {inputs[Amplitude] = ampl; inputs[Period] = period; inputs[Duration] = duration; inputs[StartDelay] = offset;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitPulse) - - -class BrickEmitStrobe: public BrickEmitBase { - MBRICK(BrickEmitStrobe) - BrickEmitStrobe(): BrickEmitBase(0, 1) {type = "Strobe"; setName(type);} - virtual bool tick_body(double time) {outputs[0] = 1. - outputs[0]; return true;} -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitStrobe) - - -class BrickEmitTime: public BrickEmitBase { - MBRICK(BrickEmitTime) - BrickEmitTime(): BrickEmitBase(0, 1) {type = "Time"; setName(type);} - virtual bool tick_body(double time) {outputs[0] = time; return true;} -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitTime) - - -class BrickEmitFrequency: public BrickEmitBase { - MBRICK(BrickEmitFrequency) - BrickEmitFrequency(): BrickEmitBase(0, 1) {type = "Frequency"; setName(type);} - virtual bool tick_body(double time) {outputs[0] = manager_->freq; return true;} -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitFrequency) - - -class BrickEmitNoiseRandom: public BrickEmitBase { - MBRICK(BrickEmitNoiseRandom) - BrickEmitNoiseRandom(double shift = 0., double scale = 1.): BrickEmitBase(2, 1) {setParameters(shift, scale); type = "Random"; setName(type); inNames[0] = "Shift"; inNames[1] = "Scale";} - enum Inputs {Shift, Scale}; - void setParameters(double shift, double scale) {inputs[Shift] = shift; inputs[Scale] = scale;} - virtual bool tick_body(double time) {outputs[0] = randomd() * inputs[Scale] + inputs[Shift]; return true;} -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitNoiseRandom) - - -class BrickEmitNoiseNormal: public BrickEmitBase { - MBRICK(BrickEmitNoiseNormal) - BrickEmitNoiseNormal(double shift = 0., double scale = 1.): BrickEmitBase(2, 1) {setParameters(shift, scale); type = "NormalNoise"; setName(type); inNames[0] = "Shift"; inNames[1] = "Scale";} - enum Inputs {Shift, Scale}; - void setParameters(double shift, double scale) {inputs[Shift] = shift; inputs[Scale] = scale;} - virtual bool tick_body(double time) {outputs[0] = randomn(inputs[Shift], inputs[Scale]); return true;} -}; -ADD_NEW_TO_COLLECTION(Emitters, BrickEmitNoiseNormal) - -#endif // BRICK_EMITS_H diff --git a/mbricks/brick_interface.cpp b/mbricks/brick_interface.cpp deleted file mode 100644 index 78450c6..0000000 --- a/mbricks/brick_interface.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "brick_interface.h" - - -bool BrickInterfaceBaseIn::received(void * t, uchar * d, int s) { - mutex.lock(); - BrickInterfaceBaseIn * i = (BrickInterfaceBaseIn * )t; - memcpy(i->data.data(), d + i->header_, i->data.size() - i->header_); - ///i->struct_.readData(i->data.data()); - mutex.unlock(); - return true; -} - - -bool BrickInterfaceBaseIn::tick_body(double time) { - mutex.lock(); - /**for (uint i = 0; i < struct_.count(); ++i) - outputs[i + 1] = struct_[i].value();*/ - mutex.unlock(); - return true; -} - - -bool BrickInterfaceBaseOut::tick_body(double time) { - mutex.lock(); - /**for (uint i = 0; i < struct_.count(); ++i) - struct_[i].setValue(inputs[i]); - struct_.writeData(data.data());*/ - mutex.unlock(); - return true; -} - - -void BrickInterfaceSerialIn::started() { - parameterChanged(0); - ///struct_.writeData(data.data()); - header_ = parameters[5].toInt(); - ser.stop(false); - ser.setDevice(parameters[1].toString()); - PIFlags f = 0; - if (parameters[2].toBool()) f |= PISerial::ParityControl; - if (parameters[3].toBool()) f |= PISerial::TwoStopBits; - ser.setParameters(f); - ser.setSpeed((PISerial::Speed)parameters[4].toInt()); - ser.setThreadedReadSlot(received); - ser.setThreadedReadData(this); - //ser.setReadData(data.data(), header_, data.size() - header_); - //cout << "header " << header_ << " bytes, data " << data.size() - header_ << " bytes" << endl; - ser.start(); -} - - -bool BrickInterfaceSerialIn::tick_body(double time) { - outputs[0] = dbool(ser.isOpened()); - return BrickInterfaceBaseIn::tick_body(time); -} - - -void BrickInterfaceSerialOut::started() { - parameterChanged(0); - ser.stop(false); - ser.setDevice(parameters[1].toString()); - PIFlags f = 0; - if (parameters[2].toBool()) f |= PISerial::ParityControl; - if (parameters[3].toBool()) f |= PISerial::TwoStopBits; - ser.setParameters(f); - ser.setSpeed((PISerial::Speed)parameters[4].toInt()); -} - - -bool BrickInterfaceSerialOut::tick_body(double time) { - BrickInterfaceBaseOut::tick_body(time); - ser.send(data.data(), data.size()); - outputs[0] = dbool(ser.isOpened()); - return true; -} - - -void BrickInterfaceUDPIn::started() { - parameterChanged(0); - eth.stop(false); - eth.setReadAddress(parameters[1].toString(), parameters[2].toInt()); - eth.setThreadedReadSlot(received); - eth.setThreadedReadData(this); - eth.start(); -} - - -bool BrickInterfaceUDPIn::tick_body(double time) { - outputs[0] = dbool(eth.isOpened()); - return BrickInterfaceBaseIn::tick_body(time); -} - - -bool BrickInterfaceUDPOut::tick_body(double time) { - BrickInterfaceBaseOut::tick_body(time); - if (eth.send(parameters[1].toString(), parameters[2].toInt(), data.data(), data.size())) - outputs[0] = 1.; - else outputs[0] = 0.; - return true; -} - - -void BrickInterfaceBinFileOut::started() { - if (file.isOpened()) file.close(); - file.open(parameters[1].toString()); - if (!file.isOpened()) file.open(parameters[1].toString(), PIIODevice::ReadWrite); - if (parameters[3].toBool() > 0) file.clear(); - file.seekToEnd(); - id = parameters[2].toInt(); -} - - -bool BrickInterfaceBinFileOut::tick_body(double time) { - BrickInterfaceBaseOut::tick_body(time); - ///file.writeToBinLog(id, data.data(), struct_.size()); - if (parameters[4].toBool() > 0) file.flush(); - return true; -} - diff --git a/mbricks/brick_interface.h b/mbricks/brick_interface.h deleted file mode 100644 index ba30c44..0000000 --- a/mbricks/brick_interface.h +++ /dev/null @@ -1,207 +0,0 @@ -#ifndef BRICK_INTERFACE_H -#define BRICK_INTERFACE_H - -#include "brick_base.h" -#include "piserial.h" -#include "piethernet.h" -#include "pifile.h" - -static PIMutex mutex; - -class BrickInterfaceBase: public BrickBase { -public: - BrickInterfaceBase(int inputs_num = 1, int outputs_num = 1, int parameters_num = 1): BrickBase(inputs_num, outputs_num, parameters_num + 1) { - lib = "Interfaces"; - paramNames[0] = "Struct file"; - parameters[0].setValue("struct_example.conf"); - parameters[0].setType(BrickBase::File); - header_ = 0; - note_ = "\"Struct file\" is a path to *.conf file that describe struct to send or receive. See \"struct_example.conf\" for details."; - rtOnly = true; - } - virtual ~BrickInterfaceBase() {;} -protected: - virtual void parameterChanged(int index) { - if (index != 0) return; - mutex.lock(); - ///struct_.parseFile(parameters[0].toString()); - ///data.resize(struct_.size()); - mutex.unlock(); - } - ///PIStruct struct_; - PIVector data; - int header_; -}; - -class BrickInterfaceBaseIn: public BrickInterfaceBase { -public: - BrickInterfaceBaseIn(int inputs_num = 1, int outputs_num = 1, int parameters_num = 1): BrickInterfaceBase(inputs_num, outputs_num, parameters_num) {parameterChanged(0);} - virtual ~BrickInterfaceBaseIn() {;} -protected: - static bool received(void * t, uchar * d, int s); - virtual bool tick_body(double time); - virtual void parameterChanged(int index) { - if (index != 0) return; - BrickInterfaceBase::parameterChanged(0); - /**setOutputsCount(struct_.count() + 1); - for (uint i = 0; i < struct_.count(); ++i) { - outNames[i + 1] = struct_[i].name(); - outputs[i + 1] = struct_[i].value(); - }*/ - } -}; - -class BrickInterfaceBaseOut: public BrickInterfaceBase { -public: - BrickInterfaceBaseOut(int inputs_num = 1, int outputs_num = 1, int parameters_num = 1): BrickInterfaceBase(inputs_num, outputs_num, parameters_num) {parameterChanged(0);} - virtual ~BrickInterfaceBaseOut() {;} -protected: - virtual bool tick_body(double time); - virtual void parameterChanged(int index) { - if (index != 0) return; - BrickInterfaceBase::parameterChanged(0); - /**setInputsCount(struct_.count()); - for (uint i = 0; i < struct_.count(); ++i) { - inNames[i] = struct_[i].name(); - inputs[i] = struct_[i].value(); - }*/ - } -}; - - -class BrickInterfaceSerialIn: public BrickInterfaceBaseIn { - MBRICK(BrickInterfaceSerialIn) - BrickInterfaceSerialIn(): BrickInterfaceBaseIn(0, 1,5) { - type = "SerialIn"; - setName(type); - outNames[0] = "Init"; - paramNames[1] = "Device"; - paramNames[2] = "Parity control"; - paramNames[3] = "Two stop bits"; - paramNames[4] = "Baud rate"; - paramNames[5] = "Header size"; -#ifdef WINDOWS - parameters[1].setValue("COM1"); -#else - parameters[1].setValue("/dev/ttyS0"); -#endif - parameters[2].setValue(false); - parameters[3].setValue(false); - parameters[4].setValue(115200); - parameters[5].setValue(0); - note_ += "\n\"Header size\" is a bytes count from begin of struct, that contains packet attribute (need for advanced receive algorithm)."; - note_ += "\n\"Device\" is a name of your serial device, e.g. \"/dev/ttyS0\" or \"COM1\"."; - } - virtual ~BrickInterfaceSerialIn() {;} - virtual void started(); - virtual void finished() {ser.stop(false);} - virtual bool tick_body(double time); -private: - PISerial ser; -}; -ADD_NEW_TO_COLLECTION(Interfaces, BrickInterfaceSerialIn) - - -class BrickInterfaceSerialOut: public BrickInterfaceBaseOut { - MBRICK(BrickInterfaceSerialOut) - BrickInterfaceSerialOut(): BrickInterfaceBaseOut(0, 1, 4) { - type = "SerialOut"; - setName(type); - outNames[0] = "Init"; - paramNames[1] = "Device"; - paramNames[2] = "Parity control"; - paramNames[3] = "Two stop bits"; - paramNames[4] = "Baud rate"; -#ifdef WINDOWS - parameters[1].setValue("COM1"); -#else - parameters[1].setValue("/dev/ttyS0"); -#endif - parameters[2].setValue(false); - parameters[3].setValue(false); - parameters[4].setValue(115200); - note_ += "\n\"Header size\" is a bytes count from begin of struct, that contains packet attribute (need for advanced receive algorithm)."; - note_ += "\n\"Device\" is a name of your serial device, e.g. \"/dev/ttyS0\" or \"COM1\"."; - } - virtual ~BrickInterfaceSerialOut() {;} - virtual void started(); - virtual void finished() {ser.stop(false);} - virtual bool tick_body(double time); -private: - PISerial ser; -}; -ADD_NEW_TO_COLLECTION(Interfaces, BrickInterfaceSerialOut) - - -class BrickInterfaceUDPIn: public BrickInterfaceBaseIn { - MBRICK(BrickInterfaceUDPIn) - BrickInterfaceUDPIn(): BrickInterfaceBaseIn(0, 1, 2) { - type = "UDPIn"; - setName(type); - outNames[0] = "Init"; - paramNames[1] = "IP"; - paramNames[2] = "Port"; - parameters[1].setValue("127.0.0.1"); - parameters[2].setValue(1638); - } - virtual ~BrickInterfaceUDPIn() {;} - virtual void started(); - virtual void finished() {eth.stop(false);} - virtual bool tick_body(double time); -private: - PIEthernet eth; -}; -ADD_NEW_TO_COLLECTION(Interfaces, BrickInterfaceUDPIn) - - -class BrickInterfaceUDPOut: public BrickInterfaceBaseOut { - MBRICK(BrickInterfaceUDPOut) - BrickInterfaceUDPOut(): BrickInterfaceBaseOut(0, 1, 2) { - type = "UDPOut"; - setName(type); - outNames[0] = "Init"; - paramNames[1] = "IP"; - paramNames[2] = "Port"; - parameters[1].setValue("127.0.0.1"); - parameters[2].setValue(1638); - } - virtual ~BrickInterfaceUDPOut() {;} - virtual void started() {parameterChanged(0); eth.open();} - //virtual void finished() {eth.terminate();} - virtual bool tick_body(double time); -private: - PIEthernet eth; -}; -ADD_NEW_TO_COLLECTION(Interfaces, BrickInterfaceUDPOut) - - -class BrickInterfaceBinFileOut: public BrickInterfaceBaseOut { - MBRICK(BrickInterfaceBinFileOut) - BrickInterfaceBinFileOut(): BrickInterfaceBaseOut(0, 0, 4) { - type = "BinFileOut"; - setName(type); - paramNames[1] = "File"; - paramNames[2] = "ID(Dec)"; - paramNames[3] = "Overwrite"; - paramNames[4] = "Flush"; - parameters[1].setValue("binlog.dat"); - parameters[2].setValue(1); - parameters[3].setValue(false); - parameters[4].setValue(false); - parameters[1].setType(BrickBase::File); - note_ += "\nIf \"Overwrite\" is true file will be cleared before every start."; - note_ += "\nIf \"Flush\" is true file will be flushed after every tick."; - note_ += "\nb{NOTE:} this brick create binary file for \"Log Parser\"."; - rtOnly = false; - } - virtual ~BrickInterfaceBinFileOut() {;} - virtual void started(); - virtual void finished() {file.flush(); file.close();} - virtual bool tick_body(double time); -private: - ushort id; - PIFile file; -}; -ADD_NEW_TO_COLLECTION(Interfaces, BrickInterfaceBinFileOut) - -#endif // BRICK_INTERFACE_H diff --git a/mbricks/brick_link.cpp b/mbricks/brick_link.cpp deleted file mode 100644 index 42826cc..0000000 --- a/mbricks/brick_link.cpp +++ /dev/null @@ -1,158 +0,0 @@ -#include "brick_link.h" - - -BrickLinkTransferFunction::BrickLinkTransferFunction(const PIString & num, const PIString & denom): BrickLinkBase(1, 1, 2) { - type = "TransferFunction"; - setName(type); - paramNames[1] = "Numerator"; - paramNames[2] = "Denominator"; - parameters[1] = num; - parameters[2] = denom; - interactive = true; - parameterChanged(1); -} - - -void BrickLinkTransferFunction::parameterChanged(int index) { - if (index == 0) { - KF.setMethod((PIMathSolver::Method)parameters[0].toInt()); - return; - } - PIString ts; - note_ = "Your function:\n\n"; - PIStringList sl = parameters[1].toString().split(" "); - sl.removeStrings(""); sl.removeStrings("\t"); - TF.vector_Bm.resize(sl.size()); - for (int i = sl.size_s() - 1; i >= 0; --i) { - TF.vector_Bm[i] = sl[sl.size_s() - 1 - i].toDouble(); - ts += PIString::fromNumber(TF.vector_Bm[i]); - if (i > 1) { - ts += "s^{" + PIString::fromNumber(i) + "} + "; - continue; - } - if (i > 0) ts += "s" + PIString(" + "); - } - if (ts.left(2) == "1s") ts.pop_front(); - if (ts.left(3) == "-1s") ts.replace(0, 2, "-"); - note_ += ts + "\n---\n"; - sl = parameters[2].toString().split(" "); - sl.removeStrings(""); sl.removeStrings("\t"); - TF.vector_An.resize(sl.size()); - ts.clear(); - for (int i = sl.size_s() - 1; i >= 0; --i) { - TF.vector_An[i] = sl[sl.size_s() - 1 - i].toDouble(); - ts += PIString::fromNumber(TF.vector_An[i]); - if (i > 1) { - ts += "s^{" + PIString::fromNumber(i) + "} + "; - continue; - } - if (i > 0) ts += "s" + PIString(" + "); - } - if (ts.left(2) == "1s") ts.pop_front(); - if (ts.left(3) == "-1s") ts.replace(0, 2, "-"); - note_ += ts; - note_.replaceAll("+ -", "- "); - note_.replaceAll(" + 1s", " + s"); - note_.replaceAll(" - 1s", " - s"); -} - - -bool BrickLinkTransferFunction::tick_body(double time) { - KF.setTime(time); - KF.solve(inputs[0], dt); - outputs[0] = KF.X[0]; - return true; -} - - -BrickLinkFilter1Degree::BrickLinkFilter1Degree(double t, double k): BrickLinkBase(3, 2) { - setParameters(t, k); - type = "Filter1Degree"; - setName(type); - inNames[1] = "T"; - inNames[2] = "K"; - outNames[0] = "Low Pass"; - outNames[1] = "High Pass"; - s.setPreamp(1, -1.); - makeConnections(); -} - - -bool BrickLinkFilter1Degree::tick_body(double time) { - if (inputs[T] == 0.) return false; - a.setGain(1. / inputs[T]); - s.setPreamp(0, inputs[K]); - s.setInputValue(0, inputs[Input]); - s.tick(time); - s.proceedConnections(); - a.tick(time); - a.proceedConnections(); - i.tick(time); - i.proceedConnections(); - outputs[LowPass] = i.output(); - outputs[HighPass] = s.output(); - return true; -} - - -BrickLinkFilterBandpass::BrickLinkFilterBandpass(double lf, double hf): BrickLinkBase(3, 1) { - TF.vector_Bm.resize(2); - TF.vector_An.resize(3, 1.); - type = "FilterBandpass"; - setName(type); - inNames[1] = "Low Freq"; - inNames[2] = "High Freq"; - setParameters(lf, hf); - pl = ph = 0.; -} - - -bool BrickLinkFilterBandpass::tick_body(double time) { - if (pl != inputs[LF] || ph != inputs[HF]) { - pl = inputs[LF]; - ph = inputs[HF]; - if (pl != 0.) t0 = 1. / pl; - else t0 = 0.; - if (ph != 0.) t1 = 1. / ph; - else t1 = 0.; - TF.vector_Bm[0] = 0.; - TF.vector_Bm[1] = t0; - TF.vector_An[0] = 1.; - TF.vector_An[1] = t0 + t1; - TF.vector_An[2] = t0 * t1; - KF.fromTF(TF); - } - KF.solve(inputs[0], dt); - outputs[0] = KF.X[0]; - return true; -} - - - -BrickLinkFilterMedian::BrickLinkFilterMedian(int count): BrickLinkBase(1, 1, 0) { - type = "FilterMedian"; - setName(type); - paramNames[0] = "Count"; - parameters[0].setValue(count); - note_ = "Size of window"; - setParameters(count); -} - - -bool BrickLinkFilterMedian::tick_body(double time) { - for (int i = 0; i < hist_.size_s() - 1; ++i) - hist_[i] = hist_[i + 1]; - hist_.back() = inputs[0]; - hsort = hist_; - hsort.sort(); - int size = hsort.size_s(); - if (size == 0) { - outputs[0] = 0.; - } else { - if (size % 2 == 1) - outputs[0] = hsort[size / 2]; - else - outputs[0] = (hsort[size / 2 - 1] + hsort[size / 2]) / 2.; - } - return true; -} diff --git a/mbricks/brick_link.h b/mbricks/brick_link.h deleted file mode 100644 index ec4341b..0000000 --- a/mbricks/brick_link.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef BRICK_LINK_H -#define BRICK_LINK_H - -#include "brick_math.h" - - -class BrickLinkBase: public BrickBase { -public: - BrickLinkBase(int inputs_num = 1, int outputs_num = 1, int parameters_num = 0): BrickBase(inputs_num, outputs_num, parameters_num + 1) { - lib = "Links"; - paramNames[0] = "Method"; - parameters[0].setValue(-1); - note_ = "Methods description you can find in help content.";//PIMathSolver::methods_desc; - } -}; - - -class BrickLinkTransferFunction: public BrickLinkBase { - MBRICK(BrickLinkTransferFunction) - BrickLinkTransferFunction(const PIString & num = "1", const PIString & denom = "1 1"); - virtual void parameterChanged(int index); - virtual void reset_specified() {started();} - virtual void started() {parameterChanged(1); KF.fromTF(TF);} - virtual bool tick_body(double time); -private: - void copied() {reset_specified();} - PIMathSolver KF; - TransferFunction TF; -}; - - -class BrickLinkFilter1Degree: public BrickLinkBase { - MBRICK(BrickLinkFilter1Degree) - BrickLinkFilter1Degree(double t = 1., double k = 1.); - virtual void reset_specified() {s.reset(); a.reset(); i.reset(); i.setInputValue(1, 1.);} - enum Inputs {Input, T, K}; - enum Outputs {LowPass, HighPass}; - void setParameters(double t, double k) {inputs[T] = t; inputs[K] = k;} - virtual void started() {parameterChanged(0);} - virtual bool tick_body(double time); - virtual void parameterChanged(int index) {i.setParameterValue(index, parameters[index]);} -private: - void copied() {clearConnections(); makeConnections();} - void clearConnections() {s.clearConnections(); a.clearConnections(); i.clearConnections();} - void makeConnections() {connect(s, 0, a, BrickMathAmplifier::Input); BrickBase::connect(a, 0, i, 0); BrickBase::connect(i, 0, s, 1);} - BrickMathSum s; - BrickMathAmplifier a; - BrickMathIntegral i; -}; -ADD_NEW_TO_COLLECTION(Links, BrickLinkFilter1Degree) - - -class BrickLinkFilterBandpass: public BrickLinkBase { - MBRICK(BrickLinkFilterBandpass) - BrickLinkFilterBandpass(double lf = 1., double hf = 2.); - enum Inputs {Input, LF, HF}; - inline void setParameters(double lf, double hf) {inputs[LF] = lf; inputs[HF] = hf;} - virtual void parameterChanged(int index) {KF.setMethod((PIMathSolver::Method)parameters[0].toInt());} - virtual void reset_specified() {started();} - virtual void started() {pl = ph = 0.; setParameters(inputs[LF], inputs[HF]); KF.fromTF(TF);} - virtual bool tick_body(double time); -private: - double pl, ph, t0, t1; - PIMathSolver KF; - TransferFunction TF; -}; -ADD_NEW_TO_COLLECTION(Links, BrickLinkFilterBandpass) - - -class BrickLinkFilterMedian: public BrickLinkBase { - MBRICK(BrickLinkFilterMedian) - BrickLinkFilterMedian(int count = 3); - inline void setParameters(int count) {parameters[0].setValue(piMaxi(count, 0)); hist_.resize(count);} - virtual void parameterChanged(int index) {hist_.resize(parameters[0].toInt()); hsort.resize(hist_.size());} - virtual void reset_specified() {started();} - virtual void started() {hist_.fill(0.);} - virtual bool tick_body(double time); -private: - PIVector hist_, hsort; -}; -ADD_NEW_TO_COLLECTION(Links, BrickLinkFilterMedian) - -#endif // BRICK_LINK_H diff --git a/mbricks/brick_logic.cpp b/mbricks/brick_logic.cpp deleted file mode 100644 index d2932be..0000000 --- a/mbricks/brick_logic.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "brick_logic.h" - - -bool BrickLogicAnd::tick_body(double time) { - bool b = true; - for (int i = 0; i < inputs_count; ++i) - if (inputs[i] <= 0) {b = false; break;} - outputs[0] = dbool(b); - return true; -} - - -bool BrickLogicOr::tick_body(double time) { - bool b = false; - for (int i = 0; i < inputs_count; ++i) - if (inputs[i] > 0) {b = true; break;} - outputs[0] = dbool(b); - return true; -} - - -bool BrickLogicXor::tick_body(double time) { - bool b = false; - for (int i = 0; i < inputs_count; ++i) { - if (inputs[i] > 0) b = (b ^ true); - else b = b ^ false; - } - outputs[0] = dbool(b); - return true; -} - - -bool BrickLogicNAnd::tick_body(double time) { - bool b = true; - for (int i = 0; i < inputs_count; ++i) - if (inputs[i] <= 0) {b = false; break;} - outputs[0] = 1. - dbool(b); - return true; -} - - -bool BrickLogicNOr::tick_body(double time) { - bool b = false; - for (int i = 0; i < inputs_count; ++i) - if (inputs[i] > 0) {b = true; break;} - outputs[0] = 1. - dbool(b); - return true; -} - - -bool BrickLogicNXor::tick_body(double time) { - bool b = false; - for (int i = 0; i < inputs_count; ++i) { - if (inputs[i] > 0) b = (b ^ true); - else b = b ^ false; - } - outputs[0] = 1. - dbool(b); - return true; -} - - -bool BrickLogicCompare::tick_body(double time) { - outputs[0] = outputs[1] = outputs[2] = 0.; - if (inputs[0] > inputs[1]) outputs[0] = 1.; - if (inputs[0] < inputs[1]) outputs[2] = 1.; - if (fabs(inputs[0] - inputs[1]) <= inputs[Tolerance]) outputs[1] = 1.; - return true; -} diff --git a/mbricks/brick_logic.h b/mbricks/brick_logic.h deleted file mode 100644 index 40bdd3f..0000000 --- a/mbricks/brick_logic.h +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef BRICK_LOGIC_H -#define BRICK_LOGIC_H - -#include "brick_base.h" - -class BrickLogicBase: public BrickBase { -public: BrickLogicBase(int inputs_num = 1, int outputs_num = 1, int parameters_num = 0): BrickBase(inputs_num, outputs_num, parameters_num) {lib = "Logics";} -}; - -class BrickLogicNot: public BrickLogicBase { - MBRICK(BrickLogicNot) - BrickLogicNot(): BrickLogicBase(1, 1) {type = "Not"; setName(type);} - virtual bool tick_body(double time) {outputs[0] = (inputs[0] > 0. ? 0. : 1.); return true;} -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicNot) - - -class BrickLogicAnd: public BrickLogicBase { - MBRICK(BrickLogicAnd) - BrickLogicAnd(int inputs_num = 2): BrickLogicBase(inputs_num, 1) {type = "And"; setName(type); inNames[0] = ""; io_Type = BrickBase::VariableInputs;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicAnd) - - -class BrickLogicOr: public BrickLogicBase { - MBRICK(BrickLogicOr) - BrickLogicOr(int inputs_num = 2): BrickLogicBase(inputs_num, 1) {type = "Or"; setName(type); inNames[0] = ""; io_Type = BrickBase::VariableInputs;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicOr) - - -class BrickLogicXor: public BrickLogicBase { - MBRICK(BrickLogicXor) - BrickLogicXor(int inputs_num = 2): BrickLogicBase(inputs_num, 1) {type = "Xor"; setName(type); inNames[0] = ""; io_Type = BrickBase::VariableInputs;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicXor) - - -class BrickLogicNAnd: public BrickLogicBase { - MBRICK(BrickLogicNAnd) - BrickLogicNAnd(int inputs_num = 2): BrickLogicBase(inputs_num, 1) {type = "NAnd"; setName(type); inNames[0] = ""; io_Type = BrickBase::VariableInputs;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicNAnd) - - -class BrickLogicNOr: public BrickLogicBase { - MBRICK(BrickLogicNOr) - BrickLogicNOr(int inputs_num = 2): BrickLogicBase(inputs_num, 1) {type = "NOr"; setName(type); inNames[0] = ""; io_Type = BrickBase::VariableInputs;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicNOr) - - -class BrickLogicNXor: public BrickLogicBase { - MBRICK(BrickLogicNXor) - BrickLogicNXor(int inputs_num = 2): BrickLogicBase(inputs_num, 1) {type = "NXor"; setName(type); inNames[0] = ""; io_Type = BrickBase::VariableInputs;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicNXor) - - -class BrickLogicMemory: public BrickLogicBase { - MBRICK(BrickLogicMemory) - BrickLogicMemory(): BrickLogicBase(2, 1) {type = "Memory"; setName(type); inNames[1] = "Write";} - enum Inputs {Input, Write}; - virtual bool tick_body(double time) {if (inputs[Write] > 0) outputs[0] = inputs[Input]; return true;} -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicMemory) - - -class BrickLogicCompare: public BrickLogicBase { - MBRICK(BrickLogicCompare) - BrickLogicCompare(double tolerance = 0.): BrickLogicBase(3, 3) {setTolerance(tolerance); type = "Compare"; setName(type); inNames[0] = ""; inNames[2] = "Tolerance"; outNames[0] = "<"; outNames[1] = "="; outNames[2] = ">";} - enum Inputs {Tolerance = 2}; - void setTolerance(double tolerance) {inputs[Tolerance] = tolerance;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Logics, BrickLogicCompare) - -#endif // BRICK_LOGIC_H diff --git a/mbricks/brick_manager.cpp b/mbricks/brick_manager.cpp deleted file mode 100644 index 361c642..0000000 --- a/mbricks/brick_manager.cpp +++ /dev/null @@ -1,321 +0,0 @@ -#include "brick_manager.h" -#include "brick_composite.h" - - -BrickManager::BrickManager(double frequency, double time_step) { - timer = new PITimer(run, this, PITimer::ThreadRT); - setFrequency(frequency, time_step); - ctime = 0.; - paused = false; - wasReset = true; - mode_ = BrickBase::Asynchronous; - BrickComposite::fillBaseBricks(); -} - - -void BrickManager::setFrequency(double frequency, double time_step) { - freq = frequency; - tstep = (time_step == 0.) ? 1. / frequency : time_step; - if (!isRunning()) return; - stop(); - start(); -} - - -void BrickManager::addBrick(BrickBase * brick, bool uniqueName) { - //lock(); - pause(); - brick->saveInputsToDefault(); - if (uniqueName) brick->setName(getUniqueName(brick->name())); - brick->setManager(this); - bricks.push_back(brick); - if (mode_ == BrickBase::Asynchronous) buildSchemeTree(); - resume(); - //unlock(); -} - - -void BrickManager::removeBrick(BrickBase * brick) { - for (uint i = 0; i < bricks.size(); ++i) { - if (bricks[i] == brick) { - disconnectBrick(brick); - bricks.remove(i); - buildSchemeTree(); - return; - } - } -} - - -void BrickManager::replaceBrick(BrickBase * old_b, BrickBase * new_b) { - PIVector conns = old_b->connections; - PIFlags ion = new_b->io_Type; - BrickBase * cb; - new_b->setName(getUniqueName(new_b->name())); - new_b->freqDivider_ = old_b->freqDivider_; - new_b->manager_ = old_b->manager_; - if (ion[BrickBase::VariableInputs]) - new_b->setInputsCount(old_b->inputsCount()); - if (ion[BrickBase::VariableOutputs]) - new_b->setOutputsCount(old_b->outputsCount()); - for (uint i = 0; i < bricks.size(); ++i) { - cb = bricks[i]; - if (cb == old_b) { - bricks[i] = new_b; - for (uint j = 0; j < conns.size(); ++j) { - if (conns[j].out_num > new_b->outputsCount()) continue; - new_b->addConnection(conns[j]); - } - continue; - } - for (int j = 0; j < cb->connectionsCount(); ++j) { - if (cb->connections[j].brick_to != old_b) continue; - if (cb->connections[j].out_num > new_b->inputsCount()) - cb->removeConnection(j); - else - cb->connections[j].brick_to = new_b; - } - } - PIVector * tl; - for (uint i = 0; i < tree.size(); ++i) { - tl = &tree[i]; - for (uint j = 0; j < tl->size(); ++j) { - if (tl->at(j) != old_b) continue; - (*tl)[j] = new_b; - } - } - int pc = piMin(old_b->parametersCount(), new_b->parametersCount()), ic = piMin(old_b->inputsCount(), new_b->inputsCount()); - for (int i = 0; i < pc; ++i) - new_b->setParameterValueOnly(i, old_b->parameter(i).toString()); - for (int i = 0; i < ic; ++i) - new_b->setInputValue(i, old_b->input(i)); - //delete old_b; -} - - -void BrickManager::tick(bool realTime) { - //cout << "tick\n"; - if (paused) return; - wasReset = false; - if (realTime) { - //lock(); - switch (mode_) { - case BrickBase::Synchronous: - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->tick(ctime); - proceedConnections(); - break; - case BrickBase::Asynchronous: - PIVector * tl; - for (uint i = 0; i < tree.size(); ++i) { - tl = &tree[i]; - for (uint j = 0; j < tl->size(); ++j) - (*tl)[j]->tick(ctime); - for (uint j = 0; j < tl->size(); ++j) - (*tl)[j]->proceedConnections(); - } - break; - } - ctime += tstep; - //unlock(); - } else { - switch (mode_) { - case BrickBase::Synchronous: - for (uint i = 0; i < bricks.size(); ++i) - if (!bricks[i]->isRealTimeOnly()) - bricks[i]->tick(ctime); - proceedConnections(); - break; - case BrickBase::Asynchronous: - PIVector * tl; - for (uint i = 0; i < tree.size(); ++i) { - tl = &tree[i]; - for (uint j = 0; j < tl->size(); ++j) - if (!(*tl)[j]->isRealTimeOnly()) - (*tl)[j]->tick(ctime); - for (uint j = 0; j < tl->size(); ++j) - (*tl)[j]->proceedConnections(); - } - break; - } - ctime += tstep; - } - //cout << bricks[0]->output() << endl; -} - - -void BrickManager::proceedConnections(bool compositeToo) { - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->proceedConnections(compositeToo); -} - - -void BrickManager::start() { - paused = false; - startBricks(); - timer->stop(); -#ifdef WINDOWS - timer->waitForFinish(); -#endif - timer->start(1000. / freq); -} - - -void BrickManager::stop() { - paused = false; - timer->stop(); - stopBricks(); -} - - -void BrickManager::reset() { - paused = false; - wasReset = true; - if (isRunning()) lock(); - for (int i = 0; i < 3; ++i) { - resetOutputs(); - proceedConnections(true); - } - saveInputsToDefault(); - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->reset(); - ctime = 0.; - if (isRunning()) unlock(); -} - - -void BrickManager::resetOutputs() { - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->resetOutputs(); -} - - -void BrickManager:: resetScheme() { - stop(); - paused = false; - tree.clear(); - for (uint i = 0; i < bricks.size(); ++i) - delete bricks[i]; - bricks.clear(); -} - - -bool BrickManager::loadScheme(const PIString & file) { - PIConfig sr(file); - if (!sr.isOpened()) return false; - resetScheme(); - - setFrequency(sr.getValue("Real-time Frequency", 100), sr.getValue("Real-time Step", 0.)); - setMode((BrickBase::Mode)(int)sr.getValue("Mode", 1)); - PIMathSolver::method_global = static_cast((int)sr.getValue("PIMathSolver", 0)); - - PIStringList names = sr.getValue("Bricks", PIStringList()); - PIString prefix, cname; - BrickBase * b, * bf, * bt; - if (BrickComposite::baseBricks.size() == 0) - BrickComposite::fillBaseBricks(); - for (int i = 0; i < names.size_s(); ++i) { - prefix = names[i] + " "; - cname = sr.getValue(prefix + "codeName", ""); - b = BrickComposite::findBaseBrick(cname); - if (b == 0) { - piCout << "Error while loading scheme: can`t find brick \"" << cname << "\""; - continue; - } else - b = b->copy(); - loadBrick(sr, prefix, b); - bricks.push_back(b); - } - int cnt = sr.getValue("Connections count", 0); - for (int i = 0; i < cnt; ++i) { - prefix = "Connection " + PIString::fromNumber(i) + " "; - bf = findBrick(sr.getValue(prefix + "from Name", "").value().stdString()); - bt = findBrick(sr.getValue(prefix + "to Name", "").value().stdString()); - if (bf == 0 || bt == 0) { - piCout << "Error while loading scheme: can`t create connection " << i; - continue; - } - BrickBase::connect(bf, sr.getValue(prefix + "from Port", 0), bt, sr.getValue(prefix + "to Port", 0)); - } - buildSchemeTree(); - startBricks(); - return true; -} - - -void BrickManager::disconnectBrick(BrickBase * brick) { - for (uint i = 0; i < bricks.size(); ++i) { - for (int j = 0; j < bricks[i]->connectionsCount(); ++j) { - if (bricks[i]->connection(j).brick_to == brick) - bricks[i]->removeConnection(j); - } - } -} - - -void BrickManager::loadBrick(PIConfig & sr, const PIString & prefix, BrickBase * b) { - BrickComposite::loadBrick(sr, prefix, b, this); -} - - -void BrickManager::saveInputsToDefault() { - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->saveInputsToDefault(); -} - - -bool BrickManager::checkUniqueName(const PIString & name, BrickBase * brick) { - for (uint i = 0; i < bricks.size(); ++i) - if ((bricks[i]->name() == name) && (bricks[i] != brick)) - return false; - return true; -} - - -PIString BrickManager::getUniqueName(const PIString & name) { - int cind = 0, di = 0; - if (name.size() > 0) { - di = name.size_s() - 1; - while (name[di].isDigit()) --di; - di = name.size_s() - di - 1; - } - PIString cname, bname; - if (di > 0) bname = PIString(name).cutRight(di); - else bname = name + "_"; - cname = bname + PIString::fromNumber(cind); - bool ok = false; - while (!ok) { - ok = true; - for (uint i = 0; i < bricks.size(); ++i) { - if (bricks[i]->name() == cname) { - cind++; - ok = false; - break; - } - } - cname = bname + PIString::fromNumber(cind); - } - return cname; -} - - -BrickBase* BrickManager::findBrick(const PIString & name) { - for (uint i = 0; i < bricks.size(); ++i) - if (bricks[i]->name() == name) - return bricks[i]; - return 0; -} - - -void BrickManager::setMode(BrickBase::Mode m) { - mode_ = m; - if (mode_ == BrickBase::Asynchronous) buildSchemeTree(); - else - for (uint i = 0; i < bricks.size(); ++i) - bricks[i]->level_ = -666; -} - - -void BrickManager::buildSchemeTree() { - buildTree(bricks, tree); -} diff --git a/mbricks/brick_manager.h b/mbricks/brick_manager.h deleted file mode 100644 index 525c8c4..0000000 --- a/mbricks/brick_manager.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef BRICK_MANAGER_H -#define BRICK_MANAGER_H - -#include "pitimer.h" -#include "brick_base.h" - -class BrickManager -{ - friend class BrickEmitFrequency; -public: - BrickManager(double frequency = 20., double time_step = 0.); - ~BrickManager() {delete timer;} - - void start(); - void stop(); - void reset(); - void resetOutputs(); - void resetScheme(); - bool loadScheme(const PIString & file); - inline void pause() {paused = true;} - inline void resume() {paused = false;} - inline void needLockRun(bool yes) {timer->needLockRun(yes);} - inline void lock() {timer->lock();} - inline void unlock() {timer->unlock();} - inline bool isPaused() const {return paused;} - inline bool isRunning() const {return timer->isRunning();} - inline bool isReset() const {return wasReset;} - inline double frequency() const {return freq;} - inline double * frequency_ptr() {return &freq;} - void setFrequency(double frequency, double time_step = 0.); - void addBrick(BrickBase * brick, bool uniqueName = true); - inline void addBrick(BrickBase & brick) {addBrick(&brick);} - inline void clearBricks() {bricks.clear();} - void removeBrick(BrickBase * brick); - void replaceBrick(BrickBase * old_b, BrickBase * new_b); - inline int bricksCount() const {return bricks.size();} - inline double time() const {return ctime;} - inline double * time_ptr() {return &ctime;} - inline void setTime(double time) {ctime = time;} - void tick(bool realTime = true); - void proceedConnections(bool compositeToo = false); - void saveInputsToDefault(); - inline void startBricks() {for (uint i = 0; i < bricks.size(); ++i) bricks[i]->started();} - inline void stopBricks() {for (uint i = 0; i < bricks.size(); ++i) bricks[i]->finished();} - bool checkUniqueName(const PIString & name, BrickBase * brick); - PIString getUniqueName(const PIString & name); - BrickBase * findBrick(const PIString & name); - inline BrickBase::Mode mode() const {return mode_;} - void setMode(BrickBase::Mode m); - void buildSchemeTree(); - - PIVector > tree; - -private: - static void run(void * d, int ) {((BrickManager * )d)->tick();} - void disconnectBrick(BrickBase * brick); - void loadBrick(PIConfig & sr, const PIString & prefix, BrickBase * b); - - PITimer * timer; - PIVector bricks; - BrickBase::Mode mode_; - double freq, ctime, tstep; - bool paused, wasReset; - -}; - -#endif // BRICK_MANAGER_H diff --git a/mbricks/brick_math.cpp b/mbricks/brick_math.cpp deleted file mode 100644 index 1e5a5db..0000000 --- a/mbricks/brick_math.cpp +++ /dev/null @@ -1,242 +0,0 @@ -#include "brick_math.h" - - -bool BrickMathSum::tick_body(double time) { - double t = 0.; - for (int i = 0; i < inputs_count; ++i) - t += inputs[i] * parameters[i].toFloat(); - outputs[0] = t; - return true; -} - - -bool BrickMathMultiply::tick_body(double time) { - double t = 1.; - for (int i = 0; i < inputs_count; ++i) - t *= inputs[i]; - outputs[0] = t; - return true; -} - - -BrickMathIntegral::BrickMathIntegral(double initial): BrickMathBase(4, 1, 1) { - type = "Integral"; - setName(type); - inNames[1] = "Enable"; - inNames[2] = "Reset"; - inNames[3] = "Initial"; - paramNames[0] = "Method"; - note_ = "While \"Reset\" = 1 \"Output\" = \"Initial\", integrate while \"Enable\" = 1.\n"; - note_ += "Methods description you can find in help content.";//PIMathSolver::methods_desc; - inputs[1] = 1.; - parameters[0].setValue(-1); - saveInputsToDefault(); - started(); -} - - -void BrickMathIntegral::started() { - TF.vector_Bm.resize(1); TF.vector_An.resize(2); - TF.vector_Bm[0] = 1.; - TF.vector_An[0] = 0.; TF.vector_An[1] = 1.; - KF.fromTF(TF); - KF.X[0] = inputs[Initial]; - KF.setMethod((PIMathSolver::Method)parameters[0].toInt()); -} - - -bool BrickMathIntegral::tick_body(double time) { - if (inputs[Reset] > 0.) - outputs[0] = KF.X[0] = inputs[Initial]; - else { - if (inputs[Enable] > 0.) { - KF.setTime(time); - KF.solve(inputs[0], dt); - outputs[0] = KF.X[0]; - } - } - return true; -} - - -bool BrickMathDerivate::tick_body(double time) { - outputs[0] = (inputs[0] - v) / dt; - v = inputs[0]; - return true; -} - - -bool BrickMathDeadZone::tick_body(double time) { - if (fabs(inputs[Input]) < inputs[Zone]) { - outputs[Output] = 0.; - outputs[InZone] = 1.; - } else { - outputs[Output] = (fabs(inputs[Input]) - inputs[Zone]) * sign(inputs[Input]); - outputs[InZone] = 0.; - } - return true; -} - - -bool BrickMathSaturation::tick_body(double time) { - if (inputs[Input] > inputs[Max]) { - outputs[Output] = inputs[Max]; - outputs[InMin] = 0.; - outputs[InMax] = 1.; - } else { - if (inputs[Input] < inputs[Min]) { - outputs[Output] = inputs[Min]; - outputs[InMin] = 1.; - outputs[InMax] = 0.; - } else { - outputs[Output] = inputs[Input]; - outputs[InMin] = 0.; - outputs[InMax] = 0.; - } - } - return true; -} - - -bool BrickMathRelay::tick_body(double time) { - if (inputs[Input] - v >= 0) {if (inputs[Input] >= inputs[Size]) outputs[0] = inputs[ActiveValue];} - else {if (inputs[Input] <= -inputs[Size]) outputs[0] = inputs[InactiveValue];} - v = inputs[Input]; - return true; -} - - -bool BrickMathDelayTicks::tick_body(double time) { - if (parameters[0].toUInt() != v.size()) setDelay(parameters[0].toInt()); - v.pop_back(); - v.push_front(inputs[0]); - outputs[0] = v.back(); - return true; -} - - -bool BrickMathDelaySeconds::tick_body(double time) { - t = piRound(parameters[0].toFloat() / dt) + 1; - if (t < 1) t = 1; - if (v.size() != t) { - v.resize(t); - v.assign(t, 0.); - } - v.pop_back(); - v.push_front(inputs[0]); - outputs[0] = v.back(); - return true; -} - - -BrickMathFunction::BrickMathFunction(): BrickMathBase(0, 2, 1) { - type = "Function"; - setName(type); - paramNames[0] = "Function"; - outNames[0] = "Re out"; - outNames[1] = "Im out"; - parameters[0].setValue(""); - parameterChanged(0); - interactive = true; -} - - -void BrickMathFunction::parameterChanged(int index) { - eval.clearCustomVariables(); - eval.check(parameters[0].toString()); - PIStringList sl = eval.unknownVariables(); - setInputsCount(sl.size()); - for (uint i = 0; i < sl.size(); ++i) { - eval.setVariable(sl[i]); - inNames[i] = sl[i]; - } - eval.check(parameters[0].toString()); - note_ = "Your expression:\n" + eval.error() + "\n" + eval.expression(); -} - - -bool BrickMathFunction::tick_body(double time) { - for (int i = 0; i < inputs_count; ++i) - eval.setCustomVariableValue(i, complexd(inputs[i], 0.)); - res = eval.evaluate(); - outputs[0] = res.real(); - outputs[1] = res.imag(); - return true; -} - - -BrickMathFFT::BrickMathFFT(): BrickMathBase(1, 1, 2) { - type = "FFT"; - setName(type); - paramNames[0] = "Buffer size (2^n)"; - paramNames[1] = "Inverse"; - parameters[0].setValue(256); - parameters[1].setValue(0); - t = 0; - buffered = true; -} - - -bool BrickMathFFT::tick_body(double time) { - if (v.size() != parameters[0].toUInt()) { - t = parameters[0].toInt(); - v.resize(t); - v.fill(complexd(0., 0.)); - buffer.resize(t, 0.); - t = 0; - } - outputs[0] = (t >= 0 && t < o.size_s()) ? o[t].real() : 0.; - if (t >= v.size()) { - t = 0; - if (parameters[1].toInt() > 0.) - o = *fft.calcFFTinverse(v); - else - o = *fft.calcFFT(v); - for (uint i = 0; i < buffer.size(); ++i) - buffer[i] = abs(o[i]); - } else { - v[t] = inputs[0]; - ++t; - } - return true; -} - - -void BrickMathBessel::parameterChanged(int index) { - k = parameters[0].toInt(); - o = parameters[1].toInt(); - if (k == 0) outNames[0] = "J"; - if (k == 1) outNames[0] = "Y"; - if (k < 0 || k > 1) { - outNames[0] = "?"; - return; - } - outNames[0] += PIString::fromNumber(o); -} - - -bool BrickMathBessel::tick_body(double time) { - k = parameters[0].toInt(); - o = parameters[1].toInt(); - if (k == 0) { - if (o == 0) - outputs[0] = piJ0(inputs[0]); - else { - if (o == 1) - outputs[0] = piJ1(inputs[0]); - else - outputs[0] = piJn(o, inputs[0]); - } - } - if (k == 1) { - if (o == 0) - outputs[0] = piY0(inputs[0]); - else { - if (o == 1) - outputs[0] = piY1(inputs[0]); - else - outputs[0] = piYn(o, inputs[0]); - } - }; - return true; -} diff --git a/mbricks/brick_math.h b/mbricks/brick_math.h deleted file mode 100644 index 4b4ef72..0000000 --- a/mbricks/brick_math.h +++ /dev/null @@ -1,248 +0,0 @@ -#ifndef BRICK_MATH_H -#define BRICK_MATH_H - -#include "brick_base.h" -#include "brick_manager.h" - - -class BrickMathBase: public BrickBase { -public: BrickMathBase(int inputs_num = 1, int outputs_num = 1, int parameters_num = 0): BrickBase(inputs_num, outputs_num, parameters_num) {lib = "Mathematic";} -}; - - -class BrickMathAmplifier: public BrickMathBase { - MBRICK(BrickMathAmplifier) - BrickMathAmplifier(double gain = 1.): BrickMathBase(2, 1) {setGain(gain); type = "Gain"; setName(type); inNames[1] = "Gain";} - enum Inputs {Input, Gain}; - void setGain(double gain) {inputs[Gain] = gain;} - virtual bool tick_body(double time) {outputs[0] = inputs[Gain] * inputs[Input]; return true;} -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathAmplifier) - - -class BrickMathSum: public BrickMathBase { - MBRICK(BrickMathSum) - BrickMathSum(int inputs_num = 2): BrickMathBase(inputs_num, 1, inputs_num) { - parameters.resize(inputs_num); - parameters.fill(1.); - type = "Sum"; - setName(type); - parametersName_ = "Preamps"; - inNames[0] = ""; - io_Type = BrickBase::VariableInputs; - } - virtual void parameterChanged(int index) {inNames[index] = parameters[index].toFloat() >= 0. ? "+" : "-";} - virtual void inputsCountChanged(int count) {setParametersCount(count, 1.);} - void setPreamp(int input, double preamp) {parameters[input].setValue(preamp);} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathSum) - - -class BrickMathSign: public BrickMathBase { - MBRICK(BrickMathSign) - BrickMathSign(): BrickMathBase(1, 1) {type = "Sign"; setName(type);} - enum Inputs {Input}; - virtual bool tick_body(double time) {outputs[0] = sign(inputs[Input]); return true;} -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathSign) - - -class BrickMathAbsolute: public BrickMathBase { - MBRICK(BrickMathAbsolute) - BrickMathAbsolute(): BrickMathBase(1, 1) {type = "Abs"; setName(type);} - enum Inputs {Input}; - virtual bool tick_body(double time) {outputs[0] = fabs(inputs[Input]); return true;} -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathAbsolute) - - -class BrickMathMultiply: public BrickMathBase { - MBRICK(BrickMathMultiply) - BrickMathMultiply(int inputs_num = 2): BrickMathBase(inputs_num, 1) {type = "Multiply"; setName(type); inNames[0] = ""; io_Type = BrickBase::VariableInputs;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathMultiply) - - -class BrickMathDivide: public BrickMathBase { - MBRICK(BrickMathDivide) - BrickMathDivide(): BrickMathBase(2, 1) {type = "Divide"; setName(type); inNames[0] = "Divident"; inNames[1] = "Divider"; inputs[1] = 2.;} - enum Inputs {Input, Divider}; - virtual bool tick_body(double time) {if (inputs[Divider] == 0.) return false; outputs[0] = inputs[Input] / inputs[Divider]; return true;} -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathDivide) - - -class BrickMathPower: public BrickMathBase { - MBRICK(BrickMathPower) - BrickMathPower(double power = 2.): BrickMathBase(2, 1) {setPower(power); type = "Power"; setName(type); inNames[1] = "Power";} - enum Inputs {Input, Power}; - void setPower(double power) {inputs[Power] = power;} - virtual bool tick_body(double time) {outputs[0] = pow(inputs[Input], inputs[Power]); return true;} -private: - double p; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathPower) - - -class BrickMathIntegral: public BrickMathBase { - MBRICK(BrickMathIntegral) - BrickMathIntegral(double initial = 0.); - enum Inputs {Input, Enable, Reset, Initial}; - virtual void started(); - virtual void reset_specified() {KF.fromTF(TF); KF.X[0] = inputs[Initial];} - virtual void parameterChanged(int index) {KF.setMethod((PIMathSolver::Method)parameters[0].toInt());} - virtual bool tick_body(double time); -protected: - PIMathSolver KF; - TransferFunction TF; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathIntegral) - - -class BrickMathIntegralSaturated: public BrickMathIntegral { - MBRICK(BrickMathIntegralSaturated) - BrickMathIntegralSaturated(double initial = 0.): BrickMathIntegral(initial) {type = "IntegralSaturated"; setName(type); setInputsCount(6); inputs[Min] = -1.; inputs[Max] = 1.; inNames[4] = "Min"; inNames[5] = "Max";} - enum Inputs {Input, Enable, Reset, Initial, Min, Max}; - virtual bool tick_body(double time) {BrickMathIntegral::tick_body(time); if (KF.X[0] < inputs[Min]) {KF.X[0] = inputs[Min]; outputs[0] = KF.X[0];} if (KF.X[0] > inputs[Max]) {KF.X[0] = inputs[Max]; outputs[0] = KF.X[0];} return true;} -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathIntegralSaturated) - - -class BrickMathDerivate: public BrickMathBase { - MBRICK(BrickMathDerivate) - BrickMathDerivate(): BrickMathBase(1, 1) {v = 0.; type = "Derivate"; setName(type);} - virtual bool tick_body(double time); -private: - double v; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathDerivate) - - -class BrickMathDeadZone: public BrickMathBase { - MBRICK(BrickMathDeadZone) - BrickMathDeadZone(double zone = 1.): BrickMathBase(2, 2) {setZone(zone); type = "DeadZone"; setName(type); inNames[1] = "Zone"; outNames[1] = "In Zone";} - enum Inputs {Input, Zone}; - enum Outputs {Output, InZone}; - void setZone(double zone) {inputs[Zone] = zone;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathDeadZone) - - -class BrickMathSaturation: public BrickMathBase { - MBRICK(BrickMathSaturation) - BrickMathSaturation(double min = -1., double max = 1.): BrickMathBase(3, 3) {setRange(min, max); type = "Saturation"; setName(type); inNames[1] = "Min"; inNames[2] = "Max"; outNames[1] = "< min"; outNames[2] = "> max";} - enum Inputs {Input, Min, Max}; - enum Outputs {Output, InMin, InMax}; - void setRange(double min, double max) {inputs[Max] = max; inputs[Min] = min;} - virtual bool tick_body(double time); -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathSaturation) - - -class BrickMathQuantize: public BrickMathBase { - MBRICK(BrickMathQuantize) - BrickMathQuantize(double step = 1.): BrickMathBase(2, 1) {setStep(step); type = "Quantize"; setName(type); inNames[1] = "Step";} - enum Inputs {Input, Step}; - void setStep(double step) {inputs[Step] = step;} - virtual bool tick_body(double time) {outputs[0] = piRound(inputs[Input] / inputs[Step]) * inputs[Step]; return true;} -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathQuantize) - - -class BrickMathRelay: public BrickMathBase { - MBRICK(BrickMathRelay) - BrickMathRelay(double size = 1., double active = 1., double inactine = 0.): BrickMathBase(4, 1) {setParameters(size, active, inactine); v = 0.; type = "Relay"; setName(type); inNames[1] = "Size"; inNames[2] = "Active Value"; inNames[3] = "Inactive Value";} - virtual void reset_specified() {v = 0.;} - enum Inputs {Input, Size, ActiveValue, InactiveValue}; - void setParameters(double size, double active, double inactine) {inputs[Size] = size; inputs[ActiveValue] = active; inputs[InactiveValue] = inactine;} - virtual bool tick_body(double time); -private: - double v; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathRelay) - - -class BrickMathDelayTicks: public BrickMathBase { - MBRICK(BrickMathDelayTicks) - BrickMathDelayTicks(uint ticks = 1): BrickMathBase(1, 1, 1) {setDelay(ticks); type = "DelayTicks"; setName(type); paramNames[0] = "Ticks"; parameters[0].setValue(1);} - virtual void reset_specified() {v.assign(v.size(), 0.);} - void setDelay(int ticks) {parameters[0].setValue(ticks); v.resize(ticks); v.assign(ticks, 0.);} - virtual bool tick_body(double time); -private: - PIDeque v; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathDelayTicks) - - -class BrickMathDelaySeconds: public BrickMathBase { - MBRICK(BrickMathDelaySeconds) - BrickMathDelaySeconds(double secs = 1.): BrickMathBase(1, 1, 1) {setDelay(secs); type = "DelaySeconds"; setName(type); paramNames[0] = "Seconds"; parameters[0].setValue(1.);} - virtual void reset_specified() {v.assign(v.size(), 0.);} - void setDelay(double secs) {parameters[0].setValue(secs);} - virtual bool tick_body(double time); -private: - PIDeque v; - uint t; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathDelaySeconds) - - -class BrickMathThreshold: public BrickMathBase { - MBRICK(BrickMathThreshold) - BrickMathThreshold(double value = 0.5): BrickMathBase(2, 1) {setThreshold(value); type = "Threshold"; setName(type); inNames[1] = "Threshold";} - enum Inputs {Input, Threshold}; - void setThreshold(double value) {inputs[Threshold] = value;} - virtual bool tick_body(double time) {outputs[0] = (inputs[Input] >= inputs[Threshold] ? 1. : 0.); return true;} -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathThreshold) - - -class BrickMathFunction: public BrickMathBase { - MBRICK(BrickMathFunction) - BrickMathFunction(); - virtual void parameterChanged(int index); - virtual bool tick_body(double time); -private: - PIEvaluator eval; - complexd res; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathFunction) - - -class BrickMathFFT: public BrickMathBase { - MBRICK(BrickMathFFT) - BrickMathFFT(); - virtual bool tick_body(double time); - virtual void started() {t = 0;} - virtual void reset_specified() {t = 0;} -private: - PIFFT fft; - PIVector v, o; - uint t; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathFFT) - - -class BrickMathCopy: public BrickMathBase { - MBRICK(BrickMathCopy) - BrickMathCopy(): BrickMathBase(1, 2) {type = "Copy"; setName(type); outNames[0] = "0"; io_Type = VariableOutputs;} - virtual bool tick_body(double time) {for (int i = 0; i < outputs_count; ++i) outputs[i] = inputs[0]; return true;} -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathCopy) - - -class BrickMathBessel: public BrickMathBase { - MBRICK(BrickMathBessel) - BrickMathBessel(): BrickMathBase(1, 1, 2) {type = "Bessel"; setName(type); paramNames[0] = "Kind"; paramNames[1] = "Order"; setParameterValue(0, 0); setParameterValue(1, 0);} - virtual void parameterChanged(int index); - virtual bool tick_body(double time); -private: - int o, k; -}; -ADD_NEW_TO_COLLECTION(Mathematic, BrickMathBessel) - - -#endif // BRICK_MATH_H diff --git a/mbricks/brick_statistic.cpp b/mbricks/brick_statistic.cpp deleted file mode 100644 index f58ab23..0000000 --- a/mbricks/brick_statistic.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "brick_statistic.h" - - -bool BrickStatisticMinMaxI::tick_body(double time) { - if (inputs_count == 0) return true; - min = max = inputs[0]; - for (int i = 1; i < inputs_count; ++i) { - if (min > inputs[i]) min = inputs[i]; - if (max < inputs[i]) max = inputs[i]; - } - outputs[Min] = min; - outputs[Max] = max; - return true; -} - - -bool BrickStatisticExpectation::tick_body(double time) { - t += time - pt; - pt = time; - i.setInputValue(0, inputs[0]); - i.tick(time); - if (t == 0.) outputs[0] = 0.; - else outputs[0] = i.output(0) / t; - return true; -} - - -bool BrickStatisticVariance::tick_body(double time) { - t += time - pt; - pt = time; - i0.setInputValue(0, inputs[0]); - i0.tick(time); - if (t == 0.) i1.setInputValue(0, 0.); - else i1.setInputValue(0, sqr(inputs[0] - i0.output(0) / t)); - i1.tick(time); - if (t == 0.) outputs[0] = 0.; - else outputs[0] = i1.output(0) / t; - return true; -} diff --git a/mbricks/brick_statistic.h b/mbricks/brick_statistic.h deleted file mode 100644 index 7a6e651..0000000 --- a/mbricks/brick_statistic.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef BRICK_STATISTIC_H -#define BRICK_STATISTIC_H - -#include "brick_math.h" - - -class BrickStatisticBase: public BrickBase { -public: BrickStatisticBase(int inputs_num = 1, int outputs_num = 1, int parameters_num = 0): BrickBase(inputs_num, outputs_num, parameters_num) {lib = "Statistic";} -}; - - -class BrickStatisticMinMaxI: public BrickStatisticBase { - MBRICK(BrickStatisticMinMaxI) - BrickStatisticMinMaxI(): BrickStatisticBase(2, 2) {type = "MinMax_Immediate"; setName(type); outNames[Min] = "Min"; outNames[Max] = "Max"; inNames[0] = "0"; io_Type = VariableInputs;} - enum Inputs {Min, Max}; - virtual void reset_specified() {min = max = 0.;} - virtual bool tick_body(double time); -private: - double min, max; -}; -ADD_NEW_TO_COLLECTION(Statistic, BrickStatisticMinMaxI) - - -class BrickStatisticMinMaxF: public BrickStatisticBase { - MBRICK(BrickStatisticMinMaxF) - BrickStatisticMinMaxF(): BrickStatisticBase(1, 2) {type = "MinMax_Function"; setName(type); outNames[Min] = "Min"; outNames[Max] = "Max";} - enum Inputs {Min, Max}; - virtual void reset_specified() {min = max = 0.;} - virtual bool tick_body(double time) {if (min > inputs[0]) min = inputs[0]; if (max < inputs[0]) max = inputs[0]; outputs[Min] = min; outputs[Max] = max; return true;} -private: - double min, max; -}; -ADD_NEW_TO_COLLECTION(Statistic, BrickStatisticMinMaxF) - - -class BrickStatisticExpectation: public BrickStatisticBase { - MBRICK(BrickStatisticExpectation) - BrickStatisticExpectation(): BrickStatisticBase(1, 1) {type = "Expectation"; setName(type);} - virtual void reset_specified() {i.reset(); pt = t = 0.;} - virtual bool tick_body(double time); -private: - BrickMathIntegral i; double pt, t; -}; -ADD_NEW_TO_COLLECTION(Statistic, BrickStatisticExpectation) - - -class BrickStatisticVariance: public BrickStatisticBase { - MBRICK(BrickStatisticVariance) - BrickStatisticVariance(): BrickStatisticBase(1, 1) {type = "Variance"; setName(type);} - virtual void reset_specified() {i0.reset(); i1.reset(); pt = t = 0.;} - virtual bool tick_body(double time); -private: - BrickMathIntegral i0, i1; double pt, t; -}; -ADD_NEW_TO_COLLECTION(Statistic, BrickStatisticVariance) - -#endif // BRICK_STATISTIC_H diff --git a/mbricks/clean b/mbricks/clean deleted file mode 100644 index bd4c90d..0000000 --- a/mbricks/clean +++ /dev/null @@ -1,4 +0,0 @@ -#! /bin/bash -VERBOSE=1 make clean -rm -rvf CMakeFiles -rm -vf CMakeCache.txt Makefile cmake_install.cmake install_manifest.txt *~ *cxx moc_* *.o *.so *.dll *.a diff --git a/mbricks/clean.bat b/mbricks/clean.bat deleted file mode 100644 index 3a657d9..0000000 --- a/mbricks/clean.bat +++ /dev/null @@ -1,4 +0,0 @@ -#make clean -del /q /f /s CMakeFiles -rmdir /q /s CMakeFiles -del /q /f CMakeCache.txt Makefile cmake_install.cmake install_manifest.txt *.user* *~ *cxx moc_* ui_* qrc_* *.o *.exe *.a *.dll *.lib core *.qrc.depends diff --git a/mbricks/make_install.sh b/mbricks/make_install.sh deleted file mode 100644 index 9e65aa3..0000000 --- a/mbricks/make_install.sh +++ /dev/null @@ -1,5 +0,0 @@ -#! /bin/bash -cmake . -make $@ -cp -vf *.h /usr/include/ -cp -vf lib*.so /usr/lib/ diff --git a/mbricks/make_lib.bat b/mbricks/make_lib.bat deleted file mode 100644 index 0148a43..0000000 --- a/mbricks/make_lib.bat +++ /dev/null @@ -1 +0,0 @@ -cmake -G "MinGW Makefiles" -DLIB=1 && make install . %* diff --git a/mbricks/mbricks.h b/mbricks/mbricks.h deleted file mode 100644 index 43471e6..0000000 --- a/mbricks/mbricks.h +++ /dev/null @@ -1,9 +0,0 @@ -#include "brick_manager.h" -#include "brick_emits.h" -#include "brick_math.h" -#include "brick_statistic.h" -#include "brick_logic.h" -#include "brick_digital.h" -#include "brick_link.h" -#include "brick_interface.h" -#include "brick_composite.h"