Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

timer.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * \addtogroup timer
  3. * @{
  4. */
  5. /**
  6. * \file
  7. * Timer library implementation.
  8. * \author
  9. * Adam Dunkels <[email protected]>
  10. */
  11. /*
  12. * Copyright (c) 2004, Swedish Institute of Computer Science.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. Neither the name of the Institute nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. *
  39. * This file is part of the uIP TCP/IP stack
  40. *
  41. * Author: Adam Dunkels <[email protected]>
  42. *
  43. * $Id: timer.c,v 1.2 2006/06/12 08:00:30 adam Exp $
  44. */
  45. #include "clock.h"
  46. #include "timer.h"
  47. /*---------------------------------------------------------------------------*/
  48. /**
  49. * Set a timer.
  50. *
  51. * This function is used to set a timer for a time sometime in the
  52. * future. The function timer_expired() will evaluate to true after
  53. * the timer has expired.
  54. *
  55. * \param t A pointer to the timer
  56. * \param interval The interval before the timer expires.
  57. *
  58. */
  59. void
  60. timer_set(struct timer *t, clock_time_t interval)
  61. {
  62. t->interval = interval;
  63. t->start = clock_time();
  64. }
  65. /*---------------------------------------------------------------------------*/
  66. /**
  67. * Reset the timer with the same interval.
  68. *
  69. * This function resets the timer with the same interval that was
  70. * given to the timer_set() function. The start point of the interval
  71. * is the exact time that the timer last expired. Therefore, this
  72. * function will cause the timer to be stable over time, unlike the
  73. * timer_restart() function.
  74. *
  75. * \param t A pointer to the timer.
  76. *
  77. * \sa timer_restart()
  78. */
  79. void
  80. timer_reset(struct timer *t)
  81. {
  82. t->start += t->interval;
  83. }
  84. /*---------------------------------------------------------------------------*/
  85. /**
  86. * Restart the timer from the current point in time
  87. *
  88. * This function restarts a timer with the same interval that was
  89. * given to the timer_set() function. The timer will start at the
  90. * current time.
  91. *
  92. * \note A periodic timer will drift if this function is used to reset
  93. * it. For periodic timers, use the timer_reset() function instead.
  94. *
  95. * \param t A pointer to the timer.
  96. *
  97. * \sa timer_reset()
  98. */
  99. void
  100. timer_restart(struct timer *t)
  101. {
  102. t->start = clock_time();
  103. }
  104. /*---------------------------------------------------------------------------*/
  105. /**
  106. * Check if a timer has expired.
  107. *
  108. * This function tests if a timer has expired and returns true or
  109. * false depending on its status.
  110. *
  111. * \param t A pointer to the timer
  112. *
  113. * \return Non-zero if the timer has expired, zero otherwise.
  114. *
  115. */
  116. int
  117. timer_expired(struct timer *t)
  118. {
  119. return (clock_time_t)(clock_time() - t->start) >= (clock_time_t)t->interval;
  120. }
  121. /*---------------------------------------------------------------------------*/
  122. /** @} */