Firmware
timestamped_list.h
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 
45 #pragma once
46 
47 #include <drivers/drv_hrt.h>
48 
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 
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 
101  {
102  _current_i = -1;
103  }
104 
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 
130  {
131  if (_current_i < _list_len) {
132  _list[_current_i].timestamp_us = 0;
133  }
134  }
135 
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 };
High-resolution timer with callouts and timekeeping.
Definition: timestamped_list.h:53
void reset_to_start()
Before iterating using get_next(), reset to start.
Definition: timestamped_list.h:100
void drop_current()
Disable the last item that we have gotten.
Definition: timestamped_list.h:129
T * get_next()
Iterate through all active values (not sorted).
Definition: timestamped_list.h:111
void update_current()
Update the timestamp of the item we have gotten.
Definition: timestamped_list.h:139
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
hrt_abstime hrt_absolute_time()
Get absolute time in [us] (does not wrap).
Definition: drv_hrt.cpp:155
void put(const T &new_value)
Insert a value into the list, overwrite the oldest entry if full.
Definition: timestamped_list.h:70