MicroPython Driver for Testing Logic Gates

Test Logic Gates Driver Code for micro:bit

Download as zip file

'''
Tester for 1, 2, 3, 4-input positive-logic gates.
Identify/test the following gate tyes:
 NOT (Inverter), Buffer
 AND, NAND, OR
 NOR, XOR, XNOR

AUTHOR  : fredscave.com
DATE    : 2024/10
VERSION : 1.00
'''

from microbit import *

LG_Dict1 = {'Buffer':'01', 'NOT (Inverter)':'10'}
LG_Dict2 = {'AND':'0001', 'NAND':'1110',
           'OR':'0111', 'NOR':'1000',
           'XOR':'0110', 'XNOR':'1001'}
LG_Dict3 = {'AND':'00000001', 'OR':'01111111',
             'NAND':'11111110', 'NOR':'10000000'}
LG_Dict4 = {'AND':'0000000000000001',
             'OR':'0111111111111111',
             'NAND':'1111111111111110',
             'NOR':'1000000000000000'}

class LGT():
    def __init__(self, PINout, PINin1,
                 PINin2 =None, PINin3 = None,
                 PINin4 = None):
        self.PINout = PINout
        self.PINin1 = PINin1
        self.PINin2 = PINin2
        self.PINin3 = PINin3
        self.PINin4 = PINin4
        if PINin2 == None:
            self.Inputs = 1
        elif PINin3 == None:
            self.Inputs = 2
        elif PINin4 == None:
            self.Inputs = 3
        else:
            self.Inputs = 4

    @staticmethod
    def get_key(Dict, value):
        for key in Dict:
            if Dict[key] == value:
                return key
        return None

    def GetTruthTable(self):
        if self.Inputs == 1:
            return self.GetTruthTable1()
        elif self.Inputs == 2:
            return self.GetTruthTable2()
        elif self.Inputs == 3:
            return self.GetTruthTable3()
        else:
            return self.GetTruthTable4()

    def GetTruthTable1(self):
        truth = list()
        for x in range(2):
            self.PINin1.write_digital(x)
            out = self.PINout.read_digital()
            truth.append([x, out])
        return truth

    def GetTruthTable2(self):
        truth = list()
        for x1 in range(2):
            for x2 in range(2):
                self.PINin1.write_digital(x1)
                self.PINin2.write_digital(x2)
                out = self.PINout.read_digital()
                l = [x1, x2, out]
                truth.append(l)
        return truth

    def GetTruthTable3(self):
        truth = list()
        for x1 in range(2):
            for x2 in range(2):
                for x3 in range(2):
                    self.PINin1.write_digital(x1)
                    self.PINin2.write_digital(x2)
                    self.PINin3.write_digital(x3)
                    out = self.PINout.read_digital()
                    l = [x1, x2, x3, out]
                    truth.append(l)
        return truth

    def GetTruthTable4(self):
        truth = list()
        for x1 in range(2):
            for x2 in range(2):
                for x3 in range(2):
                   for x4 in range(2):
                       self.PINin1.write_digital(x1)
                       self.PINin2.write_digital(x2)
                       self.PINin3.write_digital(x3)
                       self.PINin4.write_digital(x4)
                       out = self.PINout.read_digital()
                       l = [x1, x2, x3, x4, out]
                       truth.append(l)
        return truth

    def PrintTruthTable(self):
        print('TRUTH TABLE')
        if self.Inputs == 1:
            self.PrintTruthTable1()
        elif self.Inputs == 2:
            self.PrintTruthTable2()
        elif self.Inputs == 3:
            self.PrintTruthTable3()
        else:
            self.PrintTruthTable4()

    def PrintTruthTable1(self):
        truth = self.GetTruthTable()
        print('In', 'Out')
        print(truth[0][0], ' ', truth[0][1])
        print(truth[1][0], ' ', truth[1][1])

    def PrintTruthTable2(self):
        truth = self.GetTruthTable()
        print('In1', 'In2', 'Out')
        for x in range(4):
            print(' ', truth[x][0], '   ', truth[x][1],
                  '   ', truth[x][2], sep='')

    def PrintTruthTable3(self):
        truth = self.GetTruthTable()
        print('In1', 'In2', 'In3', 'Out')
        for x in range(8):
            print(' ', truth[x][0], '   ', truth[x][1],
                  '   ', truth[x][2], '   ', truth[x][3],
                  sep='')

    def PrintTruthTable4(self):
        truth = self.GetTruthTable()
        print('In1', 'In2', 'In3', 'In4', 'Out')
        for x in range(16):
            print(' ', truth[x][0], '   ', truth[x][1],
                  '   ', truth[x][2], '   ', truth[x][3],
                  '   ', truth[x][4], sep='')

    def LogicGate(self):
        Dict_List = [LG_Dict1, LG_Dict2, LG_Dict3, LG_Dict4]
        Dict = Dict_List[self.Inputs - 1]
        truth = self.GetTruthTable()
        output = ''
        for x in range(2 ** self.Inputs):
            output += str(truth[x][self.Inputs])
        gate = LGT.get_key(Dict, output)
        if gate != None:
            return gate
        else:
            return 'Unknown gate'
            
    def Report(self):
        print('\n')
        self.PrintTruthTable()
        print('\nLogic Gate:', self.LogicGate(), '\n')