def power_off(params, result): # For now, I'm going to have it so the power_off function only works when the key is actually off # if we run this code and find that the car is actually back on, we'll emit a key_on if is_off(): # Okay, the key is off, we want to pull the relay logging.info("Shutting down the system in a few seconds...") # first, sync the filesystem (output before sync so things are more likely to be saved) os.system("sync") # sleep a little time.sleep(3) # unlatch if not isCI(): GPIO.output(UNLATCH, True) # we shouldn't get here unless the unlatch is broken time.sleep(5) logging.error("We tried to shut off the car but it didn't work!!!") else: logging.info( "This is where the car would be shut down, but we're in a CI environment" ) else: # The key isn't off? let the logs know logging.error("Not shutting off the car because the key isn't off?") if result != None: result.Pass()
def print_pins(): if not isCI(): try: logging.info("##############################") logging.info(f"KEY_OFF (PIN 33)={str(GPIO.input(KEY_OFF))}") logging.info(f"KEY_ON (PIN 35)={str(GPIO.input(KEY_ON))}") logging.info("##############################") except Exception as e: logging.error(e)
def stop_preview(params, result): if not isCI(): logging.info("Stopping the preview...") try: global camera camera.stop_preview() except Exception as e: logging.error(e) else: logging.info("We're in CI, we would have stopped the preview") result.Pass()
def stop_recording(params, result): if not isCI(): logging.info("Stopping the recording...") try: global camera camera.stop_recording() camera.close() except Exception as e: logging.error(e) else: logging.info("We're in CI, we would have stopped recording") result.Pass()
def start_preview(params, result): if not isCI(): logging.info("Starting the preview...") try: global camera # Cheat and place the preview inside a window that the GUI will have a black box around # camera.start_preview(fullscreen=False, window=(100, 100, 400, 600)) camera.start_preview() # The preview alpha has to be set after the preview is already active camera.preview.alpha = 128 except Exception as e: logging.error(e) else: logging.info("We're in CI, but we would have started the preview") result.Pass()
def gpio_setup(): if not isCI(): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(True) # Set up output pins GPIO.setup(GREEN_LED, GPIO.OUT) GPIO.setup(UNLATCH, GPIO.OUT) # Set up input pins # Used to check if ignition is on GPIO.setup(KEY_OFF, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Used to check if ignition is onclear GPIO.setup(KEY_ON, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Start a polling thread that will say if the key is on or off on a state change t = threading.Thread(target=poll_key_state) t.start() else: logging.info( "We're in the CI, no GPIO setup. Sleeping for 60 seconds, and then pretending the key went off." ) time.sleep(60) k.emitEvent("key_off")
def start_recording(params, result): logging.info("params for start_recording are: {}".format(params)) HRES = int(params.get('hres', 1280)) VRES = int(params.get('vres', 720)) ROT = int(params.get('rot', 0)) FRAMERATE = int(params.get('framerate', 10)) if not isCI(): logging.info("Starting the recording...") # try: global camera # Do all the camera setup camera = picamera.PiCamera() # the camera object camera.resolution = (HRES, VRES) # annotations camera.annotate_foreground = picamera.Color('white') camera.annotate_background = picamera.Color('black') camera.annotate_frame_num = True camera.annotate_text_size = 48 camera.annotate_text = datetime.datetime.now().strftime( "%Y-%m-%d-%H:%M:%S") # set the framerate camera.framerate = FRAMERATE # set the rotation camera.rotation = ROT camera.start_recording('/recordings/' + get_new_filename(), sps_timing=True) # spawn a thread that handles updating the time/frame counter t = threading.Thread(target=update_annotations) t.start() # except Exception as e: # logging.error(e) else: logging.info( "We're in CI, we would have started recording. Instead creating a fake big file" ) f = open('/recordings/' + get_new_filename(), "wb") f.seek(1000000 - 1) f.write(b"\0") f.close() result.Pass()
import karmen import threading import queue import logging import time import os import datetime from common import isCI if not isCI(): import picamera ##get_new_filename## camera = None def getserial(): # Extract serial from cpuinfo file cpuserial = "0000000000000000" try: f = open('/proc/cpuinfo', 'r') for line in f: if line[0:6] == 'Serial': cpuserial = line[10:26] f.close() except: cpuserial = "ERROR000000000" return cpuserial def get_new_filename():
def is_off(): if not isCI(): return GPIO.input(KEY_OFF) and not GPIO.input(KEY_ON) else: return True