import time
import RPi.GPIO as GPIO

#Raspberry PI pinout: https://pinout.xyz/pinout/wiringpi
#Declare the pins. They will be defined through physical pins
RIGHT = 22 #physical pin 22, wiring pin 6
LEFT = 16 #physical pin 16, wiring pin 4
PARK = 29 #physical pin 29, wiring pin 21
CON = 15 #physical pin 15, wiring pin 3
REVERSE = 11 #physical 11, wiring pin 0
FORWARD = 32 #physical 32, wiring pin 26
SPEED = 13 #physical pin 13, wiring pin 2

GPIO.setmode(GPIO.BOARD) #Tell the pi that we will be using physical pins

#initialize all the pins
GPIO.setup(RIGHT, GPIO.OUT)
GPIO.setup(LEFT, GPIO.OUT)
GPIO.setup(PARK, GPIO.OUT)
GPIO.setup(CON, GPIO.OUT)
GPIO.setup(REVERSE, GPIO.OUT)
GPIO.setup(FORWARD, GPIO.OUT)
GPIO.setup(SPEED, GPIO.OUT)

#This function pushes a button for the input duration
def pushButton(inPin, duration):
    GPIO.output(inPin, GPIO.HIGH)
    time.sleep(duration)
    GPIO.output(inPin, GPIO.LOW)
    time.sleep(0.05) #wait a tiny amount to avoid any strange behaviour

#This is a demo of the different ways to control the car

#This pairing process only has to be done once and
#and then the car will always remember the remote
#Pushing the pairing button for 4 seconds
pushButton(CON, 4)
#Waiting for 3 seconds
time.sleep(3)

#Pushing reverse for three seconds.
pushButton(REVERSE, 3)

#Pushing forwards for three seconds.
pushButton(FORWARD, 3)

#Pushing left for three seconds.
pushButton(LEFT, 3)

#Pushing right for three seconds.
pushButton(RIGHT, 3)

#Pushing park toggles the parking mode on.
#Pushing park for 1 second
pushButton(PARK, 1)
#The car is now parked and will not move.

#Pushing park for 1 second.
pushButton(PARK, 1)
#The car is no longer parked and will now move.

#The speedmode button cycles through each speedmode.
#Each press increases the speed by 1, from 1 - 3.
#Pressing it while it is at 3 simply brings it back
#down to 1.

#Push speed for one second, assuming we are at 1
pushButton(SPEED, 1)
#We are now in speedmode 2
#Push speed for one second
pushButton(SPEED, 1)
#We are now in speedmode 3

#Push speed for one second
pushButton(SPEED, 1)
#We are now back in speedmode 1

GPIO.cleanup() #good practice to clean up and free everything when we finish