wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
Clock.h
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
7
8#pragma once
9
10#include <chrono>
11
13
23{
24 Q_OBJECT
25
26public:
30 static constexpr double kDefaultFrequency = 1.0;
31
37 static constexpr std::chrono::microseconds halfPeriod(const double freq)
38 {
39 using namespace std::chrono_literals;
40 return std::chrono::duration_cast<std::chrono::microseconds>(1s / (2 * freq));
41 }
42
43 // --- Lifecycle ---
44
46 explicit Clock(QGraphicsItem *parent = nullptr);
47
48 // --- State Queries ---
49
51 bool isOn(const int port = 0) const override;
53 double frequency() const override;
56 double delay() const override;
58 QString genericProperties() override;
59
60 // --- State Setters ---
61
63 void setOn() override;
65 void setOff() override;
66
67 // Bring the base setOn(bool, int) overload into scope (hidden by setOn() above).
69
71 void setFrequency(const double freq) override;
74 void setDelay(const double delay) override;
75
76 // --- Simulation ---
77
79 void resetClock(std::chrono::steady_clock::time_point globalTime);
81 void updateClock(std::chrono::steady_clock::time_point globalTime);
82
83 // --- Visual ---
84
86 QList<std::pair<int, QString>> appearanceStates() const override;
87
88 // --- Serialization ---
89
91 void load(QDataStream &stream, SerializationContext &context) override;
93 void save(QDataStream &stream, SerializationOptions options) const override;
94
95private:
96 // --- Members ---
97
98 double m_delay = 0;
99 double m_frequency = kDefaultFrequency;
103 std::chrono::microseconds m_interval = halfPeriod(kDefaultFrequency);
104 std::chrono::steady_clock::time_point m_startTime;
105};
Abstract base class for user-controllable input elements.
static constexpr double kDefaultFrequency
Definition Clock.h:30
void save(QDataStream &stream, SerializationOptions options) const override
Definition Clock.cpp:112
bool isOn(const int port=0) const override
Returns true if the clock output is currently logic-high.
Definition Clock.cpp:96
void load(QDataStream &stream, SerializationContext &context) override
Definition Clock.cpp:134
double delay() const override
Definition Clock.cpp:204
void resetClock(std::chrono::steady_clock::time_point globalTime)
Resets the clock phase reference to globalTime.
Definition Clock.cpp:220
void setOn() override
Drives all output ports logic-high.
Definition Clock.cpp:107
void setOff() override
Drives all output ports logic-low.
Definition Clock.cpp:102
void setFrequency(const double freq) override
Sets the clock output frequency to freq Hz.
Definition Clock.cpp:187
QList< std::pair< int, QString > > appearanceStates() const override
Definition Clock.cpp:239
void updateClock(std::chrono::steady_clock::time_point globalTime)
Advances the clock state based on elapsed time since globalTime.
Definition Clock.cpp:62
double frequency() const override
Returns the clock frequency in Hz.
Definition Clock.cpp:182
QString genericProperties() override
Returns a summary string of the clock's current frequency and delay settings.
Definition Clock.cpp:234
Clock(QGraphicsItem *parent=nullptr)
Constructs the element with optional parent.
Definition Clock.cpp:54
static constexpr std::chrono::microseconds halfPeriod(const double freq)
Definition Clock.h:37
void setDelay(const double delay) override
Definition Clock.cpp:209
virtual void setOn()=0
Sets all outputs to active (logic-1).
GraphicElementInput(ElementType type, QGraphicsItem *parent=nullptr)
Bundles all per-deserialization state so that load() overrides receive it through one explicit parame...
Options passed to GraphicElement::save() (and friends); the save-side counterpart of SerializationCon...