from RPi import GPIO from matrix_keypad import RPi_GPIO import time #settitngs time_to_wait = 1 #in seconds position_pins = (22,27,17,24) segment_pins = (11,4,23,8,7,10,18,25) rows = (6, 13, 19, 26) columns = (16, 20 ,21) symbol_codes = { ' ':(0,0,0,0,0,0,0,0), '0':(1,1,1,1,1,1,0,0), '1':(0,1,1,0,0,0,0,0), '2':(1,1,0,1,1,0,1,0), '3':(1,1,1,1,0,0,1,0), '4':(0,1,1,0,0,1,1,0), '5':(1,0,1,1,0,1,1,0), '6':(1,0,1,1,1,1,1,0), '7':(1,1,1,0,0,0,0,0), '8':(1,1,1,1,1,1,1,0), '9':(1,1,1,1,0,1,1,0) } class keypad(): def __init__(self): GPIO.setmode(GPIO.BCM) # CONSTANTS self.KEYPAD = [ [1,2,3], [4,5,6], [7,8,9], ["*",0,"#"] ] def getKey(self): # Set all columns as output low for j in range(len(columns)): GPIO.setup(columns[j], GPIO.OUT) GPIO.output(columns[j], GPIO.LOW) # Set all rows as input for i in range(len(rows)): GPIO.setup(rows[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) # Scan rows for pushed key/button # A valid key press should set "rowVal" between 0 and 3. rowVal = -1 for i in range(len(rows)): tmpRead = GPIO.input(rows[i]) if tmpRead == 0: rowVal = i # if rowVal is not 0 thru 3 then no button was pressed and we can exit if rowVal <0 or rowVal >3: self.exit() return # Convert columns to input for j in range(len(columns)): GPIO.setup(columns[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Switch the i-th row found from scan to output GPIO.setup(rows[rowVal], GPIO.OUT) GPIO.output(rows[rowVal], GPIO.HIGH) # Scan columns for still-pushed key/button # A valid key press should set "colVal" between 0 and 2. colVal = -1 for j in range(len(columns)): tmpRead = GPIO.input(columns[j]) if tmpRead == 1: colVal=j # if colVal is not 0 thru 2 then no button was pressed and we can exit if colVal <0 or colVal >2: self.exit() return # Return the value of the key pressed self.exit() return self.KEYPAD[rowVal][colVal] def exit(self): # Reinitialize all rows and columns as input at exit for i in range(len(rows)): GPIO.setup(rows[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) for j in range(len(columns)): GPIO.setup(columns[j], GPIO.IN, pull_up_down=GPIO.PUD_UP) class Main(object): def __init__(self): self.display_text = '' self.wait_time = 0 self.need_update = False def activate_digit(self, position): GPIO.output(position_pins[position], 0) time.sleep(0.003) GPIO.output(position_pins[position], 1) def print_symbol_on_display(self, symbol, position): GPIO.output(segment_pins, symbol_codes[symbol]) self.activate_digit(position) def display(self): text = self.display_text.rjust(4) for i in range(len(text)): self.print_symbol_on_display(text[i], i) def clear_display(self): self.display_text = '' display() def write_to_file(self, filename, text): file = None try: file = open(filename, 'w') file.write(text) finally: if file: file.close() def save_display_text_to_file(self): self.write_to_file(self.display_text, 'false') time.sleep(time_to_wait) self.write_to_file(self.display_text, 'true') def process_events(self, key): if not key: return self.wait_time = 0.25 if isinstance(key, int): # If a digit was entered if self.need_update: self.display_text = '' self.need_update = False self.display_text += str(key) self.display() if len(self.display_text) == 3: print('displayed') self.save_display_text_to_file() self.wait_time = 10 self.need_update = True elif key == '#': #Else if it '#' self.display_text = '' #Start from here try: GPIO.setmode(GPIO.BCM) GPIO.setup(position_pins, GPIO.OUT, initial=1) GPIO.setup(segment_pins, GPIO.OUT, initial=1) kp = keypad() app = Main() t = time.time() while True: key = kp.getKey() if time.time() - t >= app.wait_time: app.wait_time = 0.001; t = time.time() app.process_events(key) app.display() finally: GPIO.cleanup()