add code to control the audio buzzer
[protos/xbee-avr.git] / beep.c
diff --git a/beep.c b/beep.c
new file mode 100644 (file)
index 0000000..f34219c
--- /dev/null
+++ b/beep.c
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the University of California, Berkeley nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+
+#include <aversive.h>
+#include <aversive/wait.h>
+
+#include <scheduler.h>
+#include <cirbuf.h>
+
+#include "main.h"
+
+/* 100 ms */
+#define BEEP_PERIOD (100000UL/SCHEDULER_UNIT)
+
+static struct cirbuf beep_fifo;
+static char beep_fifo_buf[16];
+volatile uint8_t beep_mask = 1; /* init beep */
+
+union beep_t {
+       uint8_t u08;
+       struct {
+               uint8_t tone:2;
+               uint8_t len:3;
+               uint8_t pause:3;
+       };
+};
+static volatile union beep_t current_beep;
+
+/* called by the scheduler */
+static void beep_cb(void *arg)
+{
+       (void)arg;
+
+       beep_mask = 0;
+       if (current_beep.len == 0 && current_beep.pause == 0) {
+               if (CIRBUF_GET_LEN(&beep_fifo) == 0)
+                       return;
+               current_beep.u08 = cirbuf_get_head(&beep_fifo);
+               cirbuf_del_head(&beep_fifo);
+       }
+
+       if (current_beep.len > 0) {
+               current_beep.len --;
+               switch (current_beep.tone) {
+                       case 0: beep_mask = 1; break;
+                       case 1: beep_mask = 2; break;
+                       case 2: beep_mask = 4; break;
+                       case 3: beep_mask = 8; break;
+                       default: break;
+               }
+               return;
+       }
+
+       if (current_beep.pause > 0) {
+               current_beep.pause --;
+       }
+}
+
+void beep(uint8_t tone, uint8_t len, uint8_t pause)
+{
+       uint8_t flags;
+       union beep_t b;
+
+       b.tone = tone;
+       b.len = len;
+       b.pause = pause;
+       IRQ_LOCK(flags);
+       cirbuf_add_tail(&beep_fifo, b.u08);
+       IRQ_UNLOCK(flags);
+}
+
+void beep_init(void)
+{
+       cirbuf_init(&beep_fifo, beep_fifo_buf, 0, sizeof(beep_fifo_buf));
+       scheduler_add_periodical_event_priority(&beep_cb, NULL,
+                                           BEEP_PERIOD, BEEP_PRIO);
+}