

    --PSP Car Control by Vince Casale
    --This code is experimental, and should not be used for any purposes
    --I will not be responsible if you crash your car into a wall by implementing this
    --Please check the laws of your state before attempting to drive any real car wirelessly
    --This code requires the use of Lua player version .15 or above. See luaplayer.org for more info

    buttons = {}
    buttons.cross = 0
    buttons.circle = 0
    buttons.triangle = 0
    buttons.square = 0
    buttons.up = 0
    buttons.down = 0
    buttons.left = 0
    buttons.right = 0
    buttons.l = 0
    buttons.r = 0

    blinkerDanceStatus = 0
    RblinkerStatus = 0
    LblinkerStatus = 0
    DoorStatus = 0
    Headlights = 0

    white = Color.new(255, 255, 255)
    offscreen = Image.createEmpty(480, 272)
    offscreen:clear(Color.new(0, 0, 0))
    y = 0
    x = 0
    background = Image.load("pspImages.png")
    screen:blit(0, 0, background)

    function graphicsPrint(text)
    for i = 1, string.len(text) do
    char = string.sub(text, i, i)
    if char == "\n" then
    y = y + 8
    x = 0
    elseif char ~= "\r" then
    --offscreen:print(x, y, char, white)
    x = x + 8
    end
    end
    --screen:blit(0, 0, offscreen)
    --screen.waitVblankStart()
    --screen.flip()
    end

    -- init WLAN and choose connection config
    Wlan.init()
    configs = Wlan.getConnectionConfigs()
    graphicsPrint("available connections:\n")
    for key, value in configs do
    graphicsPrint(key .. ": " .. value .. "\n")
    end

    function sendToWACI(text)
    length = string.len(text)
    socket, error = Socket.connect("192.168.1.101", 80)
    while not socket:isConnected() do System.sleep(100) end
    bytesSent = socket:send("POST /RPC HTTP/1.1\r\n")
    bytesSent = socket:send("Content-Type: application/x-www-form-urlencoded\r\n")
    bytesSent = socket:send("Content-Length: "..length.."\r\n")
    bytesSent = socket:send("\r\n")
    bytesSent = socket:send(text.."\r\n\r\n")
    closeIt = socket:close()
    end

     

    Wlan.useConnectionConfig(0)
    -- graphicsPrint("your IP address is " .. Wlan.getIPAddress() .. "\n") TODO: print garbage

    play = true
    while play do

    local keyin = Controls.read()

    screen:blit(0,0, background)
    -- screen:print(160, 10, "xpos=" .. keyin:analogX() .. " ypos=" .. keyin:analogY(), Color.new(0,0,0))


    if keyin:up() then
    if buttons.up == 0 then
    sendToWACI("method=Serial_Send&Param1=1&Param2=IH1%0D&Param3=200")
    screen:print(160, 30, "Horn On", Color.new(0,0,0))
    buttons.up = 1
    end
    else
    if buttons.up == 1 then
    sendToWACI("method=Serial_Send&Param1=1&Param2=IL1%0D&Param3=200")
    screen:print(160, 30, "Horn Off", Color.new(0,0,0))
    buttons.up = 0
    end
    end

    if keyin:down() then
    if buttons.down == 0 then
    if blinkerDanceStatus == 1 then
    sendToWACI("method=TriggerEventByName&Param1=DanceStart&Param2=0&Param3=0&Param4=0")
    blinkerDanceStatus = 0
    else
    sendToWACI("method=TriggerEventByName&Param1=DanceStop&Param2=0&Param3=0&Param4=0")
    blinkerDanceStatus = 1
    end
    buttons.down = 1
    end
    else
    if buttons.down == 1 then
    buttons.down = 0
    end

    end

    if keyin:left() then
    if buttons.left == 0 then
    sendToWACI("method=Relay_On&Param1=1")
    buttons.left = 1
    end
    else
    if buttons.left == 1 then
    sendToWACI("method=Relay_Off&Param1=1")
    buttons.left = 0
    end
    end


    if keyin:right() then
    if buttons.right == 0 then
    sendToWACI("method=Relay_On&Param1=2")
    buttons.right = 1
    end
    else
    if buttons.right == 1 then
    sendToWACI("method=Relay_Off&Param1=2")
    buttons.right = 0
    end
    end

     

    if keyin:circle() then
    buttons.circle= 1
    else
    if buttons.circle== 1 then
    sendToWACI("method=TriggerEventByName&Param1=A2_toggle&Param2=0&Param3=0&Param4=0")
    buttons.circle= 0
    end
    end

    if keyin:triangle() then
    if buttons.triangle == 0 then
    if DoorStatus == 1 then
    sendToWACI("method=TriggerEventByName&Param1=H6&Param2=0&Param3=0&Param4=0")
    sendToWACI("method=TriggerEventByName&Param1=H8&Param2=0&Param3=0&Param4=0")
    DoorStatus = 0
    else
    sendToWACI("method=TriggerEventByName&Param1=H5&Param2=0&Param3=0&Param4=0")
    sendToWACI("method=TriggerEventByName&Param1=H7&Param2=0&Param3=0&Param4=0")
    DoorStatus = 1
    end
    buttons.triangle = 1
    end
    else
    if buttons.triangle == 1 then
    buttons.triangle = 0
    end

    end

     

    if keyin:square() then
    if buttons.square == 0 then
    sendToWACI("method=Serial_Send&Param1=1&Param2=AH2%0D&Param3=200")
    buttons.square = 1
    end
    else
    if buttons.square == 1 then
    sendToWACI("method=Serial_Send&Param1=1&Param2=AL2%0D&Param3=200")
    buttons.square = 0
    end
    end


    if keyin:cross() then

    if buttons.cross == 0 then
    sendToWACI("method=TriggerEventByName&Param1=accel_up&Param2=0&Param3=0&Param4=0")
    buttons.cross = 1
    end

    else

    if buttons.cross == 1 then
    sendToWACI("method=TriggerEventByName&Param1=accel_down&Param2=0&Param3=0&Param4=0")
    buttons.cross = 0
    end
    end

     

    if keyin:l() then
    if buttons.l == 0 then
    if LblinkerStatus == 1 then
    sendToWACI("method=TriggerEventByName&Param1=A5_0&Param2=0&Param3=0&Param4=0")
    LblinkerStatus = 0
    else
    sendToWACI("method=TriggerEventByName&Param1=A5_1&Param2=0&Param3=0&Param4=0")
    LblinkerStatus = 1
    end
    buttons.l = 1
    end
    else
    if buttons.l == 1 then
    buttons.l = 0
    end

    end


    if keyin:r() then
    if buttons.r == 0 then
    if RblinkerStatus == 1 then
    sendToWACI("method=TriggerEventByName&Param1=A6_0&Param2=0&Param3=0&Param4=0")
    RblinkerStatus = 0
    else
    sendToWACI("method=TriggerEventByName&Param1=A6_1&Param2=0&Param3=0&Param4=0")
    RblinkerStatus = 1
    end
    buttons.r = 1
    end
    else
    if buttons.r == 1 then
    buttons.r = 0
    end

    end



    if keyin:start() then
    play = false
    end

     

    screen.flip()
    screen.waitVblankStart()

    end

    Wlan.term()

