experiments with Pylontech/GroWatt PV tech
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.4 KiB

  1. /*
  2. * SimpleTimer.h
  3. *
  4. * SimpleTimer - A timer library for Arduino.
  5. * Author: mromani@ottotecnica.com
  6. * Copyright (c) 2010 OTTOTECNICA Italy
  7. *
  8. * This library is free software; you can redistribute it
  9. * and/or modify it under the terms of the GNU Lesser
  10. * General Public License as published by the Free Software
  11. * Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will
  15. * be useful, but WITHOUT ANY WARRANTY; without even the
  16. * implied warranty of MERCHANTABILITY or FITNESS FOR A
  17. * PARTICULAR PURPOSE. See the GNU Lesser General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser
  21. * General Public License along with this library; if not,
  22. * write to the Free Software Foundation, Inc.,
  23. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. */
  26. #ifndef SIMPLETIMER_H
  27. #define SIMPLETIMER_H
  28. #if defined(ARDUINO) && ARDUINO >= 100
  29. #include <Arduino.h>
  30. #else
  31. #include <WProgram.h>
  32. #endif
  33. typedef void (*timer_callback)(void);
  34. class SimpleTimer {
  35. public:
  36. // maximum number of timers
  37. const static int MAX_TIMERS = 10;
  38. // setTimer() constants
  39. const static int RUN_FOREVER = 0;
  40. const static int RUN_ONCE = 1;
  41. // constructor
  42. SimpleTimer();
  43. void init();
  44. // this function must be called inside loop()
  45. void run();
  46. // call function f every d milliseconds
  47. int setInterval(long d, timer_callback f);
  48. // call function f once after d milliseconds
  49. int setTimeout(long d, timer_callback f);
  50. // call function f every d milliseconds for n times
  51. int setTimer(long d, timer_callback f, int n);
  52. // destroy the specified timer
  53. void deleteTimer(int numTimer);
  54. // restart the specified timer
  55. void restartTimer(int numTimer);
  56. // returns true if the specified timer is enabled
  57. boolean isEnabled(int numTimer);
  58. // enables the specified timer
  59. void enable(int numTimer);
  60. // disables the specified timer
  61. void disable(int numTimer);
  62. // enables the specified timer if it's currently disabled,
  63. // and vice-versa
  64. void toggle(int numTimer);
  65. // returns the number of used timers
  66. int getNumTimers();
  67. // returns the number of available timers
  68. int getNumAvailableTimers() { return MAX_TIMERS - numTimers; };
  69. private:
  70. // deferred call constants
  71. const static int DEFCALL_DONTRUN = 0; // don't call the callback function
  72. const static int DEFCALL_RUNONLY = 1; // call the callback function but don't delete the timer
  73. const static int DEFCALL_RUNANDDEL = 2; // call the callback function and delete the timer
  74. // find the first available slot
  75. int findFirstFreeSlot();
  76. // value returned by the millis() function
  77. // in the previous run() call
  78. unsigned long prev_millis[MAX_TIMERS];
  79. // pointers to the callback functions
  80. timer_callback callbacks[MAX_TIMERS];
  81. // delay values
  82. long delays[MAX_TIMERS];
  83. // number of runs to be executed for each timer
  84. int maxNumRuns[MAX_TIMERS];
  85. // number of executed runs for each timer
  86. int numRuns[MAX_TIMERS];
  87. // which timers are enabled
  88. boolean enabled[MAX_TIMERS];
  89. // deferred function call (sort of) - N.B.: this array is only used in run()
  90. int toBeCalled[MAX_TIMERS];
  91. // actual number of timers in use
  92. int numTimers;
  93. };
  94. #endif