NXWidgets: cnxtimer.cxx Source File

NXWidgets 1.19

cnxtimer.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/cnxtimer.cxx
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 /****************************************************************************
71 * Included Files
72 ****************************************************************************/
73
74 #include <nuttx/config.h>
75
76 #include <stdint.h>
77 #include <stdbool.h>
78 #include <cstring>
79 #include <ctime>
80 #include <debug.h>
81 #include <errno.h>
82
83 #include <nuttx/clock.h>
84 #include <nuttx/wqueue.h>
85
86 #include "cnxtimer.hxx"
87
88 /****************************************************************************
89 * Pre-Processor Definitions
90 ****************************************************************************/
91
92 /****************************************************************************
93 * Static Data Members
94 ****************************************************************************/
95
96 using namespace NXWidgets;
97
98 /****************************************************************************
99 * Implementation Classes
100 ****************************************************************************/
101
102 /**
103 * Constructor.
104 *
105 * @param pWidgetControl The controlling widget for the display.
106 * @param timeout Time, in milliseconds, before the timer fires an
107 * EVENT_ACTION event.
108 * @param repeat If true, the timer will fire multiple events. If false,
109 * the timer will fire just once and stop.
110 */
111
112 CNxTimer::CNxTimer(CWidgetControl *pWidgetControl, uint32_t timeout, bool repeat)
113 : CNxWidget(pWidgetControl, 0, 0, 0, 0, 0, 0)
114 {
115 // Remember the timer configuration
116
117 m_timeout = timeout;
118 m_isRepeater = repeat;
119 m_isRunning = false;
120
121 // Reset the work structure
122
123 memset(&m_work, 0, sizeof(m_work));
124 }
125
126 /**
127 * Destructor.
128 */
129
130 CNxTimer::~CNxTimer(void)
131 {
132 stop();
133 }
134
135 /**
136 * Resets the millisecond timer.
137 */
138
139 void CNxTimer::reset(void)
140 {
141 // It does not make sense to reset the timer if the timer is not running
142
143 if (m_isRunning)
144 {
145 stop();
146 start();
147 }
148 }
149
150 /**
151 * Starts the timer.
152 */
153
154 void CNxTimer::start(void)
155 {
156 // If the timer is running, reset should be used to restart it
157
158 if (!m_isRunning)
159 {
160 uint32_t ticks = m_timeout / MSEC_PER_TICK;
161 int ret = work_queue(USRWORK, &m_work, workQueueCallback, this, ticks);
162
163 if (ret < 0)
164 {
165 gerr("ERROR: work_queue failed: %d\n", ret);
166 }
167
168 m_isRunning = true;
169 }
170 }
171
172 /**
173 * Stops the timer
174 */
175
176 void CNxTimer::stop(void)
177 {
178 if (m_isRunning)
179 {
180 int ret = work_cancel(USRWORK, &m_work);
181
182 if (ret < 0)
183 {
184 gerr("ERROR: work_cancel failed: %d\n", ret);
185 }
186
187 m_isRunning = false;
188 }
189 }
190
191 void CNxTimer::workQueueCallback(FAR void *arg)
192 {
193 CNxTimer* This = (CNxTimer*)arg;
194
195 This->m_isRunning = false;
196
197 // Restart the timer if this is a repeating timer
198
199 if (This->m_isRepeater)
200 {
201 This->start();
202 }
203
204 // Raise the action event.
205
206 This->m_widgetEventHandlers->raiseActionEvent();
207 }
208
209
NXWidgets::CNxTimer::CNxTimer
CNxTimer(const CNxTimer &timer)
Definition: cnxtimer.hxx:130
NXWidgets::CNxWidget
Definition: cnxwidget.hxx:121
NXWidgets::CNxTimer::reset
void reset(void)
Definition: cnxtimer.cxx:139
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
NXWidgets::CWidgetEventHandlerList::raiseActionEvent
void raiseActionEvent(void)
Definition: cwidgeteventhandlerlist.cxx:507
NXWidgets::CNxTimer::m_isRepeater
bool m_isRepeater
Definition: cnxtimer.hxx:116
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CNxTimer::m_work
struct work_s m_work
Definition: cnxtimer.hxx:113
NXWidgets::CNxTimer
Definition: cnxtimer.hxx:110
cnxtimer.hxx
NXWidgets::CNxTimer::m_timeout
uint32_t m_timeout
Definition: cnxtimer.hxx:114
NXWidgets::CNxWidget::m_widgetEventHandlers
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
NXWidgets::CNxTimer::~CNxTimer
~CNxTimer(void)
Definition: cnxtimer.cxx:130
NXWidgets::CNxTimer::stop
void stop(void)
Definition: cnxtimer.cxx:176
NXWidgets::CNxTimer::m_isRunning
bool m_isRunning
Definition: cnxtimer.hxx:115
NXWidgets::CNxTimer::start
void start(void)
Definition: cnxtimer.cxx:154
NXWidgets::CNxTimer::workQueueCallback
static void workQueueCallback(FAR void *arg)
Definition: cnxtimer.cxx:191