NXWidgets  1.19
cnxtimer.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cnxtimer.hxx
3  *
4  * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5  * Author: Gregory Nutt <gnutt@nuttx.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18  * me be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************
35  *
36  * Portions of this package derive from Woopsi (http://woopsi.org/) and
37  * portions are original efforts. It is difficult to determine at this
38  * point what parts are original efforts and which parts derive from Woopsi.
39  * However, in any event, the work of Antony Dzeryn will be acknowledged
40  * in most NxWidget files. Thanks Antony!
41  *
42  * Copyright (c) 2007-2011, Antony Dzeryn
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions are met:
47  *
48  * * Redistributions of source code must retain the above copyright
49  * notice, this list of conditions and the following disclaimer.
50  * * Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in the
52  * documentation and/or other materials provided with the distribution.
53  * * Neither the names "Woopsi", "Simian Zombie" nor the
54  * names of its contributors may be used to endorse or promote products
55  * derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
61  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  ****************************************************************************/
69 
70 #ifndef __INCLUDE_CNXTIMER_HXX
71 #define __INCLUDE_CNXTIMER_HXX
72 
73 /****************************************************************************
74  * Included Files
75  ****************************************************************************/
76 
77 #include <nuttx/config.h>
78 
79 #include <stdint.h>
80 #include <stdbool.h>
81 #include <ctime>
82 
83 #include <nuttx/wqueue.h>
84 
85 #include "cnxwidget.hxx"
86 
87 /****************************************************************************
88  * Pre-Processor Definitions
89  ****************************************************************************/
90 
91 /****************************************************************************
92  * Implementation Classes
93  ****************************************************************************/
94 
95 #if defined(__cplusplus)
96 
97 namespace NXWidgets
98 {
99  class CWidgetcontrol;
100 
101  /**
102  * Timer widget. It can drive time-based events, animations, etc.
103  *
104  * Using the timer is simple:
105  * - Create an instance of the CNxTimer and add it as a child to a widget.
106  * - Call the instance's "start()" method.
107  * - Catch the timer's action event and call any code that should run.
108  */
109 
110  class CNxTimer : public CNxWidget
111  {
112  protected:
113  struct work_s m_work; /**< Work queue entry */
114  uint32_t m_timeout; /**< The timeout value in milliseconds */
115  bool m_isRunning; /**< Indicates whether or not the timer is running */
116  bool m_isRepeater; /**< Indicates whether or not the timer repeats */
117 
118  /**
119  * Static function called from work queue when the timeout expires.
120  *
121  * @param arg Pointer to the CNxTimer instance.
122  */
123 
124  static void workQueueCallback(FAR void *arg);
125 
126  /**
127  * Copy constructor is protected to prevent usage.
128  */
129 
130  inline CNxTimer(const CNxTimer &timer) : CNxWidget(timer) { }
131 
132  public:
133 
134  /**
135  * Constructor.
136  *
137  * @param pWidgetControl The controlling widget for the display.
138  * @param timeout Time, in milliseconds, before the timer fires an
139  * EVENT_ACTION event.
140  * @param repeat If true, the timer will fire multiple events. If false,
141  * the timer will fire just once and stop.
142  */
143 
144  CNxTimer(CWidgetControl *pWidgetControl, uint32_t timeout, bool repeat);
145 
146  /**
147  * Destructor.
148  */
149 
150  ~CNxTimer(void);
151 
152  /**
153  * Resets the (running) timer to its initial timeout value. This
154  * call does nothing if the timer is not running.
155  */
156 
157  void reset(void);
158 
159  /**
160  * Starts the timer. This call does nothing if the timer is already
161  * running.
162  */
163 
164  void start(void);
165 
166  /**
167  * Stops the timer. Does nothing if the timer is not running.
168  */
169 
170  void stop(void);
171 
172  /**
173  * Returns true if the timer is currently running.
174  */
175 
176  inline bool isRunning() const { return m_isRunning; }
177 
178  /**
179  * Set the timeout of this timer. This timeout value will not
180  * take effect until start() or reset() is called.
181  *
182  * @param timeout The number of milliseconds that this timer will run
183  * before firing an event.
184  */
185 
186  inline void setTimeout(uint32_t timeout)
187  {
188  m_timeout = timeout;
189  }
190 
191  /**
192  * Return the timeout of this timer.
193  *
194  * @return The number of milliseconds that this timer will run before
195  * firing an event.
196  */
197 
198  inline uint32_t getTimeout(void) const
199  {
200  return m_timeout;
201  }
202  };
203 }
204 
205 #endif // __cplusplus
206 
207 #endif // __INCLUDE_CNXTIMER_HXX
CNxTimer(const CNxTimer &timer)
Definition: cnxtimer.hxx:130
void reset(void)
Definition: cnxtimer.cxx:139
struct work_s m_work
Definition: cnxtimer.hxx:113
bool isRunning() const
Definition: cnxtimer.hxx:176
void start(void)
Definition: cnxtimer.cxx:154
void setTimeout(uint32_t timeout)
Definition: cnxtimer.hxx:186
static void workQueueCallback(FAR void *arg)
Definition: cnxtimer.cxx:191
uint32_t getTimeout(void) const
Definition: cnxtimer.hxx:198