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.setup(22, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.output(22, True)
GPIO.output(17, True)

WHITE = (255,255,255)

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)

lcd = pygame.display.set_mode((800, 480))

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

touch_buttons = {'Bumper On', 'Roof On', 'Bumper Off', 'Roof off'}

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)


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"
                    GPIO.output(22, False)
      print "Roof On"
                if x < TOP_LEFT.width:
                    GPIO.output(17, True)
                    print "Bumper Off"
                    GPIO.output(22, True)
                    print "Roof Off"