Qt timer emulation.
[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.1  2005/11/27 03:06:36  bernie
18  *#* Qt timer emulation.
19  *#*
20  *#*/
21
22 #include <cfg/compiler.h> /* hptime.t */
23
24 // Qt headers
25 #include <qdatetime.h>
26 #include <qtimer.h>
27
28
29 // The user interrupt server routine
30 void timer_isr(void);
31
32
33 /**
34  * Singleton class for Qt-based hardware timer emulation.
35  */
36 class EmulTimer : public QObject
37 {
38 private:
39         Q_OBJECT;
40
41         /// System timer (counts ms since application startup)
42         QTime system_time;
43
44         /// The 1ms "hardware" tick counter.
45         QTimer timer;
46
47         /**
48          * We deliberately don't use RAII because the real hardware
49          * we're simulating needs to be initialized manually.
50          */
51         bool initialized;
52
53         /// Private ctor (singleton)
54         EmulTimer() : initialized(false) { }
55
56 public:
57         /// Return singleton instance
58         static EmulTimer &instance()
59         {
60                 static EmulTimer et;
61                 return et;
62         }
63
64         /// Start timer emulator.
65         void init()
66         {
67                 // Timer initialized twice?
68                 ASSERT(!initialized);
69
70                 // Record initial time
71                 system_time.start();
72
73                 // Activate 1ms timer interrupt
74                 timer.connect(&timer, SIGNAL(timeout()), this, SLOT(timerInterrupt()));
75                 timer.start(1);
76
77                 initialized = true;
78         }
79
80         /// Return current time in high-precision format.
81         hptime_t hpread()
82         {
83                 ASSERT(initialized);
84                 return system_time.elapsed();
85         }
86
87 public slots:
88         void timerInterrupt(void)
89         {
90                 // Just call user interrupt server, timer restarts automatically.
91                 timer_isr();
92         }
93
94 };
95
96 #include "timer_qt_moc.cpp"
97
98
99 /// HW dependent timer initialization.
100 extern "C" static void timer_hw_init(void)
101 {
102         // Kick EmulTimer initialization
103         EmulTimer::instance().init();
104 }
105
106 extern "C" INLINE hptime_t timer_hw_hpread(void)
107 {
108         return EmulTimer::instance().hpread();
109 }
110