Last active
August 29, 2015 14:06
-
-
Save karaage0703/2b338a3a6b700e43de8d to your computer and use it in GitHub Desktop.
auto camera script for Nakayoshi camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*0 coding: utf-8 -*- | |
import RPi.GPIO as GPIO | |
import time | |
import picamera | |
import os | |
import pygame | |
import syslog | |
# GPIO pin | |
LeftSh = 17 | |
RightSh = 18 | |
UpSw = 27 | |
RightSw = 22 | |
LowSw = 23 | |
LeftSw = 24 | |
RightLowSw = 10 | |
LeftLowSw = 9 | |
# GPIO setting | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(RightSh, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.setup(LeftSh, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.setup(UpSw, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.setup(RightSw, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.setup(LowSw, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.setup(LeftSw, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.setup(RightLowSw, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.setup(LeftLowSw, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
# Camera Setting | |
shutter_numb = 0 | |
preview_numb = 0 | |
# global | |
SleepStepSec = 0.1 | |
home_dir = '/home/pi/camera/data' | |
pygame.init() | |
size = (pygame.display.Info().current_w, pygame.display.Info().current_h) | |
print "Framebuffer size: %d x %d" % (size[0], size[1]) | |
screen = pygame.display.set_mode(size, pygame.FULLSCREEN) | |
keifont = pygame.font.Font(os.path.join(home_dir, 'keifont.ttf'), 80) | |
def screen_clear(): | |
screen.fill((0,0,0)) | |
pygame.display.update() | |
def screen_opening(): | |
title = keifont.render(u"なかよしデジカメ", True, (180,0,0)) | |
screen.fill((0,230,0)) | |
screen.blit(title, (20,150)) | |
pygame.display.update() | |
time.sleep(3) | |
screen_clear() | |
def load(): | |
global shutter_numb | |
filename = os.path.join(home_dir, 'camera.set') | |
fp = open(filename) | |
tmp_shutter_numb = fp.readlines() | |
tmp2_shutter_numb = tmp_shutter_numb[0].rstrip() | |
shutter_numb = int(tmp2_shutter_numb) | |
fp.close() | |
print "shutter_numb:" + str(shutter_numb) | |
def save(): | |
filename = os.path.join(home_dir, 'camera.set') | |
fp = open(filename, 'w') | |
fp.write(str(shutter_numb)) | |
fp.close() | |
def shutter(channel): | |
global shutter_numb | |
global preview_numb | |
print "shutter" | |
screen_clear() | |
shutter_numb += 1 | |
preview_numb = shutter_numb | |
save() | |
filename = os.path.join(home_dir, str(shutter_numb) + '.jpg') | |
photofile = open(filename, 'wb') | |
with picamera.PiCamera() as camera: | |
camera.resolution = (1024,768) | |
camera.start_preview() | |
time.sleep(2) # initialize camera | |
camera.capture(photofile) | |
photofile.close() | |
def KeepWatchForSeconds(seconds): | |
GoFlag = True | |
if seconds < 1: | |
seconds = 1 | |
while seconds > 0: | |
time.sleep(SleepStepSec) | |
seconds -= SleepStepSec | |
if (GPIO.input(LeftSh) == True): | |
GoFlag = False | |
break | |
return GoFlag | |
def CallShutdown(): | |
syslog.syslog(syslog.LOG_NOTICE, "Going shutdown by GPIO.") | |
os.system("/sbin/shutdown -h now 'Poweroff by GPIO'") | |
def preview(channel): | |
if KeepWatchForSeconds(5): | |
CallShutdown() | |
global preview_numb | |
print "preview" | |
filename = os.path.join(home_dir, str(preview_numb) + '.jpg') | |
img = pygame.image.load(filename).convert() | |
img = pygame.transform.scale(img, (pygame.display.Info().current_w, pygame.display.Info().current_h)) | |
screen.blit(img, (0,0)) | |
pygame.display.update() | |
preview_numb -= 1 | |
if preview_numb < 1: | |
preview_numb = shutter_numb | |
GPIO.add_event_detect(RightSh, GPIO.FALLING, callback=shutter, bouncetime=300) | |
GPIO.add_event_detect(LeftSh, GPIO.FALLING, callback=preview, bouncetime=300) | |
screen_opening() | |
load() | |
try: | |
GPIO.wait_for_edge(UpSw, GPIO.FALLING) | |
except KeyboardInterrupt: | |
GPIO.cleanup() # clean up GPIO on CTRL+C exit | |
GPIO.cleanup() # clean up GPIO on normal exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment