Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
/*
2
 * AP_Loop.pde
3
 * Copyright (C) James Goppert 2010 <james.goppert@gmail.com>
4
 *
5
 * This file is free software: you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License as published by the
7
 * Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This file is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 * See the GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
 
19
#include "AP_Loop.h"
20
 
21
Loop::Loop(float _frequency, void (*fptr)(void *), void * data) :
22
    _fptr(fptr),
23
    _data(data),
24
    _period(1.0e6/_frequency),
25
    _subLoops(),
26
    _timeStamp(micros()),
27
    _load(0),
28
    _dt(0)
29
{
30
}
31
 
32
void Loop::update()
33
{
34
    // quick exit if not ready
35
    if (micros() - _timeStamp < _period) return;
36
 
37
    // update time stamp
38
    uint32_t timeStamp0 = _timeStamp;
39
    _timeStamp = micros();
40
    _dt = (_timeStamp - timeStamp0)/1.0e6;
41
 
42
    // update sub loops
43
    for (uint8_t i=0; i<_subLoops.getSize(); i++) _subLoops[i]->update();
44
 
45
    // callback function
46
    if (_fptr) _fptr(_data);
47
 
48
    // calculated load with a low pass filter
49
    _load = 0.9*_load + 10*(float(micros()-_timeStamp)/(_timeStamp-timeStamp0));
50
}
51
 
52
// vim:ts=4:sw=4:expandtab