Need help optimizing a script
35 posts
• Page 1 of 4 • 1, 2, 3, 4
Need help optimizing a script
I really need help optimizing a script I created in order to minimize the CPU usage as much as possible, because I tried to do it myself, and caused the CPU usage to somehow jump from an extremely erratic 5-90% to a theoretically impossible 157-166% as a result of trying to condense things using arrays, of which I have almost no knowledge on how to use.
I actually went and tried to use some code from a script Scachi came up with called "ATBH" as a basis for the optimization, as the script they made was highly efficient and had some similar functions, though I clearly implemented it poorly.
Also, I actually did not run any tests on the second script, because the instant I saw the CPU usage, I unloaded it from the Titan Two, and did not load it again due to fear of damaging the device, and as such, I HIGHLY advise against running the second script.
I will of course do my best to answer any questions anyone may have, as well as help with the testing, debugging, and possibly coding, though my help with coding will probably be rather limited.
I will provide both the original script and my failed attempt at optimization so that you may compare them if you wish, as well as if you would rather try and fix my failed attempt instead of working on the original.
Here's the original script:
And here's my failed attempt at optimizing the script:
Everything being said and done though, I would really appreciate any help I can get, as I need to reduce the CPU usage as much as possible, while keeping as much functionality as I can.
I actually went and tried to use some code from a script Scachi came up with called "ATBH" as a basis for the optimization, as the script they made was highly efficient and had some similar functions, though I clearly implemented it poorly.
Also, I actually did not run any tests on the second script, because the instant I saw the CPU usage, I unloaded it from the Titan Two, and did not load it again due to fear of damaging the device, and as such, I HIGHLY advise against running the second script.
I will of course do my best to answer any questions anyone may have, as well as help with the testing, debugging, and possibly coding, though my help with coding will probably be rather limited.
I will provide both the original script and my failed attempt at optimization so that you may compare them if you wish, as well as if you would rather try and fix my failed attempt instead of working on the original.
Here's the original script:
- Code: Select all
#pragma METAINFO("Testing Script", 0, 0, "Fenria")
#define NOT_USE 0
#define DZ_CIRCLE 101
#define PIO_NONE 0
#define PIO_AUTO 0
#define PIO_PS4 4
#define PIO_PS3 1
#define PIO_XB1 5
#define PIO_XB360 2
#define PIO_WII 3
#define PIO_HID 6
#define PIO_SWITCH 7
#define PS4_PS BUTTON_1
#define PS4_TOUCH BUTTON_2
#define PS4_OPTIONS BUTTON_3
#define PS4_R1 BUTTON_4
#define PS4_R2 BUTTON_5
#define PS4_R3 BUTTON_6
#define PS4_L1 BUTTON_7
#define PS4_L2 BUTTON_8
#define PS4_L3 BUTTON_9
#define PS4_UP BUTTON_10
#define PS4_DOWN BUTTON_11
#define PS4_LEFT BUTTON_12
#define PS4_RIGHT BUTTON_13
#define PS4_TRIANGLE BUTTON_14
#define PS4_CIRCLE BUTTON_15
#define PS4_CROSS BUTTON_16
#define PS4_SQUARE BUTTON_17
#define PS4_SHARE BUTTON_18
#define PS4_TOUCH1 BUTTON_19
#define PS4_TOUCH2 BUTTON_20
#define PS4_RX STICK_1_X
#define PS4_RY STICK_1_Y
#define PS4_LX STICK_2_X
#define PS4_LY STICK_2_Y
#define PS4_TOUCH1X POINT_1_X
#define PS4_TOUCH1Y POINT_1_Y
#define PS4_TOUCH2X POINT_2_X
#define PS4_TOUCH2Y POINT_2_Y
#define PS4_ACCX ACCEL_1_X
#define PS4_ACCY ACCEL_1_Y
#define PS4_ACCZ ACCEL_1_Z
#define PS4_GYROX GYRO_1_X
#define PS4_GYROY GYRO_1_Y
#define PS4_GYROZ GYRO_1_Z
#define TRACE_1 ACCEL_1_X
#define TRACE_2 ACCEL_1_Y
#define TRACE_3 ACCEL_1_Z
#define TRACE_4 ACCEL_2_X
#define TRACE_5 ACCEL_2_Y
#define TRACE_6 ACCEL_2_Z
int16 LC = 0;
int16 LC1 = 0;
int16 LC2 = 0;
int16 LC3 = 0;
int16 DZ = 10;
int16 RS = 100;
int16 LS = 100;
int16 DT = 0;
int16 TT = 0;
int16 CS = 0;
int16 CP = 0;
int16 IOP = 1;
int16 PT = 100;
int16 TRT[30];
int16 v, RV;
int16 ON = 40;
int16 OFF = 40;
int16 ALT = 0;
int16 F1 = 0;
int16 F2 = 0;
int16 F3 = 0;
int16 F4 = 0;
int16 F5 = 0;
int16 F6 = 0;
int16 F7 = 0;
int16 F8 = 0;
int16 F9 = 0;
int16 F10 = 0;
int16 F11 = 0;
int16 F12 = 0;
int16 F13 = 0;
int16 F14 = 0;
int16 QS = 0;
int16 QSS = PS4_L2;
int16 QSF = PS4_R2;
init {
while(v < 30) {
TRT[v] = -1;
v++;
}
}
main {
fLED_Control();
fMisc();
fScript_Control();
}
combo Turbo {
fTurbo(100.0);
wait(ON);
fTurbo(0.0);
wait(OFF);
}
combo Press {
fPress(100.0);
}
combo Alt_Turbo {
fAlt_Turbo(100.0);
wait(ON);
fAlt_Turbo(0.0);
wait(OFF);
}
combo Quick_Scope {
set_val(QSS, 100);
wait(500);
set_val(QSS, 100);
set_val(QSF, 100);
wait(50);
set_val(QSF, 0);
set_val(QSS, 0);
}
void fLED_Control() {
if (LC1 || LC2 || LC3 == 1) {
LC = 1;
}
else {
LC = 0;
}
if (LC1 == 0 && led_get(LED_1, NULL) != 0.0) {
led_set(LED_1, 0.0, 0);
}
if (LC2 == 0 && led_get(LED_3, NULL) != 0.0) {
led_set(LED_3, 0.0, 0);
}
if (LC2 == 1 && led_get(LED_3, NULL) != 100.0) {
led_set(LED_3, 100.0, 0);
}
if (LC3 == 0 && led_get(LED_2, NULL) != 0.0) {
led_set(LED_2, 0.0, 0);
}
if (LC == 0 && led_get(LED_4, NULL) != 100.0) {
led_set(LED_4, 100.0, 0);
}
if (LC == 1 && led_get(LED_4, NULL) != 0.0) {
led_set(LED_4, 0.0, 0);
}
}
void fMisc() {
fRight_Stick(PS4_RX);
fRight_Stick(PS4_RY);
fLeft_Stick(PS4_LX);
fLeft_Stick(PS4_LY);
if (DT > 0) {
DT = DT - (uint16)elapsed_time();
}
if (TT > 0) {
TT = TT - (uint16)elapsed_time();
}
}
void fRight_Stick(int8 s) {
if (abs(get_val(s)) <= (fix32)DZ) {
set_val(s, 0.0);
}
sensitivity(s, 50, RS);
}
void fLeft_Stick(int8 s) {
if (abs(get_val(s)) <= (fix32)DZ) {
set_val(s, 0.0);
}
sensitivity(s, 50, LS);
}
void fScript_Control() {
if (event_active(PS4_TOUCH)) {
if (DT <= 0) {
DT = 500;
}
else if (DT > 0 && TT <= 0) {
TT = 250;
}
else if (TT > 0) {
if (CS == 0) {
CS = 1;
CP = 0;
}
else if (CS == 1) {
CS = 0;
}
}
}
if (CS == 1) {
fConfig_Script();
}
if (CS == 0) {
fRun_Script();
}
}
void fConfig_Script() {
fNull_Output();
fDebug();
if (CP == 0) {
if (event_active(PS4_UP)) {
CP = 1;
}
if (event_active(PS4_RIGHT)) {
CP = 2;
}
if (event_active(PS4_DOWN)) {
CP = 3;
}
}
if (CP != 0 ) {
if (event_active(PS4_OPTIONS)) {
CP = 0;
}
if (CP == 1) {
if (event_active(PS4_PS)) {
fDefault_Features();
}
else {
fSet_Feature1();
}
}
if (CP == 2) {
if (event_active(PS4_PS)) {
fDefault_Timing();
}
else {
fAdjust_Timing();
}
}
if (CP == 3) {
if (event_active(PS4_TOUCH)) {
fSet_Output();
}
if (event_active(PS4_PS)) {
fDefault_Sensitivity();
}
else {
fAdjust_Sensitivity();
}
}
}
}
void fRun_Script() {
fToggle1();
fMultifunction_Toggle1();
fAlternate();
combo_run(Press);
fQuick_Scope();
}
void fNull_Output() {
int8 o;
for(o = PS4_PS; o <= PS4_GYROZ; ++o) {
set_val(o, NULL);
}
}
void fDebug() {
set_val(TRACE_1, RS);
set_val(TRACE_2, LS);
set_val(TRACE_3, ON / 10);
set_val(TRACE_4, OFF / 10);
set_val(TRACE_5, QS);
set_val(TRACE_6, CP);
}
void fDefault_Features() {
F1 = 0;
F2 = 0;
F3 = 0;
F4 = 0;
F5 = 0;
F6 = 0;
F7 = 0;
F8 = 0;
F9 = 0;
F10 = 0;
F11 = 0;
F12 = 0;
F13 = 0;
F14 = 0;
QS = 0;
}
void fSet_Feature1() {
if (event_active(PS4_R1)) {
F1 = fSet_Feature2(PS4_R1, F1);
}
if (event_active(PS4_R2)) {
F2 = fSet_Feature2(PS4_R2, F2);
}
if (event_active(PS4_R3)) {
F3 = fSet_Feature2(PS4_R3, F3);
}
if (event_active(PS4_L1)) {
F4 = fSet_Feature2(PS4_L1, F4);
}
if (event_active(PS4_L2)) {
F5 = fSet_Feature2(PS4_L2, F5);
}
if (event_active(PS4_L3)) {
F6 = fSet_Feature2(PS4_L3, F6);
}
if (event_active(PS4_TRIANGLE)) {
F7 = fSet_Feature2(PS4_TRIANGLE, F7);
}
if (event_active(PS4_CIRCLE)) {
F8 = fSet_Feature2(PS4_CIRCLE, F8);
}
if (event_active(PS4_CROSS)) {
F9 = fSet_Feature2(PS4_CROSS, F9);
}
if (event_active(PS4_SQUARE)) {
F10 = fSet_Feature2(PS4_SQUARE, F10);
}
if (event_active(PS4_UP)) {
F11 = fSet_Feature2(PS4_UP, F11);
}
if (event_active(PS4_RIGHT)) {
F12 = fSet_Feature2(PS4_RIGHT, F12);
}
if (event_active(PS4_DOWN)) {
F13 = fSet_Feature2(PS4_DOWN, F13);
}
if (event_active(PS4_LEFT)) {
F14 = fSet_Feature2(PS4_LEFT, F14);
}
if (event_active(PS4_TOUCH)) {
if (QS < 3) {
QS = QS + 1;
}
else if (QS >= 3) {
QS = 0;
}
}
}
int16 fSet_Feature2(int8 b, int16 f) {
if (event_active(b)) {
if (DT <= 0) {
DT = 250;
}
else if (DT > 0) {
if (f < 7) {
f = f + 1;
}
else if (f >= 7) {
f = 0;
}
}
}
return(f);
}
void fDefault_Timing() {
ON = 40;
OFF = 40;
fReset();
}
void fReset() {
combo_stop(Turbo);
combo_stop(Alt_Turbo);
}
void fAdjust_Timing() {
if (event_active(PS4_R1) && ON < 2000) {
ON = ON + 10;
fReset();
}
if (event_active(PS4_L1) && ON > 10) {
ON = ON - 10;
fReset();
}
if (event_active(PS4_R2) && OFF < 2000) {
OFF = OFF + 10;
fReset();
}
if (event_active(PS4_L2) && OFF > 10) {
OFF = OFF - 10;
fReset();
}
}
void fDefault_Sensitivity() {
RS = 100;
LS = 100;
}
void fAdjust_Sensitivity() {
if (event_active(PS4_R1) && RS < 200) {
RS = RS + 10;
}
if (event_active(PS4_R2) && RS > 20) {
RS = RS - 10;
}
if (event_active(PS4_L1) && LS < 200) {
LS = LS + 10;
}
if (event_active(PS4_L2) && LS > 20) {
LS = LS - 10;
}
}
void fSet_Output() {
if (event_active(PS4_TOUCH)) {
if (IOP < 4) {
IOP = IOP + 1;
}
else if (IOP >= 4) {
IOP = 1;
}
}
if (IOP == 1) {
output_protocol(PIO_XB360);
}
if (IOP == 2) {
output_protocol(PIO_PS3);
}
if (IOP == 3) {
output_protocol(PIO_XB1);
}
if (IOP == 4) {
output_protocol(PIO_PS4);
}
}
void fToggle1() {
if (F1) {
F1 = fToggle2(PS4_R1, F1);
}
if (F2) {
F2 = fToggle2(PS4_R2, F2);
}
if (F3) {
F3 = fToggle2(PS4_R3, F3);
}
if (F4) {
F4 = fToggle2(PS4_L1, F4);
}
if (F5) {
F5 = fToggle2(PS4_L2, F5);
}
if (F6) {
F6 = fToggle2(PS4_L3, F6);
}
if (F7) {
F7 = fToggle2(PS4_TRIANGLE, F7);
}
if (F8) {
F8 = fToggle2(PS4_CIRCLE, F8);
}
if (F9) {
F9 = fToggle2(PS4_CROSS, F9);
}
if (F10) {
F10 = fToggle2(PS4_SQUARE, F10);
}
if (F11) {
F11 = fToggle2(PS4_UP, F11);
}
if (F12) {
F12 = fToggle2(PS4_RIGHT, F12);
}
if (F13) {
F13 = fToggle2(PS4_DOWN, F13);
}
if (F14) {
F14 = fToggle2(PS4_LEFT, F14);
}
}
int16 fToggle2(int8 b, int16 f) {
if (time_active(b) < PT && event_release(b)) {
if (f == 2) {
return(222);
}
if (f == 4) {
return(44);
}
if (f == 6) {
return(666);
}
}
if (time_active(b) < PT && event_release(b)) {
if (f == 222) {
return(2);
}
if (f == 33) {
return(3);
}
if (f == 333) {
return(3);
}
if (f == 44) {
return(4);
}
if (f == 55) {
return(5);
}
if (f == 555) {
return(5);
}
if (f == 666) {
return(6);
}
}
return(f);
}
void fMultifunction_Toggle1() {
if (F1 == 3 || F1 == 5) {
F1 = fMultifunction_Toggle2(PS4_R1, F1);
}
if (F2 == 3 || F2 == 5) {
F2 = fMultifunction_Toggle2(PS4_R2, F2);
}
if (F3 == 3 || F3 == 5) {
F3 = fMultifunction_Toggle2(PS4_R3, F3);
}
if (F4 == 3 || F4 == 5) {
F4 = fMultifunction_Toggle2(PS4_L1, F4);
}
if (F5 == 3 || F5 == 5) {
F5 = fMultifunction_Toggle2(PS4_L2, F5);
}
if (F6 == 3 || F6 == 5) {
F6 = fMultifunction_Toggle2(PS4_L3, F6);
}
if (F7 == 3 || F7 == 5) {
F7 = fMultifunction_Toggle2(PS4_TRIANGLE, F7);
}
if (F8 == 3 || F8 == 5) {
F8 = fMultifunction_Toggle2(PS4_CIRCLE, F8);
}
if (F9 == 3 || F9 == 5) {
F9 = fMultifunction_Toggle2(PS4_CROSS, F9);
}
if (F10 == 3 || F10 == 5) {
F10 = fMultifunction_Toggle2(PS4_SQUARE, F10);
}
if (F11 == 3 || F11 == 5) {
F11 = fMultifunction_Toggle2(PS4_UP, F11);
}
if (F12 == 3 || F12 == 5) {
F12 = fMultifunction_Toggle2(PS4_RIGHT, F12);
}
if (F13 == 3 || F13 == 5) {
F13 = fMultifunction_Toggle2(PS4_DOWN, F13);
}
if (F14 == 3 || F14 == 5) {
F14 = fMultifunction_Toggle2(PS4_LEFT, F14);
}
}
int16 fMultifunction_Toggle2(int8 b, int16 f) {
RV = fMultifunction_Toggle3(b);
if (RV == 1) {
if (f == 3) {
f = 33;
}
if (f == 5) {
f = 55;
}
}
else if (RV == 2) {
if (f == 3) {
f = 333;
}
if (f == 5) {
f = 555;
}
}
return(f);
}
int8 fMultifunction_Toggle3(int8 b) {
if (event_release(b)) {
if (time_active(b) <= PT) {
TRT[b] = 0;
}
else {
TRT[b] = -1;
}
}
else if (!get_val(b)) {
if (TRT[b] >= 0) {
TRT[b] = TRT[b] + elapsed_time();
if (TRT[b] > PT) {
TRT[b] = -1;
}
}
}
else if (get_val(b)) {
if (TRT[b] == -1) {
if (time_active(b) >= 1000) {
return(1);
}
}
else if (TRT[b] != -1) {
if (time_active(b) >= 250) {
return(2);
}
}
}
return(0);
}
void fAlternate() {
if (elapsed_time()) {
if (!(Turbo)) {
ALT = (ON + OFF) / 2;
combo_run(Turbo);
}
if (ALT) {
ALT = ALT - (int16)elapsed_time();
if (ALT <= 0) {
combo_run(Alt_Turbo);
}
}
}
}
void fTurbo(fix32 v) {
LC1 = 0;
if (F1 == 222 || F1 == 333 || F1 == 1 && get_val(PS4_R1)) {
set_val(PS4_R1, v);
LC1 = 1;
}
if (F2 == 222 || F2 == 333 || F2 == 1 && get_val(PS4_R2)) {
set_val(PS4_R2, v);
LC1 = 1;
}
if (F3 == 222 || F3 == 333 || F3 == 1 && get_val(PS4_R3)) {
set_val(PS4_R3, v);
LC1 = 1;
}
if (F4 == 222 || F4 == 333 || F4 == 1 && get_val(PS4_L1)) {
set_val(PS4_L1, v);
LC1 = 1;
}
if (F5 == 222 || F5 == 333 || F5 == 1 && get_val(PS4_L2)) {
set_val(PS4_L2, v);
LC1 = 1;
}
if (F6 == 222 || F6 == 333 || F6 == 1 && get_val(PS4_L3)) {
set_val(PS4_L3, v);
LC1 = 1;
}
if (F7 == 222 || F7 == 333 || F7 == 1 && get_val(PS4_TRIANGLE)) {
set_val(PS4_TRIANGLE, v);
LC1 = 1;
}
if (F8 == 222 || F8 == 333 || F8 == 1 && get_val(PS4_CIRCLE)) {
set_val(PS4_CIRCLE, v);
LC1 = 1;
}
if (F9 == 222 || F9 == 333 || F9 == 1 && get_val(PS4_CROSS)) {
set_val(PS4_CROSS, v);
LC1 = 1;
}
if (F10 == 222 || F10 == 333 || F10 == 1 && get_val(PS4_SQUARE)) {
set_val(PS4_SQUARE, v);
LC1 = 1;
}
if (F11 == 222 || F11 == 333 || F11 == 1 && get_val(PS4_UP)) {
set_val(PS4_UP, v);
LC1 = 1;
}
if (F12 == 222 || F12 == 333 || F12 == 1 && get_val(PS4_RIGHT)) {
set_val(PS4_RIGHT, v);
LC1 = 1;
}
if (F13 == 222 || F13 == 333 || F13 == 1 && get_val(PS4_DOWN)) {
set_val(PS4_DOWN, v);
LC1 = 1;
}
if (F14 == 222 || F14 == 333 || F14 == 1 && get_val(PS4_LEFT)) {
set_val(PS4_LEFT, v);
LC1 = 1;
}
if (LC1 == 1 && led_get(LED_1, NULL) != v) {
led_set(LED_1, v, 0);
}
}
void fPress(fix32 v) {
LC2 = 0;
if (F1 == 33 || F1 == 44 || F1 == 55) {
set_val(PS4_R1, v);
LC2 = 1;
}
if (F2 == 33 || F2 == 44 || F2 == 55) {
set_val(PS4_R2, v);
LC2 = 1;
}
if (F3 == 33 || F3 == 44 || F3 == 55) {
set_val(PS4_R3, v);
LC2 = 1;
}
if (F4 == 33 || F4 == 44 || F4 == 55) {
set_val(PS4_L1, v);
LC2 = 1;
}
if (F5 == 33 || F5 == 44 || F5 == 55) {
set_val(PS4_L2, v);
LC2 = 1;
}
if (F6 == 33 || F6 == 44 || F6 == 55) {
set_val(PS4_L3, v);
LC2 = 1;
}
if (F7 == 33 || F7 == 44 || F7 == 55) {
set_val(PS4_TRIANGLE, v);
LC2 = 1;
}
if (F8 == 33 || F8 == 44 || F8 == 55) {
set_val(PS4_CIRCLE, v);
LC2 = 1;
}
if (F9 == 33 || F9 == 44 || F9 == 55) {
set_val(PS4_CROSS, v);
LC2 = 1;
}
if (F10 == 33 || F10 == 44 || F10 == 55) {
set_val(PS4_SQUARE, v);
LC2 = 1;
}
if (F11 == 33 || F11 == 44 || F11 == 55) {
set_val(PS4_UP, v);
LC2 = 1;
}
if (F12 == 33 || F12 == 44 || F12 == 55) {
set_val(PS4_RIGHT, v);
LC2 = 1;
}
if (F13 == 33 || F13 == 44 || F13 == 55) {
set_val(PS4_DOWN, v);
LC2 = 1;
}
if (F14 == 33 || F14 == 44 || F14 == 55) {
set_val(PS4_LEFT, v);
LC2 = 1;
}
}
void fAlt_Turbo(fix32 v) {
LC3 = 0;
if (F1 == 555 || F1 == 666 || F1 == 7 && get_val(PS4_R1)) {
set_val(PS4_R1, v);
LC3 = 1;
}
if (F2 == 555 || F2 == 666 || F2 == 7 && get_val(PS4_R2)) {
set_val(PS4_R2, v);
LC3 = 1;
}
if (F3 == 555 || F3 == 666 || F3 == 7 && get_val(PS4_R3)) {
set_val(PS4_R3, v);
LC3 = 1;
}
if (F4 == 555 || F4 == 666 || F4 == 7 && get_val(PS4_L1)) {
set_val(PS4_L1, v);
LC3 = 1;
}
if (F5 == 555 || F5 == 666 || F5 == 7 && get_val(PS4_L2)) {
set_val(PS4_L2, v);
LC3 = 1;
}
if (F6 == 555 || F6 == 666 || F6 == 7 && get_val(PS4_L3)) {
set_val(PS4_L3, v);
LC3 = 1;
}
if (F7 == 555 || F7 == 666 || F7 == 7 && get_val(PS4_TRIANGLE)) {
set_val(PS4_TRIANGLE, v);
LC3 = 1;
}
if (F8 == 555 || F8 == 666 || F8 == 7 && get_val(PS4_CIRCLE)) {
set_val(PS4_CIRCLE, v);
LC3 = 1;
}
if (F9 == 555 || F9 == 666 || F9 == 7 && get_val(PS4_CROSS)) {
set_val(PS4_CROSS, v);
LC3 = 1;
}
if (F10 == 555 || F10 == 666 || F10 == 7 && get_val(PS4_SQUARE)) {
set_val(PS4_SQUARE, v);
LC3 = 1;
}
if (F11 == 555 || F11 == 666 || F11 == 7 && get_val(PS4_UP)) {
set_val(PS4_UP, v);
LC3 = 1;
}
if (F12 == 555 || F12 == 666 || F12 == 7 && get_val(PS4_RIGHT)) {
set_val(PS4_RIGHT, v);
LC3 = 1;
}
if (F13 == 555 || F13 == 666 || F13 == 7 && get_val(PS4_DOWN)) {
set_val(PS4_DOWN, v);
LC3 = 1;
}
if (F14 == 555 || F14 == 666 || F14 == 7 && get_val(PS4_LEFT)) {
set_val(PS4_LEFT, v);
LC3 = 1;
}
if (LC3 == 1 && led_get(LED_2, NULL) != v) {
led_set(LED_2, v, 0);
}
}
void fQuick_Scope() {
if (QS != 0 && time_active(QSS) < PT && event_release(QSS)) {
combo_run(Quick_Scope);
}
if (QS == 1) {
QSS = PS4_L2;
QSF = PS4_R2;
}
if (QS == 2) {
QSS = PS4_R3;
QSF = PS4_R2;
}
if (QS == 3) {
QSS = PS4_SQUARE;
QSF = PS4_R2;
}
}
void sensitivity(int16 id, int16 mid, int16 sen) {
int16 val = (int16)get_val(id);
if(mid != NOT_USE) {
int16 val_s = (val >= 0) ? 1 : -1;
val *= val_s;
if(val <= mid) {
val = (val * 50) / mid;
}
else {
val = ((50 * (val - mid)) / (100 - mid)) + 50;
}
val *= val_s;
}
if(sen != NOT_USE) {
val = (val * sen) / 100;
}
set_val(id, clamp(val, -100, 100));
return;
}
void output_protocol(int16 id) {
port_disconnect();
switch(id) {
case PIO_PS3:
port_connect(PORT_USB_C, PROTOCOL_PS3);
break;
case PIO_XB360:
port_connect(PORT_USB_C, PROTOCOL_XB360);
break;
case PIO_PS4:
port_connect(PORT_USB_C, PROTOCOL_PS4);
break;
case PIO_XB1:
port_connect(PORT_USB_C, PROTOCOL_XB1);
break;
case PIO_SWITCH:
port_connect(PORT_USB_C, PROTOCOL_SWITCH);
break;
}
return;
}
And here's my failed attempt at optimizing the script:
- Code: Select all
#pragma METAINFO("Testing Script", 0, 0, "Fenria")
#define NOT_USE 0
#define DZ_CIRCLE 101
#define PIO_NONE 0
#define PIO_AUTO 0
#define PIO_PS4 4
#define PIO_PS3 1
#define PIO_XB1 5
#define PIO_XB360 2
#define PIO_WII 3
#define PIO_HID 6
#define PIO_SWITCH 7
#define PS4_PS BUTTON_1
#define PS4_TOUCH BUTTON_2
#define PS4_OPTIONS BUTTON_3
#define PS4_R1 BUTTON_4
#define PS4_R2 BUTTON_5
#define PS4_R3 BUTTON_6
#define PS4_L1 BUTTON_7
#define PS4_L2 BUTTON_8
#define PS4_L3 BUTTON_9
#define PS4_UP BUTTON_10
#define PS4_DOWN BUTTON_11
#define PS4_LEFT BUTTON_12
#define PS4_RIGHT BUTTON_13
#define PS4_TRIANGLE BUTTON_14
#define PS4_CIRCLE BUTTON_15
#define PS4_CROSS BUTTON_16
#define PS4_SQUARE BUTTON_17
#define PS4_SHARE BUTTON_18
#define PS4_TOUCH1 BUTTON_19
#define PS4_TOUCH2 BUTTON_20
#define PS4_RX STICK_1_X
#define PS4_RY STICK_1_Y
#define PS4_LX STICK_2_X
#define PS4_LY STICK_2_Y
#define PS4_TOUCH1X POINT_1_X
#define PS4_TOUCH1Y POINT_1_Y
#define PS4_TOUCH2X POINT_2_X
#define PS4_TOUCH2Y POINT_2_Y
#define PS4_ACCX ACCEL_1_X
#define PS4_ACCY ACCEL_1_Y
#define PS4_ACCZ ACCEL_1_Z
#define PS4_GYROX GYRO_1_X
#define PS4_GYROY GYRO_1_Y
#define PS4_GYROZ GYRO_1_Z
#define TRACE_1 ACCEL_1_X
#define TRACE_2 ACCEL_1_Y
#define TRACE_3 ACCEL_1_Z
#define TRACE_4 ACCEL_2_X
#define TRACE_5 ACCEL_2_Y
#define TRACE_6 ACCEL_2_Z
int16 LC = 0;
int16 LC1 = 0;
int16 LC2 = 0;
int16 LC3 = 0;
int16 DZ = 10;
int16 RS = 100;
int16 LS = 100;
int16 DT = 0;
int16 TT = 0;
int16 CS = 0;
int16 CP = 0;
int16 IOP = 1;
int16 PT = 100;
int16 TRT[30];
int16 V;
int16 RV;
int16 ON = 40;
int16 OFF = 40;
int16 BTN;
int16 BF[21]; // <-- Supposed to replace
int16 AF[21]; // <-- the F# Flags.
int16 QS = 0;
int16 QSS = PS4_L2;
int16 QSF = PS4_R2;
init {
while(V < 30) {
TRT[V] = -1;
V++;
}
}
main {
fLED_Control();
fMisc();
fScript_Control();
}
combo Quick_Scope {
set_val(QSS, 100);
wait(500);
set_val(QSS, 100);
set_val(QSF, 100);
wait(50);
set_val(QSF, 0);
set_val(QSS, 0);
}
void fLED_Control() {
for (BTN = PS4_R1; BTN <= PS4_SQUARE; ++BTN) {
if (1 || 2 || 3 == AF[BTN]) {
LC = 1;
if (AF[BTN] == 1) {
LC1 = 1;
}
else {
LC1 = 0;
}
if (AF[BTN] == 2) {
LC2 = 1;
}
else {
LC2 = 0;
}
if (AF[BTN] == 3) {
LC3 = 1;
}
else {
LC3 = 0;
}
}
else {
LC = 0;
}
}
if (LC == 0) {
if (led_get(LED_1, NULL) != 0.0) {
led_set(LED_1, 0.0, 0);
}
if (led_get(LED_2, NULL) != 0.0) {
led_set(LED_2, 0.0, 0);
}
if (led_get(LED_3, NULL) != 0.0) {
led_set(LED_3, 0.0, 0);
}
if (led_get(LED_4, NULL) != 100.0) {
led_set(LED_4, 100.0, 0);
}
}
if (LC == 1) {
if (led_get(LED_4, NULL) != 0.0) {
led_set(LED_4, 0.0, 0);
}
if (LC1 == 0 && led_get(LED_1, NULL) != 0.0) {
led_set(LED_1, 0.0, 0);
}
if (LC2 == 0 && led_get(LED_3, NULL) != 0.0) {
led_set(LED_3, 0.0, 0);
}
if (LC3 == 0 && led_get(LED_2, NULL) != 0.0) {
led_set(LED_2, 0.0, 0);
}
}
}
void fMisc() {
fRight_Stick(PS4_RX);
fRight_Stick(PS4_RY);
fLeft_Stick(PS4_LX);
fLeft_Stick(PS4_LY);
if (DT > 0) {
DT = DT - (uint16)elapsed_time();
}
if (TT > 0) {
TT = TT - (uint16)elapsed_time();
}
}
void fRight_Stick(int8 ST) {
if (abs(get_val(ST)) <= (fix32)DZ) {
set_val(ST, 0.0);
}
sensitivity(ST, 50, RS);
}
void fLeft_Stick(int8 ST) {
if (abs(get_val(ST)) <= (fix32)DZ) {
set_val(ST, 0.0);
}
sensitivity(ST, 50, LS);
}
void fScript_Control() {
if (event_active(PS4_TOUCH)) {
if (DT <= 0) {
DT = 500;
}
else if (DT > 0 && TT <= 0) {
TT = 250;
}
else if (TT > 0) {
if (CS == 0) {
CS = 1;
CP = 0;
}
else if (CS == 1) {
CS = 0;
}
}
}
if (CS == 1) {
fConfig_Script();
}
if (CS == 0) {
fRun_Script();
}
}
void fConfig_Script() {
fNull_Output();
fDebug();
if (CP == 0) {
if (event_active(PS4_UP)) {
CP = 1;
}
if (event_active(PS4_RIGHT)) {
CP = 2;
}
if (event_active(PS4_DOWN)) {
CP = 3;
}
}
if (CP != 0 ) {
if (event_active(PS4_OPTIONS)) {
CP = 0;
}
if (CP == 1) {
if (event_active(PS4_PS)) {
fDefault_Features();
}
else {
fSet_Feature();
}
}
if (CP == 2) {
if (event_active(PS4_PS)) {
fDefault_Timing();
}
else {
fAdjust_Timing();
}
}
if (CP == 3) {
if (event_active(PS4_TOUCH)) {
fSet_Output();
}
if (event_active(PS4_PS)) {
fDefault_Sensitivity();
}
else {
fAdjust_Sensitivity();
}
}
}
}
void fRun_Script() {
for (BTN = PS4_R1; BTN <= PS4_SQUARE; ++BTN) {
fToggle(BTN);
fTurbo(BTN);
fPress(BTN);
fMultifunction_Toggle1(BTN);
}
fTurbo_Timer();
fQuick_Scope();
}
void fNull_Output() {
int8 OUT;
for(OUT = PS4_PS; OUT <= PS4_GYROZ; ++OUT) {
set_val(OUT, NULL);
}
}
void fDebug() {
for (BTN = PS4_R1; BTN <= PS4_SQUARE; ++BTN) { // <-- This bit here is new,
set_val(BTN, (BF[BTN] * 10) + AF[BTN]); // <-- and is supposed to allow for a real-time view of what features
} // <-- are set and active or not on each button.
set_val(TRACE_1, RS);
set_val(TRACE_2, LS);
set_val(TRACE_3, ON / 10);
set_val(TRACE_4, OFF / 10);
set_val(TRACE_5, QS);
set_val(TRACE_6, CP);
}
void fDefault_Features(int16 BTN) { // <-- Functionality should be unchanged, though I can't test it.
for (BTN = PS4_R1; BTN <= PS4_SQUARE; ++BTN) {
BF[BTN] = 0;
AF[BTN] = 0;
}
QS = 0;
}
void fSet_Feature(int16 BTN) { // <-- Attempted to combine fSet_Feature2 and fSet_Feature2.
for (BTN = PS4_R1; BTN <= PS4_SQUARE; ++BTN) {
if (event_active(BTN)) {
if (DT <= 0) {
DT = 250;
}
else if (DT > 0) {
if (BF[BTN] < 7) {
BF[BTN] += 1;
}
else if (BF[BTN] >= 7) {
BF[BTN] = 0;
}
}
}
}
if (event_active(PS4_TOUCH)) {
if (QS < 3) {
QS += 1;
}
else if (QS >= 3) {
QS = 0;
}
}
}
void fDefault_Timing() {
ON = 40;
OFF = 40;
}
void fAdjust_Timing() {
if (event_active(PS4_R1) && ON < 2000) {
ON += 10;
}
if (event_active(PS4_L1) && ON > 10) {
ON -= 10;
}
if (event_active(PS4_R2) && OFF < 2000) {
OFF += 10;
}
if (event_active(PS4_L2) && OFF > 10) {
OFF -= 10;
}
}
void fDefault_Sensitivity() {
RS = 100;
LS = 100;
}
void fAdjust_Sensitivity() {
if (event_active(PS4_R1) && RS < 200) {
RS += 10;
}
if (event_active(PS4_R2) && RS > 20) {
RS -= 10;
}
if (event_active(PS4_L1) && LS < 200) {
LS += 10;
}
if (event_active(PS4_L2) && LS > 20) {
LS -= 10;
}
}
void fSet_Output() {
if (event_active(PS4_TOUCH)) {
if (IOP < 4) {
IOP += 1;
}
else if (IOP >= 4) {
IOP = 1;
}
}
if (IOP == 1) {
output_protocol(PIO_XB360);
}
if (IOP == 2) {
output_protocol(PIO_PS3);
}
if (IOP == 3) {
output_protocol(PIO_XB1);
}
if (IOP == 4) {
output_protocol(PIO_PS4);
}
}
void fToggle(int16 BTN) { // <-- Removed fToggle1 as it was no longer needed.
if (time_active(BTN) < PT && event_release(BTN)) {
if (AF[BTN] == 0 && BF[BTN] == 2) {
AF[BTN] = 1;
}
if (AF[BTN] == 0 && BF[BTN] == 4) {
AF[BTN] = 2;
}
if (AF[BTN] == 0 && BF[BTN] == 6) {
AF[BTN] = 3;
}
}
if (time_active(BTN) < PT && event_release(BTN)) {
if (AF[BTN] != 0) {
AF[BTN] = 0;
}
}
}
void fMultifunction_Toggle1(int16 BTN) {
if (BF[BTN] == 3 || BF[BTN] == 5) {
fMultifunction_Toggle2(BTN);
}
}
void fMultifunction_Toggle2(int16 BTN) {
RV = fMultifunction_Toggle3(BTN);
if (RV == 1) {
AF[BTN] = 2;
}
else if (RV == 2) {
if (AF[BTN] == 0) {
if (BF[BTN] == 3) {
AF[BTN] = 1;
}
else {
AF[BTN] = 3;
}
}
}
}
int8 fMultifunction_Toggle3(int16 BTN) {
if (event_release(BTN)) {
if (time_active(BTN) <= PT) {
TRT[BTN] = 0;
}
else {
TRT[BTN] = -1;
}
}
else if (!get_val(BTN)) {
if (TRT[BTN] >= 0) {
TRT[BTN] = TRT[BTN] + elapsed_time();
if (TRT[BTN] > PT) {
TRT[BTN] = -1;
}
}
}
else if (get_val(BTN)) {
if (TRT[BTN] == -1) {
if (time_active(BTN) >= 1000) {
return(1);
}
}
else if (TRT[BTN] != -1) {
if (time_active(BTN) >= 250) {
return(2);
}
}
}
return(0);
}
void fTurbo(int16 BTN) {
if (BF[BTN] == 1) {
if (get_val(BTN)) {
AF[BTN] = 1;
}
else {
AF[BTN] = 0;
}
}
if (BF[BTN] == 7) {
if (get_val(BTN)) {
AF[BTN] = 3;
}
else {
AF[BTN] = 0;
}
}
}
void fPress(int16 BTN) { // <-- Removed the Press combo and put it's functionality here.
AF[2] = 100;
if (LC2 == 1 && led_get(LED_3, NULL) != 100.0) {
led_set(LED_3, 100.0, 0);
}
}
void fTurbo_Timer() { // <-- This replaces the turbo and alt_turbo combos, as well as the fAlternate function.
uint32 ST = system_time();
static uint32 TT = ST;
static uint32 ATT = 0;
if (ST > TT && TT != 0) {
if (ATT == 0) {
ATT = TT + (ON + OFF) / 2;
}
if (ST > TT && ST <= TT + ON) {
AF[1] = 100;
if (LC1 == 1 && led_get(LED_1, NULL) != 100.0) {
led_set(LED_1, 100.0, 0);
}
}
else if (ST > TT + ON && ST < TT + ON + OFF) {
AF[1] = 0;
if (LC1 == 1 && led_get(LED_1, NULL) != 0.0) {
led_set(LED_1, 0.0, 0);
}
}
else if (ST >= TT + ON + OFF) TT = ST;
}
if (ST > ATT && ATT != 0) {
if (ST > ATT && ST <= ATT + ON) {
AF[3] = 100;
if (LC3 == 1 && led_get(LED_2, NULL) != 100.0) {
led_set(LED_2, 100.0, 0);
}
}
else if (ST > ATT + ON && ST < ATT + ON + OFF){
AF[3] = 0;
if (LC3 == 1 && led_get(LED_2, NULL) != 0.0) {
led_set(LED_2, 0.0, 0);
}
}
else if (ST >= ATT + ON + OFF) {
ATT = 0;
}
}
}
void fQuick_Scope() {
if (QS != 0 && time_active(QSS) < PT && event_release(QSS)) {
combo_run(Quick_Scope);
}
if (QS == 1) {
QSS = PS4_L2;
QSF = PS4_R2;
}
if (QS == 2) {
QSS = PS4_R3;
QSF = PS4_R2;
}
if (QS == 3) {
QSS = PS4_SQUARE;
QSF = PS4_R2;
}
}
void sensitivity(int16 id, int16 mid, int16 sen) {
int16 val = (int16)get_val(id);
if(mid != NOT_USE) {
int16 val_s = (val >= 0) ? 1 : -1;
val *= val_s;
if(val <= mid) {
val = (val * 50) / mid;
}
else {
val = ((50 * (val - mid)) / (100 - mid)) + 50;
}
val *= val_s;
}
if(sen != NOT_USE) {
val = (val * sen) / 100;
}
set_val(id, clamp(val, -100, 100));
return;
}
void output_protocol(int16 id) {
port_disconnect();
switch(id) {
case PIO_PS3:
port_connect(PORT_USB_C, PROTOCOL_PS3);
break;
case PIO_XB360:
port_connect(PORT_USB_C, PROTOCOL_XB360);
break;
case PIO_PS4:
port_connect(PORT_USB_C, PROTOCOL_PS4);
break;
case PIO_XB1:
port_connect(PORT_USB_C, PROTOCOL_XB1);
break;
case PIO_SWITCH:
port_connect(PORT_USB_C, PROTOCOL_SWITCH);
break;
}
return;
}
Everything being said and done though, I would really appreciate any help I can get, as I need to reduce the CPU usage as much as possible, while keeping as much functionality as I can.
Last edited by Fenria on Wed Feb 06, 2019 2:40 am, edited 1 time in total.
-
Fenria - Command Sergeant Major
- Posts: 147
- Joined: Thu May 21, 2015 5:56 pm
Re: Need help optimizing a script
(changed plans, delayed by one week )
Some script changes:
Some script changes:
- Entering and leaving the config mode required 3x tapping of the touchclick button.
- In SensCfg mode (dpad-down) you have to press the share button to cycle through the output protocols
- Turbo and hold button mode selection in config mode requires the same share+button or button+share combo like the abth script does. turbo/hold selection outside of the config mode is not enabled at the moment but can be done easily.
- Quick scope off/button layout cycle is with hold of touchclick >500ms
- LED feedback only as the abth script has as I have no clue how your script is handling the led, I am to dumb to understand your LED code.
Maybe you can explain it in a few sentences or long text/drawing how/when/what led should blink/shine/change and the colors to use.
- Code: Select all
// 3x fast press of touchclick to enter and leave config mode
// DPAD-UP: button specific turbo&alternate&hold selection
// (short <= 250ms)
// Share hold + short button press = select turbo feature
// Share hold + longer button press = select alternate turbo feature
// button hold + short Share press = (FNORMAL) feature : no features selected
// button hold + longer Share press = FHOLD feature
// touchclick hold > 500ms for cycling through quickscope settings
// DPAD-RIGHT: turbo timing adjustment and turbo mode toggle (auto press or on hold via Share double tap)
// DPAD-DOWN: sensitivity adjustment
//
#include "ColorLED.gph"
// led blink colors for actions
#define LEDFN CW // white on FNORMAL / Non special mode
#define LEDFH CB // blue on FHOLD
#define LEDFT CR // red on FTURBON (normal)
#define LEDFA CG // green on FTURBOA (alternate)
#define LEDCTH CA // amber on Turbos set to active on button hold
#define LEDCTA CP // pink on Turbos set to full Auto Turbos with enable/disable tap
// ++ BUTTON SPECIFIC FEATURES
//
// feature/mode identifier, for each button
#define FNORMAL 0 // no special function
#define FHOLD 1 // short tap = hold button down
#define FTURBOHN 2 // hold = normal turbo
#define FTURBOHA 3 // hold = alt turbo
#define FTURBOTN 4 // short tap = normal turbo
#define FTURBOTA 5 // short tap = alt turbo
// toggle state ids
#define TOff 0 // toggled off
#define TOn 1 // toggled on
uint8 BtnsFeature[21];// button feature flags (FNORMAL, FHOLD, ...)
uint8 BtnsToggle[21]; // button feature toggle state (enabled/disabled)
uint8 BtnTurboVal[2]; // button value to set for 0:turbo, 1:alt turbo, set by function fTurboTimer
#define TurboIdxN 0 // normal turbo is at this index in BtnTurboVal
#define TurboIdxA 1 // alternate turbo is at this index in BtnTurboVal
int16 TPress =60; // turbo button press time
int16 TRelease=60; // turbo button release time
bool bTurboHold=TRUE; // if yes the button must be help down for the turbo press
// ++ GENERAL FEATURES - non button specific
//
// ######## Quick Scope
bool bQScope =0; // quick scope state flag
uint32 QSTimeLimit=100; // timelimit of press time for starting quickscope combo
// scope , fire
uint8 QSBtns[8] ={BUTTON_8 ,BUTTON_5, // L2 & R2 default
BUTTON_6 ,BUTTON_5, // R3 & R2
BUTTON_17,BUTTON_5, // Square & R2
200 ,200 ,
};
uint8 QSCfg =6; // button pair to use 0,2,4
// ######## Stick Sensitivity and Deadzone
fix32 DeadZone=10.0;
int16 RSensitivity=100; // right stick sensitivity
int16 LSensitivity=100; // left stick sensitivity
// ######## Output Protocol
#define PIO_NONE 0
#define PIO_AUTO 0
#define PIO_PS4 4
#define PIO_PS3 1
#define PIO_XB1 5
#define PIO_XB360 2
#define PIO_WII 3
#define PIO_HID 6
#define PIO_SWITCH 7
// ++ Configuration Mode
bool bConfigure=FALSE;
uint8 CfgMode =0;
#define CFGNONE 0
#define CFGBTN 1
#define CFGTURBO 2
#define CFGSENS 3
init {
// for testing
/*
BtnsF[BUTTON_10]=FTURBOHN;
BtnsT[BUTTON_10]=TRUE;
BtnsF[BUTTON_11]=FTURBOHA;
BtnsT[BUTTON_11]=TRUE;
*/
}
main {
// handling configuration logic
fConfiguration();
// configure
if (bConfigure) { // ## for stuff active during configuration only
fNullOutput(); // set everything to zero during configuration mode
fDebug(); // debug output via set_val
} else { // ## for stuff when not in configuration mode
fButtons(); // buttons specific feature toggle and usage: hold, turbo hold, turbo toggle
} // eo : !bConfigure
// ## always active stuff
fTurboTimer(); // turbo and alternate turbo time tracker, sets values of array BtnTurboVal
// quick scope
fQuickScope();
// stick deadzone and sensitivity
fSticks();
}
void fConfiguration() {
static bool TClicked=FALSE; // for trigger quickscope button layout cycle with longer button hold
static uint8 clicks=0;
if (event_active(BUTTON_2) && time_release(BUTTON_2) < 200) clicks++;
if (time_release(BUTTON_2) > 500) clicks=0;
if ( clicks==2 ) {
printf("tripple click, toggle bConfigure");
clicks=0;
bConfigure=!bConfigure;
}
if (bConfigure) { // ## entered configuration
if (CfgMode==CFGNONE) { // no sub entry selected
if (event_active(BUTTON_10)) { CfgMode=CFGBTN; printf("CFGBTN"); }
if (event_active(BUTTON_13)) { CfgMode=CFGTURBO; printf("CFGTURBO"); }
if (event_active(BUTTON_11)) { CfgMode=CFGSENS; printf("CFGSENS"); }
} // eo: CfgMode==CFGNONE
else { // ## in sub entry
if (event_active(BUTTON_3)) CfgMode=CFGNONE; // back to main entry
switch(CfgMode) { // sub entry specific
case CFGBTN:
if (event_active(BUTTON_1)) fBtnDefault(); // PS button
else {
fButtons(TRUE); // TRUE for feature selection
if (event_release(BUTTON_2)) TClicked=FALSE;
if (check_active(BUTTON_2,500) && !TClicked) {
TClicked=TRUE;
QSCfg=(QSCfg+2)%8;
if (QSCfg>=6) bQScope=FALSE; else bQScope=TRUE;
if (bQScope) printf("Flag: %d, Button layout idx: %d, scope: Btn_%d, fire: Btn_%d",bQScope,QSCfg,QSBtns[QSCfg]+1,QSBtns[QSCfg+1]+1);
else printf("Flag: %d (disabled)",bQScope);
}
}
break;
case CFGTURBO:
if (event_active(BUTTON_1)) fTurboDefault(); // PS button
else fTurboAdjust();
break;
case CFGSENS:
if (event_active(BUTTON_18)) fOutput(); // Share : change output protocol
if (event_active(BUTTON_1)) fSensitivityDefault(); // PS button
else fSensitivityAdjust();
break;
}
}
} // eo: bConfigure
}
void fButtons(bool cfg) {
uint8 btn;
for (btn=3;btn<18;btn++) { // buttons PS4: R1..Square
if (cfg) fBtnSelect(btn); // select buttons feature
else fBtnUsage(btn); // use buttons feature
}
}
// clear all button features assignments
void fBtnDefault() {
uint8 btn;
for (btn=3;btn<18;btn++) { // buttons PS4: R1..Square
BtnsFeature[btn]=FNORMAL;
}
}
// select feature to use a button
void fBtnSelect(uint8 btn) {
// FNORMAL and FHOLD selection , Share TAP, button hold
if (get_actual(btn)) {
if (event_release(BUTTON_18)) { // on release of Share set feature
BtnsToggle[btn]=TOff; // toggle state reset
if (time_active(BUTTON_18) <= 250) { // short press
BtnsFeature[btn]=FNORMAL; // feature flag
ColorLED(LEDFN,200,200,2);
printf("Cleared feature for BUTTON_%d selected",btn+1);
} else { // longer press
BtnsFeature[btn]=FHOLD; // feature flag
ColorLED(LEDFH,200,200,2);
printf("HOLD feature for BUTTON_%d selected",btn+1);
}
}
} // eo: get_actual
// FTURBO and FTURBOA selection , Share HOLD, button tap
if (get_actual(BUTTON_18)) { // Share is active : select feature
if (event_release(btn)) {
BtnsToggle[btn]=TOff; // toggle state
if (time_active(btn) <= 250) { // short press
if (bTurboHold) BtnsFeature[btn]=FTURBOHN; // Turbo hold
else BtnsFeature[btn]=FTURBOTN; // Turbo toggle
ColorLED(LEDFT,200,200,2);
printf("FTurbo feature for BUTTON_%d selected",btn+1);
} else { // longer press
// alternate turbo
if (bTurboHold) BtnsFeature[btn]=FTURBOHA; // TurboA hold
else BtnsFeature[btn]=FTURBOTA; // TurboA toggle
ColorLED(LEDFA,200,200,2);
printf("FTurboA feature for BUTTON_%d selected",btn+1);
}
}
} // eo: Share hold
}
void fBtnUsage(uint8 btn) {
// Button feature ON/OFF toggle with short tap
if (event_release(btn) && time_active(btn) <=250) {
BtnsToggle[btn]=!BtnsToggle[btn];
}
// feature has to be enabled with a short tap first
if (BtnsToggle[btn]==TOn) {
if (BtnsFeature[btn]==FHOLD) { set_val(btn,100); }
if (BtnsFeature[btn]==FTURBOTN) { set_val(btn,BtnTurboVal[TurboIdxN]); }
if (BtnsFeature[btn]==FTURBOTA) { set_val(btn,BtnTurboVal[TurboIdxA]); }
}
// doesn't need to be enabled, is always on
if (BtnsFeature[btn]==FTURBOHN && get_actual(btn)) { set_val(btn,BtnTurboVal[TurboIdxN]); }
if (BtnsFeature[btn]==FTURBOHA && get_actual(btn)) { set_val(btn,BtnTurboVal[TurboIdxA]); }
}
// changes turbos to full auto with on/off toggle tap like the Hold feature
void fTurboModeSet(bool hold) {
uint8 btn;
if (hold) {
for (btn=3;btn<18;btn++) {
if (BtnsFeature[btn]==FTURBOTN) BtnsFeature[btn]=FTURBOHN;
if (BtnsFeature[btn]==FTURBOTA) BtnsFeature[btn]=FTURBOHA;
BtnsToggle[btn]=TOff;
}
printf("Turbos are active when held down");
ColorLED(LEDCTH,200,200,2);
} else {
for (btn=3;btn<18;btn++) {
if (BtnsFeature[btn]==FTURBOHN) BtnsFeature[btn]=FTURBOTN;
if (BtnsFeature[btn]==FTURBOHA) BtnsFeature[btn]=FTURBOTA;
BtnsToggle[btn]=TOff;
}
printf("Turbos are active by On/Off toggle tap");
ColorLED(LEDCTA,200,200,2);
}
}
void fTurboAdjust() {
// R1: TPress+10
if (event_active(BUTTON_4) && TPress < 2000) { TPress += 10; }
// L1: TPress-10
if (event_active(BUTTON_7) && TPress > 20 ) { TPress -= 10; }
// R2: TRelease+10
if (event_active(BUTTON_5) && TRelease < 2000) { TRelease += 10; }
// L2: TRelease-10
if (event_active(BUTTON_8) && TRelease > 20 ) { TRelease -= 10;}
// Share double tap for turbo toggle mode / hold mode change
if (event_active(BUTTON_18) && time_release(BUTTON_18)<250) {
bTurboHold=!bTurboHold;
fTurboModeSet(bTurboHold);
}
}
void fTurboDefault() {
TPress = 40;
TRelease = 40;
//fReset();
}
void fTurboTimer() {
uint32 syst = system_time();
static uint32 TurboTimer=system_time();
static uint32 AltTurboTimer=0;
if ( syst > TurboTimer && TurboTimer != 0 ) { // TURBO
if (AltTurboTimer == 0 ) AltTurboTimer=TurboTimer + TPress + TRelease/2 - TPress/2;
if ( syst > TurboTimer && syst <=TurboTimer+TPress ) BtnTurboVal[TurboIdxN]=100;
else if ( syst > TurboTimer+TPress && syst < TurboTimer+TPress+TRelease) BtnTurboVal[TurboIdxN]=0;
else if ( syst >= TurboTimer+TPress+TRelease) TurboTimer = syst;
}
if ( syst > AltTurboTimer && AltTurboTimer != 0 ) { // TURBO ALTERNATE
if ( syst > AltTurboTimer && syst <=AltTurboTimer+TPress ) BtnTurboVal[TurboIdxA]=100;
else if ( syst > AltTurboTimer+TPress && syst < AltTurboTimer+TPress+TRelease) BtnTurboVal[TurboIdxA]=0;
else if ( syst >= AltTurboTimer+TPress+TRelease) AltTurboTimer = 0;
}
// debug: turbo timing output
/*
static uint8 last=0;
static uint32 tlast=0;
static uint32 tdiff=0;
static uint32 tdlst=0;
if ( last != BtnTurboVal[0] ) {
tdiff=syst-tlast;
last=BtnTurboVal[0];
printf("now %d, time: %d, tlast %d, tdiff: %d",last,syst,tlast,tdiff);
tlast=syst;
}
*/
}
void fQuickScope() {
if (bQScope && time_active(QSBtns[QSCfg]) < QSTimeLimit && event_release(QSBtns[QSCfg])) {
combo_run(cQuickScope);
}
}
combo cQuickScope {
set_val(QSBtns[QSCfg], 100);
wait(500);
set_val(QSBtns[QSCfg], 100);
set_val(QSBtns[QSCfg+1], 100);
wait(50);
set_val(QSBtns[QSCfg], 0);
set_val(QSBtns[QSCfg+1], 0);
}
void fSensitivityAdjust() {
// R1: TPress+10
if (event_active(BUTTON_4) && RSensitivity < 200) { RSensitivity += 10; }
// L1: TPress-10
if (event_active(BUTTON_7) && LSensitivity < 200) { LSensitivity += 10; }
// R2: TRelease+10
if (event_active(BUTTON_5) && RSensitivity > 20) { RSensitivity -= 10; }
// L2: TRelease-10
if (event_active(BUTTON_8) && LSensitivity > 20) { LSensitivity -= 10; }
}
void fSensitivityDefault() {
RSensitivity = 100;
LSensitivity = 100;
}
void fSticks() {
fDZSens(STICK_1_X,DeadZone,RSensitivity);
fDZSens(STICK_1_Y,DeadZone,RSensitivity);
fDZSens(STICK_2_X,DeadZone,LSensitivity);
fDZSens(STICK_2_Y,DeadZone,LSensitivity);
}
void fDZSens(uint8 axis, fix32 dz,int16 sens) {
if (abs(get_val(axis)) <= dz) {
set_val(axis, 0.0);
}
fSensitivity(axis, 50, sens);
}
void fSensitivity(uint8 id, int16 mid, int16 sen) {
int16 val = (int16)get_val(id);
if(mid != 0) {
int16 val_s = (val >= 0) ? 1 : -1;
val *= val_s;
if(val <= mid) {
val = (val * 50) / mid;
}
else {
val = ((50 * (val - mid)) / (100 - mid)) + 50;
}
val *= val_s;
}
if(sen != 0) {
val = (val * sen) / 100;
}
set_val(id, clamp(val, -100, 100));
return;
}
void fOutput() {
static uint8 IOP=4;
printf("output");
IOP=(IOP+1)%4;
if (IOP == 0) {
fOutputProtocol(PIO_XB360);
}
if (IOP == 1) {
fOutputProtocol(PIO_PS3);
}
if (IOP == 2) {
fOutputProtocol(PIO_XB1);
}
if (IOP == 3) {
fOutputProtocol(PIO_PS4);
}
}
void fOutputProtocol(uint8 id) {
port_disconnect();
switch(id) {
case PIO_PS3:
port_connect(PORT_USB_C, PROTOCOL_PS3);
printf("PROTOCOL_PS3");
break;
case PIO_XB360:
port_connect(PORT_USB_C, PROTOCOL_XB360);
printf("PROTOCOL_XB360");
break;
case PIO_PS4:
port_connect(PORT_USB_C, PROTOCOL_PS4);
printf("PROTOCOL_PS4");
break;
case PIO_XB1:
port_connect(PORT_USB_C, PROTOCOL_XB1);
printf("PROTOCOL_XB1");
break;
case PIO_SWITCH:
port_connect(PORT_USB_C, PROTOCOL_SWITCH);
printf("PROTOCOL_SWITCH");
break;
}
return;
}
void fNullOutput() {
int8 o;
for(o = BUTTON_1; o <= GYRO_1_Z; ++o) {
set_val(o, NULL);
}
}
void fDebug() {
set_val(ACCEL_1_Z, TPress / 10);
set_val(ACCEL_2_X, TRelease / 10);
set_val(ACCEL_2_Z, CfgMode);
}
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: Need help optimizing a script
I'll have to look through the code and test it when I get up in in the morning, but for now I'll do my best to explain how the LEDs work.
By default, the LED is pink/magenta, but when Turbo is actively running on a button, the pink LED turns off, and the blue LED flashes, using the same on/press and off/release timings that the turbo function uses as a visual indicator of both turbo being active and the speed of the turbo.
Alternate turbo does the same using the red LED, and the green LED is used to show if the hold function is active or not, though in this case it is steady, and does not flash.
The red, blue, and green LED indicators can all be active at the same time, though they require that their corresponding function is actively in use, but the pink/magenta LED can only be on when no function is actively in use.
Thanks for your help so far Scachi.
I really appreciate it.
By default, the LED is pink/magenta, but when Turbo is actively running on a button, the pink LED turns off, and the blue LED flashes, using the same on/press and off/release timings that the turbo function uses as a visual indicator of both turbo being active and the speed of the turbo.
Alternate turbo does the same using the red LED, and the green LED is used to show if the hold function is active or not, though in this case it is steady, and does not flash.
The red, blue, and green LED indicators can all be active at the same time, though they require that their corresponding function is actively in use, but the pink/magenta LED can only be on when no function is actively in use.
Thanks for your help so far Scachi.
I really appreciate it.
-
Fenria - Command Sergeant Major
- Posts: 147
- Joined: Thu May 21, 2015 5:56 pm
Re: Need help optimizing a script
The constant use of (while) will jack up the use of CPU pass the 100% mark!
- bonefisher
- Lieutenant General
- Posts: 5413
- Joined: Thu Jan 29, 2015 10:49 am
Re: Need help optimizing a script
led added for turbo/toggle/hold/idle state feedback
- Code: Select all
// changelog:
// added event get_actual and flag checking in for button loop to reduce cpu usage
// added color notifications during run
// changed defined CFGmode names
//
// 3x fast press of touchclick to enter and leave config mode
// DPAD-UP: button specific turbo&alternate&hold selection
// (short <= 250ms)
// Share hold + short button press = select turbo feature
// Share hold + longer button press = select alternate turbo feature
// button hold + short Share press = (FNORMAL) feature : no features selected
// button hold + longer Share press = FHOLD feature
// touchclick hold > 500ms for cycling through quickscope settings
// DPAD-RIGHT: turbo timing adjustment and turbo mode hold/tap toggle (via double tap Share button)
// DPAD-DOWN: sensitivity adjustment
//
#define INFO // output printf for script toggle actions
#define INFOLED // output printf for led changes on button feature usage
#include "ColorLED.gph"
// led blink colors for toggles / setting changes / activity
// led info : viewtopic.php?f=26&t=11679#p79464
#define LEDCFGE CP // pink cfgmode entered +blink 3x
#define LEDCFGL CP // pink cfgmode left +blink 3x
#define LEDFN CP // pink on FNORMAL / Non special mode +static (idle)
#define LEDFH CG // green on FHOLD +static on active
#define LEDFT CB // blue on FTURBON (normal) +blink on active
#define LEDFA CR // red on FTURBOA (alternate) +blink on active
#define LEDCTH CA // amber on Turbos set to active on button hold +blink on toggle
#define LEDCTA CW // white on Turbos set to full Auto Turbos with enable/disable tap +blink on toggle
// ++ BUTTON SPECIFIC FEATURES
//
// feature/mode identifier, for each button
#define FNORMAL 0 // no special function
#define FHOLD 1 // short tap = hold button down
#define FTURBOHN 2 // hold = normal turbo
#define FTURBOHA 3 // hold = alt turbo
#define FTURBOTN 4 // short tap = normal turbo
#define FTURBOTA 5 // short tap = alt turbo
// toggle state ids
#define TOff 0 // toggled off
#define TOn 1 // toggled on
uint8 BtnsFeature[21];// button feature flags (FNORMAL, FHOLD, ...)
uint8 BtnsToggle[21]; // button feature toggle state (enabled/disabled)
uint8 BtnTurboVal[2]; // button value to set for 0:turbo, 1:alt turbo, set by function fTurboTimer
#define TurboIdxN 0 // normal turbo is at this index in BtnTurboVal
#define TurboIdxA 1 // alternate turbo is at this index in BtnTurboVal
int16 TPress =60; // turbo button press time
int16 TRelease=60; // turbo button release time
bool bTurboHold=TRUE; // if yes the button must be help down for the turbo press
// ++ GENERAL FEATURES - non button specific
//
// ######## Quick Scope
bool bQScope =0; // quick scope state flag
uint32 QSTimeLimit=100; // timelimit of press time for starting quickscope combo
// scope , fire
uint8 QSBtns[8] ={BUTTON_8 ,BUTTON_5, // L2 & R2 default
BUTTON_6 ,BUTTON_5, // R3 & R2
BUTTON_17,BUTTON_5, // Square & R2
200 ,200 , // dummy for off state
};
uint8 QSCfg =6; // quickscope button pair to use 0,2,4,6 (6==off)
// ######## Stick Sensitivity and Deadzone
fix32 DeadZone=10.0;
int16 RSensitivity=100; // right stick sensitivity
int16 LSensitivity=100; // left stick sensitivity
// ######## Output Protocol
#define PIO_NONE 0
#define PIO_AUTO 0
#define PIO_PS4 4
#define PIO_PS3 1
#define PIO_XB1 5
#define PIO_XB360 2
#define PIO_WII 3
#define PIO_HID 6
#define PIO_SWITCH 7
// ++ Configuration Mode
bool bConfigure=FALSE;
uint8 CfgMode =0;
#define CFGNONE 0 // no config sub entry (1,2,3) selected
#define CFGBTN_SCOPE 1
#define CFGTURBO 2
#define CFGSENS_PROT 3
// ++ LED Feedback flags in RGB order , position in array
#define idx_LEDFTA 0 // FTURBOA (alternate) +blink on active
#define idx_LEDFH 1 // FHOLD +static on active
#define idx_LEDFTN 2 // FTURBON (normal) +blink on active
bool bLED[] = { FALSE,FALSE,FALSE, };
init {
// for testing
/*
TPress =200; // turbo button press time
TRelease=200; // turbo button release time
BtnsFeature[BUTTON_10]=FHOLD; // hold feature
BtnsFeature[BUTTON_5]=FTURBOHN; // turbo feature normal
BtnsFeature[BUTTON_8]=FTURBOHA; // turbo feature alternate
*/
}
main {
// reset led flags
bLED[0]=FALSE; bLED[1]=FALSE; bLED[2]=FALSE;
// handling configuration logic
fConfiguration();
// configure
if (bConfigure) { // ## for stuff active during configuration only
fNullOutput(); // set everything to zero during configuration mode
fDebug(); // debug output via set_val
} else { // ## for stuff when not in configuration mode
fButtons(); // buttons specific feature toggle and usage: hold, turbo hold, turbo toggle
fButtonLED(); // usage led feedback
} // eo : !bConfigure
// ## always active stuff
fTurboTimer(); // turbo and alternate turbo time tracker, sets values of array BtnTurboVal
// quick scope
fQuickScope();
// stick deadzone and sensitivity
fSticks();
}
void fButtonLED() {
// bLED , on active feature value is 100
// bLED[0] is hold feature, green = LED_3
// bLED[1] is turbo(normal) feature, blue = LED_1
// bLED[2] is turbo(alternate) feature, red = LED_2
static bool bIdle=FALSE;
static uint8 LastState[3]={2, 2, 2};
static uint8 LastVal[3]= {2, 2, 2};
// state change test
if (LastState[0]!=bLED[0] || LastState[1]!=bLED[1] || LastState[2]!=bLED[2]) {
LastState[0]=bLED[0]; LastState[1]=bLED[1]; LastState[2]=bLED[2]; // reset
#ifdef INFO
printf("State changed");
#endif
if (!(bLED[0] + bLED[1] + bLED[2])) { // idle
bIdle=TRUE;
ColorLED(LEDFN); // pink led
#ifdef INFO
printf("LED State: normal == idle == pink");
#endif
} else { // busy
bIdle=FALSE;
LastVal[0]=2; LastVal[1]=2; LastVal[2]=2; // reset
#ifdef INFO
printf("LED State: busy");
#endif
}
} // eo: state change test
if (!bIdle) { // not idle...set color according to active features
// bLED[0] is hold feature , green = LED_3
if (LastVal[idx_LEDFH]!=bLED[idx_LEDFH]) {
LastVal[idx_LEDFH]=bLED[idx_LEDFH];
led_set(LED_4,0.0,0);
led_set(LED_3,(fix32)bLED[idx_LEDFH]*100f,0);
#ifdef INFOLED
printf("led3: %d",LastVal[idx_LEDFH]);
#endif
}
// bLED[1] is turbo(normal) feature, blue = LED_1
if (LastVal[idx_LEDFTN]!=bLED[idx_LEDFTN]*BtnTurboVal[TurboIdxN]) {
LastVal[idx_LEDFTN]=bLED[idx_LEDFTN]*BtnTurboVal[TurboIdxN];
led_set(LED_4,0.0,0);
led_set(LED_1,(fix32)LastVal[idx_LEDFTN],0);
#ifdef INFOLED
printf("led1: %d",LastVal[idx_LEDFTN]);
#endif
}
// bLED[2] is turbo(alternate) feature, red = LED_2
if (LastVal[idx_LEDFTA]!=bLED[idx_LEDFTA]*BtnTurboVal[TurboIdxA]) {
LastVal[idx_LEDFTA]=bLED[idx_LEDFTA]*BtnTurboVal[TurboIdxA];
led_set(LED_4,0.0,0);
led_set(LED_2,(fix32)LastVal[idx_LEDFTA],0);
#ifdef INFOLED
printf("led2: %d",LastVal[idx_LEDFTA]);
#endif
}
// feature are selected and active but only one turbo is used, set non-turbo time to pink
if (LastState[idx_LEDFH]==0 && LastVal[idx_LEDFTN]==0 && LastVal[idx_LEDFTA]==0) {
ColorLED(LEDFN); // pink led
}
}
}
void fConfiguration() {
static bool TClicked=FALSE; // for trigger quickscope button layout cycle with longer button hold
static uint8 clicks=0;
if (event_active(BUTTON_2) && time_release(BUTTON_2) < 200) clicks++;
if (time_release(BUTTON_2) > 500) clicks=0;
if ( clicks==2 ) {
#ifdef INFO
printf("tripple click, toggle bConfigure");
#endif
clicks=0;
bConfigure=!bConfigure;
CfgMode=CFGNONE; // set active submenu to none
// CfgMode toggle notification
if (bConfigure) {
ColorLED(LEDCFGE,140,140,3,LEDFN);
#ifdef INFO
printf("CfgMode entered");
#endif
} else {
ColorLED(LEDCFGL,140,140,3,LEDFN);
#ifdef INFO
printf("CfgMode left");
#endif
}
} // eo: tripple click
if (bConfigure) { // ## entered configuration
if (CfgMode==CFGNONE) { // no sub entry selected
if (event_active(BUTTON_10)) {
CfgMode=CFGBTN_SCOPE;
#ifdef INFO
printf("CFGBTN_SCOPE");
#endif
}
if (event_active(BUTTON_13)) {
CfgMode=CFGTURBO;
#ifdef INFO
printf("CFGTURBO");
#endif
}
if (event_active(BUTTON_11)) {
CfgMode=CFGSENS_PROT;
#ifdef INFO
printf("CFGSENS_PROT");
#endif
}
} // eo: CfgMode==CFGNONE
else { // ## in sub entry
if (event_active(BUTTON_3)) {
CfgMode=CFGNONE; // back to main entry
ColorLED(LEDFN,140,140,3,LEDFN);
}
switch(CfgMode) { // sub entry specific
case CFGBTN_SCOPE: // button feature and quickscope selection
if (event_active(BUTTON_1)) fBtnDefault(); // PS button
else {
fButtons(TRUE); // TRUE for feature selection
if (event_release(BUTTON_2)) TClicked=FALSE;
if (check_active(BUTTON_2,500) && !TClicked) {
TClicked=TRUE;
QSCfg=(QSCfg+2)%8;
if (QSCfg>=6) bQScope=FALSE; else bQScope=TRUE;
#ifdef INFO
if (bQScope) printf("Flag: %d, Button layout idx: %d, scope: Btn_%d, fire: Btn_%d",bQScope,QSCfg,QSBtns[QSCfg]+1,QSBtns[QSCfg+1]+1);
else printf("Flag: %d (disabled)",bQScope);
#endif
}
}
break;
case CFGTURBO: // button turbo timing adjustment
if (event_active(BUTTON_1)) fTurboDefault(); // PS button
else fTurboAdjust();
break;
case CFGSENS_PROT:
if (event_active(BUTTON_18)) fOutput(); // Share : change output protocol
if (event_active(BUTTON_1)) fSensitivityDefault(); // PS button
else fSensitivityAdjust();
break;
}
}
} // eo: bConfigure
}
void fButtons(bool cfg) {
uint8 btn;
/* //unfiletred
for (btn=3;btn<18;btn++) { // buttons PS4: R1..Square
if (cfg) fBtnSelect(btn); // select buttons feature
else fBtnUsage(btn); // use buttons feature
}
*/
// new, filtered, less CPU usage
for (btn=2;btn<18;btn++) { // buttons PS4: R1..Square
// only do further processing when the button is active or has a toggle mode
if ( !event_release(btn) && !get_actual(btn) && !BtnsToggle[btn]==TOn ) continue;
if (cfg) fBtnSelect(btn); // select buttons feature
else fBtnUsage(btn); // use buttons feature
}
}
// clear all button features assignments
void fBtnDefault() {
uint8 btn;
for (btn=3;btn<18;btn++) { // buttons PS4: R1..Square
BtnsFeature[btn]=FNORMAL;
}
}
// select feature to use a button
void fBtnSelect(uint8 btn) {
// FNORMAL and FHOLD selection , Share TAP, button hold
if (get_actual(btn)) {
if (event_release(BUTTON_18)) { // on release of Share set feature
BtnsToggle[btn]=TOff; // toggle state reset
if (time_active(BUTTON_18) <= 250) { // short press
BtnsFeature[btn]=FNORMAL; // feature flag
ColorLED(LEDFN,200,200,2,LEDFN);
#ifdef INFO
printf("Cleared feature for BUTTON_%d selected",btn+1);
#endif
} else { // longer press
BtnsFeature[btn]=FHOLD; // feature flag
ColorLED(LEDFH,200,200,2,LEDFN);
#ifdef INFO
printf("HOLD feature for BUTTON_%d selected",btn+1);
#endif
}
}
} // eo: get_actual
// FTURBO and FTURBOA selection , Share HOLD, button tap
if (get_actual(BUTTON_18)) { // Share is active : select feature
if (event_release(btn)) {
BtnsToggle[btn]=TOff; // toggle state
if (time_active(btn) <= 250) { // short press
if (bTurboHold) BtnsFeature[btn]=FTURBOHN; // Turbo hold
else BtnsFeature[btn]=FTURBOTN; // Turbo toggle
ColorLED(LEDFT,200,200,2,LEDFN);
#ifdef INFO
printf("FTurbo feature for BUTTON_%d selected",btn+1);
#endif
} else { // longer press
// alternate turbo
if (bTurboHold) BtnsFeature[btn]=FTURBOHA; // TurboA hold
else BtnsFeature[btn]=FTURBOTA; // TurboA toggle
ColorLED(LEDFA,200,200,2,LEDFN);
#ifdef INFO
printf("FTurboA feature for BUTTON_%d selected",btn+1);
#endif
}
}
} // eo: Share hold
}
void fBtnUsage(uint8 btn) {
// Button feature ON/OFF toggle with short tap
if (event_release(btn) && time_active(btn) <=250) {
BtnsToggle[btn]=!BtnsToggle[btn];
}
// feature has to be enabled with a short tap first
if (BtnsToggle[btn]==TOn) {
if (BtnsFeature[btn]==FHOLD) { bLED[idx_LEDFH] =1; set_val(btn,100); }
if (BtnsFeature[btn]==FTURBOTN) { bLED[idx_LEDFTN]=1; set_val(btn,BtnTurboVal[TurboIdxN]); }
if (BtnsFeature[btn]==FTURBOTA) { bLED[idx_LEDFTA]=1; set_val(btn,BtnTurboVal[TurboIdxA]); }
}
// doesn't need to be enabled, is always on
if (BtnsFeature[btn]==FTURBOHN && get_actual(btn)) { bLED[idx_LEDFTN]=1; set_val(btn,BtnTurboVal[TurboIdxN]); }
if (BtnsFeature[btn]==FTURBOHA && get_actual(btn)) { bLED[idx_LEDFTA]=1; set_val(btn,BtnTurboVal[TurboIdxA]); }
}
// adjust turbo pres and release times and toggle turbo hold/tap mode
void fTurboAdjust() {
// R1: TPress+10
if (event_active(BUTTON_4) && TPress < 2000) { TPress += 10; }
// L1: TPress-10
if (event_active(BUTTON_7) && TPress > 20 ) { TPress -= 10; }
// R2: TRelease+10
if (event_active(BUTTON_5) && TRelease < 2000) { TRelease += 10; }
// L2: TRelease-10
if (event_active(BUTTON_8) && TRelease > 20 ) { TRelease -= 10;}
// Share double tap for turbo toggle mode / hold mode change
if (event_active(BUTTON_18) && time_release(BUTTON_18)<250) {
bTurboHold=!bTurboHold;
fTurboModeSet(bTurboHold);
}
}
// changes turbos to full auto with on/off toggle tap like the Hold feature
void fTurboModeSet(bool hold) {
uint8 btn;
if (hold) {
for (btn=3;btn<18;btn++) {
if (BtnsFeature[btn]==FTURBOTN) BtnsFeature[btn]=FTURBOHN;
if (BtnsFeature[btn]==FTURBOTA) BtnsFeature[btn]=FTURBOHA;
BtnsToggle[btn]=TOff;
}
#ifdef INFO
printf("Turbos are active when held down");
#endif
ColorLED(LEDCTH,200,200,2,LEDFN);
} else {
for (btn=3;btn<18;btn++) {
if (BtnsFeature[btn]==FTURBOHN) BtnsFeature[btn]=FTURBOTN;
if (BtnsFeature[btn]==FTURBOHA) BtnsFeature[btn]=FTURBOTA;
BtnsToggle[btn]=TOff;
}
#ifdef INFO
printf("Turbos are active by On/Off toggle tap");
#endif
ColorLED(LEDCTA,200,200,2,LEDFN);
}
}
void fTurboDefault() {
TPress = 40;
TRelease = 40;
//fReset();
}
void fTurboTimer() {
uint32 syst = system_time();
static uint32 TurboTimer=system_time();
static uint32 AltTurboTimer=0;
if ( syst > TurboTimer && TurboTimer != 0 ) { // TURBO
if (AltTurboTimer == 0 ) AltTurboTimer=TurboTimer + TPress + TRelease/2 - TPress/2;
if ( syst > TurboTimer && syst <=TurboTimer+TPress ) BtnTurboVal[TurboIdxN]=100;
else if ( syst > TurboTimer+TPress && syst < TurboTimer+TPress+TRelease) BtnTurboVal[TurboIdxN]=0;
else if ( syst >= TurboTimer+TPress+TRelease) TurboTimer = syst;
}
if ( syst > AltTurboTimer && AltTurboTimer != 0 ) { // TURBO ALTERNATE
if ( syst > AltTurboTimer && syst <=AltTurboTimer+TPress ) BtnTurboVal[TurboIdxA]=100;
else if ( syst > AltTurboTimer+TPress && syst < AltTurboTimer+TPress+TRelease) BtnTurboVal[TurboIdxA]=0;
else if ( syst >= AltTurboTimer+TPress+TRelease) AltTurboTimer = 0;
}
// debug: turbo timing output
/*
static uint8 last=0;
static uint32 tlast=0;
static uint32 tdiff=0;
static uint32 tdlst=0;
if ( last != BtnTurboVal[0] ) {
tdiff=syst-tlast;
last=BtnTurboVal[0];
printf("now %d, time: %d, tlast %d, tdiff: %d",last,syst,tlast,tdiff);
tlast=syst;
}
*/
}
void fQuickScope() {
if (bQScope && time_active(QSBtns[QSCfg]) < QSTimeLimit && event_release(QSBtns[QSCfg])) {
combo_run(cQuickScope);
}
}
combo cQuickScope {
set_val(QSBtns[QSCfg], 100);
wait(500);
set_val(QSBtns[QSCfg], 100);
set_val(QSBtns[QSCfg+1], 100);
wait(50);
set_val(QSBtns[QSCfg], 0);
set_val(QSBtns[QSCfg+1], 0);
}
void fSensitivityAdjust() {
// R1: TPress+10
if (event_active(BUTTON_4) && RSensitivity < 200) { RSensitivity += 10; }
// L1: TPress-10
if (event_active(BUTTON_7) && LSensitivity < 200) { LSensitivity += 10; }
// R2: TRelease+10
if (event_active(BUTTON_5) && RSensitivity > 20) { RSensitivity -= 10; }
// L2: TRelease-10
if (event_active(BUTTON_8) && LSensitivity > 20) { LSensitivity -= 10; }
}
void fSensitivityDefault() {
RSensitivity = 100;
LSensitivity = 100;
}
// deadzone and sensitivity
void fSticks() {
fDZSens(STICK_1_X,DeadZone,RSensitivity);
fDZSens(STICK_1_Y,DeadZone,RSensitivity);
fDZSens(STICK_2_X,DeadZone,LSensitivity);
fDZSens(STICK_2_Y,DeadZone,LSensitivity);
}
void fDZSens(uint8 axis, fix32 dz,int16 sens) {
if (abs(get_val(axis)) <= dz) {
set_val(axis, 0.0);
}
fSensitivity(axis, 50, sens);
}
void fSensitivity(uint8 id, int16 mid, int16 sen) {
int16 val = (int16)get_val(id);
if(mid != 0) {
int16 val_s = (val >= 0) ? 1 : -1;
val *= val_s;
if(val <= mid) {
val = (val * 50) / mid;
}
else {
val = ((50 * (val - mid)) / (100 - mid)) + 50;
}
val *= val_s;
}
if(sen != 0) {
val = (val * sen) / 100;
}
set_val(id, clamp(val, -100, 100));
return;
}
// cycle through output protocols
void fOutput() {
static uint8 IOP=4;
IOP=(IOP+1)%4;
if (IOP == 0) {
fOutputProtocol(PIO_XB360);
}
if (IOP == 1) {
fOutputProtocol(PIO_PS3);
}
if (IOP == 2) {
fOutputProtocol(PIO_XB1);
}
if (IOP == 3) {
fOutputProtocol(PIO_PS4);
}
}
// activate output protocol
void fOutputProtocol(uint8 id) {
port_disconnect();
switch(id) {
case PIO_PS3:
port_connect(PORT_USB_C, PROTOCOL_PS3);
#ifdef INFO
printf("PROTOCOL_PS3");
#endif
break;
case PIO_XB360:
port_connect(PORT_USB_C, PROTOCOL_XB360);
#ifdef INFO
printf("PROTOCOL_XB360");
#endif
break;
case PIO_PS4:
port_connect(PORT_USB_C, PROTOCOL_PS4);
#ifdef INFO
printf("PROTOCOL_PS4");
#endif
break;
case PIO_XB1:
port_connect(PORT_USB_C, PROTOCOL_XB1);
#ifdef INFO
printf("PROTOCOL_XB1");
#endif
break;
case PIO_SWITCH:
port_connect(PORT_USB_C, PROTOCOL_SWITCH);
#ifdef INFO
printf("PROTOCOL_SWITCH");
#endif
break;
}
return;
}
// block output
void fNullOutput() {
int8 o;
for(o = BUTTON_1; o <= GYRO_1_Z; ++o) {
set_val(o, NULL);
}
}
void fDebug() {
set_val(ACCEL_1_Z, TPress / 10);
set_val(ACCEL_2_X, TRelease / 10);
set_val(ACCEL_2_Z, CfgMode);
}
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: Need help optimizing a script
Okay, so I finally managed to get some time to test the script, and I found out that the config button mode causes what seems to be an oddly large and erratic amount of CPU usage that ranges from 5-100%, and cycling the protocol seems to break the script until it is unloaded and reloaded.
There may be other bugs that I did not find, but I only did some basic testing.
I think requiring that a button is double-tapped to cycle through the modes, like Normal -> Turbo -> Toggle Turbo -> Hold -> Alternate Turbo -> Alternate Toggle Turbo, instead of having a specific action for each mode may help, though I am unsure how to impliment such a thing when using arrays.
Actually, do you know of anything that has a detailed explaination on the use of arrays in the titan two?
Also, may I ask exactly how this line works?
I imagine that is intended to cycle IOP from 0 to 4 and then go back to 0, incrimenting by 1 each time that line is activated, but I imagine that I am probably wrong due to the fact that, as far as I can tell, 4 would do nothing in the code you have provided.
In addition, when looking through the code I noticed that the multifunction features were no longer there.
Is it because they were a major cause in the increased CPU usage, and that usage could not be rectified by any method you could come up with?
There may be other bugs that I did not find, but I only did some basic testing.
I think requiring that a button is double-tapped to cycle through the modes, like Normal -> Turbo -> Toggle Turbo -> Hold -> Alternate Turbo -> Alternate Toggle Turbo, instead of having a specific action for each mode may help, though I am unsure how to impliment such a thing when using arrays.
Actually, do you know of anything that has a detailed explaination on the use of arrays in the titan two?
Also, may I ask exactly how this line works?
- Code: Select all
IOP=(IOP+1)%4
I imagine that is intended to cycle IOP from 0 to 4 and then go back to 0, incrimenting by 1 each time that line is activated, but I imagine that I am probably wrong due to the fact that, as far as I can tell, 4 would do nothing in the code you have provided.
In addition, when looking through the code I noticed that the multifunction features were no longer there.
Is it because they were a major cause in the increased CPU usage, and that usage could not be rectified by any method you could come up with?
-
Fenria - Command Sergeant Major
- Posts: 147
- Joined: Thu May 21, 2015 5:56 pm
Re: Need help optimizing a script
I can't see that changing the protocol does "break the script" or I don't know what isn't working then. It seems to be working fine for me.Fenria wrote:Okay, so I finally managed to get some time to test the script, and I found out that the config button mode causes what seems to be an oddly large and erratic amount of CPU usage that ranges from 5-100%, and cycling the protocol seems to break the script until it is unloaded and reloaded.
You have to leave the config mode again with tripple TouchClick in order to be able to use the button functions again.
They are active/usable in normal mode only.
Can you explain what you are expecting the script should do instead in the case where you think where it is broken ?
For me the CPU usage doesn't reach 100%, it is reaching max 75% here.
I don't care much about cpu usage as long as the script works fine/lag free.
There is some strange cpu usage going on when using mouse/keyboard and using the mouse around a lot, this is happening with a connected ds4 controller to. I think you have/had something similar reported at github.
It seems to be caused by the same issue as this one at github: https://github.com/J2Kbr/TitanTwo/issues/193
So I would like to wait for J2kbr to investigate the issue and update it at github before spending much time at the code trying to fix something that my be a issue by the firmware.
There may be other bugs that I did not find, but I only did some basic testing.
I think requiring that a button is double-tapped to cycle through the modes, like Normal -> Turbo -> Toggle Turbo -> Hold -> Alternate Turbo -> Alternate Toggle Turbo, instead of having a specific action for each mode may help, though I am unsure how to impliment such a thing when using arrays.
I can change it to cycle through the buttons modes by double tap of the button if you prefer this.
I did understand that you want to be able to use turbo on hold and for a different button turbo toggle at the same time. I should be able to change the code to be able to do both for different buttons.
Actually, do you know of anything that has a detailed explaination on the use of arrays in the titan two?
No, sorry, but I don't think that the T2 array handling is much different from it in C language.
It is only a bit special when doing char arrays for storing "string"s.
Also, may I ask exactly how this line works?
- Code: Select all
IOP=(IOP+1)%4
I imagine that is intended to cycle IOP from 0 to 4 and then go back to 0, incrimenting by 1 each time that line is activated, but I imagine that I am probably wrong due to the fact that, as far as I can tell, 4 would do nothing in the code you have provided.
The % is the modulo. The remainder when dividing two numbers. The range in the code you have posted is 0,1,2,3
7%4 == 3 , 3%4== 3 , 4%4 = 0
The reminder of 4/4 == 0
7/4 == 1 , remainder 3 ..... (1x4+3) == 7
In addition, when looking through the code I noticed that the multifunction features were no longer there.
Is it because they were a major cause in the increased CPU usage, and that usage could not be rectified by any method you could come up with?
I couldn't find a "multifunction feature" in your code. What is your "multifunction feature" able to do ?
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: Need help optimizing a script
Replacing the fNullOutput function with this one will reduce the cpu usage a lot:
- Code: Select all
// block output
void fNullOutput() {
// this one:
// cpu usage low
set_val(BUTTON_1,0); set_val(BUTTON_2,0); set_val(BUTTON_3,0); set_val(BUTTON_4,0);
set_val(BUTTON_5,0); set_val(BUTTON_6,0); set_val(BUTTON_7,0); set_val(BUTTON_8,0);
set_val(BUTTON_9,0); set_val(BUTTON_10,0); set_val(BUTTON_11,0); set_val(BUTTON_12,0);
set_val(BUTTON_13,0); set_val(BUTTON_14,0); set_val(BUTTON_15,0); set_val(BUTTON_16,0);
set_val(BUTTON_17,0); set_val(BUTTON_18,0); set_val(BUTTON_19,0); set_val(BUTTON_20,0);
set_val(BUTTON_21,0);
set_val(STICK_1_X,0); set_val(STICK_1_Y,0); set_val(STICK_2_X,0); set_val(STICK_2_Y,0);
set_val(POINT_1_X,0); set_val(POINT_1_Y,0); set_val(POINT_2_X,0); set_val(POINT_2_Y,0);
set_val(ACCEL_1_X,0); set_val(ACCEL_1_Y,0); set_val(ACCEL_1_Z,0);
set_val(ACCEL_2_X,0); set_val(ACCEL_2_Y,0); set_val(ACCEL_2_Z,0);
set_val(GYRO_1_X,0); set_val(GYRO_1_Y,0); set_val(GYRO_1_Z,0);
/*
// or instead this one
// cpu usage high
int8 o;
for(o = BUTTON_1; o <= GYRO_1_Z; o++) {
set_val(o, NULL);
}
*/
}
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: Need help optimizing a script
I can change it to cycle through the buttons modes by double tap of the button if you prefer this.
I did understand that you want to be able to use turbo on hold and for a different button turbo toggle at the same time. I should be able to change the code to be able to do both for different buttons.
Doing this would be very much appreciated.
I can't see that changing the protocol does "break the script" or I don't know what isn't working then. It seems to be working fine for me.
You have to leave the config mode again with tripple TouchClick in order to be able to use the button functions again.
They are active/usable in normal mode only.
Can you explain what you are expecting the script should do instead in the case where you think where it is broken ?
For me the CPU usage doesn't reach 100%, it is reaching max 75% here.
I don't care much about cpu usage as long as the script works fine/lag free.
There is some strange cpu usage going on when using mouse/keyboard and using the mouse around a lot, this is happening with a connected ds4 controller to. I think you have/had something similar reported at github.
It seems to be caused by the same issue as this one at github: https://github.com/J2Kbr/TitanTwo/issues/193
So I would like to wait for J2kbr to investigate the issue and update it at github before spending much time at the code trying to fix something that my be a issue by the firmware.
I think I might have figured out why the Protocol cycling doesn't work in my case, and why I have higher CPU usage.
I use the Bluetooth module, and I've noticed that it causes higher/more erratic CPU usage in general, plus there's occasionally things that cause it to bug out my controller for some reason.
Like, at one point when I was testing some LED functions, the LED wouldn't update when it would if I used it with a USB cable.
The script runs much more smoothly when using the controller with a USB cable, and the output protocol cycling functions properly as well.
No, sorry, but I don't think that the T2 array handling is much different from it in C language.
It is only a bit special when doing char arrays for storing "string"s.
I'll have to go and look up how C handles arrays then.
Thanks for the info.
The % is the modulo. The remainder when dividing two numbers. The range in the code you have posted is 0,1,2,3
7%4 == 3 , 3%4== 3 , 4%4 = 0
The reminder of 4/4 == 0
7/4 == 1 , remainder 3 ..... (1x4+3) == 7
Never thought about using something like this to create a cycle, so I'll have to keep it in mind.
Thanks for explaining it.
I couldn't find a "multifunction feature" in your code. What is your "multifunction feature" able to do ?
Here it is:
- Code: Select all
void fMultifunction_Toggle1() {
if (F1 == 3 || F1 == 5) {
F1 = fMultifunction_Toggle2(PS4_R1, F1);
}
if (F2 == 3 || F2 == 5) {
F2 = fMultifunction_Toggle2(PS4_R2, F2);
}
if (F3 == 3 || F3 == 5) {
F3 = fMultifunction_Toggle2(PS4_R3, F3);
}
if (F4 == 3 || F4 == 5) {
F4 = fMultifunction_Toggle2(PS4_L1, F4);
}
if (F5 == 3 || F5 == 5) {
F5 = fMultifunction_Toggle2(PS4_L2, F5);
}
if (F6 == 3 || F6 == 5) {
F6 = fMultifunction_Toggle2(PS4_L3, F6);
}
if (F7 == 3 || F7 == 5) {
F7 = fMultifunction_Toggle2(PS4_TRIANGLE, F7);
}
if (F8 == 3 || F8 == 5) {
F8 = fMultifunction_Toggle2(PS4_CIRCLE, F8);
}
if (F9 == 3 || F9 == 5) {
F9 = fMultifunction_Toggle2(PS4_CROSS, F9);
}
if (F10 == 3 || F10 == 5) {
F10 = fMultifunction_Toggle2(PS4_SQUARE, F10);
}
if (F11 == 3 || F11 == 5) {
F11 = fMultifunction_Toggle2(PS4_UP, F11);
}
if (F12 == 3 || F12 == 5) {
F12 = fMultifunction_Toggle2(PS4_RIGHT, F12);
}
if (F13 == 3 || F13 == 5) {
F13 = fMultifunction_Toggle2(PS4_DOWN, F13);
}
if (F14 == 3 || F14 == 5) {
F14 = fMultifunction_Toggle2(PS4_LEFT, F14);
}
}
int16 fMultifunction_Toggle2(int8 b, int16 f) {
RV = fMultifunction_Toggle3(b);
if (RV == 1) {
if (f == 3) {
f = 33;
}
if (f == 5) {
f = 55;
}
}
else if (RV == 2) {
if (f == 3) {
f = 333;
}
if (f == 5) {
f = 555;
}
}
return(f);
}
int8 fMultifunction_Toggle3(int8 b) {
if (event_release(b)) {
if (time_active(b) <= PT) {
TRT[b] = 0;
}
else {
TRT[b] = -1;
}
}
else if (!get_val(b)) {
if (TRT[b] >= 0) {
TRT[b] = TRT[b] + elapsed_time();
if (TRT[b] > PT) {
TRT[b] = -1;
}
}
}
else if (get_val(b)) {
if (TRT[b] == -1) {
if (time_active(b) >= 1000) {
return(1);
}
}
else if (TRT[b] != -1) {
if (time_active(b) >= 250) {
return(2);
}
}
}
return(0);
}
It's used for 2 additional functions: One will apply a toggle-able turbo if tapped twice then held for 250 ms, or apply an toggle-able hold if the button is just held down for 1 second, and the other does the same, though it applies alternate turbo instead of turbo.
Also, would it be possible to have a way to see if a button has something assigned to it through the Debug function?
Preferably by having the BtnsFeature's number in the tens column, and BtnsToggle's number in the ones column?
I tried to do that when I was optimizing the script myself, but it didn't go so well, cause I couldn't even use the script long enough to test if it had worked.
Oh, and a way to quickly reset BtnsFeature and BtnsToggle across all buttons, as well as QSBtns would also be very much appreciated, as it would replace this:
- Code: Select all
void fDefault_Features() {
F1 = 0;
F2 = 0;
F3 = 0;
F4 = 0;
F5 = 0;
F6 = 0;
F7 = 0;
F8 = 0;
F9 = 0;
F10 = 0;
F11 = 0;
F12 = 0;
F13 = 0;
F14 = 0;
QS = 0;
}
-
Fenria - Command Sergeant Major
- Posts: 147
- Joined: Thu May 21, 2015 5:56 pm
Re: Need help optimizing a script
Fenria wrote:I can change it to cycle through the buttons modes by double tap of the button if you prefer this...cut...
Doing this would be very much appreciated.
Done.
I couldn't find a "multifunction feature" in your code. What is your "multifunction feature" able to do ?
It's used for 2 additional functions: One will apply a toggle-able turbo if tapped twice then held for 250 ms, or apply an toggle-able hold if the button is just held down for 1 second, and the other does the same, though it applies alternate turbo instead of turbo.
Are the 2 additional toggles only available when you assign this options to a button like you have to do it for all the other features like HOLD or the TURBO modes at the moment ?
What is the cycle order, where do you want to have them added ..at the moment it is like you have requested:
double-tapped to cycle through the modes: Normal -> Turbo -> Toggle Turbo -> Hold -> Alternate Turbo -> Alternate Toggle Turbo
I'll give it a try.Also, would it be possible to have a way to see if a button has something assigned to it through the Debug function?
Preferably by having the BtnsFeature's number in the tens column, and BtnsToggle's number in the ones column?
Oh, and a way to quickly reset BtnsFeature and BtnsToggle across all buttons, as well as QSBtns would also be very much appreciated, as it would replace this:
- Code: Select all
void fDefault_Features() {
F1 = 0;
F2 = 0;
F3 = 0;
F4 = 0;
F5 = 0;
F6 = 0;
F7 = 0;
F8 = 0;
F9 = 0;
F10 = 0;
F11 = 0;
F12 = 0;
F13 = 0;
F14 = 0;
QS = 0;
}
This should already be working the same way as your script does it. I only missed to put it in the description at the top of the script.
Here is a newer version, still missing the multifunc toggles but allows cycle through buttons features by double-tap now.
- Code: Select all
// CONFIG MODE: 3x fast press TouchClick to enter and leave config mode
// +DPAD-UP: CFGBTN_SCOPE : button specific Turbo&Alternate&Hold AND quickscope selection
// -Double-Tap a button : to cycle through the available button specific features
// -PS : clear all button features settings assignments and flags
// -TouchClick Hold > 500ms: cycling through quickscope settings
// +DPAD-RIGHT: CFGTURBO : Turbo Timing adjustment
// -PS : Turbo Timing reset
// +DPAD-DOWN: CFGSENS_PROT : Sensitivity adjustment and Output Protocol selection
// -Share : Output Protocol selection
// -PS : Sensitivity reset
#include "ColorLED.gph"
// ++ BUTTON SPECIFIC FEATURES
//
// feature/mode identifier, for each button
#define FNORMAL 0 // no special function
#define FTURBOHN 1 // hold = normal turbo
#define FTURBOTN 2 // short tap = normal turbo
#define FHOLD 3 // short tap = hold button down
#define FTURBOHA 4 // hold = alt turbo
#define FTURBOTA 5 // short tap = alt turbo
#define FMODES 6 // number of modes available to cycle through with dbl tap
// LED blink colors for feature toggles / activity
// led info : viewtopic.php?f=26&t=11679#p79464
uint8 LEDF[] = {
CP, // pink on FNORMAL / Non special mode +static (idle)
CB, // blue on FTURBOHN (hold,normal) +blink on active
CA, // amber on FTURBOTN (toggle,normal) +blink on active
CG, // green on FHOLD +static on active
CR, // red on FTURBOHA (hold,alternate) +blink on active
CW, // white on FTURBOTA (toggle,alternate) +blink on active
};
// ++ LED Feedback state tracking flags in RGB order , position in array
#define idx_LEDFTA 0 // FTURBOA (alternate) +blink on active
#define idx_LEDFH 1 // FHOLD +static on active
#define idx_LEDFTN 2 // FTURBON (normal) +blink on active
bool bLED[] = { FALSE,FALSE,FALSE, };
// toggle state ids
#define TOff 0 // toggled off
#define TOn 1 // toggled on
uint8 BtnsFeature[21];// buttons feature flags (FNORMAL, FTURBOHN, ...)
uint8 BtnsToggle[21]; // buttons feature toggle state (enabled/disabled)
uint8 BtnTurboVal[2]; // buttons value to set for 0:turbo, 1:alt turbo, set by function fTurboTimer
#define TurboIdxN 0 // normal turbo is at this index in BtnTurboVal
#define TurboIdxA 1 // alternate turbo is at this index in BtnTurboVal
int16 TPress =60; // turbo button press time
int16 TRelease=60; // turbo button release time
// ++ GENERAL FEATURES - non button specific
//
// ######## Quick Scope
bool bQScope =0; // quick scope state flag
uint32 QSTimeLimit=100; // timelimit of press time for starting quickscope combo
// scope , fire
uint8 QSBtns[8] ={BUTTON_8 ,BUTTON_5, // L2 & R2 default
BUTTON_6 ,BUTTON_5, // R3 & R2
BUTTON_17,BUTTON_5, // Square & R2
200 ,200 , // dummy for off state
};
uint8 QSCfg =6; // quickscope button pair to use 0,2,4,6 (6==off)
// ######## Stick Sensitivity and Deadzone
fix32 DeadZone=10.0;
int16 RSensitivity=100; // right stick sensitivity
int16 LSensitivity=100; // left stick sensitivity
// ######## Output Protocol
#define PIO_NONE 0
#define PIO_AUTO 0
#define PIO_PS4 4
#define PIO_PS3 1
#define PIO_XB1 5
#define PIO_XB360 2
#define PIO_WII 3
#define PIO_HID 6
#define PIO_SWITCH 7
// ++ Configuration Mode
bool bConfigure=FALSE;
uint8 CfgMode =0;
#define CFGNONE 0 // no config sub entry (1,2,3) selected
#define CFGBTN_SCOPE 1
#define CFGTURBO 2
#define CFGSENS_PROT 3
// led feedback for configuration menu position
#define LCFGENTER 0
#define LCFGLEAVE 0
uint8 LEDCFG[] = {
CP, // pink , entering+leaving config mode
};
init {
// for testing
/*
TPress =200; // turbo button press time
TRelease=200; // turbo button release time
BtnsFeature[BUTTON_10]=FHOLD; // hold feature
BtnsFeature[BUTTON_5]=FTURBOHN; // turbo feature normal
BtnsFeature[BUTTON_8]=FTURBOHA; // turbo feature alternate
*/
/*
bConfigure=TRUE;
CfgMode=CFGBTN_SCOPE;
*/
}
main {
// reset led flags
bLED[0]=FALSE; bLED[1]=FALSE; bLED[2]=FALSE;
// handling configuration logic
fConfiguration();
// configure
if (bConfigure) {
fNullOutput(); // zero output via set_val
fDebug(); // debug output via set_val
} else {
fButtons(); // buttons specific feature toggle and usage: hold, turbo hold, turbo toggle
fButtonLED(); // usage led feedback
}
// ## always active stuff
fTurboTimer(); // turbo and alternate turbo time tracker, sets values of array BtnTurboVal
fQuickScope(); // quick scope
fSticks(); // stick deadzone and sensitivity
}
// button feature activity LED feedback
void fButtonLED() {
// bLED[0] is hold feature, green = LED_3
// bLED[1] is turbo(normal) feature, blue = LED_1
// bLED[2] is turbo(alternate) feature, red = LED_2
static bool bIdle=FALSE;
static uint8 LastState[3]={2, 2, 2};
static uint8 LastVal[3]= {2, 2, 2};
// state change test
if (LastState[0]!=bLED[0] || LastState[1]!=bLED[1] || LastState[2]!=bLED[2]) {
LastState[0]=bLED[0]; LastState[1]=bLED[1]; LastState[2]=bLED[2]; // reset
//printf("State changed");
if (!(bLED[0] + bLED[1] + bLED[2])) { // idle
bIdle=TRUE;
ColorLED(LEDF[FNORMAL]); // pink led
//printf("LED State: normal == idle == pink");
} else { // busy
bIdle=FALSE;
LastVal[0]=2; LastVal[1]=2; LastVal[2]=2; // reset
//printf("LED State: busy");
}
} // eo: state change test
if (!bIdle) { // not idle...set color according to active features
// bLED[0] is hold feature , green = LED_3
if (LastVal[idx_LEDFH]!=bLED[idx_LEDFH]) {
LastVal[idx_LEDFH]=bLED[idx_LEDFH];
led_set(LED_4,0.0,0);
led_set(LED_3,(fix32)bLED[idx_LEDFH]*100f,0);
//printf("led3: %d",LastVal[idx_LEDFH]);
}
// bLED[1] is turbo(normal) feature, blue = LED_1
if (LastVal[idx_LEDFTN]!=bLED[idx_LEDFTN]*BtnTurboVal[TurboIdxN]) {
LastVal[idx_LEDFTN]=bLED[idx_LEDFTN]*BtnTurboVal[TurboIdxN];
led_set(LED_4,0.0,0);
led_set(LED_1,(fix32)LastVal[idx_LEDFTN],0);
//printf("led1: %d",LastVal[idx_LEDFTN]);
}
// bLED[2] is turbo(alternate) feature, red = LED_2
if (LastVal[idx_LEDFTA]!=bLED[idx_LEDFTA]*BtnTurboVal[TurboIdxA]) {
LastVal[idx_LEDFTA]=bLED[idx_LEDFTA]*BtnTurboVal[TurboIdxA];
led_set(LED_4,0.0,0);
led_set(LED_2,(fix32)LastVal[idx_LEDFTA],0);
//printf("led2: %d",LastVal[idx_LEDFTA]);
}
// feature are selected and active but only one turbo is used, set non-turbo time to pink
if (LastState[idx_LEDFH]==0 && LastVal[idx_LEDFTN]==0 && LastVal[idx_LEDFTA]==0) {
ColorLED(LEDF[FNORMAL]); // pink led
}
}
}
//
void fConfiguration() {
static bool TClicked=FALSE; // for trigger quickscope button layout cycle with longer button hold
static uint8 clicks=0;
if (event_active(BUTTON_2) && time_release(BUTTON_2) < 250) clicks++;
if (time_release(BUTTON_2) > 500) clicks=0;
if ( clicks==2 ) {
//printf("tripple click, toggle bConfigure");
clicks=0;
bConfigure=!bConfigure;
CfgMode=CFGNONE; // set active submenu to none
// CfgMode toggle notification
if (bConfigure) {
ColorLED(LEDCFG[LCFGENTER],140,140,3,LEDF[FNORMAL]);
printf("CfgMode entered");
} else {
ColorLED(LEDCFG[LCFGLEAVE],140,140,3,LEDF[FNORMAL]);
printf("CfgMode left");
}
} // eo: tripple click
if (bConfigure) { // ## entered configuration
if (CfgMode==CFGNONE) { // no sub entry selected
if (event_active(BUTTON_10)) {
CfgMode=CFGBTN_SCOPE;
printf("CFGBTN_SCOPE");
}
if (event_active(BUTTON_13)) {
CfgMode=CFGTURBO;
printf("CFGTURBO");
}
if (event_active(BUTTON_11)) {
CfgMode=CFGSENS_PROT;
printf("CFGSENS_PROT");
}
} // eo: CfgMode==CFGNONE
else { // ## in sub entry
if (event_active(BUTTON_3)) {
CfgMode=CFGNONE; // back to main entry
ColorLED(LEDCFG[0],140,140,3,LEDF[FNORMAL]);
}
switch(CfgMode) { // sub entry specific
case CFGBTN_SCOPE: // button feature and quickscope selection
if (event_active(BUTTON_1)) fDefaultFeatures(); // PS button
else {
fButtons(TRUE); // TRUE for feature selection
if (event_release(BUTTON_2)) TClicked=FALSE;
if (check_active(BUTTON_2,500) && !TClicked) {
TClicked=TRUE;
QSCfg=(QSCfg+2)%8;
if (QSCfg>=6) bQScope=FALSE; else bQScope=TRUE;
printf("quicksope (1:on/0:off) %d ,Button layout idx: %d, scope: Btn_%d, fire: Btn_%d",bQScope,QSCfg,QSBtns[QSCfg]+1,QSBtns[QSCfg+1]+1);
}
}
break;
case CFGTURBO: // button turbo timing adjustment
if (event_active(BUTTON_1)) fTurboDefault(); // PS button
else fTurboAdjust();
break;
case CFGSENS_PROT: // sensitivity and output protocol adjustment
if (event_active(BUTTON_18)) fOutput(); // Share : change output protocol
if (event_active(BUTTON_1)) fSensitivityDefault(); // PS button
else fSensitivityAdjust();
break;
}
}
} // eo: bConfigure
}
// button loop for config and usage
void fButtons(bool cfg) {
uint8 btn;
for (btn=3;btn<18;btn++) { // buttons PS4: R1...Square
// only do further processing when the button is active or has a toggle mode, reduces cpu usage
if ( !event_release(btn) && !get_actual(btn) && !BtnsToggle[btn]==TOn ) continue;
if (cfg) fBtnSelect(btn); // select buttons feature
else fBtnUsage(btn); // use buttons feature
}
}
// clear all button features assignments and quick scope
void fDefaultFeatures() {
uint8 btn;
for (btn=3;btn<18;btn++) { // buttons PS4: R1..Square
BtnsFeature[btn]=FNORMAL;
BtnsToggle[btn]=TOff;
}
QSCfg=6;
bQScope=FALSE;
}
// select feature to use a button
void fBtnSelect(uint8 btn) {
static uint8 lastbtn=250;
static uint32 lasttime;
// remember how long the last button press was for double-tap check
if (event_release(btn)) {
lasttime=time_active(btn);
lastbtn=btn;
}
// cycle through modes with dbl tap
if (event_active(btn) && lastbtn==btn && lasttime<=250 && time_release(btn)<=250) {
BtnsFeature[btn]=(BtnsFeature[btn]+1)%FMODES;
BtnsToggle[btn]=TOff; // toggle state reset
printf("buttons feature ID: %d",BtnsFeature[btn]);
// led toggle feedback
if (BtnsFeature[btn] < FMODES && BtnsFeature[btn] < sizeof(LEDF)) ColorLED(BtnsFeature[btn],200,200,2,LEDF[FNORMAL]);
}
}
// use buttons selected features
void fBtnUsage(uint8 btn) {
// Button feature ON/OFF toggle with short tap
if (event_release(btn) && time_active(btn) <=250) {
BtnsToggle[btn]=!BtnsToggle[btn];
}
// feature has to be enabled with a short tap first
if (BtnsToggle[btn]==TOn) {
if (BtnsFeature[btn]==FHOLD) { bLED[idx_LEDFH] =1; set_val(btn,100); }
if (BtnsFeature[btn]==FTURBOTN) { bLED[idx_LEDFTN]=1; set_val(btn,BtnTurboVal[TurboIdxN]); }
if (BtnsFeature[btn]==FTURBOTA) { bLED[idx_LEDFTA]=1; set_val(btn,BtnTurboVal[TurboIdxA]); }
}
// doesn't need to be enabled, is always on and active when the button is held down
if (BtnsFeature[btn]==FTURBOHN && get_actual(btn)) { bLED[idx_LEDFTN]=1; set_val(btn,BtnTurboVal[TurboIdxN]); }
if (BtnsFeature[btn]==FTURBOHA && get_actual(btn)) { bLED[idx_LEDFTA]=1; set_val(btn,BtnTurboVal[TurboIdxA]); }
}
// adjust turbo press and release times
void fTurboAdjust() {
// R1: TPress+10
if (event_active(BUTTON_4) && TPress < 2000) { TPress += 10; }
// L1: TPress-10
if (event_active(BUTTON_7) && TPress > 20 ) { TPress -= 10; }
// R2: TRelease+10
if (event_active(BUTTON_5) && TRelease < 2000) { TRelease += 10; }
// L2: TRelease-10
if (event_active(BUTTON_8) && TRelease > 20 ) { TRelease -= 10;}
}
// reset turbo timings
void fTurboDefault() {
TPress = 40;
TRelease = 40;
//fReset();
}
// sets current button value for the turbo modes to use when active
void fTurboTimer() {
uint32 syst = system_time();
static uint32 TurboTimer=system_time();
static uint32 AltTurboTimer=0;
if ( syst > TurboTimer && TurboTimer != 0 ) { // TURBO
if (AltTurboTimer == 0 ) AltTurboTimer=TurboTimer + TPress + TRelease/2 - TPress/2;
if ( syst > TurboTimer && syst <=TurboTimer+TPress ) BtnTurboVal[TurboIdxN]=100;
else if ( syst > TurboTimer+TPress && syst < TurboTimer+TPress+TRelease) BtnTurboVal[TurboIdxN]=0;
else if ( syst >= TurboTimer+TPress+TRelease) TurboTimer = syst;
}
if ( syst > AltTurboTimer && AltTurboTimer != 0 ) { // TURBO ALTERNATE
if ( syst > AltTurboTimer && syst <=AltTurboTimer+TPress ) BtnTurboVal[TurboIdxA]=100;
else if ( syst > AltTurboTimer+TPress && syst < AltTurboTimer+TPress+TRelease) BtnTurboVal[TurboIdxA]=0;
else if ( syst >= AltTurboTimer+TPress+TRelease) AltTurboTimer = 0;
}
}
void fQuickScope() {
if (bQScope && time_active(QSBtns[QSCfg]) < QSTimeLimit && event_release(QSBtns[QSCfg])) {
combo_run(cQuickScope);
}
}
combo cQuickScope {
set_val(QSBtns[QSCfg], 100);
wait(500);
set_val(QSBtns[QSCfg], 100);
set_val(QSBtns[QSCfg+1], 100);
wait(50);
set_val(QSBtns[QSCfg], 0);
set_val(QSBtns[QSCfg+1], 0);
}
void fSensitivityAdjust() {
// R1: TPress+10
if (event_active(BUTTON_4) && RSensitivity < 200) { RSensitivity += 10; }
// L1: TPress-10
if (event_active(BUTTON_7) && LSensitivity < 200) { LSensitivity += 10; }
// R2: TRelease+10
if (event_active(BUTTON_5) && RSensitivity > 20) { RSensitivity -= 10; }
// L2: TRelease-10
if (event_active(BUTTON_8) && LSensitivity > 20) { LSensitivity -= 10; }
}
void fSensitivityDefault() {
RSensitivity = 100;
LSensitivity = 100;
}
// deadzone and sensitivity
void fSticks() {
fDZSens(STICK_1_X,DeadZone,RSensitivity);
fDZSens(STICK_1_Y,DeadZone,RSensitivity);
fDZSens(STICK_2_X,DeadZone,LSensitivity);
fDZSens(STICK_2_Y,DeadZone,LSensitivity);
}
void fDZSens(uint8 axis, fix32 dz,int16 sens) {
if (abs(get_val(axis)) <= dz) {
set_val(axis, 0.0);
}
fSensitivity(axis, 50, sens);
}
void fSensitivity(uint8 id, int16 mid, int16 sen) {
int16 val = (int16)get_val(id);
if(mid != 0) {
int16 val_s = (val >= 0) ? 1 : -1;
val *= val_s;
if(val <= mid) {
val = (val * 50) / mid;
}
else {
val = ((50 * (val - mid)) / (100 - mid)) + 50;
}
val *= val_s;
}
if(sen != 0) {
val = (val * sen) / 100;
}
set_val(id, clamp(val, -100, 100));
return;
}
// cycle through output protocols
void fOutput() {
static uint8 IOP=4;
IOP=(IOP+1)%4;
if (IOP == 0) {
fOutputProtocol(PIO_XB360);
}
if (IOP == 1) {
fOutputProtocol(PIO_PS3);
}
if (IOP == 2) {
fOutputProtocol(PIO_XB1);
}
if (IOP == 3) {
fOutputProtocol(PIO_PS4);
}
}
// activate output protocol
void fOutputProtocol(uint8 id) {
port_disconnect();
switch(id) {
case PIO_PS3:
port_connect(PORT_USB_C, PROTOCOL_PS3);
printf("PROTOCOL_PS3");
break;
case PIO_XB360:
port_connect(PORT_USB_C, PROTOCOL_XB360);
printf("PROTOCOL_XB360");
break;
case PIO_PS4:
port_connect(PORT_USB_C, PROTOCOL_PS4);
printf("PROTOCOL_PS4");
break;
case PIO_XB1:
port_connect(PORT_USB_C, PROTOCOL_XB1);
printf("PROTOCOL_XB1");
break;
case PIO_SWITCH:
port_connect(PORT_USB_C, PROTOCOL_SWITCH);
printf("PROTOCOL_SWITCH");
break;
}
return;
}
// block output
void fNullOutput() {
set_val(BUTTON_1,0); set_val(BUTTON_2,0); set_val(BUTTON_3,0); set_val(BUTTON_4,0);
set_val(BUTTON_5,0); set_val(BUTTON_6,0); set_val(BUTTON_7,0); set_val(BUTTON_8,0);
set_val(BUTTON_9,0); set_val(BUTTON_10,0); set_val(BUTTON_11,0); set_val(BUTTON_12,0);
set_val(BUTTON_13,0); set_val(BUTTON_14,0); set_val(BUTTON_15,0); set_val(BUTTON_16,0);
set_val(BUTTON_17,0); set_val(BUTTON_18,0); set_val(BUTTON_19,0); set_val(BUTTON_20,0);
set_val(BUTTON_21,0);
set_val(STICK_1_X,0); set_val(STICK_1_Y,0); set_val(STICK_2_X,0); set_val(STICK_2_Y,0);
set_val(POINT_1_X,0); set_val(POINT_1_Y,0); set_val(POINT_2_X,0); set_val(POINT_2_Y,0);
set_val(ACCEL_1_X,0); set_val(ACCEL_1_Y,0); set_val(ACCEL_1_Z,0);
set_val(ACCEL_2_X,0); set_val(ACCEL_2_Y,0); set_val(ACCEL_2_Z,0);
set_val(GYRO_1_X,0); set_val(GYRO_1_Y,0); set_val(GYRO_1_Z,0);
}
void fDebug() {
set_val(ACCEL_1_Z, TPress / 10);
set_val(ACCEL_2_X, TRelease / 10);
set_val(ACCEL_2_Z, CfgMode);
}
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
35 posts
• Page 1 of 4 • 1, 2, 3, 4
Return to GPC2 Script Programming
Who is online
Users browsing this forum: No registered users and 101 guests