Advertisement
AverageMan

8x2.py

Oct 9th, 2014
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.27 KB | None | 0 0
  1. #!/usr/bin/python
  2. #
  3. # 8x2.py By @AverageManVsPi
  4. # AverageManVsRaspberryPi.com
  5. # This project uses a ProtoCam prototyping board - goo.gl/YsaaDu
  6. #
  7. # The wiring for the LCD is as follows:
  8. # 1 : GND
  9. # 2 : 5V
  10. # 3 : Contrast (0-5V)*
  11. # 4 : RS (Register Select)
  12. # 5 : R/W (Read Write)       - GROUND THIS PIN! We do not want the LCD to send anything to the Pi @ 5v
  13. # 6 : Enable or Strobe
  14. # 7 : Data Bit 0             - NOT USED
  15. # 8 : Data Bit 1             - NOT USED
  16. # 9 : Data Bit 2             - NOT USED
  17. # 10: Data Bit 3             - NOT USED
  18. # 11: Data Bit 4
  19. # 12: Data Bit 5
  20. # 13: Data Bit 6
  21. # 14: Data Bit 7
  22. # 15: LCD Backlight +5V
  23. # 16: LCD Backlight GND
  24.  
  25. #import
  26. import RPi.GPIO as GPIO
  27. import time
  28. import os
  29. import datetime
  30.  
  31. # Define GPIO to LCD mapping
  32. LCD_RS = 17
  33. LCD_E  = 18
  34. LCD_D4 = 27
  35. LCD_D5 = 22
  36. LCD_D6 = 23
  37. LCD_D7 = 24
  38.  
  39. # Define GPIO for button Controls
  40. LEFTBUTTON = 11 #GPIO 11 (BCM)
  41. RIGHTBUTTON = 9 #GPIO 9 (BCM)
  42. GREENLED = 14 #GPIO 14 (BCM)
  43.  
  44. # Define some device constants
  45. LCD_WIDTH = 8    # Maximum characters per line
  46. LCD_CHR = True
  47. LCD_CMD = False
  48.  
  49. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  50. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  51.  
  52. # Timing constants
  53. E_PULSE = 0.00005
  54. E_DELAY = 0.00005
  55.  
  56. def main():
  57.   GPIO.setwarnings(False) #Turn off annoying GPIO warnings
  58.   GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  59.   GPIO.setup(LCD_E, GPIO.OUT)  # E
  60.   GPIO.setup(LCD_RS, GPIO.OUT) # RS
  61.   GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  62.   GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  63.   GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  64.   GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  65.  
  66.   GPIO.setup(LEFTBUTTON, GPIO.IN) # set left button to input
  67.   GPIO.setup(RIGHTBUTTON, GPIO.IN) # set right button to input
  68.   GPIO.setup(GREENLED, GPIO.OUT) # set LED GPIO 14 to output
  69.  
  70.   # Initialise display
  71.   lcd_init()
  72.  
  73.   # Initial splash screen
  74.   lcd_byte(LCD_LINE_1, LCD_CMD)
  75.   lcd_string("ProtoCam",2)
  76.   lcd_byte(LCD_LINE_2, LCD_CMD)
  77.   lcd_string("Rules!",2)
  78.   time.sleep(1) #Little breather
  79.   menu() #go to menu()
  80.  
  81.  
  82.  
  83. def menu(): #main menu
  84.   timelastchecked = 0 # timelastchecked section makes the code run every couple of seconds
  85.   time.sleep(1)
  86.   while(1):
  87.    if time.time() >= timelastchecked:
  88.     timelastchecked = time.time()+2
  89.     imagecount = ""
  90.     f=os.popen("ls -l | grep ^- | wc -l") #this command counts the files in the directory
  91.     for i in f.readlines():
  92.      imagecount += i
  93.      imagecount = imagecount[0:-1] #the part at the end chops a character oiff the end of the string
  94.      imagecount = imagecount.zfill(4) #this makes the number a 4 digit number
  95.      lcd_byte(LCD_LINE_1, LCD_CMD)
  96.      lcd_string("^ Camera",1) #the '1' is align left, 2 is align center, 3 is align right
  97.      lcd_byte(LCD_LINE_2, LCD_CMD)
  98.      lcd_string(imagecount,3) #our image count, aligned right
  99.    else:
  100.     if ( GPIO.input(LEFTBUTTON) == True): #if left button clicked, go to camera menu
  101.      camera()
  102.              
  103. def camera(): #camera menu
  104.   time.sleep(0.5)
  105.   while(1):
  106.    if ( GPIO.input(LEFTBUTTON) == True): #if left button clicked, go to still() to take a still shot
  107.     still()
  108.    if ( GPIO.input(RIGHTBUTTON) == True): #if right button clicked, go to video() to take a vid
  109.     video()
  110.    else:
  111.     lcd_byte(LCD_LINE_1, LCD_CMD)
  112.     lcd_string("< Still",1)
  113.     lcd_byte(LCD_LINE_2, LCD_CMD)
  114.     lcd_string("Video >",2)
  115.            
  116. def still(): # the first part of the code here generates the date and time string which will be our picture filename, ensuring a different file name every time.
  117.   utc_datetime = datetime.datetime.utcnow()
  118.   s=utc_datetime.strftime("%d_%m_%Y_%H-%M-%S.jpg") #this is our filename string
  119.   filename = s #our string is now called 'filename'
  120.   time.sleep(0.5)
  121.   lcd_byte(LCD_LINE_1, LCD_CMD)
  122.   lcd_string("Say",2)
  123.   lcd_byte(LCD_LINE_2, LCD_CMD)
  124.   lcd_string("Cheese",2)
  125.  
  126.   time.sleep(1.5)
  127.  
  128.   print filename #print the file name
  129.  
  130.   GPIO.output(GREENLED, True) #LED on
  131.  
  132.   os.system('raspistill -o ' + filename + ' ') #take a still using the filename set above
  133.  
  134.   GPIO.output(GREENLED, False) #LED off
  135.  
  136.   lcd_byte(LCD_LINE_1, LCD_CMD)
  137.   lcd_string("Great",2)
  138.   lcd_byte(LCD_LINE_2, LCD_CMD)
  139.   lcd_string("Shot!",2)
  140.  
  141.   time.sleep(1.5)
  142.  
  143.   menu() #back to menu after shot taken
  144.  
  145. def video(): #same setup as the still, just with a video
  146.   utc_datetime = datetime.datetime.utcnow()
  147.   s=utc_datetime.strftime("%d_%m_%Y_%H-%M-%S.h264")
  148.   filename = s
  149.  
  150.   time.sleep(0.5)
  151.   lcd_byte(LCD_LINE_1, LCD_CMD)
  152.   lcd_string("Ready?",2)
  153.   lcd_byte(LCD_LINE_2, LCD_CMD)
  154.   lcd_string("3",2)
  155.   time.sleep(1)
  156.   lcd_byte(LCD_LINE_2, LCD_CMD)
  157.   lcd_string("2",2)
  158.   time.sleep(1)
  159.   lcd_byte(LCD_LINE_2, LCD_CMD)
  160.   lcd_string("1",2)
  161.   time.sleep(1)
  162.  
  163.   lcd_byte(LCD_LINE_1, LCD_CMD)
  164.   lcd_string("Video",2)
  165.   lcd_byte(LCD_LINE_2, LCD_CMD)
  166.   lcd_string("Rolling",2)
  167.  
  168.   GPIO.output(GREENLED, True)
  169.  
  170.   os.system('raspivid -o ' + filename + ' -t 10000') #10000 is the length of the video - 10 seconds
  171.  
  172.   GPIO.output(GREENLED, False)  
  173.  
  174.   lcd_byte(LCD_LINE_1, LCD_CMD)
  175.   lcd_string("Superb",1)
  176.   lcd_byte(LCD_LINE_2, LCD_CMD)
  177.   lcd_string("Filming!",2)
  178.  
  179.   time.sleep(1.5)
  180.  
  181.   menu() #back to menu
  182. # Everything after this is LCD setup 'stuff' - apart from the very last part
  183. def lcd_init():
  184.   # Initialise display
  185.   lcd_byte(0x33,LCD_CMD)
  186.   lcd_byte(0x32,LCD_CMD)
  187.   lcd_byte(0x28,LCD_CMD)
  188.   lcd_byte(0x0C,LCD_CMD)  
  189.   lcd_byte(0x06,LCD_CMD)
  190.   lcd_byte(0x01,LCD_CMD)  
  191.  
  192. def lcd_string(message,style):
  193.   # Send string to display
  194.   # style=1 Left justified
  195.   # style=2 Centred
  196.   # style=3 Right justified
  197.  
  198.   if style==1:
  199.     message = message.ljust(LCD_WIDTH," ")  
  200.   elif style==2:
  201.     message = message.center(LCD_WIDTH," ")
  202.   elif style==3:
  203.     message = message.rjust(LCD_WIDTH," ")
  204.  
  205.   for i in range(LCD_WIDTH):
  206.     lcd_byte(ord(message[i]),LCD_CHR)
  207.  
  208. def lcd_byte(bits, mode):
  209.   # Send byte to data pins
  210.   # bits = data
  211.   # mode = True  for character
  212.   #        False for command
  213.  
  214.   GPIO.output(LCD_RS, mode) # RS
  215.  
  216.   # High bits
  217.   GPIO.output(LCD_D4, False)
  218.   GPIO.output(LCD_D5, False)
  219.   GPIO.output(LCD_D6, False)
  220.   GPIO.output(LCD_D7, False)
  221.   if bits&0x10==0x10:
  222.     GPIO.output(LCD_D4, True)
  223.   if bits&0x20==0x20:
  224.     GPIO.output(LCD_D5, True)
  225.   if bits&0x40==0x40:
  226.     GPIO.output(LCD_D6, True)
  227.   if bits&0x80==0x80:
  228.     GPIO.output(LCD_D7, True)
  229.  
  230.   # Toggle 'Enable' pin
  231.   time.sleep(E_DELAY)    
  232.   GPIO.output(LCD_E, True)  
  233.   time.sleep(E_PULSE)
  234.   GPIO.output(LCD_E, False)  
  235.   time.sleep(E_DELAY)      
  236.  
  237.   # Low bits
  238.   GPIO.output(LCD_D4, False)
  239.   GPIO.output(LCD_D5, False)
  240.   GPIO.output(LCD_D6, False)
  241.   GPIO.output(LCD_D7, False)
  242.   if bits&0x01==0x01:
  243.     GPIO.output(LCD_D4, True)
  244.   if bits&0x02==0x02:
  245.     GPIO.output(LCD_D5, True)
  246.   if bits&0x04==0x04:
  247.     GPIO.output(LCD_D6, True)
  248.   if bits&0x08==0x08:
  249.     GPIO.output(LCD_D7, True)
  250.  
  251.   # Toggle 'Enable' pin
  252.   time.sleep(E_DELAY)    
  253.   GPIO.output(LCD_E, True)  
  254.   time.sleep(E_PULSE)
  255.   GPIO.output(LCD_E, False)  
  256.   time.sleep(E_DELAY)  
  257.  
  258. #This part leaves a message on the LCD once the script is exited, and cleans up GPIO
  259. try:
  260.   main()
  261. except:
  262.   print "Exit - cleaning up GPIO"
  263.  
  264.   lcd_byte(LCD_LINE_1, LCD_CMD)
  265.   lcd_string("Camera",2)
  266.   lcd_byte(LCD_LINE_2, LCD_CMD)
  267.   lcd_string(" Off",2)
  268.  
  269.   GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement