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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 30
  32. uint8_t maxVal(uint8_t data, uint8_t log);
  33. uint8_t average(uint8_t *data);
  34. uint8_t maximum(uint8_t *data);
  35. void filterData(uint8_t *data, uint8_t log);
  36. void simpleVisualization(uint8_t *data);
  37. void simpleSwitch(uint8_t *data);
  38. void fullDepthVisualization(uint8_t *data);
  39. void simpleLog(uint8_t *data);
  40. void fullDepthLog(uint8_t *data);
  41. void linLog(uint8_t *data);
  42. #define LINEARVISUALS 3
  43. void (*linear[LINEARVISUALS])(uint8_t *data) = { &simpleVisualization, &fullDepthVisualization,
  44. &simpleSwitch };
  45. #define LOGARITHMICVISUALS 3
  46. void (*logarithmic[LOGARITHMICVISUALS])(uint8_t *data) = { &linLog, &simpleLog, &fullDepthLog };
  47. #define NUMOFVISUALIZATIONS (LINEARVISUALS + LOGARITHMICVISUALS)
  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 < LINEARVISUALS) {
  55. filterData(data, 0); // Linear filter
  56. linear[id](data); // run it
  57. } else if ((id - LINEARVISUALS) < LOGARITHMICVISUALS) {
  58. filterData(data, 1); // Logarithmic filter
  59. logarithmic[id - LINEARVISUALS](data); // run it
  60. } else {
  61. // Invalid, do nothing!
  62. }
  63. }
  64. }
  65. uint8_t maximum(uint8_t *data) {
  66. uint8_t i, max = 0;
  67. for (i = 0; i < 7; i++) {
  68. if (data[i] > max) {
  69. max = data[i];
  70. }
  71. }
  72. return max;
  73. }
  74. uint8_t maxVal(uint8_t data, uint8_t log) {
  75. uint8_t max = 0;
  76. if (log) {
  77. while ((max <= 7) && (data > logScale[max])) // Some bitshifting would do it...
  78. max++; // But this is more fine grained
  79. } else {
  80. max = data / FACTOR;
  81. }
  82. return max;
  83. }
  84. uint8_t average(uint8_t *data) {
  85. uint16_t sum = 0;
  86. uint8_t i;
  87. for (i = 0; i < 7; i++) {
  88. sum += data[i];
  89. }
  90. sum /= 7;
  91. return (uint8_t)sum;
  92. }
  93. void filterData(uint8_t *data, uint8_t log) {
  94. uint8_t i;
  95. uint8_t max = THRESHOLD;
  96. if (log && (average(data) >= max)) {
  97. max = THRESHOLD / 2;
  98. }
  99. if (average(data) < max) {
  100. for (i = 0; i < 7; i++) {
  101. data[i] = 0;
  102. }
  103. }
  104. }
  105. void simpleVUMeter(uint8_t *data, uint8_t *buff, uint8_t z, uint8_t log, uint8_t change) {
  106. uint8_t i, h = 0, max;
  107. for(i = 0; i < 7; i++) {
  108. max = maxVal(data[i], log);
  109. for (h = 0; h < max; h++) {
  110. if (i == 0) {
  111. if (change) {
  112. buffSetPixel(buff, i, z, (h * 10 / 15));
  113. } else {
  114. buffSetPixel(buff, i, (h * 10 / 15), z);
  115. }
  116. }
  117. if (change) {
  118. buffSetPixel(buff, i + 1, z, h);
  119. } else {
  120. buffSetPixel(buff, i + 1, h, z);
  121. }
  122. }
  123. }
  124. }
  125. void simpleLog(uint8_t *data) {
  126. uint8_t *buff;
  127. buff = buffNew();
  128. // setRow(0, 0, maxVal(average(data), 1), buff); // Show average
  129. simpleVUMeter(data, buff, 7, 1, 0);
  130. setImage(buff);
  131. buffFree(buff);
  132. }
  133. void simpleVisualization(uint8_t *data) {
  134. uint8_t *buff;
  135. buff = buffNew();
  136. // setRow(0, 0, maxVal(average(data), 0), buff); // Show average
  137. simpleVUMeter(data, buff, 7, 0, 0);
  138. setImage(buff);
  139. buffFree(buff);
  140. }
  141. void simpleSwitch(uint8_t *data) {
  142. uint8_t *buff;
  143. uint8_t i;
  144. buff = buffNew();
  145. for (i = 2; i < 6; i++) {
  146. simpleVUMeter(data, buff, i, 0, 1);
  147. }
  148. setImage(buff);
  149. buffFree(buff);
  150. }
  151. void linLog(uint8_t *data) {
  152. uint8_t *buff;
  153. buff = buffNew();
  154. simpleVUMeter(data, buff, 2, 1, 0);
  155. filterData(data, 0);
  156. simpleVUMeter(data, buff, 5, 0, 0);
  157. setImage(buff);
  158. buffFree(buff);
  159. }
  160. void fullDepthLog(uint8_t *data) {
  161. uint8_t *buff;
  162. uint8_t i;
  163. buff = buffNew();
  164. for (i = 0; i < 8; i++) {
  165. simpleVUMeter(data, buff, i, 1, 0);
  166. }
  167. setImage(buff);
  168. buffFree(buff);
  169. }
  170. void fullDepthVisualization(uint8_t *data) {
  171. uint8_t *buff;
  172. uint8_t i;
  173. buff = buffNew();
  174. for (i = 0; i < 8; i++) {
  175. simpleVUMeter(data, buff, i, 0, 0);
  176. }
  177. setImage(buff);
  178. buffFree(buff);
  179. }