PX4 Firmware
PX4 Autopilot Software http://px4.io
timestamped_list.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file timestamped list.h
36  * Fixed size list with timestamps.
37  *
38  * The list has a fixed size that is set at instantiation and is based
39  * on timestamps. If a new value is put into a full list, the oldest value
40  * is overwritten.
41  *
42  * @author Julian Oes <julian@oes.ch>
43  */
44 
45 #pragma once
46 
47 #include <drivers/drv_hrt.h>
48 
49 /**
50  * @class TimestampedList
51  */
52 template <class T>
54 {
55 public:
56  TimestampedList(int num_items)
57  {
58  _list = new item_t[num_items];
59  _list_len = num_items;
60  }
61 
63  {
64  delete[] _list;
65  }
66 
67  /**
68  * Insert a value into the list, overwrite the oldest entry if full.
69  */
70  void put(const T &new_value)
71  {
73 
74  // Insert it wherever there is a free space.
75  for (int i = 0; i < _list_len; ++i) {
76  if (_list[i].timestamp_us == 0) {
77  _list[i].timestamp_us = now;
78  _list[i].value = new_value;
79  return;
80  }
81  }
82 
83  // Find oldest entry.
84  int oldest_i = 0;
85 
86  for (int i = 1; i < _list_len; ++i) {
87  if (_list[i].timestamp_us < _list[oldest_i].timestamp_us) {
88  oldest_i = i;
89  }
90  }
91 
92  // And overwrite oldest.
93  _list[oldest_i].timestamp_us = now;
94  _list[oldest_i].value = new_value;
95  }
96 
97  /**
98  * Before iterating using get_next(), reset to start.
99  */
101  {
102  _current_i = -1;
103  }
104 
105  /**
106  * Iterate through all active values (not sorted).
107  * Return nullptr if at end of list.
108  *
109  * This is basically a poor man's iterator.
110  */
111  T *get_next()
112  {
113  // Increment first, then leave it until called again.
114  ++_current_i;
115 
116  for (int i = _current_i; i < _list_len; ++i) {
117  if (_list[i].timestamp_us != 0) {
118  _current_i = i;
119  return &_list[i].value;
120  }
121  }
122 
123  return nullptr;
124  }
125 
126  /**
127  * Disable the last item that we have gotten.
128  */
130  {
131  if (_current_i < _list_len) {
133  }
134  }
135 
136  /**
137  * Update the timestamp of the item we have gotten.
138  */
140  {
141  if (_current_i < _list_len) {
142  _list[_current_i].timestamp = hrt_absolute_time();
143  }
144  }
145 
146  /* do not allow copying or assigning this class */
147  TimestampedList(const TimestampedList &) = delete;
148  TimestampedList operator=(const TimestampedList &) = delete;
149 
150 private:
151  typedef struct {
152  hrt_abstime timestamp_us = 0; // 0 signals inactive.
153  T value;
154  } item_t;
155 
156  item_t *_list = nullptr;
157  int _list_len = 0;
158  int _current_i = -1;
159 };
T * get_next()
Iterate through all active values (not sorted).
void reset_to_start()
Before iterating using get_next(), reset to start.
High-resolution timer with callouts and timekeeping.
TimestampedList(int num_items)
TimestampedList operator=(const TimestampedList &)=delete
void update_current()
Update the timestamp of the item we have gotten.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
void put(const T &new_value)
Insert a value into the list, overwrite the oldest entry if full.
void drop_current()
Disable the last item that we have gotten.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).