Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #
- # 8x2.py By @AverageManVsPi
- # AverageManVsRaspberryPi.com
- # This project uses a ProtoCam prototyping board - goo.gl/YsaaDu
- #
- # The wiring for the LCD is as follows:
- # 1 : GND
- # 2 : 5V
- # 3 : Contrast (0-5V)*
- # 4 : RS (Register Select)
- # 5 : R/W (Read Write) - GROUND THIS PIN! We do not want the LCD to send anything to the Pi @ 5v
- # 6 : Enable or Strobe
- # 7 : Data Bit 0 - NOT USED
- # 8 : Data Bit 1 - NOT USED
- # 9 : Data Bit 2 - NOT USED
- # 10: Data Bit 3 - NOT USED
- # 11: Data Bit 4
- # 12: Data Bit 5
- # 13: Data Bit 6
- # 14: Data Bit 7
- # 15: LCD Backlight +5V
- # 16: LCD Backlight GND
- #import
- import RPi.GPIO as GPIO
- import time
- import os
- import datetime
- # Define GPIO to LCD mapping
- LCD_RS = 17
- LCD_E = 18
- LCD_D4 = 27
- LCD_D5 = 22
- LCD_D6 = 23
- LCD_D7 = 24
- # Define GPIO for button Controls
- LEFTBUTTON = 11 #GPIO 11 (BCM)
- RIGHTBUTTON = 9 #GPIO 9 (BCM)
- GREENLED = 14 #GPIO 14 (BCM)
- # Define some device constants
- LCD_WIDTH = 8 # Maximum characters per line
- LCD_CHR = True
- LCD_CMD = False
- LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
- LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
- # Timing constants
- E_PULSE = 0.00005
- E_DELAY = 0.00005
- def main():
- GPIO.setwarnings(False) #Turn off annoying GPIO warnings
- GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
- GPIO.setup(LCD_E, GPIO.OUT) # E
- GPIO.setup(LCD_RS, GPIO.OUT) # RS
- GPIO.setup(LCD_D4, GPIO.OUT) # DB4
- GPIO.setup(LCD_D5, GPIO.OUT) # DB5
- GPIO.setup(LCD_D6, GPIO.OUT) # DB6
- GPIO.setup(LCD_D7, GPIO.OUT) # DB7
- GPIO.setup(LEFTBUTTON, GPIO.IN) # set left button to input
- GPIO.setup(RIGHTBUTTON, GPIO.IN) # set right button to input
- GPIO.setup(GREENLED, GPIO.OUT) # set LED GPIO 14 to output
- # Initialise display
- lcd_init()
- # Initial splash screen
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("ProtoCam",2)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("Rules!",2)
- time.sleep(1) #Little breather
- menu() #go to menu()
- def menu(): #main menu
- timelastchecked = 0 # timelastchecked section makes the code run every couple of seconds
- time.sleep(1)
- while(1):
- if time.time() >= timelastchecked:
- timelastchecked = time.time()+2
- imagecount = ""
- f=os.popen("ls -l | grep ^- | wc -l") #this command counts the files in the directory
- for i in f.readlines():
- imagecount += i
- imagecount = imagecount[0:-1] #the part at the end chops a character oiff the end of the string
- imagecount = imagecount.zfill(4) #this makes the number a 4 digit number
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("^ Camera",1) #the '1' is align left, 2 is align center, 3 is align right
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string(imagecount,3) #our image count, aligned right
- else:
- if ( GPIO.input(LEFTBUTTON) == True): #if left button clicked, go to camera menu
- camera()
- def camera(): #camera menu
- time.sleep(0.5)
- while(1):
- if ( GPIO.input(LEFTBUTTON) == True): #if left button clicked, go to still() to take a still shot
- still()
- if ( GPIO.input(RIGHTBUTTON) == True): #if right button clicked, go to video() to take a vid
- video()
- else:
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("< Still",1)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("Video >",2)
- 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.
- utc_datetime = datetime.datetime.utcnow()
- s=utc_datetime.strftime("%d_%m_%Y_%H-%M-%S.jpg") #this is our filename string
- filename = s #our string is now called 'filename'
- time.sleep(0.5)
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("Say",2)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("Cheese",2)
- time.sleep(1.5)
- print filename #print the file name
- GPIO.output(GREENLED, True) #LED on
- os.system('raspistill -o ' + filename + ' ') #take a still using the filename set above
- GPIO.output(GREENLED, False) #LED off
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("Great",2)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("Shot!",2)
- time.sleep(1.5)
- menu() #back to menu after shot taken
- def video(): #same setup as the still, just with a video
- utc_datetime = datetime.datetime.utcnow()
- s=utc_datetime.strftime("%d_%m_%Y_%H-%M-%S.h264")
- filename = s
- time.sleep(0.5)
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("Ready?",2)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("3",2)
- time.sleep(1)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("2",2)
- time.sleep(1)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("1",2)
- time.sleep(1)
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("Video",2)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("Rolling",2)
- GPIO.output(GREENLED, True)
- os.system('raspivid -o ' + filename + ' -t 10000') #10000 is the length of the video - 10 seconds
- GPIO.output(GREENLED, False)
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("Superb",1)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string("Filming!",2)
- time.sleep(1.5)
- menu() #back to menu
- # Everything after this is LCD setup 'stuff' - apart from the very last part
- def lcd_init():
- # Initialise display
- lcd_byte(0x33,LCD_CMD)
- lcd_byte(0x32,LCD_CMD)
- lcd_byte(0x28,LCD_CMD)
- lcd_byte(0x0C,LCD_CMD)
- lcd_byte(0x06,LCD_CMD)
- lcd_byte(0x01,LCD_CMD)
- def lcd_string(message,style):
- # Send string to display
- # style=1 Left justified
- # style=2 Centred
- # style=3 Right justified
- if style==1:
- message = message.ljust(LCD_WIDTH," ")
- elif style==2:
- message = message.center(LCD_WIDTH," ")
- elif style==3:
- message = message.rjust(LCD_WIDTH," ")
- for i in range(LCD_WIDTH):
- lcd_byte(ord(message[i]),LCD_CHR)
- def lcd_byte(bits, mode):
- # Send byte to data pins
- # bits = data
- # mode = True for character
- # False for command
- GPIO.output(LCD_RS, mode) # RS
- # High bits
- GPIO.output(LCD_D4, False)
- GPIO.output(LCD_D5, False)
- GPIO.output(LCD_D6, False)
- GPIO.output(LCD_D7, False)
- if bits&0x10==0x10:
- GPIO.output(LCD_D4, True)
- if bits&0x20==0x20:
- GPIO.output(LCD_D5, True)
- if bits&0x40==0x40:
- GPIO.output(LCD_D6, True)
- if bits&0x80==0x80:
- GPIO.output(LCD_D7, True)
- # Toggle 'Enable' pin
- time.sleep(E_DELAY)
- GPIO.output(LCD_E, True)
- time.sleep(E_PULSE)
- GPIO.output(LCD_E, False)
- time.sleep(E_DELAY)
- # Low bits
- GPIO.output(LCD_D4, False)
- GPIO.output(LCD_D5, False)
- GPIO.output(LCD_D6, False)
- GPIO.output(LCD_D7, False)
- if bits&0x01==0x01:
- GPIO.output(LCD_D4, True)
- if bits&0x02==0x02:
- GPIO.output(LCD_D5, True)
- if bits&0x04==0x04:
- GPIO.output(LCD_D6, True)
- if bits&0x08==0x08:
- GPIO.output(LCD_D7, True)
- # Toggle 'Enable' pin
- time.sleep(E_DELAY)
- GPIO.output(LCD_E, True)
- time.sleep(E_PULSE)
- GPIO.output(LCD_E, False)
- time.sleep(E_DELAY)
- #This part leaves a message on the LCD once the script is exited, and cleans up GPIO
- try:
- main()
- except:
- print "Exit - cleaning up GPIO"
- lcd_byte(LCD_LINE_1, LCD_CMD)
- lcd_string("Camera",2)
- lcd_byte(LCD_LINE_2, LCD_CMD)
- lcd_string(" Off",2)
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement