-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimalCounter.cpp
More file actions
121 lines (95 loc) · 2.51 KB
/
decimalCounter.cpp
File metadata and controls
121 lines (95 loc) · 2.51 KB
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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <chrono>
#include <thread>
struct ErrorMsg{
[[noreturn]] static void CriticalError(const std::string& msg){
std::cerr << msg << std::endl;
std::exit(EXIT_FAILURE);
}
};
struct Converter{
static void toBinary(unsigned int decimal, int bits){
for(int i = bits-1; i >= 0; i--){
std::cout << ((decimal >> i) & 1);
}
std::cout << std::endl;
}
};
class Chip4017{
private:
unsigned int Out = 1;
unsigned int LimitReset;
public:
explicit Chip4017(unsigned int limitReset) : LimitReset(limitReset) {
if(limitReset == 0 || limitReset > 10){
throw std::invalid_argument("Reset pin must be between 1 and 10");
}
};
void shift(){
if(Out >= (1u << (LimitReset-1))){
reset();
} else {
Out = Out << 1;
}
}
void reset(){
Out = 1;
}
unsigned int getOut() const{
return Out;
}
};
class Chip555{
private:
bool output = false;
double R1, R2, C;
const double Ln2 = 0.693;
double tHigh(){
return Ln2*(R1 + R2)*C;
}
double tLow(){
return Ln2*R2*C;
}
void clock(){
output = !output;
}
public:
Chip555(double r1, double r2, double c )
: R1(r1), R2(r2), C(c) {};
void pulse(){
clock();
std::this_thread::sleep_for(std::chrono::duration<double>(tHigh()));
clock();
std::this_thread::sleep_for(std::chrono::duration<double>(tLow()));
}
};
class runBoard{
private:
Chip4017& chip4017;
Chip555& chip555;
unsigned int resetQP = 0;
public:
runBoard (Chip555& c555, Chip4017& c4017, unsigned int resetQP)
: chip4017(c4017), chip555(c555), resetQP(resetQP) {}
void run(){
while(true){
Converter::toBinary(chip4017.getOut(), resetQP);
chip555.pulse();
chip4017.shift();
}
}
};
int main(){
try{
const int resetQP = 4;
Chip4017 chip4017(resetQP);
Chip555 chip555(1000, 10000, 7.37e-6);
runBoard board(chip555, chip4017, resetQP);
board.run();
} catch(const std::exception& e) {
ErrorMsg::CriticalError(e.what());
}
return 0;
}