Wednesday, August 5, 2015

Dream Setup for Light Control

I was on a random wrangler forum.. I don't even remember which one. And someone was showing off their new "SPLOD" clone that they had made. And one of the comments 100 or so pages into the forum the guy posted a picture of his dream controller for a splod. In it he put a touch screen where the blank hole for power windows was.

That got me thinking that this is not a hard thing to do. I'm assuming that if you are reading this you already know how to wire up automotive relays to handle "Manual switches".



The things you will need to make your touchscreen controller are:

This is in no way all you would need.. It's just what I used to do the proof of concept. A finished version would also require:
  • some case and mounting for the touchscreen.
  • A 12 volt regulated power supply for the pi... Along with some sort of battery to make it so that it can survive temporary power drops/spikes that cars are VERY well known to have.

import pygame
from pygame.locals import *
import os
from time import sleep
import RPi.GPIO as GPIO

#Setup the GPIOs as outputs - only 4 and 17 are available
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.output(22, True)
GPIO.output(17, True)

#Colours
WHITE = (255,255,255)

#SETUP RECTS
TOP_LEFT = pygame.Rect(0,0,400,240)
TOP_RIGHT = pygame.Rect(400,0,400,240)
BOTTOM_LEFT = pygame.Rect(0,240,400,240)
BOTTOM_RIGHT = pygame.Rect(400,240,400,240)

pygame.init()
pygame.mouse.set_visible(False)
lcd = pygame.display.set_mode((800, 480))
lcd.fill((0,0,0))
pygame.display.update()

font_big = pygame.font.Font(None, 50)

touch_buttons = {'Bumper On':TOP_LEFT.center, 'Roof On':TOP_RIGHT.center, 'Bumper Off':BOTTOM_LEFT.center, 'Roof off':BOTTOM_RIGHT.center}

for k,v in touch_buttons.items():
    text_surface = font_big.render('%s'%k, True, WHITE)
    rect = text_surface.get_rect(center=v)
    lcd.blit(text_surface, rect)

pygame.display.update()

while True:
    # Scan touchscreen events
    for event in pygame.event.get():
        if(event.type is MOUSEBUTTONDOWN):
            pos = pygame.mouse.get_pos()
            print pos
        elif(event.type is MOUSEBUTTONUP):
            pos = pygame.mouse.get_pos()
            print pos
            #Find which quarter of the screen we're in
            x,y = pos
            if y < TOP_LEFT.height:
                if x < TOP_LEFT.width:
                    GPIO.output(17, False)
      print "Bumper On"
                else:
                    GPIO.output(22, False)
      print "Roof On"
            else:
                if x < TOP_LEFT.width:
                    GPIO.output(17, True)
                    print "Bumper Off"
                else:
                    GPIO.output(22, True)
                    print "Roof Off"
    sleep(0.1)