ramp_ctrl.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "ramp_ctrl.h"
  2. #define RAMP_INTVAL 5 //ms
  3. static void ramp_timer_handler(timer_t *timer);
  4. void ramp_ctrl_init(ramp_t *ramp, float start, float final, u32 duration_ms){
  5. ramp->start_point = start;
  6. ramp->target = start;
  7. ramp->final_point = final;
  8. ramp->duration_ms = duration_ms;
  9. ramp->steps = (final - start) / (duration_ms / RAMP_INTVAL);
  10. if (ramp->timer.handler != NULL) {
  11. timer_cancel(&ramp->timer);
  12. }
  13. ramp->timer.handler = NULL;
  14. }
  15. void ramp_clear(ramp_t *ramp) {
  16. ramp_ctrl_init(ramp, ramp->start_point, ramp->final_point, ramp->duration_ms);
  17. }
  18. void ramp_exc(ramp_t *ramp){
  19. if (ramp->timer.handler == NULL) {
  20. ramp->timer.handler = ramp_timer_handler;
  21. timer_post(&ramp->timer, RAMP_INTVAL);
  22. }
  23. }
  24. float ramp_get_target(ramp_t *ramp){
  25. return ramp->target;
  26. }
  27. bool ramp_complete(ramp_t *ramp) {
  28. return ramp->target == ramp->final_point;
  29. }
  30. static void ramp_timer_handler(timer_t *timer) {
  31. ramp_t *ramp = (ramp_t *)timer;
  32. float target = ramp->target + ramp->steps;
  33. if (target > ramp->final_point) {
  34. target = ramp->final_point;
  35. }
  36. ramp->target = target;
  37. if (target != ramp->final_point) {
  38. timer_post(&ramp->timer, RAMP_INTVAL);
  39. }
  40. }