Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_ramp.c
1 #include <bc_ramp.h>
2 
3 static float _bc_ramp_interpolate(bc_tick_t x, bc_tick_t x_min, bc_tick_t x_max, float y_min, float y_max);
4 
5 void bc_ramp_init(bc_ramp_t *self, float start, float stop, bc_tick_t duration)
6 {
7  memset(self, 0, sizeof(*self));
8 
9  self->_start = start;
10  self->_stop = stop;
11  self->_now = start;
12 
13  self->_duration = duration;
14 }
15 
16 void bc_ramp_start(bc_ramp_t *self)
17 {
18  self->_tick_start = bc_tick_get();
19  self->_tick_end = self->_tick_start + self->_duration;
20 
21  self->_active = true;
22 }
23 
24 float bc_ramp_get(bc_ramp_t *self)
25 {
26  if (bc_tick_get() >= self->_tick_end)
27  {
28  self->_active = false;
29  }
30 
31  if (!self->_active)
32  {
33  return self->_stop;
34  }
35 
36  return _bc_ramp_interpolate(bc_tick_get(), self->_tick_start, self->_tick_end, self->_start, self->_stop);
37 }
38 
39 static float _bc_ramp_interpolate(bc_tick_t x, bc_tick_t x_min, bc_tick_t x_max, float y_min, float y_max)
40 {
41  if (x < x_min) { x = x_min; }
42  if (x > x_max) { x = x_max; }
43 
44  return (float) (x - x_min) * (y_max - y_min) / (float) (x_max - x_min) + y_min;
45 }
uint64_t bc_tick_t
Timestamp data type.
Definition: bc_tick.h:16
void bc_ramp_init(bc_ramp_t *self, float start, float stop, bc_tick_t duration)
Initialize ramp instance.
Definition: bc_ramp.c:5
bc_tick_t bc_tick_get(void)
Get absolute timestamp since start of program.
Definition: bc_tick.c:7
void bc_ramp_start(bc_ramp_t *self)
Start ramp sequence.
Definition: bc_ramp.c:16
float bc_ramp_get(bc_ramp_t *self)
Get current ramp value.
Definition: bc_ramp.c:24