Created
January 20, 2016 07:17
-
-
Save libertylocked/80e0aa3e609477f97f75 to your computer and use it in GitHub Desktop.
Use LEDs to show wanted level in GTAV with Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You need to run VStats plugin and server | |
# See https://github.com/LibertyLocked/VStats | |
import RPi.GPIO as GPIO | |
import time | |
import urllib2 | |
import json | |
led0=17 | |
led1=18 | |
led2=19 | |
led3=20 | |
led4=21 | |
host="http://192.168.1.2:25555/pull" | |
jsonStrCache="" | |
def getJSON(): | |
try: | |
dataRead=urllib2.urlopen(host).read() | |
jsonStrCache=dataRead | |
except: | |
pass | |
# If we run into an error somehow, return the cached copy | |
return json.loads(jsonStrCache) | |
def updateLEDs(stars): | |
GPIO.output(led0, 1 if stars > 0 else 0) | |
GPIO.output(led1, 1 if stars > 1 else 0) | |
GPIO.output(led2, 1 if stars > 2 else 0) | |
GPIO.output(led3, 1 if stars > 3 else 0) | |
GPIO.output(led4, 1 if stars > 4 else 0) | |
def mainLoop(): | |
try: | |
time.sleep(0.5) | |
data = getJSON() | |
updateLEDs(data["WantedLevel"]) | |
except: | |
#pass | |
raise | |
# Set up | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
# Light them all up first! | |
GPIO.setup(led0, GPIO.OUT) | |
GPIO.setup(led1, GPIO.OUT) | |
GPIO.setup(led2, GPIO.OUT) | |
GPIO.setup(led3, GPIO.OUT) | |
GPIO.setup(led4, GPIO.OUT) | |
GPIO.output(led0, 1) | |
GPIO.output(led1, 1) | |
GPIO.output(led2, 1) | |
GPIO.output(led3, 1) | |
GPIO.output(led4, 1) | |
try: | |
while True: | |
mainLoop() | |
except: | |
GPIO.cleanup() | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment