buzzer.c
Go to the documentation of this file.00001
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 #include "buzzer.h"
00080
00081 #include <hw_buzzer.h>
00082 #include <drv/timer.h>
00083
00084 #include <mware/event.h>
00085
00086 #include <cfg/debug.h>
00087 #include <cfg/module.h>
00088
00089
00090
00091 static Timer buz_timer;
00092 static bool buz_timer_running;
00093 static mtime_t buz_repeat_interval;
00094 static mtime_t buz_repeat_duration;
00095
00096
00100 static void buz_softint(void)
00101 {
00102 if (IS_BUZZER_ON)
00103 {
00104 BUZZER_OFF;
00105 if (buz_repeat_interval)
00106 {
00107
00108 timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_interval));
00109 timer_add(&buz_timer);
00110 }
00111 else
00112 buz_timer_running = false;
00113 }
00114 else if (buz_repeat_interval)
00115 {
00116
00117 BUZZER_ON;
00118 timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_duration));
00119 timer_add(&buz_timer);
00120 }
00121 else
00122 buz_timer_running = false;
00123 }
00124
00125
00129 void buz_beep(mtime_t time)
00130 {
00131 cpuflags_t flags;
00132 IRQ_SAVE_DISABLE(flags);
00133
00134
00135 if (buz_timer_running)
00136 timer_abort(&buz_timer);
00137
00138
00139 BUZZER_ON;
00140
00141
00142 buz_timer_running = true;
00143 timer_setDelay(&buz_timer, ms_to_ticks(time));
00144 timer_add(&buz_timer);
00145
00146 IRQ_RESTORE(flags);
00147 }
00148
00149
00153 void buz_repeat_start(mtime_t duration, mtime_t interval)
00154 {
00155 buz_repeat_interval = interval;
00156 buz_repeat_duration = duration;
00157 buz_beep(duration);
00158 }
00159
00160
00164 void buz_repeat_stop(void)
00165 {
00166 cpuflags_t flags;
00167 IRQ_SAVE_DISABLE(flags);
00168
00169
00170 if (buz_timer_running)
00171 {
00172 timer_abort(&buz_timer);
00173 buz_timer_running = false;
00174 }
00175
00176 buz_repeat_interval = 0;
00177 BUZZER_OFF;
00178
00179 IRQ_RESTORE(flags);
00180 }
00181
00182 MOD_DEFINE(buzzer)
00183
00184
00187 void buz_init(void)
00188 {
00189 MOD_CHECK(timer);
00190
00191 BUZZER_HW_INIT;
00192
00193
00194 timer_set_event_softint(&buz_timer, (Hook)buz_softint, 0);
00195
00196 MOD_INIT(buzzer);
00197 }