00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef TLC_FADES_H
00020 #define TLC_FADES_H
00021
00025 #include <avr/interrupt.h>
00026
00027 #include "Tlc5940.h"
00028 #include "WProgram.h"
00029
00030 #ifndef TLC_FADE_BUFFER_LENGTH
00031
00032 #define TLC_FADE_BUFFER_LENGTH 24
00033 #endif
00034
00036 struct Tlc_Fade {
00037 TLC_CHANNEL_TYPE channel;
00038 int16_t startValue;
00039 int16_t changeValue;
00040 uint32_t startMillis;
00041 uint32_t endMillis;
00042 } tlc_fadeBuffer[TLC_FADE_BUFFER_LENGTH];
00043
00045 uint8_t tlc_fadeBufferSize;
00046
00047 uint8_t tlc_updateFades();
00048 uint8_t tlc_updateFades(uint32_t currentMillis);
00049 uint8_t tlc_addFade(struct Tlc_Fade *fade);
00050 uint8_t tlc_addFade(TLC_CHANNEL_TYPE channel, int16_t startValue,
00051 int16_t endValue, uint32_t startMillis, uint32_t endMillis);
00052 uint8_t tlc_isFading(TLC_CHANNEL_TYPE channel);
00053 uint8_t tlc_removeFades(TLC_CHANNEL_TYPE channel);
00054 static void tlc_removeFadeFromBuffer(Tlc_Fade *current, Tlc_Fade *end);
00055
00070
00071
00076 uint8_t tlc_addFade(struct Tlc_Fade *fade)
00077 {
00078 if (tlc_fadeBufferSize == TLC_FADE_BUFFER_LENGTH) {
00079 return 0;
00080 }
00081 struct Tlc_Fade *p = tlc_fadeBuffer + tlc_fadeBufferSize++;
00082 p->channel = fade->channel;
00083 p->startValue = fade->startValue;
00084 p->changeValue = fade->changeValue;
00085 p->startMillis = fade->startMillis;
00086 p->endMillis = fade->endMillis;
00087 return tlc_fadeBufferSize;
00088 }
00089
00098 uint8_t tlc_addFade(TLC_CHANNEL_TYPE channel, int16_t startValue,
00099 int16_t endValue, uint32_t startMillis, uint32_t endMillis)
00100 {
00101 if (tlc_fadeBufferSize == TLC_FADE_BUFFER_LENGTH) {
00102 return 0;
00103 }
00104 struct Tlc_Fade *p = tlc_fadeBuffer + tlc_fadeBufferSize++;
00105 p->channel = channel;
00106 p->startValue = startValue;
00107 p->changeValue = endValue - startValue;
00108 p->startMillis = startMillis;
00109 p->endMillis = endMillis;
00110 return tlc_fadeBufferSize;
00111 }
00112
00116 uint8_t tlc_isFading(TLC_CHANNEL_TYPE channel)
00117 {
00118 struct Tlc_Fade *end = tlc_fadeBuffer + tlc_fadeBufferSize;
00119 for (struct Tlc_Fade *p = tlc_fadeBuffer; p < end; p++) {
00120 if (p->channel == channel) {
00121 return 1;
00122 }
00123 }
00124 return 0;
00125 }
00126
00130 uint8_t tlc_removeFades(TLC_CHANNEL_TYPE channel)
00131 {
00132 uint8_t removed = 0;
00133 struct Tlc_Fade *end = tlc_fadeBuffer + tlc_fadeBufferSize;
00134 for (struct Tlc_Fade *p = tlc_fadeBuffer; p < end; p++) {
00135 if (p->channel == channel) {
00136 removed++;
00137 tlc_removeFadeFromBuffer(p, --end);
00138 }
00139 }
00140 return removed;
00141 }
00142
00148 static void tlc_removeFadeFromBuffer(struct Tlc_Fade *current,
00149 struct Tlc_Fade *endp)
00150 {
00151 if (endp != current) {
00152 current->channel = endp->channel;
00153 current->startValue = endp->startValue;
00154 current->changeValue = endp->changeValue;
00155 current->startMillis = endp->startMillis;
00156 current->endMillis = endp->endMillis;
00157 }
00158 tlc_fadeBufferSize--;
00159 }
00160
00163 uint8_t tlc_updateFades()
00164 {
00165 return tlc_updateFades(millis());
00166 }
00167
00171 uint8_t tlc_updateFades(uint32_t currentMillis)
00172 {
00173 struct Tlc_Fade *end = tlc_fadeBuffer + tlc_fadeBufferSize;
00174 uint8_t needsUpdate = 0;
00175 for (struct Tlc_Fade *p = tlc_fadeBuffer; p < end;){
00176 if (currentMillis >= p->endMillis) {
00177 Tlc.set(p->channel, p->startValue + p->changeValue);
00178 needsUpdate = 1;
00179 tlc_removeFadeFromBuffer(p, --end);
00180 continue;
00181 } else {
00182 uint32_t startMillis = p->startMillis;
00183 if (currentMillis >= startMillis) {
00184 Tlc.set(p->channel, p->startValue + p->changeValue
00185 * (int32_t)(currentMillis - startMillis)
00186 / (int32_t)(p->endMillis - startMillis));
00187 needsUpdate = 1;
00188 }
00189 }
00190 p++;
00191 }
00192 if (needsUpdate) {
00193 Tlc.update();
00194 if (tlc_fadeBufferSize == 0) {
00195 while (tlc_needXLAT)
00196 ;
00197 }
00198 }
00199 return tlc_fadeBufferSize;
00200 }
00201
00202
00203
00204 #endif
00205