Drop almost all the Qt3 cruft.
[bertos.git] / drv / timer_qt.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  *
12  * \brief Low-level timer module for Qt emulator (implementation).
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.4  2006/05/28 12:17:57  bernie
18  *#* Drop almost all the Qt3 cruft.
19  *#*
20  *#* Revision 1.3  2006/02/21 21:28:02  bernie
21  *#* New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks longer than 1ms.
22  *#*
23  *#* Revision 1.2  2006/02/20 02:01:35  bernie
24  *#* Port to Qt 4.1.
25  *#*
26  *#* Revision 1.1  2005/11/27 03:06:36  bernie
27  *#* Qt timer emulation.
28  *#*
29  *#*/
30
31 #include <cfg/compiler.h> /* hptime.t */
32
33 // Qt headers
34 #include <QtCore/QDateTime>
35 #include <QtCore/QTimer>
36
37
38 // The user interrupt server routine
39 void timer_isr(void);
40
41
42 /**
43  * Singleton class for Qt-based hardware timer emulation.
44  */
45 class EmulTimer : public QObject
46 {
47 private:
48         Q_OBJECT;
49
50         /// System timer (counts ms since application startup)
51         QTime system_time;
52
53         /// The 1ms "hardware" tick counter.
54         QTimer timer;
55
56         /**
57          * We deliberately don't use RAII because the real hardware
58          * we're simulating needs to be initialized manually.
59          */
60         bool initialized;
61
62         /// Private ctor (singleton)
63         EmulTimer() : initialized(false) { }
64
65 public:
66         /// Return singleton instance
67         static EmulTimer &instance()
68         {
69                 static EmulTimer et;
70                 return et;
71         }
72
73         /// Start timer emulator.
74         void init()
75         {
76                 // Timer initialized twice?
77                 ASSERT(!initialized);
78
79                 // Record initial time
80                 system_time.start();
81
82                 // Activate timer interrupt
83                 timer.connect(&timer, SIGNAL(timeout()), this, SLOT(timerInterrupt()));
84                 timer.start(1000 / TIMER_TICKS_PER_SEC);
85
86                 initialized = true;
87         }
88
89         /// Return current time in high-precision format.
90         hptime_t hpread()
91         {
92                 ASSERT(initialized);
93                 return system_time.elapsed();
94         }
95
96 public slots:
97         void timerInterrupt(void)
98         {
99                 // Just call user interrupt server, timer restarts automatically.
100                 timer_isr();
101         }
102
103 };
104
105 #include "timer_qt_moc.cpp"
106
107
108 /// HW dependent timer initialization.
109 extern "C" static void timer_hw_init(void)
110 {
111         // Kick EmulTimer initialization
112         EmulTimer::instance().init();
113 }
114
115 extern "C" INLINE hptime_t timer_hw_hpread(void)
116 {
117         return EmulTimer::instance().hpread();
118 }
119