Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

visualizer.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * visualizer.c
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <avr/io.h>
  24. #include <stdint.h>
  25. #include <visualizer.h>
  26. #include <cube.h>
  27. #include <buffhelp.h>
  28. #define FACTOR 31 // 8 LEDs, Max Val 255:
  29. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  30. // 255 / FACTOR = 8,...
  31. // 127 / FACTOR = 4,...
  32. void simpleVisualization(uint8_t *data);
  33. void fullDepthVisualization(uint8_t *data);
  34. void horribleWave(uint8_t *audioData);
  35. #define NUMOFVISUALIZATIONS 3
  36. void (*visualizations[NUMOFVISUALIZATIONS])(uint8_t *data) = { &simpleVisualization,
  37. &fullDepthVisualization, &horribleWave };
  38. uint8_t numberOfVisualizations(void) {
  39. return NUMOFVISUALIZATIONS;
  40. }
  41. void runVisualization(uint8_t *data, uint8_t id) {
  42. if (id < NUMOFVISUALIZATIONS)
  43. visualizations[id](data);
  44. }
  45. void simpleVUMeter(uint8_t *data, uint8_t *buff, uint8_t z) {
  46. uint8_t i, h, max;
  47. for(i = 0; i < 7; i++) {
  48. max = data[i] / FACTOR;
  49. for (h = 0; h < max; h++) {
  50. if (i == 0) {
  51. buffSetPixel(buff, i, (h * 10 / 15), z);
  52. }
  53. buffSetPixel(buff, i + 1, h, z);
  54. }
  55. }
  56. }
  57. void simpleVisualization(uint8_t *data) {
  58. uint8_t *buff;
  59. buff = buffNew();
  60. buffClearAllPixels(buff);
  61. simpleVUMeter(data, buff, 7);
  62. setImage(buff);
  63. buffFree(buff);
  64. }
  65. void fullDepthVisualization(uint8_t *data) {
  66. uint8_t *buff;
  67. uint8_t i;
  68. buff = buffNew();
  69. buffClearAllPixels(buff);
  70. for (i = 0; i < 8; i++) {
  71. simpleVUMeter(data, buff, i);
  72. }
  73. setImage(buff);
  74. buffFree(buff);
  75. }
  76. void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  77. buf[(8 * (7 - z)) + (7 - y)] |= (1 << x); // z is inverted for beauty reasons
  78. }
  79. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
  80. uint8_t i = 0;
  81. for (; i < height; i++) {
  82. setPixelBuffer(x, i, z, buf);
  83. }
  84. }
  85. void horribleWave(uint8_t *audioData) {
  86. uint8_t *imageData = buffNew();
  87. buffClearAllPixels(imageData);
  88. // Could not figure out a way to represent this easily in a loop
  89. // without using a shitload of 'if's...
  90. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  91. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  92. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  93. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  94. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  95. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  96. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  97. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  98. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  99. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  100. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  101. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  102. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  103. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  104. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  105. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  106. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  107. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  108. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  109. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  110. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  111. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  112. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  113. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  114. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  115. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  116. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  117. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  118. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  119. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  120. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  121. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  122. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  123. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  124. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  125. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  126. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  127. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  128. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  129. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  130. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  131. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  132. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  133. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  134. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  135. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  136. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  137. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  138. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  139. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  140. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  141. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  142. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  143. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  144. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  145. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  146. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  147. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  148. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  149. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  150. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  151. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  152. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  153. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  154. setImage(imageData);
  155. buffFree(imageData);
  156. }