from pyfirmata import ArduinoMega import time import numpy as np # class to control a shift register chip class shiftRegister: # instantiates the GPIO objects based on the pin numbers def __init__(self, dataPin1, dataPin2, dataPin3, dataPin4, dataPin5, dataPin6, dataPin7, dataPin8, dataPin9, dataPin10, serialClock, registerClock): board = ArduinoMega('COM5') self.ser1 = board.digital[dataPin1] # Blue Wire self.ser2 = board.digital[dataPin2] # Blue Wire self.ser3 = board.digital[dataPin3] # Blue Wire self.ser4 = board.digital[dataPin4] # Blue Wire self.ser5 = board.digital[dataPin5] # Blue Wire self.ser6 = board.digital[dataPin6] # Blue Wire self.ser7 = board.digital[dataPin7] # Blue Wire self.ser8 = board.digital[dataPin8] # Blue Wire self.ser9 = board.digital[dataPin9] # Blue Wire self.ser10 = board.digital[dataPin10] # Blue Wire self.srclk = board.digital[serialClock] # Yellow wire self.rclk = board.digital[registerClock] # Green wire self.setup() # Pulses the latchpin - write the outputs to the data lines def latch(self): self.rclk.write(0) self.rclk.write(1) self.rclk.write(0) # Clear all the LEDS by pulsing the Serial Clock 8 times in and then the rclk once def clear(self): self.ser1.write(0) self.ser2.write(0) self.ser3.write(0) self.ser4.write(0) self.ser5.write(0) self.ser6.write(0) self.ser7.write(0) self.ser8.write(0) self.ser9.write(0) self.ser10.write(0) for x in range(0, 16): #Clears out all the values currently in the register self.srclk.write(0) self.srclk.write(1) self.srclk.write(0) self.latch() # sets the GPIOs to output with an initial value of zero def setup(self): self.ser1.write(0) self.ser2.write(0) self.ser3.write(0) self.ser4.write(0) self.ser5.write(0) self.ser6.write(0) self.ser7.write(0) self.ser8.write(0) self.ser9.write(0) self.ser10.write(0) self.srclk.write(0) self.rclk.write(0) self.clear() def inputBit(self, inputValue1, inputValue2, inputValue3, inputValue4, inputValue5, inputValue6, inputValue7, inputValue8, inputValue9, inputValue10): self.ser1.write(inputValue1) self.ser2.write(inputValue2) self.ser3.write(inputValue3) self.ser4.write(inputValue4) self.ser5.write(inputValue5) self.ser6.write(inputValue6) self.ser7.write(inputValue7) self.ser8.write(inputValue8) self.ser9.write(inputValue9) self.ser10.write(inputValue10) self.srclk.write(0) self.srclk.write(1) self.srclk.write(0) self.rclk.write(0) self.rclk.write(1) self.rclk.write(0) # push a byte to the shift regiter # splits the input values into individual values and inputs them. Then # pulses the latch pin to show the output. def outputBits(self, inputString): bitList = list(inputString) # Splits the string into a list of individual characters ("11000000" -> ["1","1","0","0","0","0","0","0"]) bitList1 = bitList[0:16] # The string to send LSB first bitList2 = bitList[16:32] bitList3 = bitList[32:48] # The string to send LSB first bitList4 = bitList[48:64] bitList5 = bitList[64:80] # The string to send LSB first bitList6 = np.flip(bitList[80:96]) bitList7 = np.flip(bitList[96:112]) # The string to send LSB first bitList8 = np.flip(bitList[112:128]) bitList9 = np.flip(bitList[128:144]) bitList10 = np.flip(bitList[144:160]) # The string to send LSB first for index in range(16): bit1 = int(bitList1[index]) # Transforms the character back into an int ("1" -> 1) bit2 = int(bitList2[index]) bit3 = int(bitList3[index]) # Transforms the character back into an int ("1" -> 1) bit4 = int(bitList4[index]) bit5 = int(bitList5[index]) # Transforms the character back into an int ("1" -> 1) bit6 = int(bitList6[index]) bit7 = int(bitList7[index]) # Transforms the character back into an int ("1" -> 1) bit8 = int(bitList8[index]) bit9 = int(bitList9[index]) # Transforms the character back into an int ("1" -> 1) bit10 = int(bitList10[index]) # print(bit1) # print(bit2) # print(bit3) # print(bit4) # print(bit5) # print(bit6) # print(bit7) # print(bit8) # print(bit9) # print(bit10) # print(" ") time.sleep(0.1) self.inputBit(bit1,bit2,bit3,bit4,bit5,bit6,bit7,bit8,bit9,bit10) self.latch()