-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from LovetheFrogs/2-to-1-mux
2 to 1 mux
- Loading branch information
Showing
8 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
PyCircTools Multiplexer module Exceptions. | ||
""" | ||
|
||
|
||
class MultiplexerException(Exception): | ||
""" | ||
MultiplexerException is the parent Exception class for all PyCircTools.Multiplexers related Exceptions | ||
""" | ||
def __init__(self, msg="Multiplexer Exception"): | ||
self.msg = msg | ||
super().__init__(self.msg) | ||
|
||
|
||
class NonExistingInput(MultiplexerException): | ||
""" | ||
NonExistingInput is raised when a Multiplexer/Demultiplexer doesn't have the input asked for. | ||
""" | ||
def __init__(self, requestedInput=None): | ||
if requestedInput is not None: | ||
self.msg = "Input " + str(requestedInput) + " does not exist." | ||
else: | ||
self.msg = "Input requested does not exist." | ||
super().__init__(self.msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from CTools.Exceptions.MultiplexersExceptions import NonExistingInput | ||
from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue | ||
|
||
|
||
class Mux2to1: | ||
""" | ||
2 to 1 Multiplexer. Takes one Set input. | ||
""" | ||
def __init__(self): | ||
""" | ||
Mux2to1 class constructor method. | ||
""" | ||
self.input = [False, False] | ||
self.set = False | ||
self.output = self.__calculate_output() | ||
|
||
def get_input(self, num): | ||
""" | ||
Method get_input is used to get the value of input[num] | ||
:param num: Number of the input you want to get, either 0 or 1. | ||
:type num: int | ||
:raises NonExistingInput: raised when a Multiplexer/Demultiplexer doesn't have the input asked for. | ||
:return: Value of the desired input (num). | ||
:rtype: bool | ||
""" | ||
if num >= len(self.input) or num < 0: | ||
raise NonExistingInput(num) | ||
|
||
return self.input[num] | ||
|
||
def get_set(self): | ||
""" | ||
Method get_set gets the value of the set control signal. | ||
:return: Value of the set control signal. | ||
:rtype: bool | ||
""" | ||
return self.set | ||
|
||
def get_output(self): | ||
""" | ||
Method get_output gets the output of the 2 to 1 Multiplexer. | ||
:return: Value of the output | ||
:rtype: bool | ||
""" | ||
return self.output | ||
|
||
def set_input(self, num, value): | ||
""" | ||
Method set_input sets a certain input to the desired value, either True or False. | ||
:param num: Number of the input selected. | ||
:type num: int | ||
:param value: Desired value of the input. | ||
:type value: bool | ||
:raises NonExistingInput: raised when a Multiplexer/Demultiplexer doesn't have the input asked for. | ||
:raises NotTruthValue: Raised when value's type is not bool. | ||
""" | ||
if num >= len(self.input) or num < 0: | ||
raise NonExistingInput(num) | ||
|
||
if type(value) is not bool: | ||
raise NotTruthValue | ||
|
||
self.input[num] = value | ||
self.output = self.__calculate_output() | ||
return self | ||
|
||
def set_set(self, value): | ||
""" | ||
Method set_set sets the value of the set control signal to the desired truth value. | ||
:param value: Desired value of the set control signal. | ||
:type value: bool | ||
:raises NotTruthValue: Raised when value's type is not bool. | ||
""" | ||
if type(value) is not bool: | ||
raise NotTruthValue | ||
|
||
self.set = value | ||
self.output = self.__calculate_output() | ||
return self | ||
|
||
def __calculate_output(self): | ||
""" | ||
Private method __calculate_output calculates the output signal using the 2-to-1 mux formula. | ||
:return: Value of the output signal. | ||
:rtype: bool | ||
""" | ||
return (self.input[0] and not(self.set)) or (self.input[1] and self.set) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
""" | ||
PyCircTools Multiplexers package. | ||
This package includes some Multiplexers and Demultiplexers (from 1 to 2 to 1 to 16) | ||
""" | ||
|
||
from CTools.Multiplexers.Mux2to1 import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
|
||
from CTools.Exceptions import * | ||
from CTools.LogicGates import * | ||
from CTools.Multiplexers import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from CTools import NotTruthValue, NonExistingInput | ||
from CTools.Multiplexers import Mux2to1 | ||
|
||
|
||
def test_Mux2to1(): | ||
errors = [] | ||
mux = Mux2to1() | ||
|
||
if mux.get_output(): | ||
errors.append("Base get output test not passed!") | ||
|
||
if mux.set_set(True).get_output(): | ||
errors.append("False, False with set to True test not passed!") | ||
|
||
if mux.set_input(1, True).set_set(False).get_output(): | ||
errors.append("False, True with set to False test not passed!") | ||
|
||
if not mux.set_set(True).get_output(): | ||
errors.append("False, True with set to True test not passed!") | ||
|
||
if not mux.set_input(0, True).set_input(1, False).set_set(False).get_output(): | ||
errors.append("True, False with set to False test not passed!") | ||
|
||
if mux.set_set(True).get_output(): | ||
errors.append("True, False with set to True test not passed!") | ||
|
||
if not mux.set_input(1, True).set_set(False).get_output(): | ||
errors.append("True, True with set to False test not passed!") | ||
|
||
if not mux.set_set(True).get_output(): | ||
errors.append("True, True with set to True test not passed!") | ||
|
||
try: | ||
mux.set_set(1) | ||
errors.append("Set to not truth test not passed!") | ||
except NotTruthValue: | ||
pass | ||
|
||
try: | ||
mux.set_input(-1, True) | ||
errors.append("Input to not existing input test not passed!") | ||
except NonExistingInput: | ||
pass | ||
|
||
assert not errors, "errors occured:\n{}".format("\n".join(errors)) |