ramp_ctrl.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "ramp_ctrl.h"
  2. #define RAMP_INTVAL 50 //ms
  3. #define min_step 0.5f
  4. void ramp_timer_handler(shark_timer_t *timer);
  5. void ramp_ctrl_init(ramp_t *ramp){
  6. ramp_clear(ramp);
  7. ramp->timer.handler = ramp_timer_handler;
  8. }
  9. void ramp_clear(ramp_t *ramp) {
  10. shark_timer_cancel(&ramp->timer);
  11. ramp->start_point = 0;
  12. ramp->step_ms = RAMP_INTVAL;
  13. ramp->interpolation = 0;
  14. ramp->final_point = 0;
  15. ramp->duration_ms = 0;
  16. ramp->step_val = 0;
  17. }
  18. void ramp_set_points(ramp_t *ramp, float start, float target) {
  19. ramp->start_point = start;
  20. ramp->final_point = target;
  21. ramp->interpolation = target;
  22. }
  23. void ramp_set_target(ramp_t *ramp, float target) {
  24. ramp->final_point = target;
  25. }
  26. float ramp_get_target(ramp_t *ramp){
  27. return ramp->final_point;
  28. }
  29. void ramp_set_step_value(ramp_t *ramp, float step) {
  30. ramp->step_val = step;
  31. }
  32. void ramp_set_step_time(ramp_t *ramp, u32 ms) {
  33. ramp->step_ms = ms;
  34. }
  35. float ramp_get_interpolation(ramp_t *ramp) {
  36. return ramp->interpolation;
  37. }
  38. void ramp_calc_step(ramp_t *ramp) {
  39. float delta = fabs(ramp->final_point - ramp->start_point);
  40. float steps = delta/(ramp->duration_ms / ramp->step_ms);
  41. if (steps < min_step) {
  42. u32 step_ms = min_step * ramp->duration_ms / delta;
  43. ramp->step_val = min_step;
  44. ramp->step_ms = step_ms;
  45. }else {
  46. ramp->step_val = steps;
  47. }
  48. if (ramp->final_point < ramp->start_point) {
  49. ramp->step_val = - ramp->step_val;
  50. }
  51. }
  52. void ramp_set_target_duration(ramp_t *ramp, float start, float final, u32 duration_ms) {
  53. shark_timer_cancel(&ramp->timer);
  54. ramp->start_point = start;
  55. ramp->final_point = final;
  56. ramp->duration_ms = duration_ms;
  57. if (duration_ms == 0) {
  58. ramp->step_val = (final - ramp->start_point);
  59. ramp->interpolation = final;
  60. }else {
  61. ramp_calc_step(ramp);
  62. ramp_exc(ramp);
  63. }
  64. }
  65. void ramp_exc(ramp_t *ramp){
  66. if (shark_timer_stopped(&ramp->timer)) {
  67. shark_timer_post(&ramp->timer, ramp->step_ms);
  68. }
  69. }
  70. bool ramp_complete(ramp_t *ramp) {
  71. return ramp->interpolation == ramp->final_point;
  72. }
  73. void ramp_timer_handler(shark_timer_t *timer) {
  74. ramp_t *ramp = (ramp_t *)timer;
  75. float target = ramp->interpolation + ramp->step_val;
  76. if (ramp->step_val < 0) {
  77. if (target < ramp->final_point) {
  78. target = ramp->final_point;
  79. }
  80. }else {
  81. if (target > ramp->final_point) {
  82. target = ramp->final_point;
  83. }
  84. }
  85. ramp->interpolation = target;
  86. if (target != ramp->final_point) {
  87. shark_timer_post(&ramp->timer, ramp->step_ms);
  88. }else {
  89. shark_timer_cancel(&ramp->timer);
  90. }
  91. }