Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

main.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * main.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 <stdlib.h>
  24. #include <stdint.h>
  25. #include <avr/io.h>
  26. #include <util/delay.h>
  27. #include <avr/interrupt.h>
  28. #include "uart.h"
  29. #include "cube.h"
  30. #include "time.h"
  31. #include "audio.h"
  32. #ifndef F_CPU
  33. #define F_CPU 16000000L
  34. #endif
  35. uint8_t audioModeSelected(void);
  36. inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
  37. inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
  38. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf);
  39. void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
  40. int main(void) {
  41. char c;
  42. uint8_t *audioData;
  43. uint8_t **imageData;
  44. uint8_t i, j;
  45. uint64_t lastTimeChecked;
  46. uint8_t audioMode;
  47. DDRD = 0xFF; // Mosfets as Output
  48. DDRB = 0xFE;
  49. DDRC = 0xFF; // Latch Enable
  50. DDRA = 0xFF; // Latch Data
  51. imageData = (uint8_t **)malloc(8 * sizeof(uint8_t *));
  52. for (i = 0; i < 8; i++) {
  53. imageData[i] = (uint8_t *)malloc(8 * sizeof(uint8_t));
  54. }
  55. init(); // Initialize Cube Low-Level Code
  56. uart_init(UART_BAUD_SELECT(19200, 16000000L)); // Initialize Serial
  57. initSystemTimer();
  58. sei(); // Enable Interrupts
  59. audioMode = audioModeSelected();
  60. lastTimeChecked = getSystemTime();
  61. while (1) {
  62. if(audioMode) {
  63. // Get Audio Data and visualize it
  64. audioData = getAudioData();
  65. visualizeAudioData(audioData, imageData);
  66. setImage(imageData);
  67. free(audioData);
  68. while(!isFinished()); // Wait for it to display
  69. } else {
  70. // Look for commands, play from fram
  71. }
  72. if ((getSystemTime() - lastTimeChecked) > 1000) {
  73. // 1 second since we checked button position last time
  74. audioMode = audioModeSelected();
  75. lastTimeChecked = getSystemTime();
  76. }
  77. }
  78. close();
  79. return 0;
  80. }
  81. // Blocks 10ms or more
  82. uint8_t audioModeSelected(void) {
  83. // Switch: PB0, Low active
  84. uint64_t startTime = getSystemTime();
  85. uint8_t startState = PINB & (1 << PB0);
  86. while((getSystemTime() - startTime) < 10); // Wait 10ms
  87. if ((PINB & (1 << PB0)) != startState) {
  88. return audioModeSelected();
  89. } else {
  90. return startState;
  91. }
  92. }
  93. inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
  94. buf[z][y] |= (1 << x);
  95. }
  96. inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
  97. buf[z][y] &= ~(1 << x);
  98. }
  99. void setBuffer(uint8_t d, uint8_t *buf, uint8_t length) {
  100. uint8_t i;
  101. for (i = 0; i < length; i++) {
  102. buf[i] = d;
  103. }
  104. }
  105. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf) {
  106. uint8_t i = 0;
  107. for (; i < height; i++) {
  108. setPixelBuffer(x, i, z, buf);
  109. }
  110. }
  111. void visualizeAudioData(uint8_t *audioData, uint8_t **imageData) {
  112. uint8_t i;
  113. for (i = 0; i < 8; i++) {
  114. setBuffer(0, imageData[i], 8);
  115. }
  116. // 8 LEDs, Max Val 255:
  117. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  118. // 255 / FACTOR = 8,...
  119. // 127 / FACTOR = 4,...
  120. #define FACTOR 31
  121. // Could not figure out a way to represent this easily in a loop
  122. // without using a shitload of 'if's...
  123. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  124. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  125. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  126. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  127. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  128. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  129. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  130. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  131. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  132. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  133. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  134. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  135. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  136. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  137. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  138. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  139. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  140. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  141. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  142. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  143. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  144. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  145. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  146. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  147. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  148. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  149. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  150. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  151. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  152. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  153. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  154. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  155. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  156. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  157. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  158. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  159. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  160. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  161. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  162. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  163. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  164. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  165. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  166. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  167. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  168. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  169. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  170. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  171. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  172. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  173. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  174. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  175. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  176. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  177. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  178. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  179. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  180. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  181. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  182. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  183. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  184. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  185. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  186. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  187. }