BeRTOS
timer_qt.c
Go to the documentation of this file.
00001 
00038 #include <cfg/compiler.h> /* hptime.t */
00039 
00040 // Qt headers
00041 #include <QtCore/QDateTime>
00042 #include <QtCore/QTimer>
00043 
00044 #if CONFIG_KERN_IRQ
00045 #include <kern/irq.h>
00046 #endif
00047 
00048 
00049 // The user interrupt server routine
00050 void timer_isr(void);
00051 
00052 
00056 class EmulTimer : public QObject
00057 {
00058 private:
00059     Q_OBJECT;
00060 
00062     QTime system_time;
00063 
00065     QTimer timer;
00066 
00071     bool initialized;
00072 
00074     EmulTimer() : initialized(false) { }
00075 
00076 public:
00078     static EmulTimer &instance()
00079     {
00080         static EmulTimer et;
00081         return et;
00082     }
00083 
00085     void init()
00086     {
00087         // Timer initialized twice?
00088         ASSERT(!initialized);
00089 
00090         // Record initial time
00091         system_time.start();
00092 
00093         #if CONFIG_KERN_IRQ
00094             irq_register(SIGALRM, timer_isr);
00095         #endif
00096 
00097         // Activate timer interrupt
00098         connect(&timer, SIGNAL(timeout()), SLOT(timerInterrupt()));
00099         timer.start(1000 / TIMER_TICKS_PER_SEC);
00100 
00101         initialized = true;
00102     }
00103 
00104     void cleanup()
00105     {
00106         // Timer cleaned twice?
00107         ASSERT(initialized);
00108 
00109         timer.stop();
00110         timer.disconnect();
00111 
00112         initialized = false;
00113     }
00114 
00116     hptime_t hpread()
00117     {
00118         ASSERT(initialized);
00119         return system_time.elapsed();
00120     }
00121 
00122 public slots:
00123     void timerInterrupt(void)
00124     {
00125         // Just call user interrupt server, timer restarts automatically.
00126         #if CONFIG_KERN_IRQ
00127             irq_entry(SIGALRM);
00128         #else
00129             timer_isr();
00130         #endif
00131     }
00132 
00133 };
00134 
00135 #include "timer_qt_moc.cpp"
00136 
00137 
00139 static void timer_hw_init(void)
00140 {
00141     EmulTimer::instance().init();
00142 }
00143 
00144 static void timer_hw_cleanup(void)
00145 {
00146     EmulTimer::instance().cleanup();
00147 }
00148 
00149 INLINE hptime_t timer_hw_hpread(void)
00150 {
00151     return EmulTimer::instance().hpread();
00152 }
00153 
00155 #define timer_hw_triggered() (true)