Initial import
[FastLED_animation.git] / FastLED_animation.cpp
1 #include "FastLED_animation.h"
2
3 Animation::Animation() {
4         init(0, NULL, 0);
5 }
6
7 Animation::Animation(uint16_t frameCount_,
8                      const prog_uint8_t* frameData_,
9                      const uint8_t ledCount_)
10 {
11         init(frameCount_, frameData_, ledCount_);
12         reset();
13 }
14
15 void Animation::init(uint16_t frameCount_,
16                      const prog_uint8_t* frameData_,
17                      const uint8_t ledCount_)
18 {
19         frameCount = frameCount_;
20         frameData = const_cast<prog_uint8_t*>(frameData_);
21         ledCount = ledCount_;
22
23         // Load the color table into memory
24         // TODO: Free this memory somewhere?
25         colorTableEntries = pgm_read_byte(frameData) + 1;
26         colorTable = (CRGB *)malloc(colorTableEntries * sizeof(CRGB));
27
28         for(int i = 0; i < colorTableEntries; i++) {
29                 colorTable[i] = CRGB(pgm_read_byte(frameData + 1 + i * 3    ),
30                                      pgm_read_byte(frameData + 1 + i * 3 + 1),
31                                      pgm_read_byte(frameData + 1 + i * 3 + 2));
32         }
33
34         reset();
35 }
36
37 void Animation::reset() {
38         frameIndex = 0;
39 }
40
41 void Animation::draw(struct CRGB strip[]) {
42         drawIndexed_RLE(strip);
43         LEDS.show();
44
45         frameIndex = (frameIndex + 1)%frameCount;
46 };
47
48 void Animation::drawIndexed_RLE(struct CRGB strip[]) {
49         if(frameIndex == 0) {
50                 currentFrameData = frameData
51                         + 1 + 3 * colorTableEntries;   // Offset for color table
52         }
53
54         // Read runs of RLE data until we get enough data.
55         uint8_t count = 0;
56         while (count < ledCount) {
57                 uint8_t run_length = pgm_read_byte(currentFrameData++);
58                 uint8_t colorIndex = pgm_read_byte(currentFrameData++);
59
60                 for(uint8_t i = 0; i < run_length; i++) {
61                         strip[count++] = colorTable[colorIndex];
62                 }
63         }
64 };