00001 /*------------------------------------------------------------------------ 00002 *--------------------- RT-WMP -------------------- 00003 *------------------------------------------------------------------------ 00004 * V7.0B 11/05/10 00005 * 00006 * 00007 * File: ./src/core/MobileAverage.c 00008 * Authors: Danilo Tardioli 00009 * ---------------------------------------------------------------------- 00010 * Copyright (C) 2000-2010, Universidad de Zaragoza, SPAIN 00011 * 00012 * Contact Addresses: Danilo Tardioli dantard@unizar.es 00013 * 00014 * RT-WMP is free software; you can redistribute it and/or modify it 00015 * under the terms of the GNU General Public License as published by the 00016 * Free Software Foundation; either version 2, or (at your option) any 00017 * later version. 00018 * 00019 * RT-WMP is distributed in the hope that it will be useful, but 00020 * WITHOUT ANY WARRANTY; without even the implied warranty of 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 * General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU General Public License 00025 * distributed with RT-WMP; see file COPYING. If not, write to the 00026 * Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 00027 * 02111-1307, USA. 00028 * 00029 * As a special exception, if you link this unit with other files to 00030 * produce an executable, this unit does not by itself cause the 00031 * resulting executable to be covered by the GNU General Public License. 00032 * This exception does not however invalidate any other reasons why the 00033 * executable file might be covered by the GNU Public License. 00034 * 00035 *----------------------------------------------------------------------*/ 00036 00037 #include "include/MobileAverage.h" 00038 #include "include/definitions.h" 00039 #include "config/compiler.h" 00040 00041 extern Status status; 00042 00043 void mobile_avg_free(MobileAverage * e){ 00044 FREE(e->elem); 00045 } 00046 void mobile_avg_init(MobileAverage * e, int n_elements){ 00047 e->elem=(char *) MALLOC(n_elements*sizeof(char)); 00048 e->n_elements=n_elements; 00049 e->seen=0; 00050 e->idx=0; 00051 e->initialized=0; 00052 e->avgd_value=0; /* or 0 better */ 00053 }; 00054 00055 void mobile_avg_new_value(MobileAverage*e, char val){ 00056 int i; 00057 int avg = 0; 00058 e->seen=getRawActualTimeus(); 00059 if (!e->initialized){ 00060 int end = (e->n_elements/status.rssi_rising_factor); 00061 for (i=0 ; i < end ; i++){ 00062 e->elem[i]=val; 00063 e->idx++; 00064 } 00065 e->avgd_value=val; 00066 e->initialized=1; 00067 } else { 00068 e->idx++; 00069 if (e->idx>=e->n_elements) e->idx=0; 00070 e->elem[e->idx]=val; 00071 for (i=0;i<e->n_elements;i++){ 00072 avg+=(e->elem[i]); 00073 } 00074 avg=avg/e->n_elements; 00075 e->avgd_value= (char) avg; 00076 } 00077 }; 00078 00079 void mobile_avg_reset(MobileAverage* e) { 00080 //XXX 00081 e->initialized = 0; 00082 e->avgd_value = 0; 00083 }; 00084 00085 00086 char mobile_avg_get_averaged_value(MobileAverage * e){ 00087 return e->avgd_value; 00088 }; 00089 00090 unsigned long mobile_avg_get_age(MobileAverage * e){ 00091 return (DO_DIV64(getRawActualTimeus(),1000) - DO_DIV64(e->seen,1000)) ; 00092 }; 00093 00094 00095 00096 00097