buzzer.c

Go to the documentation of this file.
00001 
00043 #include "buzzer.h"
00044 
00045 #include "hw/hw_buzzer.h"
00046 #include <drv/timer.h>
00047 
00048 #include <mware/event.h>
00049 
00050 #include <cfg/debug.h>
00051 #include <cfg/module.h>
00052 
00053 
00054 /* Local vars */
00055 static Timer buz_timer;
00056 static bool buz_timer_running;
00057 static mtime_t buz_repeat_interval;
00058 static mtime_t buz_repeat_duration;
00059 
00060 
00064 static void buz_softint(void)
00065 {
00066     if (IS_BUZZER_ON)
00067     {
00068         BUZZER_OFF;
00069         if (buz_repeat_interval)
00070         {
00071             /* Wait for interval time */
00072             timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_interval));
00073             timer_add(&buz_timer);
00074         }
00075         else
00076             buz_timer_running = false;
00077     }
00078     else if (buz_repeat_interval)
00079     {
00080         /* Wait for beep time */
00081         BUZZER_ON;
00082         timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_duration));
00083         timer_add(&buz_timer);
00084     }
00085     else
00086         buz_timer_running = false;
00087 }
00088 
00089 
00093 void buz_beep(mtime_t time)
00094 {
00095     cpu_flags_t flags;
00096     IRQ_SAVE_DISABLE(flags);
00097 
00098     /* Remove the software interrupt if it was already queued */
00099     if (buz_timer_running)
00100         timer_abort(&buz_timer);
00101 
00102     /* Turn on buzzer */
00103     BUZZER_ON;
00104 
00105     /* Add software interrupt to turn the buzzer off later */
00106     buz_timer_running = true;
00107     timer_setDelay(&buz_timer, ms_to_ticks(time));
00108     timer_add(&buz_timer);
00109 
00110     IRQ_RESTORE(flags);
00111 }
00112 
00113 
00117 void buz_repeat_start(mtime_t duration, mtime_t interval)
00118 {
00119     buz_repeat_interval = interval;
00120     buz_repeat_duration = duration;
00121     buz_beep(duration);
00122 }
00123 
00124 
00128 void buz_repeat_stop(void)
00129 {
00130     cpu_flags_t flags;
00131     IRQ_SAVE_DISABLE(flags);
00132 
00133     /* Remove the software interrupt if it was already queued */
00134     if (buz_timer_running)
00135     {
00136         timer_abort(&buz_timer);
00137         buz_timer_running = false;
00138     }
00139 
00140     buz_repeat_interval = 0;
00141     BUZZER_OFF;
00142 
00143     IRQ_RESTORE(flags);
00144 }
00145 
00146 MOD_DEFINE(buzzer)
00147 
00148 
00151 void buz_init(void)
00152 {
00153     MOD_CHECK(timer);
00154 
00155     BUZZER_HW_INIT;
00156 
00157     /* Init software interrupt. */
00158     timer_setSoftint(&buz_timer, (Hook)buz_softint, 0);
00159 
00160     MOD_INIT(buzzer);
00161 }