From e7cbe138bcd4c38112c956558a6571b0beda789a Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Wed, 6 Nov 2013 10:24:59 +0100 Subject: [PATCH 1/2] Feste Zeiten fuer bell_process entfernt. Es werden jetzt die Beeps in kurze und lange gebinnt und dann mit dem Code verglichen. --- software/lockcontroller/bell_process.c | 124 +++++++++++++++++++------ 1 file changed, 95 insertions(+), 29 deletions(-) diff --git a/software/lockcontroller/bell_process.c b/software/lockcontroller/bell_process.c index fa0804b..47f56b6 100644 --- a/software/lockcontroller/bell_process.c +++ b/software/lockcontroller/bell_process.c @@ -19,18 +19,21 @@ * along with this program. If not, see . */ #include "bell_process.h" -#include "pinutils.h" +#include "../common/pinutils.h" #include #include +#include +#include #define MIN_PRESS 50 #define MAX_PRESS 2500 #define MIN_BREAK 50 #define MAX_BREAK 2500 +#define MAX_BEEPS 20 -//static const int16_t times[] = {-200, -200, 500, -200, -200}; -//static const int16_t times[] = {-400, -400, 800, -400, -400}; -static const int16_t times[] = {400}; + +static char *code = ".--.---"; +static int16_t times[MAX_BEEPS + 1]; static uint8_t step; static uint8_t debounce; static bool pressed; @@ -94,34 +97,17 @@ void bell_tick(void) if( !pressed && press_timer > 0 ){ - if( times[step] > 0 ){ - if( press_timer > MIN_PRESS && - press_timer > times[step] ){ - step++; - }else{ - step = 0; - } - }else{ - if( press_timer > MIN_PRESS && - press_timer < -times[step] ){ - step++; - }else{ - step = 0; - } - } - press_timer = -1; - - if( step * sizeof(times[0]) == sizeof(times) ){ - accepted = true; + if ( press_timer > MIN_PRESS && press_timer < MAX_PRESS && step < MAX_BEEPS){ + times[step + 1] = 0; + times[step++] = press_timer; + } + else{ + step = 0; + press_timer = -1; c = 2000; } } - if( press_timer > MAX_PRESS ){ - press_timer = -1; - step = 0; - } - if( break_timer > MAX_BREAK ){ break_timer = -1; step = 0; @@ -131,7 +117,87 @@ void bell_tick(void) bool bell_isAccepted(void) { - return accepted; + int beeps = 0; + int boundary; + double best_badness = 10000000.0; + int16_t best_boundary = 0; + double badness; + int beep; + bool matches = true; + + while(times[beeps++]) + ; + + --beeps; + + printf("Found %d beeps.\n",beeps); + if(beeps != strlen(code)) + return(false); + + + for(boundary = 0; boundary <= beeps; boundary++){ + + int nshort = 0; + int nlong = 0 ; + int16_t sshort = 0; + int16_t ssshort = 0; + int16_t slong = 0; + int16_t sslong = 0; + + printf("Trying %dth boundary %d\n",boundary, times[boundary]); + for(beep = 0; beep < beeps; beep++){ + if(times[beep] <= times[boundary]){ + ++nshort; + sshort += times[beep]; + ssshort += times[beep] * times[beep]; + } + else{ + ++nlong; + slong += times[beep]; + sslong += times[beep] * times[beep]; + } + } + + if(nshort){ + badness = sqrt((double) ssshort * nshort / (double) sshort / (double) sshort - 1.0); + } + else{ + badness = 0.0; + } + if(nlong){ + badness += sqrt((double) sslong * nlong / (double) slong / (double) slong - 1.0); + } + + if(badness < best_badness){ + best_badness = badness; + best_boundary = times[boundary]; + } + } + + for(beep=0; beep < beeps; beep++){ + if(times[beep] <= best_boundary){ + if(code[beep] != '.'){ + matches = false; + printf(". does not match code!\n"); + } + else + printf(". matches code\n"); + } + else{ + if(code[beep] != '-'){ + matches = false; + printf("- does not match code!\n"); + } + else + printf("- matches code\n"); + } + } + + if(matches) + c = 2000; + + return matches; + } void bell_process(void) From 4e6281b0a631459e20c0eea6ff4133a27aafdc3c Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Wed, 6 Nov 2013 10:28:23 +0100 Subject: [PATCH 2/2] Debugging printfs entfernt --- software/lockcontroller/bell_process.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/software/lockcontroller/bell_process.c b/software/lockcontroller/bell_process.c index 47f56b6..b5363a6 100644 --- a/software/lockcontroller/bell_process.c +++ b/software/lockcontroller/bell_process.c @@ -130,7 +130,6 @@ bool bell_isAccepted(void) --beeps; - printf("Found %d beeps.\n",beeps); if(beeps != strlen(code)) return(false); @@ -144,7 +143,6 @@ bool bell_isAccepted(void) int16_t slong = 0; int16_t sslong = 0; - printf("Trying %dth boundary %d\n",boundary, times[boundary]); for(beep = 0; beep < beeps; beep++){ if(times[beep] <= times[boundary]){ ++nshort; @@ -176,20 +174,12 @@ bool bell_isAccepted(void) for(beep=0; beep < beeps; beep++){ if(times[beep] <= best_boundary){ - if(code[beep] != '.'){ + if(code[beep] != '.') matches = false; - printf(". does not match code!\n"); - } - else - printf(". matches code\n"); } else{ - if(code[beep] != '-'){ + if(code[beep] != '-') matches = false; - printf("- does not match code!\n"); - } - else - printf("- matches code\n"); } }