sensorsim.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "sensorsim.h"
  13. void sensorsim_init(sensorsim_state_t * p_state,
  14. const sensorsim_cfg_t * p_cfg)
  15. {
  16. if (p_cfg->start_at_max)
  17. {
  18. p_state->current_val = p_cfg->max;
  19. p_state->is_increasing = false;
  20. }
  21. else
  22. {
  23. p_state->current_val = p_cfg->min;
  24. p_state->is_increasing = true;
  25. }
  26. }
  27. uint32_t sensorsim_measure(sensorsim_state_t * p_state,
  28. const sensorsim_cfg_t * p_cfg)
  29. {
  30. if (p_state->is_increasing)
  31. {
  32. sensorsim_increment(p_state, p_cfg);
  33. }
  34. else
  35. {
  36. sensorsim_decrement(p_state, p_cfg);
  37. }
  38. return p_state->current_val;
  39. }
  40. void sensorsim_increment(sensorsim_state_t * p_state,
  41. const sensorsim_cfg_t * p_cfg)
  42. {
  43. if (p_cfg->max - p_state->current_val > p_cfg->incr)
  44. {
  45. p_state->current_val += p_cfg->incr;
  46. }
  47. else
  48. {
  49. p_state->current_val = p_cfg->max;
  50. p_state->is_increasing = false;
  51. }
  52. }
  53. void sensorsim_decrement(sensorsim_state_t * p_state,
  54. const sensorsim_cfg_t * p_cfg)
  55. {
  56. if (p_state->current_val - p_cfg->min > p_cfg->incr)
  57. {
  58. p_state->current_val -= p_cfg->incr;
  59. }
  60. else
  61. {
  62. p_state->current_val = p_cfg->min;
  63. p_state->is_increasing = true;
  64. }
  65. }