From f117e99984c3fac5d1602e55e18937c7d5183165 Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Wed, 16 Apr 2014 12:39:10 +0200 Subject: [PATCH 1/1] Initial import --- FastLED_animation.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ FastLED_animation.h | 51 ++++++++++++++++++++++++++++++++++++++++ LICENSE | 21 +++++++++++++++++ README | 5 ++++ 4 files changed, 141 insertions(+) create mode 100644 FastLED_animation.cpp create mode 100644 FastLED_animation.h create mode 100644 LICENSE create mode 100644 README diff --git a/FastLED_animation.cpp b/FastLED_animation.cpp new file mode 100644 index 0000000..6cd0e30 --- /dev/null +++ b/FastLED_animation.cpp @@ -0,0 +1,64 @@ +#include "FastLED_animation.h" + +Animation::Animation() { + init(0, NULL, 0); +} + +Animation::Animation(uint16_t frameCount_, + const prog_uint8_t* frameData_, + const uint8_t ledCount_) +{ + init(frameCount_, frameData_, ledCount_); + reset(); +} + +void Animation::init(uint16_t frameCount_, + const prog_uint8_t* frameData_, + const uint8_t ledCount_) +{ + frameCount = frameCount_; + frameData = const_cast(frameData_); + ledCount = ledCount_; + + // Load the color table into memory + // TODO: Free this memory somewhere? + colorTableEntries = pgm_read_byte(frameData) + 1; + colorTable = (CRGB *)malloc(colorTableEntries * sizeof(CRGB)); + + for(int i = 0; i < colorTableEntries; i++) { + colorTable[i] = CRGB(pgm_read_byte(frameData + 1 + i * 3 ), + pgm_read_byte(frameData + 1 + i * 3 + 1), + pgm_read_byte(frameData + 1 + i * 3 + 2)); + } + + reset(); +} + +void Animation::reset() { + frameIndex = 0; +} + +void Animation::draw(struct CRGB strip[]) { + drawIndexed_RLE(strip); + LEDS.show(); + + frameIndex = (frameIndex + 1)%frameCount; +}; + +void Animation::drawIndexed_RLE(struct CRGB strip[]) { + if(frameIndex == 0) { + currentFrameData = frameData + + 1 + 3 * colorTableEntries; // Offset for color table + } + + // Read runs of RLE data until we get enough data. + uint8_t count = 0; + while (count < ledCount) { + uint8_t run_length = pgm_read_byte(currentFrameData++); + uint8_t colorIndex = pgm_read_byte(currentFrameData++); + + for(uint8_t i = 0; i < run_length; i++) { + strip[count++] = colorTable[colorIndex]; + } + } +}; diff --git a/FastLED_animation.h b/FastLED_animation.h new file mode 100644 index 0000000..1b569b2 --- /dev/null +++ b/FastLED_animation.h @@ -0,0 +1,51 @@ +#ifndef FASTLED_ANIMATION_H +#define FASTLED_ANIMATION_H + +#include +#include + +class Animation { +private: + uint8_t ledCount; // Number of LEDs in the strip (max 254) + uint16_t frameCount; // Number of frames in this animation (max 65535) + prog_uint8_t* frameData; // Pointer to the begining of the frame data + + uint16_t frameIndex; // Current animation frame + prog_uint8_t* currentFrameData; // Pointer to the current position in the frame data + + uint8_t colorTableEntries; // Number of entries in the color table, minus 1 (max 255) + struct CRGB* colorTable; // Pointer to color table, if used by the encoder + + void drawIndexed_RLE(struct CRGB strip[]); + +public: + // Initialize the animation with no data. This is intended for the case + // where the animation will be re-initialized from a memory structure in ROM + // after the sketch starts. + Animation(); + + // Initialize the animation + // @param frameCount Number of frames in this animation + // @param frameData Pointer to the frame data. Format of this data is indexed RLE + // @param ledCount Number of LEDs in the strip + Animation(uint16_t frameCount, + const prog_uint8_t* frameData, + const uint8_t ledCount); + + // Re-initialize the animation with new information + // @param frameCount Number of frames in this animation + // @param frameData Pointer to the frame data. Format of this data is indexed RLE + // @param ledCount Number of LEDs in the strip + void init(uint16_t frameCount, + const prog_uint8_t* frameData, + const uint8_t ledCount); + + // Reset the animation, causing it to start over from frame 0. + void reset(); + + // Draw the next frame of the animation + // @param strip[] LED strip to draw to. + void draw(struct CRGB strip[]); +}; + +#endif // FASTLED_ANIMATION_H diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fb75abf --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Blinkinlabs, LLC +Copyright (C) 2014 Antonio Ospite + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..7177e5b --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +Simple Animation of a RLE encoded image using FastLED: +http://fastled.io/ + +This is a stripped down version of BlinkyTape_Arduino: +https://github.com/Blinkinlabs/BlinkyTape_Arduino.git -- 2.1.4