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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * visualizer.c
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <avr/io.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <visualizer.h>
  25. #include <cube.h>
  26. #include <buffhelp.h>
  27. #define FACTOR 31 // 8 LEDs, Max Val 255:
  28. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  29. // 255 / FACTOR = 8,...
  30. // 127 / FACTOR = 4,...
  31. #define THRESHOLD (FACTOR * 10 / 17)
  32. uint8_t maxVal(uint8_t data, uint8_t log);
  33. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf);
  34. uint8_t average(uint8_t *data);
  35. uint8_t maximum(uint8_t *data);
  36. void filterData(uint8_t *data, uint8_t log);
  37. void simpleVisualization(uint8_t *data);
  38. void simpleSwitch(uint8_t *data);
  39. void fullDepthVisualization(uint8_t *data);
  40. void horribleWave(uint8_t *audioData);
  41. void simpleLog(uint8_t *data);
  42. void fullDepthLog(uint8_t *data);
  43. void linLog(uint8_t *data);
  44. #define NUMOFVISUALIZATIONS 7
  45. void (*visualizations[NUMOFVISUALIZATIONS])(uint8_t *data) = { &linLog, &simpleVisualization,
  46. &fullDepthVisualization, &horribleWave,
  47. &simpleLog, &fullDepthLog, &simpleSwitch };
  48. uint8_t logScale[8] = { 2, 4, 8, 16, 31, 63, 125, 250 }; // --> ca. (1 << (led + 1));
  49. uint8_t numberOfVisualizations(void) {
  50. return NUMOFVISUALIZATIONS;
  51. }
  52. void runVisualization(uint8_t *data, uint8_t id) {
  53. if (id < NUMOFVISUALIZATIONS) {
  54. if ((id == 0) || ((id > 3) && (id <= 5))) {
  55. filterData(data, 1);
  56. } else {
  57. filterData(data, 0);
  58. }
  59. visualizations[id](data);
  60. }
  61. }
  62. uint8_t maximum(uint8_t *data) {
  63. uint8_t i, max = 0;
  64. for (i = 0; i < 7; i++) {
  65. if (data[i] > max) {
  66. max = data[i];
  67. }
  68. }
  69. return max;
  70. }
  71. uint8_t maxVal(uint8_t data, uint8_t log) {
  72. uint8_t max = 0;
  73. if (log) {
  74. while ((max <= 7) && (data > logScale[max])) // Some bitshifting would do it...
  75. max++; // But this is more fine grained
  76. } else {
  77. max = data / FACTOR;
  78. }
  79. return max;
  80. }
  81. uint8_t average(uint8_t *data) {
  82. uint16_t sum = 0;
  83. uint8_t i;
  84. for (i = 0; i < 7; i++) {
  85. sum += data[i];
  86. }
  87. sum /= 7;
  88. return (uint8_t)sum;
  89. }
  90. void filterData(uint8_t *data, uint8_t log) {
  91. uint8_t i;
  92. uint8_t max;
  93. if (log) {
  94. if (average(data) < THRESHOLD) {
  95. max = THRESHOLD;
  96. } else {
  97. max = THRESHOLD / 2;
  98. }
  99. } else {
  100. max = THRESHOLD;
  101. }
  102. if (average(data) < max) {
  103. for (i = 0; i < 7; i++) {
  104. data[i] = 0;
  105. }
  106. }
  107. }
  108. void simpleVUMeter(uint8_t *data, uint8_t *buff, uint8_t z, uint8_t log, uint8_t change) {
  109. uint8_t i, h = 0, max;
  110. for(i = 0; i < 7; i++) {
  111. max = maxVal(data[i], log);
  112. for (h = 0; h < max; h++) {
  113. if (i == 0) {
  114. if (change) {
  115. buffSetPixel(buff, i, z, (h * 10 / 15));
  116. } else {
  117. buffSetPixel(buff, i, (h * 10 / 15), z);
  118. }
  119. }
  120. if (change) {
  121. buffSetPixel(buff, i + 1, z, h);
  122. } else {
  123. buffSetPixel(buff, i + 1, h, z);
  124. }
  125. }
  126. }
  127. }
  128. void simpleLog(uint8_t *data) {
  129. uint8_t *buff;
  130. buff = buffNew();
  131. // setRow(0, 0, maxVal(average(data), 1), buff); // Show average
  132. simpleVUMeter(data, buff, 7, 1, 0);
  133. setImage(buff);
  134. buffFree(buff);
  135. }
  136. void simpleVisualization(uint8_t *data) {
  137. uint8_t *buff;
  138. buff = buffNew();
  139. // setRow(0, 0, maxVal(average(data), 0), buff); // Show average
  140. simpleVUMeter(data, buff, 7, 0, 0);
  141. setImage(buff);
  142. buffFree(buff);
  143. }
  144. void simpleSwitch(uint8_t *data) {
  145. uint8_t *buff;
  146. uint8_t i;
  147. buff = buffNew();
  148. for (i = 2; i < 6; i++) {
  149. simpleVUMeter(data, buff, i, 0, 1);
  150. }
  151. setImage(buff);
  152. buffFree(buff);
  153. }
  154. void linLog(uint8_t *data) {
  155. uint8_t *buff;
  156. buff = buffNew();
  157. simpleVUMeter(data, buff, 2, 1, 0);
  158. filterData(data, 0);
  159. simpleVUMeter(data, buff, 5, 0, 0);
  160. setImage(buff);
  161. buffFree(buff);
  162. }
  163. void fullDepthLog(uint8_t *data) {
  164. uint8_t *buff;
  165. uint8_t i;
  166. buff = buffNew();
  167. for (i = 0; i < 8; i++) {
  168. simpleVUMeter(data, buff, i, 1, 0);
  169. }
  170. setImage(buff);
  171. buffFree(buff);
  172. }
  173. void fullDepthVisualization(uint8_t *data) {
  174. uint8_t *buff;
  175. uint8_t i;
  176. buff = buffNew();
  177. for (i = 0; i < 8; i++) {
  178. simpleVUMeter(data, buff, i, 0, 0);
  179. }
  180. setImage(buff);
  181. buffFree(buff);
  182. }
  183. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
  184. uint8_t i;
  185. for (i = 0; i < height; i++) {
  186. buffSetPixel(buf, x, i, z);
  187. }
  188. }
  189. void horribleWave(uint8_t *audioData) {
  190. uint8_t *imageData = buffNew();
  191. // Could not figure out a way to represent this easily in a loop
  192. // without using a shitload of 'if's...
  193. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  194. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  195. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  196. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  197. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  198. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  199. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  200. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  201. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  202. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  203. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  204. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  205. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  206. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  207. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  208. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  209. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  210. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  211. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  212. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  213. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  214. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  215. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  216. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  217. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  218. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  219. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  220. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  221. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  222. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  223. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  224. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  225. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  226. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  227. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  228. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  229. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  230. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  231. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  232. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  233. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  234. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  235. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  236. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  237. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  238. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  239. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  240. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  241. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  242. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  243. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  244. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  245. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  246. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  247. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  248. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  249. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  250. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  251. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  252. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  253. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  254. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  255. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  256. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  257. setImage(imageData);
  258. buffFree(imageData);
  259. }