Advertisement
Gronos02

MiVOS_Lab_4

Nov 11th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.64 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Pinout of the LCD:
  4. # 1 : GND
  5. # 2 : 5V power
  6. # 3 : Display contrast - Connect to middle pin potentiometer
  7. # 4 : RS (Register Select)
  8. # 5 : R/W (Read Write) - Ground this pin (important)
  9. # 6 : Enable or Strobe
  10. # 7 : Data Bit 0 - data pin 0, 1, 2, 3 are not used
  11. # 8 : Data Bit 1 -
  12. # 9 : Data Bit 2 -
  13. # 10: Data Bit 3 -
  14. # 11: Data Bit 4
  15. # 12: Data Bit 5
  16. # 13: Data Bit 6
  17. # 14: Data Bit 7
  18. # 15: LCD Backlight +5V
  19. # 16: LCD Backlight GND
  20.  
  21.  
  22. import RPi.GPIO as GPIO
  23. import time
  24. from threading import Thread, Event
  25.  
  26. # GPIO to LCD mapping
  27. LCD_RS = 21 # Pi pin
  28. LCD_RW = 26 # Pi pin
  29. LCD_E  = 20 # Pi pin
  30. LCD_D4 = 16 # Pi pin
  31. LCD_D5 = 19 # Pi pin
  32. LCD_D6 = 13 # Pi pin
  33. LCD_D7 = 12 # Pi pin
  34.  
  35. # Device constants
  36. LCD_CHR = True # Character mode
  37. LCD_CMD = False # Command mode
  38. LCD_WIDTH = 16    # Maximum characters per line
  39.  
  40. LCD_LINE_1 = 0x80 # LCD memory location for 1st line
  41. LCD_LINE_2 = 0xC0 # LCD memory location 2nd line
  42.  
  43. # Timing constants
  44. E_PULSE = 0.00005
  45. E_DELAY = 0.00005
  46.  
  47. led1pin = 24
  48. led2pin = 22
  49. led3pin = 23
  50. led4pin = 27
  51.  
  52. button1Pin = 6
  53. button2Pin = 1
  54. button3Pin = 7
  55. button4Pin = 8
  56.  
  57. DIRECTION = "right"
  58.  
  59. # Define main program code
  60. def main():
  61.     GPIO.setwarnings(False)
  62.    
  63.     GPIO.cleanup()
  64.    
  65.     GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
  66.     GPIO.setup(LCD_E, GPIO.OUT) # Set GPIO's to output mode
  67.     GPIO.setup(LCD_RS, GPIO.OUT)
  68.     GPIO.setup(LCD_RW, GPIO.OUT, initial = 0)
  69.     GPIO.setup(LCD_D4, GPIO.OUT)
  70.     GPIO.setup(LCD_D5, GPIO.OUT)
  71.     GPIO.setup(LCD_D6, GPIO.OUT)
  72.     GPIO.setup(LCD_D7, GPIO.OUT)
  73.  
  74.     GPIO.setup(led1pin, GPIO.OUT, initial = 0)
  75.     GPIO.setup(led2pin, GPIO.OUT, initial = 0)
  76.     GPIO.setup(led3pin, GPIO.OUT, initial = 0)
  77.     GPIO.setup(led4pin, GPIO.OUT, initial = 0)
  78.    
  79.     GPIO.setup(button1Pin, GPIO.IN)
  80.     GPIO.setup(button2Pin, GPIO.IN)
  81.     GPIO.setup(button3Pin, GPIO.IN)
  82.     GPIO.setup(button4Pin, GPIO.IN)
  83.  
  84.     lcd_init() # Initialize display
  85.    
  86.     def checkButton(event):
  87.         global DIRECTION
  88.         while True:
  89.             if GPIO.input(button1Pin) == False:
  90.                 DIRECTION = "left"
  91.             elif GPIO.input(button2Pin) == False:
  92.                 DIRECTION = "right"
  93.  
  94.     def runningLights(e):
  95.         global DIRECTION
  96.         GPIO.setmode(GPIO.BCM)
  97.         GPIO.setup(led1pin, GPIO.OUT)
  98.         GPIO.setup(led2pin, GPIO.OUT)
  99.         GPIO.setup(led3pin, GPIO.OUT)
  100.         GPIO.setup(led4pin, GPIO.OUT)
  101.         while True:
  102.             if DIRECTION == "right":
  103.                 for i in range(1, 5):
  104.                     print(i)
  105.                     GPIO.output(globals()[f"led{i}pin"], GPIO.HIGH)
  106.                     e.wait(0.5)
  107.                     GPIO.output(globals()[f"led{i}pin"], GPIO.LOW)
  108.             elif DIRECTION == "left":
  109.                 for i in range(4, 0, -1):
  110.                     print(i)
  111.                     GPIO.output(globals()[f"led{i}pin"], GPIO.HIGH)
  112.                     e.wait(0.5)
  113.                     GPIO.output(globals()[f"led{i}pin"], GPIO.LOW)
  114.    
  115.     event = Event()
  116.    
  117.     thCheck = Thread(target=checkButton, args = (event,))
  118.     thRender = Thread(target=runningLights, args = (event,))
  119.    
  120.     while True:
  121.         thCheck.start()
  122.         thRender.start()
  123.         thCheck.join()
  124.         thRender.join()
  125.      
  126.     # End of main program code
  127.  
  128.  
  129. def lcd_init():
  130.     # Initialise display
  131.     lcd_write(0x33, LCD_CMD) # Initialize
  132.     lcd_write(0x32, LCD_CMD) # Set to 4-bit mode
  133.     lcd_write(0x06, LCD_CMD) # Cursor move direction
  134.     lcd_write(0x0C, LCD_CMD) # Turn cursor off
  135.     lcd_write(0x06, LCD_CMD) # 2 line display
  136.     lcd_write(0x01, LCD_CMD) # Clear display
  137.     time.sleep(0.0005) # Delay to allow commands to process
  138.  
  139. def lcd_write(bits, mode):
  140.     GPIO.output(LCD_RS, mode) # RS
  141.  
  142.     # High bits
  143.     GPIO.output(LCD_D4, False)
  144.     GPIO.output(LCD_D5, False)
  145.     GPIO.output(LCD_D6, False)
  146.     GPIO.output(LCD_D7, False)
  147.    
  148.     if bits & 0x10 == 0x10:
  149.         GPIO.output(LCD_D4, True)
  150.     if bits & 0x20 == 0x20:
  151.         GPIO.output(LCD_D5, True)
  152.     if bits & 0x40 == 0x40:
  153.         GPIO.output(LCD_D6, True)
  154.     if bits & 0x80 == 0x80:
  155.         GPIO.output(LCD_D7, True)
  156.  
  157.     # Toggle 'Enable' pin
  158.     lcd_toggle_enable()
  159.  
  160.     # Low bits
  161.     GPIO.output(LCD_D4, False)
  162.     GPIO.output(LCD_D5, False)
  163.     GPIO.output(LCD_D6, False)
  164.     GPIO.output(LCD_D7, False)
  165.    
  166.     if bits & 0x01 == 0x01:
  167.         GPIO.output(LCD_D4, True)
  168.     if bits & 0x02 == 0x02:
  169.         GPIO.output(LCD_D5, True)
  170.     if bits & 0x04 == 0x04:
  171.         GPIO.output(LCD_D6, True)
  172.     if bits & 0x08 == 0x08:
  173.         GPIO.output(LCD_D7, True)
  174.  
  175.     # Toggle 'Enable' pin
  176.     lcd_toggle_enable()
  177.  
  178. def lcd_toggle_enable():
  179.     time.sleep(0.0005)
  180.     GPIO.output(LCD_E, True)
  181.     time.sleep(0.0005)
  182.     GPIO.output(LCD_E, False)
  183.     time.sleep(0.0005)
  184.  
  185. def lcd_text(message, line):
  186.  # Send text to display
  187.     message = message.ljust(LCD_WIDTH, " ")
  188.  
  189.     lcd_write(line, LCD_CMD)
  190.  
  191.     for i in range(LCD_WIDTH):
  192.         lcd_write(ord(message[i]), LCD_CHR)
  193.  
  194. #Begin program
  195. try:
  196.     main()
  197.  
  198. except KeyboardInterrupt:
  199.     print("Exit pressed Ctrl+C") # Выход из программы по нажатию Ctrl+C
  200. except Exception as e:
  201.     print(f"Other Exception: \n{e}")  # Прочие исключения
  202. finally:
  203.     lcd_write(0x01, LCD_CMD)
  204.     lcd_text("End of program", LCD_LINE_1)
  205.     lcd_text("Waiting...", LCD_LINE_2)
  206.     GPIO.cleanup() # Возвращаем пины в исходное состояние
  207.     print("End of program") # Информируем о завершении работы программы
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement