lineramp.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef _LINE_RAMP_H__
  2. #define _LINE_RAMP_H__
  3. typedef struct {
  4. float target;
  5. float interpolation;
  6. float step;
  7. float time;
  8. float time_dec;
  9. float min_step;
  10. }lineramp_t;
  11. static __INLINE void line_ramp_set_target(lineramp_t *line, float target) {
  12. if (line->target != target) {
  13. line->target = target;
  14. float delta = (line->target - line->interpolation);
  15. if (delta >= 0) {
  16. line->step = delta / line->time;
  17. }else {
  18. line->step = -delta / line->time_dec;
  19. }
  20. if ((line->min_step > 0) && (line->step < line->min_step)) {
  21. line->step = line->min_step;
  22. }
  23. }
  24. }
  25. static __INLINE void line_ramp_init(lineramp_t *line, float time) {
  26. line->target = 0;
  27. line->interpolation = 0;
  28. line->step = 0;
  29. line->min_step = 0;
  30. line->time_dec = line->time = time;
  31. }
  32. static __INLINE void line_ramp_reset(lineramp_t *line, float target) {
  33. line->target = target;
  34. line->interpolation = target;
  35. }
  36. static __INLINE void line_ramp_set_time(lineramp_t *line, float time) {
  37. line->time_dec = line->time = time;
  38. }
  39. static __INLINE void line_ramp_set_dectime(lineramp_t *line, float time) {
  40. line->time_dec = time;
  41. }
  42. static __INLINE void line_ramp_set_acctime(lineramp_t *line, float time) {
  43. line->time = time;
  44. }
  45. static __INLINE void line_ramp_set_minstep(lineramp_t *line, float step) {
  46. line->min_step = step;
  47. }
  48. static __INLINE float line_ramp_get_target(lineramp_t *line) {
  49. return line->target;
  50. }
  51. static __INLINE float line_ramp_get_interp(lineramp_t *line) {
  52. return line->interpolation;
  53. }
  54. static __INLINE void line_ramp_update(lineramp_t *line) {
  55. float delta = (line->target - line->interpolation);
  56. if (delta >= 0) {
  57. line->step = delta / line->time;
  58. }else {
  59. line->step = -delta / line->time_dec;
  60. }
  61. }
  62. static __INLINE float line_ramp_step(lineramp_t *line) {
  63. step_towards(&line->interpolation, line->target, line->step);
  64. return line->interpolation;
  65. }
  66. #endif /* _LINE_RAMP_H__ */