Friday, 22 August 2014

Action camera #3 - Basic software

In this section I'll introduce you to the software that will drive the camera.
I will not go into details, just show you it's layout and some basic functionality of the camera.
The software is writen in python and is structured in modules that represent the submenus of the device.
As you start up the camera, you'll have the following options and submenus (indented).
You can navigate through this with the Left & Right buttons, confirm selection with the Enter button and start recording with the capture button.
  1. Image capture
  2. Video recording
  3. Image settings
    • Scene
    • Resolution
    • Shutter speed
    • Aperture
    • ISO
    • White balance
    • Saturation & sharpness
    • Back
  4. Video settings
    • Resolution
    • FPS
    • White balance
  5. Device settings
    • Time & Date
    • Exif
    • Units
    • Wifi
    • GPS
    • Sensors
  6. Power off
First thing to do is create a new python file: sudo nano main.py, you can run this later with the: sudo python main.py command.
In the newly created file you need to import some python libraries:

# module imports
import io
import time
import picamera
import RPi.GPIO as GPIO
from select import select

Then you will need to define the GPIO pins used for each button and initialize them.
The definition I placed in one function, since it's happening only one time, at device startup.
The initialization I placed in a different function, since it't run in every While True: loop.

def GPIO_pin_definition(): #function to assign GPIO pins
  GPIO.setmode(GPIO.BOARD)
  GPIO.setup(11, GPIO.IN, GPIO.PUD_UP) #selection button 1 - right
  GPIO.setup(15, GPIO.IN, GPIO.PUD_UP) #selection button 2 - left
  GPIO.setup(13, GPIO.IN, GPIO.PUD_UP) #enter button
  GPIO.setup(16, GPIO.IN, GPIO.PUD_UP) #capture button

def GPIO_init(): #funtion to initialize GPIO pins
  global select_button_1
  select_button_1 = GPIO.input(11)
  global select_button_2
  select_button_2 = GPIO.input(15)
  global enter_button
  enter_button = GPIO.input(13)
  global capture_button
  capture_button = GPIO.input(16)

Later these funtions will contain also the pins for the LCD and various sensors.

Next we need to define an infinite loop in the main program, that will continuasly check the status of the buttons and the value for two variables (temporary_sel and current_sel) and run functions according to theses values.
 
GPIO_pin_definition()
current_sel = 1 #Legend: 0-Idle, 1-Image, 2-Video, 3-Image settings, 4-Video settings, 5-Device_settings, 6-Power_off
temporary_sel = 1
 
while True:
  GPIO_init()
  display('Main menu','')
  time.sleep(0.2)
  if select_button_1 == False:
    # selection to left
    temporary_sel = temporary_sel + 1
    print 'Temporary selection:', temporary_sel
  if (select_button_2 == False) and (temporary_sel > 1):
    # selection to right
    temporary_sel = temporary_sel - 1
    print 'Temporary selection:', temporary_sel
  if enter_button == 0:
    current_sel = temporary_sel
    print 'Final selection:', current_sel
  if (capture_button == False) and (current_sel == 1):
    action('image')
  if (capture_button == False) and (current_sel == 2):
    action('video')
  if (current_sel == 3) and (enter_button == False):
    image_settings()
  if (current_sel == 4) and (enter_button == False):
    video_settings()
  if (current_sel == 5) and (enter_button == False):
    device_settings()
  if (current_sel == 6) and (enter_button == False):
    power_off()

You can notice that by pressing the left or right button, the value of the temporary_sel variable changes and when the enter button is pressed, current_sel takes over this value. Based on the value of current_sel and the pressing of enter botton, subfunctions are launched.

When the current_sel is 1 or 2 and enter is pressed, an action(xyz) function is launched. Based on the varriable passed to it, it will launch an image capturing function or video recording function.

def action(mode):
  if mode == 'image':
  image_capture(x_img_res,y_img_res,exp_img,iso_img)
  if mode == 'video':
  video_record(x_vid_res,y_vid_res)

When the current_sel is 3 an image_settings() function is launched.

def image_settings(): # Function containing the branches of image setting
  time.sleep(0.5)
  current_sel = 0
  temporary_sel = 30
  while ((current_sel <= 37) and (current_sel >= 30)) or (current_sel == 0):
    GPIO_init()
    time.sleep(0.2)
    if select_button_1 == False and (temporary_sel < 37): # selection to left
      temporary_sel = temporary_sel + 1
      display('Temporary selection',temporary_sel)
    if (select_button_2 == False) and (temporary_sel > 30): # selection to right
      temporary_sel = temporary_sel - 1
      display('Temporary selection',temporary_sel)
    if enter_button == 0:
      current_sel = temporary_sel
      display('Final selection',current_sel)
    if (current_sel == 30) and (enter_button == False):
      display('Scene selection','')
      #scene_settings()
    if (current_sel == 31) and (enter_button == False):
      display('Resolution selection','')
      #resolution_settings()
    if (current_sel == 32) and (enter_button == False):
      display('Shutter speed','')
      #shutter_settings()
    if (current_sel == 33) and (enter_button == False):
      display('Aperture size','')
      #aperture_settings()
    if (current_sel == 34) and (enter_button == False):
      display('ISO','')
      #iso_settings()
    if (current_sel == 35) and (enter_button == False):
      display('WB','')
      #white_balance_settings()
    if (current_sel == 36) and (enter_button == False):
      display('Picture settings','')
      #saturation and sharpness settings
    if (current_sel == 37) and (enter_button == False):
      display('Going back to main menu','')
      current_sel = 1
      temporary_sel = 1

You may have noticed that the GPIO pins are initialized also here in the While True: function.
Also you can notice that instead of print I used display('text1','text2') function.
The content of this at the moment is print text1, text2 but will be replaced with the commands needed to display text on the LCD screen.

That's all about for now. The conntent of the image_settings() function can be modified easily for the video and device settings. Also the setting functions itself will work in this same structure.

If you need all the python code writen up to this point you can download it from the following link (rev 01 - 1_main.py).

In the next chapters I'll write about the phisical connections, for the buttons and LCD display to the Raspberry Pi and the software needed for the LCD.

Stay tuned :)

No comments:

Post a Comment