-
Notifications
You must be signed in to change notification settings - Fork 2
/
fps.c
257 lines (211 loc) · 6.08 KB
/
fps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* FPS heurestics based on light sensor
*
* Copyright 2013 Tuomas Kulve, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "common.h"
#include "fps.h"
#include "utils.h"
#include <stdint.h>
#define FPS_PREV_FRAMES 100
#define FPS_STATE_INIT 0
#define FPS_STATE_HIGH 1
#define FPS_STATE_LOW 2
static uint32_t frames[FPS_PREV_FRAMES] = { 0 };
static uint16_t frame_i = 0;
static uint8_t state = FPS_STATE_INIT;
static uint16_t high = 0;
static uint16_t low = 0xffff;
static uint16_t new_high = 0;
static uint16_t new_low = 0xffff;
/*
* Takes brightness and timestamp as parameters and returns FPS (and limits) on a new frame
*/
uint8_t handle_adc(uint16_t adc, uint32_t timestamp_ms, uint8_t *fps, uint16_t *low_ret, uint16_t *low_limit, uint16_t *high_limit, uint16_t *high_ret)
{
uint16_t quarter_range;
uint16_t i;
uint8_t is_new_frame = 0;
*fps = 0;
// FIXME: Ignore zero adc for now.
if (adc == 0) {
return 0;
}
// Gather initial statistics
if (state == FPS_STATE_INIT && timestamp_ms < 1000) {
if (adc > high) {
high = adc;
}
if (adc < low) {
low = adc;
}
return 0;
}
// High limit = 75% of the range
// Low limit = 25% of the range
quarter_range = (high - low) >> 2;
*high_limit = high - quarter_range;
*low_limit = low + quarter_range;
// Check if we are in a known state after initialisation
if (state == FPS_STATE_INIT) {
if (adc < *low_limit) {
state = FPS_STATE_LOW;
}
if (adc > *high_limit) {
state = FPS_STATE_HIGH;
}
// Even if we are now in a valid state, we have can have a change
// next time earliest.
return 0;
}
if (state == FPS_STATE_LOW) {
// Frame has changed if we are in a low state and adc is high
if (adc > *high_limit) {
state = FPS_STATE_HIGH;
is_new_frame = 1;
if (new_low < *high_limit) {
uint16_t change;
uint8_t negative = 0;
// Adjust low value 1/4 towards the latest low value
if (new_low < low) {
negative = 1;
change = low - new_low;
} else {
change = new_low - low;
}
change >>= 2;
// Make sure low doesn't overflow
if (negative) {
if (change < low) {
low -= change;
}
} else {
// FIXME: is the following temporary register 32bit?
if (low + change < 0xffff) {
low += change;
}
}
// Reset low for the next low state
new_low = 0xffff;
}
} else {
// Get the most lowest value during this low state
if (adc < new_low) {
new_low = adc;
}
}
}
if (state == FPS_STATE_HIGH) {
// Frame has changed if we are in a high state and adc is low
if (adc < *low_limit) {
state = FPS_STATE_LOW;
is_new_frame = 1;
if (new_high > *low_limit) {
uint16_t change;
uint8_t negative = 0;
// Adjust high value 1/4 towards the latest high value
if (new_high < high) {
negative = 1;
change = high - new_high;
} else {
change = new_high - high;
}
change >>= 2;
// Make sure high doesn't overflow
if (negative) {
if (change < high) {
high -= change;
}
} else {
// FIXME: is the following temporary register 32bit?
if (high + change < 0xffff) {
high += change;
}
}
// Reset high for the next high state
new_high = 0;
}
} else {
// Get the most highest value during this high state
if (adc > new_high) {
new_high = adc;
}
}
}
if (!is_new_frame) {
return 0;
}
frames[frame_i] = timestamp_ms;
// Count the amount of new frames during last 1000 ms
for (i = 0; i < FPS_PREV_FRAMES; ++i) {
if (timestamp_ms - frames[i] < 1000) {
++*fps;
}
}
if (++frame_i == FPS_PREV_FRAMES) {
frame_i = 0;
}
*low_ret = low;
*high_ret = high;
return 1;
}
/*
* Construct a message and send it over the UART
*/
uint8_t create_message(uint8_t *buf,
uint8_t max_len,
uint32_t timestamp_ms,
uint8_t fps,
uint16_t missed_adc,
uint16_t frame_len,
uint16_t low,
uint16_t low_limit,
uint16_t high_limit,
uint16_t high)
{
uint8_t len = 0;
// FIXME: all this will fail is buf is too small
#define BUF_APPEND(a) len += sc_itoa(a, &buf[len], max_len - len); buf[len++] = ',';
BUF_APPEND(timestamp_ms);
BUF_APPEND(fps);
BUF_APPEND(missed_adc);
BUF_APPEND(frame_len);
BUF_APPEND(low);
BUF_APPEND(low_limit);
BUF_APPEND(high_limit);
BUF_APPEND(high);
#undef BUF_APPEND
// Overwrite the last ,
buf[len-1] = '\r';
buf[len++] = '\n';
return len;
}
/* Emacs indentatation information
Local Variables:
indent-tabs-mode:nil
tab-width:2
c-basic-offset:2
End:
*/