DualShock 4 LED Bar Color Toggling by Swiping the Touchpad

This script is similar to the previous two, except that the color toggling is done with swipe gestures on the touchpad. It will remember your previous color and can be used with other scripts to maintain the same color when changing memory slots. To toggle forward through colors, swipe up on the touchpad; to toggle backward, swipe down.
Version1.0
Authorperfecthuntr
Publish DateWed, 6 May 2015 - 07:05
Last UpdateWed, 6 May 2015 - 07:05
Downloads197
ForumAnnouncement Topic
RATE


3

0

Code: Select all
/* =============================================================================================================================================
* Controller:           DualShock 4
* GPC Author:           PerfectHuntr
**/

 
/* ---------------------------------------------------------------------------------------------------------------------------------------------
* LED COLOR DEFINITIONS FOR PS4
* fSetLED(0,0,0,0); // Off
* fSetLED(1,0,0,0); // Blue
* fSetLED(0,1,0,0); // Red
* fSetLED(0,0,1,0); // Green
* fSetLED(0,0,0,1); // Pink
* fSetLED(1,0,1,0); // Cyan
* fSetLED(0,1,1,0); // Amber
* fSetLED(1,1,1,1); // White
**/

 
/* -----------------------------------------------------------------------------
 *  VARIABLES
**/

int a, b, c, d, i, change, delay, location, touched;
 
/* -----------------------------------------------------------------------------
 *  INITIALIZATION
**/

init {
    a = get_pvar(PVAR_1, 0, 1, 0);
    b = get_pvar(PVAR_2, 0, 1, 0);
    c = get_pvar(PVAR_3, 0, 1, 0);
    d = get_pvar(PVAR_4, 0, 1, 0);
    if      (a == 0 && b == 1 && c == 0 && d == 0) { i = 0; }
    else if (a == 0 && b == 1 && c == 1 && d == 0) { i = 1; }
    else if (a == 0 && b == 0 && c == 1 && d == 0) { i = 2; }
    else if (a == 1 && b == 0 && c == 0 && d == 0) { i = 3; }
    else if (a == 1 && b == 0 && c == 1 && d == 0) { i = 4; }
    else if (a == 0 && b == 0 && c == 0 && d == 1) { i = 5; }
    else if (a == 1 && b == 1 && c == 1 && d == 1) { i = 6; }
    color(i);
}
 
/* ---------------------------------------------------------------------------------------------------------------------------------------------
* MAIN SCRIPT
**/

main {
    if (Swipe()) { change = 1; }
    if      (i < 0) { i = 6; }
    else if (i > 6) { i = 0; }
    if (change) { change = 0; color(i); }
    if (get_val(PS4_PS) && get_ptime(PS4_PS) > 3000) { set_val(PS4_PS, 0); turn_off(); }
}
 
/* ---------------------------------------------------------------------------------------------------------------------------------------------
* FUNCTIONS
**/

// Color Lookup
function color(a) {
    if      (i == 0) { fSetLED(0,1,0,0); } // Red
    else if (i == 1) { fSetLED(0,1,1,0); } // Amber
    else if (i == 2) { fSetLED(0,0,1,0); } // Green
    else if (i == 3) { fSetLED(1,0,0,0); } // Blue
    else if (i == 4) { fSetLED(1,0,1,0); } // Cyan
    else if (i == 5) { fSetLED(0,0,0,1); } // Magenta
    else if (i == 6) { fSetLED(1,1,1,1); } // White
}
 
// LED function for PS4
function fSetLED(a, b, c, d) {
    set_led(LED_1, a);
    set_led(LED_2, b);
    set_led(LED_3, c);
    set_led(LED_4, d);
    set_pvar(PVAR_1, a);
    set_pvar(PVAR_2, b);
    set_pvar(PVAR_3, c);
    set_pvar(PVAR_4, d);
}
 
// Swipe detection for DS4
function Swipe() {
    if (!ps4_touchpad(PS4T_P1)) {   // Not touching the pad
        touched = FALSE;            // Let the following logic statements know
        delay = 0;                  // Stop the timer
        return 0;                   // Let the main loop know
    }
    else { touched = TRUE; }        // Touching the pad. Proceed
 
    delay = delay - get_rtime();    // Remove elapsed time from the timer
    if (delay <= 0) {               // Timer ran out or was stopped, so:
        location = 0;               // Zero out the location
        delay = 0;                  // Stop the timer
    }
 
    if (touched) {                                  // Touching the pad... proceed
        if (delay > 0) {                            // If the timer is counting down,
            if (location > 50) {                    // check the previous location (near the bottom here).
                if (get_val(PS4_TOUCHY) < -50) {    // Now, check the current location. Is it near the top?
                    delay = 0;                      // If so, stop the timer.
                    i = i+1;                        // That means you swiped up, so go up in the color loop and
                    return 1;                       // tell the main function that there was a swipe.
                }
            }
                                                    // The timer is still counting down, so
            else if (location < -50) {              // check the previous location (near the top here).
                if (get_val(PS4_TOUCHY) > 50) {     // Now, check the current location. Is it near the bottom?
                    delay = 0;                      // If so, stop the timer.
                    i = i-1;                        // That means you swiped down, so go down in the color loop and
                    return 1;                       // tell the main function that there was a swipe.
                }
            }
        }
        else if (abs(get_val(PS4_TOUCHY)) > 50) {   // Check if the touchpad is being touched near one of the sides.
            location = get_val(PS4_TOUCHY);         // If so, set the location of the touch and
            delay = 300;                            // start the timer
        }
    }
 
    return 0; // Return 0 to main if nothing happened
}