-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgates.py
146 lines (122 loc) · 5.27 KB
/
gates.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Logic Circuits - gates.py
#
# Copyleft 2015 Chris Meyers <[email protected]> 2015.12.13
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
__author__ = "Chris Meyers, Jeff Elkner and Kevin Cole"
__copyright__ = "Copyleft 2015, Chris Meyers (2015.12.13)"
__credits__ = ["Chris Meyers", "Jeff Elkner",
"Kevin Cole"] # Authors and bug reporters
__license__ = "GPL"
__version__ = "2.0"
__maintainer__ = "Chris Meyers, Jeff Elkner, Kevin Cole"
__email__ = "[email protected]"
__status__ = "Development" # "Prototype", "Development" or "Production"
__appname__ = "Logic Circuits"
import pygame as pg
from component import Component
from connector import Connector
from text import textDraw
from settings import PULS_INTERVAL, MULT_PULS_HALFCYCLE, BGCOLOR, BLUE
class Nand2(Component):
def setupGraphics(s):
s.A = Connector(s, "A", s.scale1((0, 6)), 1)
s.B = Connector(s, "B", s.scale1((0, 24)), 1)
s.output = s.C = Connector(s, "C", s.scale1((30, 15)), 0)
def draw(self, screen, color=BLUE):
args = self.scaleM((15, 15), 15, # big circle
(0, 0), (15, 0), (15, 30), (0, 30), # erase rect
(15, 0), (0, 0), (0, 30), (15, 30), # draw box behind
(30, 15), 2) # little NOT circle
c1p, c1r, e1, e2, e3, e4, l1, l2, l3, l4, c2p, c2r = args
pg.draw.circle(screen, color, c1p, c1r, 1)
pg.draw.polygon(screen, BGCOLOR, (e1, e2, e3, e4), 0)
pg.draw.lines(screen, color, False, (l1, l2, l3, l4), 1)
pg.draw.circle(screen, color, c2p, c2r, 1)
for con in (self.A, self.B, self.C):
pg.draw.circle(screen, color, con.pos, 2, 1)
textDraw(screen, self.scale1((5, 5)), color, self.name, quad=4)
def computeOutput(s):
if s.A.value and s.B.value:
newValue = 0 # note inverted
else:
newValue = 1
s.C.value = newValue
# ----------------------------------------------------------
class Inv(Component):
def setupGraphics(s):
s.input = s.A = Connector(s, "A", s.scale1(( 0, 12)), 1)
s.output = s.B = Connector(s, "B", s.scale1((26, 12)), 0)
def draw(self, screen, color=BLUE):
args = self.scaleM(( 0, 0), (24, 12), (0, 24), # triangle
(26, 12), 2) # NOT circle
tp1, tp2, tp3, cp1, cr1 = args
pg.draw.polygon(screen, color, (tp1, tp2, tp3), 1)
pg.draw.circle(screen, color, cp1, cr1, 1)
pg.draw.circle(screen, color, self.A.pos, 2, 1)
pg.draw.circle(screen, color, self.B.pos, 2, 1)
textDraw(screen, self.scale1((5, 5)), color, self.name, quad=4)
def computeOutput(self):
newValue = 1 - self.A.value # swap 0 and 1
self.B.value = newValue # probably more to come
# -----------------------------------------------------------
class Swt(Component): # toggle switch w click
def setupGraphics(s):
s.output = s.B = Connector(s, "B", s.scale1((6, 3)), 0)
s.B.value = 1
def draw(self, screen, color=BLUE):
bp1, br1 = self.scaleM((3, 3), 3) # Body to click
pg.draw.circle(screen, color, bp1, br1, 1)
textDraw(screen, self.pos, color, self.name, quad=2)
def computeOutput(self):
pass # computed when clicked
def takeClick(self):
newValue = 1 - self.B.value # swap 0 and 1
self.B.value = newValue # probably more to come
# print("{0} was clicked - now {1}".format(self.name, self.B.value))
class Puls(Swt):
def setupGraphics(s):
Swt.setupGraphics(s)
s.timer = 0
def takeClick(self):
self.timer = PULS_INTERVAL
# print("Puls {0} clicked - now {1}".format(self.name, self.B.value))
def computeOutput(self):
if self.timer:
self.B.value = 0 # keep down til time done
self.timer -= 1
else:
self.B.value = 1
class MultPuls(Swt):
def setupGraphics(self):
Swt.setupGraphics(self)
self.halfcyc = MULT_PULS_HALFCYCLE
self.on = False
def takeClick(self):
self.on = not self.on # turn on or off
self.timer = 0
def computeOutput(self):
if self.on:
self.timer -= 1
if self.timer <= 0:
self.B.value = 1 - self.B.value # flip value 0/1
self.timer = self.halfcyc
else:
self.B.value = 1