#!/usr/bin/python # CarPI Web Host # CarPI is a project developed by Watsuprico # CarPI's objective is to make interfacing between the raspberry pi and car, driver and raspberry pi, and driver and raspberry pi easier # (Add bluetooth media streaming, untilize steering wheel controls to influence bluetooth media, and to interface with underdash illumination (either with user settings or car data, like RPM)) # for some reason my leds over do the blue, R255 G255 and B100 make white # Setup from __future__ import division debug = True def debugPrint(message): if debug: print colored("[DEBUG] "+message, "yellow") # Allow any characters (like jap ones) import sys import os import codecs sys.stdout = codecs.getwriter('utf8')(sys.stdout) sys.stderr = codecs.getwriter('utf8')(sys.stderr) from termcolor import colored # Allow color in the term # Get commands from the CGI script (so I don't have to scrub dbus for every method just to pause the music (it's cpu intensive and highly ineffective)) import socket from threading import * # Our end CGISocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) CGISocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) CGISocket.bind(("localhost", 8000)) # Used to get methods and stuff import dbus from xml.etree import ElementTree bus=dbus.SystemBus() # LEDs from time import sleep import pigpio from random import randint import json from multiprocessing import Process, Manager pi = pigpio.pi() Stop=False redPIN=17 greenPIN=22 bluePIN=24 colorCurrent={"red":0.0,"blue":0.0,"green":0.0} colorCurrent["red"]=0.0 colorCurrent["green"]=0.0 colorCurrent["blue"]=0.0 brightness=1 # 0-1 (.5=half) # OBDII RPM="0" SPEED="0" ENGINE_LOAD="0" COOLANT_TEMP="0" FUEL_LEVEL="0" AMBIANT_AIR_TEMP="0" # Setup complete # RGB Led stuff def colorToPin(color): if color=="red": return redPIN elif color=="green": return greenPIN else: return bluePIN def current(color): current=0.0 try: current=pi.get_PWM_dutycycle(colorToPin(color)) except Exception: current=0 return current def fadeFromTo(color,Int,End,duration): global Stop global colorCurrent pin=colorToPin(color) Int=round(abs(Int),5) End=round(abs(End),5) if Int>255: Int=255 elif Int<0: Int=0 if End>255: End=255 elif End<0: End=0 #debugPrint(color+" is fading "+str(Int)+"->"+str(End)+" using duration: " + str(duration)) if duration!=0 and Int-End!=0: duration=abs(duration)/abs(Int-End) if Int>End: while Int>=End and not Stop: pi.set_PWM_dutycycle(pin, Int) colorCurrent[color]=Int sleep(duration) Int=Int-1 else: while Int<=End and not Stop: pi.set_PWM_dutycycle(pin, Int) colorCurrent[color]=Int sleep(duration) Int=Int+1 def fadeTo(color,to,duration): fadeFromTo(color,current(color),to,duration) def fadeIn(color, duration): fadeTo(color,255,duration) def fadeOut(color,duration): fadeTo(color,0,duration) def fadeRGBToRGB(redIntValue,greenIntValue,blueIntValue,redEndValue,greenEndValue,blueEndValue,duration): redChange=redEndValue-redIntValue greenChange=greenEndValue-greenIntValue blueChange=blueEndValue-blueIntValue maxChange=max(abs(redChange),abs(greenChange),abs(blueChange),1) redInc=0.0 greenInc=0.0 blueInc=0.0 if redChange==0: redInc=0 else: redInc=round((redChange/maxChange),5) if greenChange==0: greenInc=0 else: greenInc=round((greenChange/maxChange),5) if blueChange==0: blueInc=0 else: blueInc=round((blueChange/maxChange),5) red=redIntValue green=greenIntValue blue=blueIntValue # debugPrint("maxChange: " + str(maxChange)) # debugPrint("redChange: " + str(redChange)) # debugPrint("greenChange: " + str(greenChange)) # debugPrint("blueChange: " + str(blueChange)) # debugPrint("red: " + str(red)) # debugPrint("green: " + str(green)) # debugPrint("blue: " + str(blue)) # debugPrint("redInc: " + str(redInc)) # debugPrint("greenInc: " + str(greenInc)) # debugPrint("blueInc: " + str(blueInc)) # debugPrint("redEndValue: " + str(redEndValue)) # debugPrint("greenEndValue: " + str(greenEndValue)) # debugPrint("blueEndValue: " + str(blueEndValue)) # debugPrint("duration: " + str(duration)) if duration!=0: duration=abs(duration)/maxChange i=0 while i