# shlogic.cpp

Go to the documentation of this file.

    00001 /*
    00002  * SmartHome server: Plug-in logic
    00003  *
    00004  * Copyright (C) 2010 Daniel Pisi <dan.pisi@gmail.com>
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it
    00007  * under the terms of version 2 of the GNU General Public License
    00008  * as published by the Free Software Foundation.
    00009  *
    00010  */
    00011
    00018 #include "shlogic.h"
    00019
    00020 // -----------------------------------------------------------------------------
    00021
    00025 ShLogic::ShLogic()
    00026 {
    00027     mGateType = gtAnd;
    00028     mOutput = false;
    00029 }
    00030 // -----------------------------------------------------------------------------
    00031
    00037 bool ShLogic::init(QString param)
    00038 {
    00039     param = param.toUpper();
    00040
    00041          if (param == "AND")  mGateType = gtAnd;
    00042     else if (param == "NAND") mGateType = gtNand;
    00043     else if (param == "OR")   mGateType = gtOr;
    00044     else if (param == "NOR")  mGateType = gtNor;
    00045     else if (param == "XOR")  mGateType = gtXor;
    00046
    00047     qDebug("Logic gate initialized");
    00048     return true;
    00049 }
    00050 // -----------------------------------------------------------------------------
    00051
    00056 void ShLogic::updatePins(const QList<ShPin> &pins)
    00057 {
    00058     mInputs.clear();
    00059
    00060     foreach (ShPin pin, pins)
    00061     {
    00062         if (pin.isInput())
    00063             mInputs.insert(pin.signal(), false);
    00064     }
    00065 }
    00066 // -----------------------------------------------------------------------------
    00067
    00071 void ShLogic::updateOutput()
    00072 {
    00073     bool output = ((mGateType == gtAnd) || (mGateType == gtNand));
    00074
    00075     foreach (bool input, mInputs)
    00076     {
    00077         switch (mGateType)
    00078         {
    00079         case gtAnd:
    00080         case gtNand:
    00081             output &= input;
    00082             break;
    00083
    00084         case gtOr:
    00085         case gtNor:
    00086             output |= input;
    00087             break;
    00088
    00089         case gtXor:
    00090             output ^= input;
    00091             break;
    00092         }
    00093     }
    00094
    00095     if ((mGateType == gtNand) || (mGateType == gtNor))
    00096         output = !output;
    00097
    00098     if (output != mOutput)
    00099     {
    00100         mOutput = output;
    00101         outputUpdated("Y", mOutput);
    00102     }
    00103 }
    00104 // -----------------------------------------------------------------------------
    00105
    00110 void ShLogic::processMessage(ShMessagePtr message)
    00111 {
    00112     qDebug("Logic gate: Processing message from object %d, signal = %s, value = %s",
    00113            message->senderId(), qPrintable(message->signal()), qPrintable(message->value().toString()));
    00114
    00115     if (message->msgType() == ShMessage::mtValue)
    00116     {
    00117         if (mInputs.contains(message->signal()))
    00118         {
    00119             mInputs[message->signal()] = message->value().toBool();
    00120             updateOutput();
    00121
    00122             if (message->requireAck())
    00123                 ackMessage(message);
    00124         }
    00125         else
    00126             errorMessage(message, "Invalid signal");
    00127     }
    00128     else if ((message->msgType() == ShMessage::mtGet) &&
    00129             (message->signal() == "Y"))
    00130     {
    00131         outputUpdated("Y", mOutput);
    00132     }
    00133 }
    00134 // -----------------------------------------------------------------------------
    00135
    00136 Q_EXPORT_PLUGIN2(logic, ShLogic);

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends