-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.cpp
155 lines (127 loc) · 3.88 KB
/
signals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <functional>
#include <QSettings>
#include "signals.h"
unsigned int Signal::total_length = TOTAL_LENGTH;
double Signal::ratio = 1.0;
void
Signal::inverse(std::vector<bool>& vec)
{
std::transform( vec.begin(), vec.end(), vec.begin(), std::logical_not<bool>());
}
void
Level::saveSettings(QSettings* settings)
{
settings->setValue( "signal-logical-level", inverted);
}
void
Level::loadSettings(QSettings* settings)
{
inverted = settings->value( "signal-logical-level", 0).toBool();
}
void
Level::form(std::vector<bool>& vec) const { if (inverted) inverse(vec); }
void
Level::changeRatio()
{
}
void
Pulse::saveSettings(QSettings* settings)
{
Level::saveSettings(settings);
settings->setValue( "pulse-offset", offset);
settings->setValue( "pulse-length", length);
}
void
Pulse::loadSettings(QSettings* settings)
{
Level::loadSettings(settings);
offset = settings->value( "pulse-offset", 0).toInt();
length = settings->value( "pulse-length", 0).toInt();
}
void
Pulse::form(std::vector<bool>& vec) const throw (std::overflow_error)
{
Level::form(vec);
/*
if (offset >= vec.size())
throw std::overflow_error("Pulse offset is too big.");
if (offset + length >= vec.size())
throw std::overflow_error("Pulse (offset + lenght) sum is too big.");
*/
int rel_offset = vec.size() - offset;
int rel_offlen = vec.size() - offset - length;
if (rel_offset >= 0 && rel_offlen >= 0)
std::fill( vec.begin() + offset, vec.begin() + offset + length, !inverted);
else if (rel_offset >= 0 && rel_offlen < 0)
std::fill( vec.begin() + offset, vec.end(), !inverted);
}
void
Pulse::form( std::vector<bool>& vec, std::vector<bool>& pulse) const throw (std::overflow_error)
{
Level::form(vec);
int rel_offset = vec.size() - offset;
int rel_offlen = vec.size() - offset - pulse.size();
if (rel_offset >= 0 && rel_offlen >= 0)
std::copy( pulse.begin(), pulse.end(), vec.begin() + offset);
else if (rel_offset >= 0 && rel_offlen < 0)
std::copy( pulse.begin(), pulse.end() + rel_offlen, vec.begin() + offset);
}
void
Pulse::changeRatio()
{
offset /= Signal::ratio;
length /= Signal::ratio;
}
void
Clock::saveSettings(QSettings* settings)
{
Level::saveSettings(settings);
settings->setValue( "clock-offset", offset);
settings->beginGroup("clock-pulse");
pulse.saveSettings(settings);
settings->endGroup();
settings->setValue( "clock-times", times);
}
void
Clock::loadSettings(QSettings* settings)
{
Level::loadSettings(settings);
offset = settings->value( "clock-offset", 0).toInt();
settings->beginGroup("clock-pulse");
pulse.loadSettings(settings);
settings->endGroup();
times = settings->value( "clock-times", 0).toInt();
}
void
Clock::form(std::vector<bool>& vec) const throw (std::overflow_error)
{
const int& start = pulse.offset;
const int& len = pulse.length;
std::vector<bool> ptime(start + len);
std::fill( ptime.begin() + start, ptime.begin() + start + len, 1);
pulse.Level::form(ptime);
int rel_offset = vec.size() - offset;
int rel_offlen = vec.size() - (start + len) * times - offset;
/* if (ptime.size() * times + offset >= vec.size())
throw std::overflow_error("Clock pulse values are too big.");
*/
if (rel_offset >= 0 && rel_offlen >= 0) {
for ( unsigned int i = 0, off = offset; i < times; ++i) {
std::copy( ptime.begin(), ptime.end(), vec.begin() + off);
off += ptime.size();
}
}
else if (rel_offset >= 0 && rel_offlen < 0) {
std::vector<bool> clock(ptime.size() * times);
for ( unsigned int i = 0; i < times; ++i)
std::copy( ptime.begin(), ptime.end(), clock.begin() + i * ptime.size());
Pulse::form( vec, clock);
}
Level::form(vec);
}
void
Clock::changeRatio()
{
Pulse::changeRatio();
pulse.Pulse::changeRatio();
}