Simple single-color 8x8x8 LED Cube with AVRs
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

main.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #include "memLayer.h"
  33. #ifndef F_CPU
  34. #define F_CPU 16000000L
  35. #endif
  36. #define OK 0x42
  37. #define ERROR 0x23
  38. void serialHandler(char c);
  39. void recieveAnimations(void);
  40. void transmitAnimations(void);
  41. uint8_t audioModeSelected(void);
  42. inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
  43. inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
  44. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf);
  45. void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
  46. uint8_t refreshAnimationCount = 1;
  47. int main(void) {
  48. unsigned int character;
  49. uint8_t *audioData;
  50. uint8_t **imageData;
  51. uint8_t i;
  52. uint64_t lastTimeChecked;
  53. uint8_t audioMode;
  54. uint16_t count;
  55. DDRD = 0xFF; // Mosfets as Output
  56. DDRB = 0xFE;
  57. DDRC = 0xFF; // Latch Enable
  58. DDRA = 0xFF; // Latch Data
  59. imageData = (uint8_t **)malloc(8 * sizeof(uint8_t *));
  60. for (i = 0; i < 8; i++) {
  61. imageData[i] = (uint8_t *)malloc(8 * sizeof(uint8_t));
  62. }
  63. init(); // Initialize Cube Low-Level Code
  64. uart_init(UART_BAUD_SELECT(19200, 16000000L)); // Initialize Serial
  65. initSystemTimer();
  66. sei(); // Enable Interrupts
  67. audioMode = audioModeSelected();
  68. lastTimeChecked = getSystemTime();
  69. while (1) {
  70. if(audioMode) {
  71. // Get Audio Data and visualize it
  72. audioData = getAudioData();
  73. visualizeAudioData(audioData, imageData);
  74. setImage(imageData);
  75. free(audioData);
  76. while(!isFinished()); // Wait for it to display
  77. } else {
  78. // Look for commands, play from fram
  79. // We have 128*1024 bytes
  80. // A Frame needs 65 bytes (64 data + duration)
  81. // We place 2016 Frames in mem => 131040
  82. // That gives us 32 bytes at the beginning, 0 -> 31
  83. // The first frame starts at 32
  84. character = uart_getc();
  85. if (!(character & 0xFF00)) { // No error...
  86. serialHandler((char)(character & 0x00FF));
  87. }
  88. if (refreshAnimationCount) {
  89. count = getAnimationCount();
  90. refreshAnimationCount = 0;
  91. }
  92. }
  93. if ((getSystemTime() - lastTimeChecked) > 1000) {
  94. // 1 second since we checked button position last time
  95. audioMode = audioModeSelected();
  96. lastTimeChecked = getSystemTime();
  97. }
  98. }
  99. close();
  100. return 0;
  101. }
  102. void serialHandler(char c) {
  103. // Got a char on serial line...
  104. // React accordingly
  105. // Set refreshAnimationCount if Animation Data in Ram was changed...
  106. // We do a complete transaction in one call of this routine...
  107. switch(c) {
  108. case OK:
  109. // Send "Hello World"
  110. uart_putc(OK);
  111. break;
  112. case 'd': case 'D':
  113. clearMem();
  114. uart_putc(OK);
  115. break;
  116. case 'g': case 'G':
  117. transmitAnimations();
  118. break;
  119. case 's': case 'S':
  120. recieveAnimations();
  121. break;
  122. default:
  123. uart_putc(ERROR);
  124. break;
  125. }
  126. }
  127. void recieveAnimations() {
  128. uint8_t animCount, a, frameCount, f, i;
  129. uint16_t completeCount = 0, character;
  130. uint8_t frame[65];
  131. uart_putc(OK); // We are ready...
  132. character = uart_getc();
  133. while (character & 0xFF00) { // Wait for answer
  134. character = uart_getc();
  135. }
  136. animCount = (uint8_t)(character & 0x00FF); // Got animation count
  137. uart_putc(OK);
  138. for (a = 0; a < animCount; a++) {
  139. character = uart_getc();
  140. while (character & 0xFF00) { // Wait for answer
  141. character = uart_getc();
  142. }
  143. frameCount = (uint8_t)(character & 0x00FF); // Got frame count
  144. uart_putc(OK);
  145. for (f = 0; f < frameCount; f++) {
  146. character = uart_getc();
  147. while (character & 0xFF00) { // Wait for answer
  148. character = uart_getc();
  149. }
  150. frame[64] = (uint8_t)(character & 0x00FF); // Got duration
  151. uart_putc(OK);
  152. for (i = 0; i < 64; i++) {
  153. character = uart_getc();
  154. while (character & 0xFF00) { // Wait for answer
  155. character = uart_getc();
  156. }
  157. frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
  158. }
  159. uart_putc(OK);
  160. setFrame(completeCount++, frame);
  161. }
  162. }
  163. character = uart_getc();
  164. while (character & 0xFF00) { // Wait for answer
  165. character = uart_getc();
  166. }
  167. character = uart_getc();
  168. while (character & 0xFF00) { // Wait for answer
  169. character = uart_getc();
  170. }
  171. character = uart_getc();
  172. while (character & 0xFF00) { // Wait for answer
  173. character = uart_getc();
  174. }
  175. character = uart_getc();
  176. while (character & 0xFF00) { // Wait for answer
  177. character = uart_getc();
  178. }
  179. uart_putc(OK);
  180. setAnimationCount(completeCount);
  181. refreshAnimationCount = 1;
  182. }
  183. #define CAF(x) (x % 256)
  184. #define CAS(x) (x / 256)
  185. #define CALCANIMATIONS(x) ((CAF(x) == 0) ? (CAS(x)) : (CAS(x) + 1))
  186. void transmitAnimations() {
  187. // We store no animation information in here
  188. // So we have to place all frames in one or more
  189. // animations... We need 8 animations max...
  190. uint8_t animationsToGo;
  191. uint16_t framesToGo = getAnimationCount();
  192. uint16_t character;
  193. uint8_t a;
  194. uint8_t f, fMax, i;
  195. uint8_t *frame;
  196. if ((framesToGo % 255) == 0) {
  197. animationsToGo = framesToGo / 255;
  198. } else {
  199. animationsToGo = (framesToGo / 255) + 1;
  200. }
  201. uart_putc(OK);
  202. uart_putc(animationsToGo);
  203. while ((character = uart_getc()) & 0xFF00); // Wait for answer
  204. if ((character & 0x00FF) != OK) { // Error code recieved
  205. return;
  206. }
  207. for (a = 0; a < animationsToGo; a++) {
  208. if (framesToGo > 255) {
  209. fMax = 255;
  210. } else {
  211. fMax = framesToGo;
  212. }
  213. uart_putc(fMax); // Number of Frames in current animation
  214. while ((character = uart_getc()) & 0xFF00); // Wait for answer
  215. if ((character & 0x00FF) != OK) { // Error code recieved
  216. return;
  217. }
  218. for (f = 0; f < fMax; f++) {
  219. frame = getFrame(f + (255 * a));
  220. uart_putc(frame[64]); // frame duration
  221. while ((character = uart_getc()) & 0xFF00); // Wait for answer
  222. if ((character & 0x00FF) != OK) { // Error code recieved
  223. free(frame);
  224. return;
  225. }
  226. for (i = 0; i < 64; i++) {
  227. uart_putc(frame[i]);
  228. }
  229. while ((character = uart_getc()) & 0xFF00); // Wait for answer
  230. if ((character & 0x00FF) != OK) { // Error code recieved
  231. free(frame);
  232. return;
  233. }
  234. free(frame);
  235. }
  236. framesToGo -= fMax;
  237. }
  238. uart_putc(OK);
  239. uart_putc(OK);
  240. uart_putc(OK);
  241. uart_putc(OK);
  242. while ((character = uart_getc()) & 0xFF00); // Wait for answer
  243. // Error code ignored...
  244. }
  245. // Blocks 10ms or more
  246. uint8_t audioModeSelected(void) {
  247. // Switch: PB0, Low active
  248. uint64_t startTime = getSystemTime();
  249. uint8_t startState = PINB & (1 << PB0);
  250. while((getSystemTime() - startTime) < 10); // Wait 10ms
  251. if ((PINB & (1 << PB0)) != startState) {
  252. return audioModeSelected();
  253. } else {
  254. return startState;
  255. }
  256. }
  257. inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
  258. buf[z][y] |= (1 << x);
  259. }
  260. inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
  261. buf[z][y] &= ~(1 << x);
  262. }
  263. void setBuffer(uint8_t d, uint8_t *buf, uint8_t length) {
  264. uint8_t i;
  265. for (i = 0; i < length; i++) {
  266. buf[i] = d;
  267. }
  268. }
  269. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf) {
  270. uint8_t i = 0;
  271. for (; i < height; i++) {
  272. setPixelBuffer(x, i, z, buf);
  273. }
  274. }
  275. void visualizeAudioData(uint8_t *audioData, uint8_t **imageData) {
  276. uint8_t i;
  277. for (i = 0; i < 8; i++) {
  278. setBuffer(0, imageData[i], 8);
  279. }
  280. // 8 LEDs, Max Val 255:
  281. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  282. // 255 / FACTOR = 8,...
  283. // 127 / FACTOR = 4,...
  284. #define FACTOR 31
  285. // Could not figure out a way to represent this easily in a loop
  286. // without using a shitload of 'if's...
  287. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  288. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  289. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  290. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  291. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  292. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  293. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  294. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  295. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  296. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  297. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  298. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  299. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  300. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  301. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  302. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  303. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  304. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  305. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  306. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  307. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  308. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  309. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  310. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  311. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  312. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  313. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  314. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  315. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  316. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  317. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  318. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  319. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  320. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  321. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  322. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  323. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  324. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  325. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  326. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  327. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  328. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  329. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  330. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  331. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  332. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  333. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  334. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  335. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  336. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  337. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  338. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  339. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  340. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  341. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  342. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  343. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  344. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  345. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  346. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  347. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  348. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  349. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  350. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  351. }