Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

visualizer.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. buffClearAllPixels(buff);
  132. // setRow(0, 0, maxVal(average(data), 1), buff); // Show average
  133. simpleVUMeter(data, buff, 7, 1, 0);
  134. setImage(buff);
  135. buffFree(buff);
  136. }
  137. void simpleVisualization(uint8_t *data) {
  138. uint8_t *buff;
  139. buff = buffNew();
  140. buffClearAllPixels(buff);
  141. // setRow(0, 0, maxVal(average(data), 0), buff); // Show average
  142. simpleVUMeter(data, buff, 7, 0, 0);
  143. setImage(buff);
  144. buffFree(buff);
  145. }
  146. void simpleSwitch(uint8_t *data) {
  147. uint8_t *buff;
  148. uint8_t i;
  149. buff = buffNew();
  150. buffClearAllPixels(buff);
  151. for (i = 2; i < 6; i++) {
  152. simpleVUMeter(data, buff, i, 0, 1);
  153. }
  154. setImage(buff);
  155. buffFree(buff);
  156. }
  157. void linLog(uint8_t *data) {
  158. uint8_t *buff;
  159. buff = buffNew();
  160. buffClearAllPixels(buff);
  161. simpleVUMeter(data, buff, 2, 1, 0);
  162. filterData(data, 0);
  163. simpleVUMeter(data, buff, 5, 0, 0);
  164. setImage(buff);
  165. buffFree(buff);
  166. }
  167. void fullDepthLog(uint8_t *data) {
  168. uint8_t *buff;
  169. uint8_t i;
  170. buff = buffNew();
  171. buffClearAllPixels(buff);
  172. for (i = 0; i < 8; i++) {
  173. simpleVUMeter(data, buff, i, 1, 0);
  174. }
  175. setImage(buff);
  176. buffFree(buff);
  177. }
  178. void fullDepthVisualization(uint8_t *data) {
  179. uint8_t *buff;
  180. uint8_t i;
  181. buff = buffNew();
  182. buffClearAllPixels(buff);
  183. for (i = 0; i < 8; i++) {
  184. simpleVUMeter(data, buff, i, 0, 0);
  185. }
  186. setImage(buff);
  187. buffFree(buff);
  188. }
  189. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
  190. uint8_t i;
  191. for (i = 0; i < height; i++) {
  192. buffSetPixel(buf, x, i, z);
  193. }
  194. }
  195. void horribleWave(uint8_t *audioData) {
  196. uint8_t *imageData = buffNew();
  197. buffClearAllPixels(imageData);
  198. // Could not figure out a way to represent this easily in a loop
  199. // without using a shitload of 'if's...
  200. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  201. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  202. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  203. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  204. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  205. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  206. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  207. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  208. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  209. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  210. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  211. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  212. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  213. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  214. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  215. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  216. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  217. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  218. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  219. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  220. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  221. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  222. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  223. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  224. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  225. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  226. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  227. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  228. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  229. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  230. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  231. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  232. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  233. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  234. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  235. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  236. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  237. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  238. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  239. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  240. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  241. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  242. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  243. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  244. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  245. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  246. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  247. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  248. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  249. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  250. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  251. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  252. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  253. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  254. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  255. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  256. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  257. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  258. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  259. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  260. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  261. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  262. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  263. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  264. setImage(imageData);
  265. buffFree(imageData);
  266. }