From: simimeie Date: Sat, 10 Jul 2010 09:28:52 +0000 (+0000) Subject: led framing support (not called yet). Originally by sijuhamm, but heavily X-Git-Url: http://git.rrze.uni-erlangen.de/gitweb/?p=moodlight.git;a=commitdiff_plain;h=9d076ea0c15fc4f9c3c8f67415040545be1bf0c6 led framing support (not called yet). Originally by sijuhamm, but heavily modified. --- diff --git a/ledframing.c b/ledframing.c new file mode 100644 index 0000000..132c74e --- /dev/null +++ b/ledframing.c @@ -0,0 +1,74 @@ +/* $Id: ledframing.c,v 1.1 2010/07/10 09:28:52 simimeie Exp $ + * Functions for led "animations" + */ + +#include +#include +#include +#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; +} diff --git a/ledframing.h b/ledframing.h new file mode 100644 index 0000000..900eea0 --- /dev/null +++ b/ledframing.h @@ -0,0 +1,68 @@ +/* $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 + +/* 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_ */