From cd4d0a71efd21a9a0828bec987a843272f16890f Mon Sep 17 00:00:00 2001 From: qarkai Date: Tue, 7 Jun 2016 17:13:39 +0300 Subject: [PATCH] Some refactoring --- main.c | 53 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/main.c b/main.c index 1538b3b..9459cc3 100644 --- a/main.c +++ b/main.c @@ -60,7 +60,8 @@ void common_start_button(void) { } void common_alarm_callback(void) { - int interval, randomfactor, sign, rv, alarmtime; + int interval, randomfactor, alarmtime; + int rv = 0; if (!counter) return; @@ -71,13 +72,14 @@ void common_alarm_callback(void) { click_mouse_button(); interval = get_spin_value(SPIN_INTERVAL); - sign = rand() / (RAND_MAX >> 1); randomfactor = get_spin_value(SPIN_RANDOM); - if (randomfactor > 0 ) + if (randomfactor > 0) + { + int sign = rand() / (RAND_MAX >> 1); + rv = (sign ? 1 : -1) * (rand() / (RAND_MAX / randomfactor)); - else - rv = 0; + } #ifdef DEBUG printf("rv = %i\n", rv); @@ -91,26 +93,34 @@ void common_alarm_callback(void) { else common_stop_button(); } -static void calculate_average(int *buffer, int length, int *average, int *min, - int *max) { +static int calculate_average(int *buffer, int length, int *min, int *max) { int sum = 0, x; + int average; *min = 65536; *max = -65536; - for (x=0; x*max) *max=v; + + if (v < *min) { + *min = v; + } + + if (v > *max) { + *max = v; + } } - *average = sum/length; + average = sum/length; #ifdef DEBUG - printf("average = %i // min = %i // max = %i (of %i samples)\n", - *average, *min, *max, length); + printf("average = %i // min = %i // max = %i (of %i samples)\n", + average, *min, *max, length); #endif + + return average; } #define THRESHOLD 5 * 1000 /* 5 seconds */ @@ -138,16 +148,21 @@ void common_tap_button(void) { } history[x] = interval; - x++; - fill++; + ++x; + ++fill; - if (x == HISTORYSIZE) x = 0; - if (fill > HISTORYSIZE) fill = HISTORYSIZE; + if (x == HISTORYSIZE) { + x = 0; + } + + if (fill > HISTORYSIZE) { + fill = HISTORYSIZE; + } - calculate_average(history, fill, &average, &min, &max); + average = calculate_average(history, fill, &min, &max); set_spin_value(SPIN_INTERVAL, average); - set_spin_value(SPIN_RANDOM, (max-min)>>1); + set_spin_value(SPIN_RANDOM, (max - min) >> 1); prevtime = curtime; }