common/usbledfader.h

Go to the documentation of this file.
00001 #ifndef __usbledfader_h_included__
00002 #define __usbledfader_h_included__
00003 
00329 #include <stdint.h>
00330 
00331 /* return codes for USB-communication */
00332 #define msgOK 0     
00333 #define msgErr 1    
00335 /* These are the vendor specific SETUP commands implemented by our USB device */
00336 #define CMD_ECHO  0 
00337 #define CMD_GET   1 
00338 #define CMD_SET   2 
00339 #define CMD_CLEAR 3 
00340 #define CMD_RESET 4 
00343 typedef struct S_fade_Waveform {
00344     uint8_t waveformId; 
00345     uint8_t waveformLength; 
00346     uint8_t waveformRepetition; 
00347     uint8_t waveformDuration; 
00348     uint32_t waveformUpdateTime; 
00349 } fade_Waveform;
00350 
00352 typedef struct S_fade_LedState {
00353     fade_Waveform wave[3]; 
00354     uint8_t waveCurrentId; 
00355     uint8_t waveCurrentValue; 
00356     uint8_t waveCurrentPosition; 
00357     uint8_t waveCurrentRepetition; 
00358     int32_t waveNextUpdate; 
00359 } fade_LedState;
00360 
00362 typedef struct S_fade_GlobalData {
00363     fade_LedState led[4];     
00364 } fade_GlobalData;
00365 
00366 uint8_t fade_calculateWaveform(uint8_t waveformId, uint8_t waveformPosition);
00367 
00375 uint8_t fade_calculateWaveform(uint8_t waveformId, uint8_t waveformPosition) {
00376     /*
00377      * values for sinus-wave, amplitude 31, 64 steps:
00378      * awk 'BEGIN{ pi=3.1415927; for(i=1; i<=64; i++) { printf("%.0f, ", sin(i*pi/32)*31) } printf("\n"); }'
00379      *  3,  6,  9, 12, 15, 17, 20, 22, 24, 26, 27, 29, 30, 30, 31, 31, 31, 30,
00380      *  30, 29, 27, 26, 24, 22, 20, 17, 15, 12,  9,  6,  3, -0, -3, -6, -9,
00381      *  -12, -15, -17, -20, -22, -24, -26, -27, -29, -30, -30, -31, -31, -31,
00382      *  -30, -30, -29, -27, -26, -24, -22, -20, -17, -15, -12, -9, -6, -3,  0
00383      */
00384 
00385     /* sinus-wave:
00386      * awk 'BEGIN{ pi=3.1415927; for(i=1; i<=64; i++) { printf("%.0f, ", sin((i+48)*pi/32)*15+16) } printf("\n"); }'
00387      */
00388     uint8_t sinus[] = { 1, 1, 2, 2, 3, 4, 4, 5, 6, 8, 9, 10, 12, 13, 15, 16,
00389         17, 19, 20, 22, 23, 24, 26, 27, 28, 28, 29, 30, 30, 31, 31, 31, 31, 31,
00390         30, 30, 29, 28, 28, 27, 26, 24, 23, 22, 20, 19, 17, 16, 15, 13, 12, 10,
00391         9, 8, 6, 5, 4, 4, 3, 2, 2, 1, 1, 1 };
00392 
00393     /*
00394      * another nice wave, wider than the original sinus:
00395      * awk 'BEGIN{ pi=3.1415927; for(i=1; i<=32; i++) { printf("%.0f, ", sqrt(sin(i*pi/32)*31+.00001)*sqrt(32)) } printf("\n"); }'
00396      */
00397     uint8_t widecurve[] = { 10, 14, 17, 19, 22, 23, 25, 26, 28, 29, 30, 30, 31,
00398         31, 31, 31, 31, 31, 31, 30, 30, 29, 28, 26, 25, 23, 22, 19, 17, 14, 10,
00399         0 };
00400 
00401     if (waveformId <= 31) {
00402         /* No fading, just a constant level */
00403         if (waveformPosition == 0) {
00404             return 1;
00405         } else {
00406             return waveformId;
00407         }
00408     } else {
00409         switch (waveformId) {
00410         case 32:               /* blink */
00411             if (waveformPosition == 0) {
00412                 return 2;
00413             } else {
00414                 if (waveformPosition == 1) {
00415                     return 31;
00416                 } else {
00417                     return 0;
00418                 }
00419             }
00420         case 33:               /* triangular */
00421             if (waveformPosition == 0) {
00422                 return 62;
00423             } else {
00424                 if (waveformPosition <= 32) {
00425                     return waveformPosition - 1;
00426                 } else {
00427                     return 63 - waveformPosition;
00428                 }
00429             }
00430         case 34:               /* sawtooth rising */
00431             if (waveformPosition == 0) {
00432                 return 32;
00433             } else {
00434                 return waveformPosition - 1;
00435             }
00436         case 35:               /* sawtooth falling */
00437             if (waveformPosition == 0) {
00438                 return 32;
00439             } else {
00440                 return 31 - (waveformPosition - 1);
00441             }
00442         case 36:               /* sinus */
00443             if (waveformPosition == 0) {
00444                 return 64;
00445             } else {
00446                 return sinus[waveformPosition - 1];
00447             }
00448         case 37:               /* wide curve */
00449             if (waveformPosition == 0) {
00450                 return 32;
00451             } else {
00452                 return widecurve[waveformPosition - 1];
00453             }
00454         case 38:               /* wide curve - inverted */
00455             if (waveformPosition == 0) {
00456                 return 32;
00457             } else {
00458                 return 31 - widecurve[(waveformPosition + 15) % 32];
00459             }
00460         }
00461     }
00462     return 0;
00463 }
00464 
00465 #endif
00466 

Generated on Mon Oct 2 19:31:17 2006 for USB-LED-Fader by  doxygen 1.4.7