File indexing completed on 2025-02-16 11:40:45
0001 /*************************************************************************** 0002 * Copyright (C) 2005 by David Saxton * 0003 * david@bluehaze.org * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 ***************************************************************************/ 0010 0011 #ifndef SWITCH_H 0012 #define SWITCH_H 0013 0014 #include <QObject> 0015 #include <QPointer> 0016 0017 class CircuitDocument; 0018 class Component; 0019 class Pin; 0020 class Resistance; 0021 class QTimer; 0022 0023 /** 0024 @author David Saxton 0025 */ 0026 0027 class Switch : public QObject 0028 { 0029 Q_OBJECT 0030 public: 0031 enum State { Open, Closed }; 0032 0033 Switch(Component *parent, Pin *p1, Pin *p2, State state); 0034 ~Switch() override; 0035 /** 0036 * If bouncing has been set to true, then the state will not switch 0037 * immediately to that given. 0038 */ 0039 void setState(State state); 0040 State state() const 0041 { 0042 return m_state; 0043 } 0044 0045 /** 0046 * Tell the switch whether to bounce or not, for the given duration, 0047 * when the state is changed. 0048 */ 0049 void setBounce(bool bounce, int msec = 5); 0050 /** 0051 * Tell the switch to continue bouncing (updates the resistance value). 0052 * Called from the simulator. 0053 */ 0054 void bounce(); 0055 /** 0056 * Attempts to calculate the current that is flowing through the switch. 0057 * (If all the connectors at one of the ends know their currents, then 0058 * this switch will give the current to the pins at either end). 0059 * @return whether it was successful. 0060 * @see CircuitDocument::calculateConnectorCurrents 0061 */ 0062 bool calculateCurrent(); 0063 0064 protected slots: 0065 /** 0066 * Called from a QTimer timeout - our bouncing period has come to an 0067 * end. This will then fully disconnect or connect the pins depending 0068 * on the current state. 0069 */ 0070 void stopBouncing(); 0071 0072 protected: 0073 void startBouncing(); 0074 0075 bool m_bBounce; 0076 int m_bouncePeriod_ms; 0077 unsigned long long m_bounceStart; // Simulator time that bouncing started 0078 Resistance *m_pBounceResistance; 0079 State m_state; 0080 Component *m_pComponent; 0081 QPointer<Pin> m_pP1; 0082 QPointer<Pin> m_pP2; 0083 QTimer *m_pStopBouncingTimer; 0084 }; 0085 0086 #endif