summaryrefslogtreecommitdiffstats
path: root/c/src/librtems++/include/rtems++/rtemsTimer.h
blob: 0d432a495727e30dfdc82e350391e203dbcd6125 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
  ------------------------------------------------------------------------
  $Id$
  ------------------------------------------------------------------------

  COPYRIGHT (c) 1997
  Objective Design Systems Ltd Pty (ODS)
  All rights reserved (R) Objective Design Systems Ltd Pty
  
  The license and distribution terms for this file may be found in the
  file LICENSE in this distribution or at
  http://www.rtems.com/license/LICENSE.

  ------------------------------------------------------------------------

  rtemsTimer class.

  This class allows the user to create a RTEMS timer.

  The trigger method is called when the timer expires. The method is
  called using the thread which calls the RTEMS 'rtems_clock_tick'
  method.

  Timers are always local to a node.
  
  ------------------------------------------------------------------------ */

#if !defined(_rtemsTimer_h_)
#define _rtemsTimer_h_

#include <rtems++/rtemsStatusCode.h>

/* ----
    rtemsTimer
*/

class rtemsTimer
  : public rtemsStatusCode
{
public:
  // only the first 4 characters of the name are taken,

  // create a timer object
  rtemsTimer(const char* name);
  rtemsTimer();
  
  // destroies the actual object
  virtual ~rtemsTimer();

  // create or destroy (delete) the timer
  virtual const rtems_status_code create(const char* name);
  virtual const rtems_status_code destroy();
  
  // timer control
  inline const rtems_status_code fire_after(const rtems_interval micro_secs);
  inline const rtems_status_code repeat_fire_at(const rtems_interval micro_secs);
  inline const rtems_status_code fire_when(const rtems_time_of_day& when);
  
  inline const rtems_status_code cancel();
  inline const rtems_status_code reset();
    
  // object id, and name
  const rtems_id id_is() const { return id; }
  const rtems_name name_is() const { return name; }
  const char *name_string() const { return name_str; }
  
protected:

  // triggered method is called when the timer fires
  virtual void triggered() = 0;
  
private:
  // not permitted
  rtemsTimer(const rtemsTimer& timer);
  rtemsTimer& operator=(const rtemsTimer& timer);

  // make this object reference an invalid RTEMS object
  void make_invalid();
  
  // semaphore name
  rtems_name name;
  char name_str[5];

  // repeat true restart the timer when it fires
  bool repeat;

  // the rtems id, object handle
  rtems_id id;

  // common timer handler
  static void common_handler(rtems_id id, void *user_data);
};

const rtems_status_code rtemsTimer::fire_after(const rtems_interval micro_secs)
{
  repeat = false;
  rtems_interval usecs =
    micro_secs && (micro_secs < _TOD_Microseconds_per_tick) ?
    _TOD_Microseconds_per_tick : micro_secs;
  return set_status_code(rtems_timer_fire_after(id,
                                                TOD_MICROSECONDS_TO_TICKS(usecs),
                                                common_handler,
                                                this));
}

const rtems_status_code rtemsTimer::repeat_fire_at(const rtems_interval micro_secs)
{
  repeat = true;
  rtems_interval usecs =
    micro_secs && (micro_secs < _TOD_Microseconds_per_tick) ?
    _TOD_Microseconds_per_tick : micro_secs;
  return set_status_code(rtems_timer_fire_after(id,
                                                TOD_MICROSECONDS_TO_TICKS(usecs),
                                                common_handler,
                                                this));
}

const rtems_status_code rtemsTimer::fire_when(const rtems_time_of_day& when)
{
  return set_status_code(rtems_timer_fire_when(id,
                                               (rtems_time_of_day*) &when,
                                               common_handler,
                                               this));
}

const rtems_status_code rtemsTimer::cancel()
{
  repeat = false;
  return set_status_code(rtems_timer_cancel(id));
}

const rtems_status_code rtemsTimer::reset()
{ 
  return set_status_code(rtems_timer_reset(id));
}

#endif  // _rtemsTimer_h_