--- /dev/null
+/* $Id: ledframing.c,v 1.1 2010/07/10 09:28:52 simimeie Exp $
+ * Functions for led "animations"
+ */
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <string.h>
+#include "ledframing.h"
+#include "ledpwm.h"
+#include "console.h"
+
+static uint8_t framesused;
+static uint8_t currentframe;
+static struct frame framebuffer[MAX_FRAMES];
+
+void ledframing_init() {
+ framesused = currentframe = 0;
+}
+
+uint8_t addframe_frame(struct frame fr) {
+ if ((framesused + 1) >= MAX_FRAMES) { /* Out of memory */
+ return 0;
+ }
+ if (fr.action > MAX_ACTION) { /* Invalid / unknown action */
+ return 0;
+ }
+
+ framebuffer[framesused] = fr;
+ framesused++;
+
+ return 1;
+}
+
+uint8_t addframe_fade(uint16_t duration, uint8_t red, uint8_t green, uint8_t blue) {
+ if ((framesused + 1) >= MAX_FRAMES) { /* Out of memory */
+ return 0;
+ }
+
+ framebuffer[framesused].action = ACTION_FADE;
+ framebuffer[framesused].duration = duration;
+ framebuffer[framesused].rgb.re = red;
+ framebuffer[framesused].rgb.gr = green;
+ framebuffer[framesused].rgb.bl = blue;
+ framesused++;
+
+ return 1;
+}
+
+uint8_t addframe_strobe(uint16_t duration, uint16_t onticks, uint16_t offticks) {
+ if ((framesused + 1) >= MAX_FRAMES) { /* Out of memory */
+ return 0;
+ }
+
+ framebuffer[framesused].action = ACTION_STROBE;
+ framebuffer[framesused].duration = duration;
+ framebuffer[framesused].strobe.onticks = onticks;
+ framebuffer[framesused].strobe.offticks = offticks;
+ framesused++;
+
+ return 1;
+}
+
+struct frame * getnextframe(void) {
+ if (currentframe < framesused) {
+ currentframe++;
+ return &framebuffer[currentframe - 1];
+ }
+ /* No frames left */
+ return NULL;
+}
+
+void resetframes() {
+ framesused = currentframe = 0;
+}
--- /dev/null
+/* $Id: ledframing.h,v 1.1 2010/07/10 09:28:52 simimeie Exp $
+ * Functions for led "animations"
+ */
+
+#ifndef _LEDFRAMING_H_
+#define _LEDFRAMING_H_
+
+#include <avr/io.h>
+
+/* Number of frames to allocate in memory.
+ * Note: One frame takes up 7 bytes of memory (or more if the compiler
+ * chooses a strange alignment)
+ * Must not be >255. */
+#define MAX_FRAMES 32
+
+struct frame {
+ uint16_t duration; /* in ticks */
+ unsigned char action:2;
+ union {
+ struct {
+ uint8_t re;
+ uint8_t gr;
+ uint8_t bl;
+ } rgb;
+ struct {
+ uint16_t onticks;
+ uint16_t offticks;
+ } strobe;
+ };
+};
+
+/* Values for 'action' */
+/* Fade from last to this color */
+#define ACTION_FADE 0
+/* Strobe (blink) */
+#define ACTION_STROBE 1
+/* Maximum valid ACTION */
+#define MAX_ACTION ACTION_STROBE
+
+/* Initializes internal structures. */
+void ledframing_init(void);
+
+/* Create a frame from a struct frame. */
+uint8_t addframe_frame(struct frame fr);
+
+/* Create a frame that fades from the last color to this one. duration is
+ * the number of ticks this fading should take.
+ * There is an obvious special case: fading with a duration of 0 will just
+ * switch to that color.
+ * Returns 1 if successful, 0 on error (out of memory) */
+uint8_t addframe_fade(uint16_t duration, uint8_t red, uint8_t green, uint8_t blue);
+
+/* Create a frame where the LEDs are blinking. The color is not changed,
+ * thus stays whatever it was before. onticks and offticks give the number
+ * of ticks that the LEDs are on and off (respectively) in each blink
+ * interval.
+ * This can obviously be used to turn off the LEDs for an interval:
+ * onticks = 0, offticks = duration.
+ * Returns 1 if successful, 0 on error (out of memory) */
+uint8_t addframe_strobe(uint16_t duration, uint16_t onticks, uint16_t offticks);
+
+/* Returns the next frame (or NULL if there isn't any left) */
+struct frame * getnextframe(void);
+
+/* Clears the frame buffer */
+void resetframes();
+
+#endif /* _LEDFRAMING_H_ */