fork download
  1. #include <stdio.h>
  2. #include <inttypes.h>
  3. #include <unistd.h>
  4.  
  5. inline void Delay( unsigned long ms )
  6. {
  7. usleep( ms * 1000 );
  8. }
  9.  
  10. typedef struct
  11. {
  12. /* colors */
  13. uint8_t red;
  14. uint8_t green;
  15. uint8_t blue;
  16. uint8_t white;
  17.  
  18. /* efects */
  19. uint8_t fi;
  20. uint8_t fo;
  21. uint16_t delay;
  22. } SColorRGBW;
  23.  
  24. uint8_t GetColorByIndex(SColorRGBW *s, uint8_t i) {
  25. switch(i) {
  26. case 0: return s->red;
  27. case 1: return s->green;
  28. case 2: return s->blue;
  29. case 3: return s->white;
  30. }
  31. return 0;
  32. }
  33.  
  34.  
  35.  
  36. void PrintColors2(uint8_t color_r, uint8_t color_g, uint8_t color_b, uint8_t color_w){
  37. printf("Colors: r:%d, g:%d, b:%d, w:%d\n", color_r,color_g,color_b,color_w);
  38. }
  39.  
  40. // Efects
  41. void FadeOut(SColorRGBW *s){
  42. uint8_t i=0, r=0, g=0, b=0, w=0;
  43. r = s->red;
  44. g = s->green;
  45. b = s->blue;
  46. w = s->white;
  47. PrintColors2(r,g,b,w);
  48. Delay(s->delay);
  49. for (i = s->fo-1; i > 0; --i) {
  50. if (r > 0) r--;
  51. if (g > 0) g--;
  52. if (b > 0) b--;
  53. if (w > 0) w--;
  54. PrintColors2(r,g,b,w);
  55. Delay(s->delay);
  56. }
  57. }
  58.  
  59. void FadeIn(SColorRGBW *s){
  60. uint8_t i=0, r=0, g=0, b=0, w=0, m=0;
  61. r = s->red - (s->fi-1);
  62. g = s->green - (s->fi-1);
  63. b = s->blue - (s->fi-1);
  64. w = s->white - (s->fi-1);
  65.  
  66. m = s->fi;
  67. for (i = 0; i < m; i++) {
  68. PrintColors2(r, g, b, w);
  69. if (r > 0) r++;
  70. if (g > 0) g++;
  71. if (b > 0) b++;
  72. if (w > 0) w++;
  73. Delay(s->delay);
  74. }
  75. }
  76.  
  77. void SetColorRGBW(SColorRGBW *s, uint8_t color_r, uint8_t color_g, uint8_t color_b, uint8_t color_w, uint8_t fi, uint8_t fo, uint16_t delay)
  78. {
  79. s->red = color_r;
  80. s->green = color_g;
  81. s->blue = color_b;
  82. s->white = color_w;
  83. s->delay = delay;
  84. s->fi = fi;
  85. s->fo = fo;
  86. }
  87.  
  88. int main(){
  89. SColorRGBW a,b;
  90. // R G B W Fi Fo d
  91. SetColorRGBW(&a, 255, 10, 15, 11, 10, 10, 1);
  92. SetColorRGBW(&b, 255, 10, 15, 11, 10, 10, 1);
  93. //printf("FadeOut:\n");
  94. FadeOut(&a);
  95. printf("FadeIn:\n");
  96. FadeIn(&b);
  97. return 0;
  98. }
  99.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
Colors: r:255, g:10, b:15, w:11
Colors: r:254, g:9, b:14, w:10
Colors: r:253, g:8, b:13, w:9
Colors: r:252, g:7, b:12, w:8
Colors: r:251, g:6, b:11, w:7
Colors: r:250, g:5, b:10, w:6
Colors: r:249, g:4, b:9, w:5
Colors: r:248, g:3, b:8, w:4
Colors: r:247, g:2, b:7, w:3
Colors: r:246, g:1, b:6, w:2
FadeIn:
Colors: r:246, g:1, b:6, w:2
Colors: r:247, g:2, b:7, w:3
Colors: r:248, g:3, b:8, w:4
Colors: r:249, g:4, b:9, w:5
Colors: r:250, g:5, b:10, w:6
Colors: r:251, g:6, b:11, w:7
Colors: r:252, g:7, b:12, w:8
Colors: r:253, g:8, b:13, w:9
Colors: r:254, g:9, b:14, w:10
Colors: r:255, g:10, b:15, w:11