Facebook
From ugkimg, 1 Month ago, written in Python.
Embed
Download Paste or View Raw
Hits: 142
  1. import time
  2. import os
  3. import RPi.GPIO as GPIO
  4.  
  5. GPIO.setmode(GPIO.BCM)
  6. GPIO.setwarnings(False)
  7.  
  8. # Define GPIO to LCD mapping
  9. LCD_RS = 4
  10. LCD_E  = 17
  11. LCD_D4 = 18
  12. LCD_D5 = 27
  13. LCD_D6 = 22
  14. LCD_D7 = 23
  15.  
  16. R1 = 5
  17. R2 = 6
  18. R3 = 12
  19. R4 = 13
  20.  
  21. C1 = 16
  22. C2 = 19
  23. C3 = 20
  24. C4 = 26
  25.  
  26. # Timing constants
  27. E_PULSE = 0.0005
  28. E_DELAY = 0.0005
  29.  
  30. GPIO.setup(LCD_E, GPIO.OUT)  # E
  31. GPIO.setup(LCD_RS, GPIO.OUT) # RS
  32. GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  33. GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  34. GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  35. GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  36.  
  37. GPIO.setup(R1, GPIO.OUT)
  38. GPIO.setup(R2, GPIO.OUT)
  39. GPIO.setup(R3, GPIO.OUT)
  40. GPIO.setup(R4, GPIO.OUT)
  41.  
  42. GPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  43. GPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  44. GPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  45. GPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  46.  
  47. # Define some device constants
  48. LCD_WIDTH = 16    # Maximum characters per line
  49. LCD_CHR = True
  50. LCD_CMD = False
  51. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  52. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  53.  
  54. def lcd_init():
  55.   # Initialise display
  56.   lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  57.   lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  58.   lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  59.   lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  60.   lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  61.   lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  62.   time.sleep(E_DELAY)
  63.  
  64. def lcd_byte(bits, mode):
  65.  
  66.   GPIO.output(LCD_RS, mode) # RS
  67.  
  68.   # High bits
  69.   GPIO.output(LCD_D4, False)
  70.   GPIO.output(LCD_D5, False)
  71.   GPIO.output(LCD_D6, False)
  72.   GPIO.output(LCD_D7, False)
  73.   if bits&0x10;==0x10:
  74.     GPIO.output(LCD_D4, True)
  75.   if bits&0x20;==0x20:
  76.     GPIO.output(LCD_D5, True)
  77.   if bits&0x40;==0x40:
  78.     GPIO.output(LCD_D6, True)
  79.   if bits&0x80;==0x80:
  80.     GPIO.output(LCD_D7, True)
  81.  
  82.   # Toggle 'Enable' pin
  83.   lcd_toggle_enable()
  84.  
  85.   # Low bits
  86.   GPIO.output(LCD_D4, False)
  87.   GPIO.output(LCD_D5, False)
  88.   GPIO.output(LCD_D6, False)
  89.   GPIO.output(LCD_D7, False)
  90.   if bits&0x01;==0x01:
  91.     GPIO.output(LCD_D4, True)
  92.   if bits&0x02;==0x02:
  93.     GPIO.output(LCD_D5, True)
  94.   if bits&0x04;==0x04:
  95.     GPIO.output(LCD_D6, True)
  96.   if bits&0x08;==0x08:
  97.     GPIO.output(LCD_D7, True)
  98.  
  99.   # Toggle 'Enable' pin
  100.   lcd_toggle_enable()
  101.  
  102. def lcd_toggle_enable():
  103.   # Toggle enable
  104.   time.sleep(E_DELAY)
  105.   GPIO.output(LCD_E, True)
  106.   time.sleep(E_PULSE)
  107.   GPIO.output(LCD_E, False)
  108.   time.sleep(E_DELAY)
  109.  
  110. def lcd_string(message,line):
  111.   # Send string to display
  112.  
  113.   message = message.ljust(LCD_WIDTH," ")
  114.  
  115.   lcd_byte(line, LCD_CMD)
  116.  
  117.   for i in range(LCD_WIDTH):
  118.     lcd_byte(ord(message[i]),LCD_CHR)
  119.    
  120. def readRow(line, characters):
  121.  
  122.     GPIO.output(line, GPIO.HIGH)
  123.     time.sleep(0.02)
  124.     if(GPIO.input(C1) == 1):
  125.        if line == R4:
  126.           lcd_string('',LCD_LINE_2)
  127.           time.sleep(0.01)
  128.        else:
  129.           lcd_string(characters[0],LCD_LINE_2)
  130.    
  131.     if(GPIO.input(C2) == 1):
  132.              lcd_string(characters[1],LCD_LINE_2)
  133.              time.sleep(0.01)
  134.     if(GPIO.input(C3) == 1):
  135.              lcd_string(characters[2],LCD_LINE_2)
  136.              time.sleep(0.01)    
  137.     if(GPIO.input(C4) == 1):            
  138.              lcd_string(characters[3],LCD_LINE_2)
  139.              time.sleep(0.01)
  140.    
  141.              
  142.     GPIO.output(line, GPIO.LOW)
  143.     time.sleep(0.02)
  144.  
  145. def calculator(matrix_char):
  146.     size=len(matrix_char)
  147.     func=''
  148.     result=0
  149.     number_before_char=[]
  150.     number_after_char=[]
  151.     for i in range(size):
  152.         if matrix_char[i] == '+' or matrix_char[i] == '-' or  matrix_char[i] == 'x' or matrix_char[i] == '/' :
  153.             func=matrix_char[i]
  154.             for j in range(i):
  155.              number_before_char.append(matrix_char[j])
  156.             for k in range(i+1,size):
  157.              number_after_char.append(matrix_char[k])
  158.                  
  159.     number_before_int=[float(element) for element in number_before_char]
  160.     result1 = 0
  161.     for i in range(len(number_before_int)):
  162.         result1 = result1 * 10 + number_before_int[i]
  163.  
  164.     number_after_int=[int(element) for element in number_after_char]
  165.     result2 = 0
  166.     for i in range(len(number_after_int)):
  167.         result2 = result2 * 10 + number_after_int[i]
  168.    
  169.     if func=='+':
  170.         result= result1+result2
  171.     if func=='-':
  172.         result= result1-result2
  173.        
  174.     if func=='x':
  175.         result= result1*result2
  176.     if func=='/':
  177.         result=result1/result2
  178.    
  179.     return result
  180.  
  181.    
  182. def key_pad(line, col,matrix_char):
  183.    
  184.    
  185.     GPIO.output(line, GPIO.HIGH)
  186.     time.sleep(0.01)
  187.     if(GPIO.input(C1) == 1):
  188.        if line == R4:
  189.           lcd_string('',LCD_LINE_1)
  190.           matrix_char.clear()
  191.        else:
  192.           lcd_string(col[0],LCD_LINE_1)
  193.           matrix_char.append((col[0]))
  194.           time.sleep(0.1)
  195.    
  196.     if(GPIO.input(C2) == 1):
  197.              lcd_string(col[1],LCD_LINE_1)
  198.              matrix_char.append((col[1]))
  199.              time.sleep(0.1)
  200.     if(GPIO.input(C3) == 1):
  201.         if line == R4:  
  202.              a=calculator(matrix_char)
  203.              lcd_string(str(a),LCD_LINE_1)
  204.              matrix_char.clear()
  205.              matrix_char.append(str(a))
  206.              time.sleep(0.1)        
  207.         else:
  208.              lcd_string(col[2],LCD_LINE_1)
  209.              matrix_char.append((col[2]))
  210.              time.sleep(0.1)  
  211.     if(GPIO.input(C4) == 1):            
  212.              lcd_string(col[3],LCD_LINE_1)
  213.              matrix_char.append((col[3]))
  214.              time.sleep(0.1)
  215.            
  216.     GPIO.output(line, GPIO.LOW)
  217.    
  218.     time.sleep(0.01)
  219.  
  220. # Define delay between readings
  221. lcd_init()
  222. matrix_char=[]
  223.  
  224. try:
  225.    while 1:
  226.       key_pad(R1, ["7","8","9","/"],matrix_char)
  227.       key_pad(R2, ["4","5","6","x"],matrix_char)
  228.       key_pad(R3, ["1","2","3","-"],matrix_char)
  229.       key_pad(R4, ["C","0","=","+"],matrix_char)
  230.  
  231. finally:
  232.       GPIO.cleanup()