ui_math.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2019 Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file ui_math.c
  8. */
  9. #ifdef CONFIG_LVGL
  10. # include <lvgl/lvgl.h> /* reuse the math function of LVGL */
  11. #endif
  12. #include <ui_math.h>
  13. int32_t ui_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min_out, int32_t max_out)
  14. {
  15. #ifdef CONFIG_LVGL
  16. return lv_map(x, min_in, max_in, min_out, max_out);
  17. #else
  18. if (x >= max_in) return max_out;
  19. if (x <= min_in) return min_out;
  20. /**
  21. * The equation should be:
  22. * ((x - min_in) * delta_out) / delta in) + min_out
  23. * To avoid rounding error reorder the operations:
  24. * (x - min_in) * (delta_out / delta_min) + min_out
  25. */
  26. int32_t delta_in = max_in - min_in;
  27. int32_t delta_out = max_out - min_out;
  28. return ((x - min_in) * delta_out) / delta_in + min_out;
  29. #endif
  30. }
  31. uint32_t ui_bezier3(uint32_t t, uint32_t u0, uint32_t u1, uint32_t u2, uint32_t u3)
  32. {
  33. #ifdef CONFIG_LVGL
  34. /* must make sure UI_BEZIER_VAL_MAX is equal to LV_BEZIER_VAL_MAX */
  35. return lv_bezier3(t, u0, u1, u2, u3);
  36. #else
  37. uint32_t t_rem = 1024 - t;
  38. uint32_t t_rem2 = (t_rem * t_rem) >> 10;
  39. uint32_t t_rem3 = (t_rem2 * t_rem) >> 10;
  40. uint32_t t2 = (t * t) >> 10;
  41. uint32_t t3 = (t2 * t) >> 10;
  42. uint32_t v1 = (t_rem3 * u0) >> 10;
  43. uint32_t v2 = (3 * t_rem2 * t * u1) >> 20;
  44. uint32_t v3 = (3 * t_rem * t2 * u2) >> 20;
  45. uint32_t v4 = (t3 * u3) >> 10;
  46. return v1 + v2 + v3 + v4;
  47. #endif
  48. }