Simple single-color 8x8x8 LED Cube with AVRs
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

visualizer.c 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <stdlib.h>
  26. #include <visualizer.h>
  27. #include <cube.h>
  28. #include <buffhelp.h>
  29. #define FACTOR 31 // 8 LEDs, Max Val 255:
  30. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  31. // 255 / FACTOR = 8,...
  32. // 127 / FACTOR = 4,...
  33. #define THRESHOLD (FACTOR * 10 / 17)
  34. uint8_t maxVal(uint8_t data, uint8_t log);
  35. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf);
  36. uint8_t average(uint8_t *data);
  37. uint8_t maximum(uint8_t *data);
  38. void filterData(uint8_t *data, uint8_t log);
  39. void simpleVisualization(uint8_t *data);
  40. void fullDepthVisualization(uint8_t *data);
  41. void horribleWave(uint8_t *audioData);
  42. void simpleLog(uint8_t *data);
  43. void fullDepthLog(uint8_t *data);
  44. void linLog(uint8_t *data);
  45. #define NUMOFVISUALIZATIONS 6
  46. void (*visualizations[NUMOFVISUALIZATIONS])(uint8_t *data) = { &linLog, &simpleVisualization,
  47. &fullDepthVisualization, &horribleWave,
  48. &simpleLog, &fullDepthLog };
  49. uint8_t logScale[8] = { 2, 4, 8, 16, 31, 63, 125, 250 }; // --> ca. (1 << (led + 1));
  50. uint8_t numberOfVisualizations(void) {
  51. return NUMOFVISUALIZATIONS;
  52. }
  53. void runVisualization(uint8_t *data, uint8_t id) {
  54. if (id < NUMOFVISUALIZATIONS) {
  55. if ((id == 0) || ((id > 3) && (id <= 5))) {
  56. filterData(data, 1);
  57. } else {
  58. filterData(data, 0);
  59. }
  60. visualizations[id](data);
  61. }
  62. }
  63. uint8_t maximum(uint8_t *data) {
  64. uint8_t i, max = 0;
  65. for (i = 0; i < 7; i++) {
  66. if (data[i] > max) {
  67. max = data[i];
  68. }
  69. }
  70. return max;
  71. }
  72. uint8_t maxVal(uint8_t data, uint8_t log) {
  73. uint8_t max = 0;
  74. if (log) {
  75. while ((max <= 7) && (data > logScale[max])) // Some bitshifting would do it...
  76. max++; // But this is more fine grained
  77. } else {
  78. max = data / FACTOR;
  79. }
  80. return max;
  81. }
  82. uint8_t average(uint8_t *data) {
  83. uint16_t sum = 0;
  84. uint8_t i;
  85. for (i = 0; i < 7; i++) {
  86. sum += data[i];
  87. }
  88. sum /= 7;
  89. return (uint8_t)sum;
  90. }
  91. void filterData(uint8_t *data, uint8_t log) {
  92. uint8_t i;
  93. uint8_t max;
  94. if (log) {
  95. if (average(data) < THRESHOLD) {
  96. max = THRESHOLD;
  97. } else {
  98. max = THRESHOLD / 2;
  99. }
  100. } else {
  101. max = THRESHOLD;
  102. }
  103. if (average(data) < max) {
  104. for (i = 0; i < 7; i++) {
  105. data[i] = 0;
  106. }
  107. }
  108. }
  109. void simpleVUMeter(uint8_t *data, uint8_t *buff, uint8_t z, uint8_t log) {
  110. uint8_t i, h = 0, max;
  111. for(i = 0; i < 7; i++) {
  112. max = maxVal(data[i], log);
  113. for (h = 0; h < max; h++) {
  114. if (i == 0) {
  115. buffSetPixel(buff, i, (h * 10 / 15), z);
  116. }
  117. buffSetPixel(buff, i + 1, h, z);
  118. }
  119. }
  120. }
  121. void simpleLog(uint8_t *data) {
  122. uint8_t *buff;
  123. buff = buffNew();
  124. buffClearAllPixels(buff);
  125. setRow(0, 0, maxVal(average(data), 1), buff); // Show average
  126. simpleVUMeter(data, buff, 7, 1);
  127. setImage(buff);
  128. buffFree(buff);
  129. }
  130. void simpleVisualization(uint8_t *data) {
  131. uint8_t *buff;
  132. buff = buffNew();
  133. buffClearAllPixels(buff);
  134. setRow(0, 0, maxVal(average(data), 0), buff); // Show average
  135. simpleVUMeter(data, buff, 7, 0);
  136. setImage(buff);
  137. buffFree(buff);
  138. }
  139. void linLog(uint8_t *data) {
  140. uint8_t *buff;
  141. buff = buffNew();
  142. buffClearAllPixels(buff);
  143. simpleVUMeter(data, buff, 2, 1);
  144. filterData(data, 0);
  145. simpleVUMeter(data, buff, 5, 0);
  146. setImage(buff);
  147. buffFree(buff);
  148. }
  149. void fullDepthLog(uint8_t *data) {
  150. uint8_t *buff;
  151. uint8_t i;
  152. buff = buffNew();
  153. buffClearAllPixels(buff);
  154. for (i = 0; i < 8; i++) {
  155. simpleVUMeter(data, buff, i, 1);
  156. }
  157. setImage(buff);
  158. buffFree(buff);
  159. }
  160. void fullDepthVisualization(uint8_t *data) {
  161. uint8_t *buff;
  162. uint8_t i;
  163. buff = buffNew();
  164. buffClearAllPixels(buff);
  165. for (i = 0; i < 8; i++) {
  166. simpleVUMeter(data, buff, i, 0);
  167. }
  168. setImage(buff);
  169. buffFree(buff);
  170. }
  171. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
  172. uint8_t i;
  173. for (i = 0; i < height; i++) {
  174. buffSetPixel(buf, x, i, z);
  175. }
  176. }
  177. void horribleWave(uint8_t *audioData) {
  178. uint8_t *imageData = buffNew();
  179. buffClearAllPixels(imageData);
  180. // Could not figure out a way to represent this easily in a loop
  181. // without using a shitload of 'if's...
  182. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  183. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  184. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  185. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  186. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  187. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  188. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  189. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  190. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  191. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  192. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  193. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  194. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  195. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  196. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  197. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  198. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  199. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  200. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  201. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  202. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  203. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  204. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  205. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  206. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  207. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  208. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  209. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  210. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  211. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  212. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  213. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  214. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  215. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  216. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  217. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  218. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  219. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  220. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  221. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  222. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  223. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  224. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  225. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  226. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  227. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  228. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  229. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  230. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  231. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  232. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  233. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  234. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  235. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  236. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  237. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  238. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  239. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  240. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  241. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  242. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  243. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  244. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  245. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  246. setImage(imageData);
  247. buffFree(imageData);
  248. }