-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
189 lines (158 loc) · 4.3 KB
/
index.html
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<html>
<head>
<meta charset="utf-8"/>
<title>USB GPIO</title>
<style>
body, #graph {
margin: 0;
padding: 0;
border: 0;
}
#graph {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="draw"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.7.1/svg.min.js"></script>
<script src="port.js"></script>
<script>
/*(async function() {
var device = (await Port.getPorts())[0];
await device.open();
device.read((e, d) => {
if(e) {
console.error('Read error:', e.message);
} else {
const buffer = Array.from(new Uint8Array(d.buffer));
console.log(buffer.map(e => e.toString(2)).join(' '));
}
});
window.device = device;
})().catch(e => console.error(e.message));*/
function assert(condition, message) {
if(!condition) {
throw new Error(message);
}
}
const PIN_COUNT = 6;
const VAR_COUNT = 4;
function andGate(draw, inputs = []) {
assert(Array.isArray(inputs));
const baseGroup = draw.group();
baseGroup.path('M 80 30 L 100 30').stroke('black')
baseGroup.path('M 20 0 L 50 0 C 66.57 0 80 13.43 80 30 C 80 46.57 66.57 60 50 60 L 20 60 Z').fill('white').stroke('black');
baseGroup.scale(0.5, 0.5);
const inputsGroup = baseGroup.group();
function drawInputs() {
const points = inputs.map((inverted, i) => {
const y = baseGroup.inputOffset(i);
return `M 0 ${y} L 20 ${y}`;
});
inputsGroup.clear();
inputsGroup.path(points.join(' ')).stroke('black');
inputs.forEach((inverted, i) => {
if(inverted) {
const y = baseGroup.inputOffset(i);
inputsGroup.circle(6).fill('white').stroke('black').move(14, y - 3);
}
});
}
baseGroup.inputOffset = function inputOffset(inputIndex) {
assert(inputIndex < inputs.length);
const inputPadding = 60 / inputs.length;
return inputIndex * inputPadding + inputPadding / 2;
}
baseGroup.setInputs = function setInputs(inputs_) {
assert(Array.isArray(inputs_));
inputs = inputs_;
drawInputs();
return this;
}
drawInputs();
return baseGroup;
}
function orGate(draw, inputs = []) {
assert(Array.isArray(inputs));
const baseGroup = draw.group();
baseGroup.path('M 80 30 L 100 30').stroke('black')
baseGroup.path('M 40 0 C 57.47 0.56 73.06 12.25 80 30 C 73.06 47.75 57.47 59.44 40 60 L 15 60 C 25.72 41.44 25.72 18.56 15 0 Z').fill('white').stroke('black');
baseGroup.scale(0.5, 0.5);
const inputsGroup = baseGroup.group();
function drawInputs() {
const points = inputs.map((inverted, i) => {
const y = baseGroup.inputOffset(i);
return `M 0 ${y} L 20 ${y}`;
});
inputsGroup.clear();
inputsGroup.path(points.join(' ')).stroke('black');
inputs.forEach((inverted, i) => {
if(inverted) {
const y = baseGroup.inputOffset(i);
inputsGroup.circle(6).fill('white').stroke('black').move(14, y - 3);
}
});
}
baseGroup.inputOffset = function inputOffset(inputIndex) {
assert(inputIndex < inputs.length);
const inputPadding = 60 / inputs.length;
return inputIndex * inputPadding + inputPadding / 2;
}
baseGroup.setInputs = function setInputs(inputs_) {
assert(Array.isArray(inputs_));
inputs = inputs_;
drawInputs();
return this;
}
drawInputs();
return baseGroup;
}
class Draw {
constructor(containerId) {
const that = this;
this._draw = SVG(containerId);
this._inputs = [];
this._outputs = [];
for(let i = 0; i < 6; i++) {
const input = this._draw.group();
input.circle(28).fill('white').stroke({ color: 'blue', width: 2 });
input.circle(16).fill('red').move(6, 6);
input.move(0, i * 40);
input.click(function() {
const index = that._inputs.indexOf(this);
console.log(index);
})
this._inputs.push(input);
const output = this._draw.group();
output.circle(28).fill('white').stroke({ color: 'blue', width: 2 });
output.circle(16).fill('green').move(6, 6);
output.move(400, i * 40);
output.click(function() {
const index = that._outputs.indexOf(this);
console.log(index);
})
this._outputs.push(output);
}
}
addAndGate(inverted = false) {
return andGate(this._draw, [inverted]);
}
addOrGate(inverted = false) {
return orGate(this._draw, [inverted]);
}
addXorGate(inverted = false) {
return xorGate(this._draw, [inverted]);
}
parseFormula(formula) {
}
}
const draw = new Draw('draw');
function operatorDepths(formula) {
for(let i = 0; i < formula.length; i++) {
}
}
</script>
</body>
</html>