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