PiDashCam part two – Hardware, Software Overview and Pseudo Code

Part 2 of the series on my development of a Raspberry Pi based dashcam. See this post for part 1
Photo of PiDashCam breadboard

Although I’ve only just posted part 1 I’ve actually made quite a lot of progress towards achieving the objectives (as outlined in part 1.) In this post, I’ll look at the hardware I’ve chosen and show the Software Overview and Pseudo Code for pidashcam daemon

Breadboard Hardware

The picture at the top shows the basic hardware sitting on a breadboard for development.

  • Raspberry Pi 3 with the UPS Pico HAT sitting on top (including 450mAh battery)
  • Breadboard with Adafruit Ultimate GPS (including external antenna because I’m indoors) and the two buttons.
    I haven’t got the LEDs on yet.

The one addition is the HDMI Camera Extender I purchased from Tindie. This will allow me to have the camera mounted up near the top of the screen but have the main unit down on the dashboard somewhere: maybe down behind the gear change on my Golf Cabriolet.

PiDashCam Code Overview

The code comprises an installed system daemon with three threads:
1. Main thread
3. Camera thread
4. GPS thread

There are interrupt routines to handle the two buttons and there are several Events to manage signalling between the threads.

Pseudo Code

Subject to change – I’ll come back and revise this as development progresses

Button A interrupt handler (flush video)
  Wait for ExtraTime seconds
  set the flushBuffer event

Button B interrupt handler (stop recording)
  If recording
    Wait for ExtraTime seconds
    set the flushBuffer event
    wait for 
    reset the recording event
  Else
    Set the recording event
  EndIf

Camera thread
   Do until shutDown
      If recording
         Initialise Camera
         While recording
            start recording into buffer
            If flushBuffer
               flush buffer to new file
                  If shutdown is set
                     reset recording event
            Else
               update annotation with current date-time, position and speed
            EndIf
            wait for 0.2 seconds
         EndWhile
      EndIf
      wait for 1 second
   EndDo
   END

GPS Thread
    Initialise connection to gpsd
    Do until shutDown
        update current GPS info
    EndDo
    END

Power failure
  wait for ExtraTime seconds
  set flushBuffer
  set shutdown

Main thread
  Kick off Camera thread
  Kick off GPS thread
  reset the flush video flag
  reset the shutdown flag
  set the record flag
  while not shutdown
      wait for 1 second
  while LAN is connected and there are videos in the sync folder
      wait for 1 second
  Kill threads
  initiate system shutdown
  exit

Author: Gareth Howell

IT Professional, radio amateur, scout leader and beer drinker. I enjoy helping small business use their limited IT budgets to best effect