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.

main.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #ifndef F_CPU
  24. #define F_CPU 16000000L
  25. #endif
  26. #define OK 0x42
  27. #define ERROR 0x23
  28. #include <avr/io.h>
  29. #include <util/delay.h>
  30. #include <avr/interrupt.h>
  31. #include <avr/pgmspace.h>
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <avr/wdt.h>
  35. #include "serial.h"
  36. #include "cube.h"
  37. #include "time.h"
  38. #include "audio.h"
  39. #include "mem.h"
  40. #include "memLayer.h"
  41. #include "twi.h"
  42. #include "strings.h"
  43. #include "visualizer.h"
  44. #include "animations.h"
  45. #include "transmit.h"
  46. #define NOERROR 0
  47. // Audio does not answer
  48. #define AUDIOERROR 1
  49. // Memory does not answer
  50. #define MEMORYERROR 2
  51. // Memory not writeable
  52. #define MEMORYWRITEERROR 4
  53. // x = errorcode, e = error definition, not NOERROR
  54. #define ISERROR(x, e) ((x) & (e))
  55. // Length of an idle animation frame, 24 -> 1 second
  56. #define IDLELENGTH 48
  57. void serialHandler(char c);
  58. uint8_t audioModeSelected(void);
  59. #ifdef DEBUG
  60. void printErrors(uint8_t e);
  61. uint8_t selfTest(void);
  62. void printTime(void);
  63. #include "snake.c"
  64. #endif
  65. uint8_t shouldRestart = 0;
  66. uint8_t refreshAnimationCount = 1;
  67. uint8_t lastButtonState = 0;
  68. uint8_t maxButtonState = 0;
  69. uint8_t mcusr_mirror;
  70. char buffer[11];
  71. #include "builtInFrames.c"
  72. uint8_t DebugDone = 0; // Bit 0: 10s int. count, Bit 1: idle switch
  73. // Bit 2: state changed, disable idle
  74. int main(void) {
  75. uint8_t *audioData = NULL;
  76. uint8_t *imageData = NULL;
  77. uint8_t i, length = 0;
  78. uint16_t count;
  79. uint64_t lastChecked;
  80. uint8_t idleCounter = 0;
  81. #ifdef DEBUG
  82. uint32_t temp;
  83. #endif
  84. mcusr_mirror = MCUCSR;
  85. MCUCSR = 0;
  86. wdt_disable();
  87. DDRA = 0xFF; // Latch Data Bus as Output
  88. DDRD = 0xFC; DDRB = 24; // Mosfets as Output
  89. DDRC = 0xFC; DDRB |= 6; // Latch Enable as Output
  90. DDRB &= ~(1 << PB0); // Pushbutton as Input
  91. initCube();
  92. serialInit(25, 8, NONE, 1);
  93. i2c_init();
  94. initSystemTimer();
  95. sei(); // Enable Interrupts
  96. setImage(defaultImageCube); // Display something
  97. wdt_enable(WDTO_1S); // Watchdog reset after 1 second
  98. #ifdef DEBUG
  99. // Kill animation counter in debug mode
  100. // => Don't preserve animations while power down
  101. setAnimationCount(0);
  102. serialWriteString(getString(2));
  103. i = selfTest();
  104. if (i) {
  105. serialWriteString(getString(1));
  106. serialWriteString(itoa(i, buffer, 2));
  107. serialWrite('\n');
  108. printErrors(i);
  109. }
  110. serialWriteString(getString(0));
  111. if (mcusr_mirror & WDRF) {
  112. serialWriteString(getString(31));
  113. } else if (mcusr_mirror & BORF) {
  114. serialWriteString(getString(32));
  115. } else if (mcusr_mirror & EXTRF) {
  116. serialWriteString(getString(34));
  117. } else if (mcusr_mirror & JTRF) {
  118. serialWriteString(getString(35));
  119. } else if (mcusr_mirror & PORF) {
  120. serialWriteString(getString(36));
  121. } else {
  122. serialWriteString(getString(33));
  123. }
  124. #endif
  125. maxButtonState = numberOfVisualizations() + 1; // All visualizations and anim mode
  126. audioModeSelected();
  127. lastChecked = getSystemTime();
  128. i = 0; // Image count
  129. count = getAnimationCount();
  130. while (1) {
  131. // Reset if requested
  132. if (!shouldRestart) {
  133. wdt_reset();
  134. }
  135. if(lastButtonState >= 1) {
  136. // Get Audio Data and visualize it.
  137. // Visualization id is in (lastButtonState - 1)
  138. if (isFinished()) {
  139. audioData = getAudioData(); // Not malloc'ed => Don't free
  140. if (audioData != NULL) {
  141. runVisualization(audioData, lastButtonState - 1);
  142. }
  143. }
  144. } else {
  145. if (refreshAnimationCount) {
  146. // Get animation count stored in FRAM via TWI, if needed
  147. count = getAnimationCount();
  148. refreshAnimationCount = 0;
  149. i = 0;
  150. }
  151. if (count > 0) { // We have frames stored
  152. if (isFinished() > length) {
  153. // Load next image
  154. if (i < (count - 1)) {
  155. i++;
  156. } else {
  157. i = 0;
  158. }
  159. imageData = getFrame(i);
  160. length = imageData[64];
  161. setImage(imageData);
  162. free(imageData);
  163. }
  164. } else { // No frames available
  165. if (!(DebugDone & 4)) { // Idle animation allowed
  166. if (DebugDone & 2) {
  167. if (idleCounter < numOfAnimations()) {
  168. executeAnimation(idleCounter++);
  169. } else {
  170. idleCounter = 0;
  171. DebugDone &= ~(2); // Show frames again
  172. }
  173. } else {
  174. // Show idle frames
  175. if (isFinished() >= IDLELENGTH) {
  176. setImage(idleAnimation[idleCounter]);
  177. if (idleCounter < IDLEANIMATIONCOUNT) {
  178. idleCounter++;
  179. } else {
  180. idleCounter = 0;
  181. DebugDone |= 2; // Show animation
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. if (serialHasChar()) {
  189. serialHandler((char)(serialGet()));
  190. }
  191. #ifdef DEBUG
  192. // Print frames per second
  193. if ((getSystemTime() >= 1000) && ((DebugDone & 1) == 0)) {
  194. temp = getTriggerCount();
  195. serialWriteString(ltoa(temp, buffer, 10));
  196. serialWriteString(getString(27));
  197. serialWriteString(ltoa((temp / 8), buffer, 10));
  198. serialWriteString(getString(28));
  199. DebugDone |= 1;
  200. }
  201. #endif
  202. if ((getSystemTime() - lastChecked) > 150) { // Check button state every 150ms
  203. audioModeSelected();
  204. lastChecked = getSystemTime();
  205. }
  206. }
  207. close();
  208. return 0;
  209. }
  210. uint8_t audioModeSelected(void) {
  211. // Pushbutton: PB0, Low active
  212. if (!(PINB & (1 << PB0))) {
  213. // Button pushed
  214. if (lastButtonState < (maxButtonState - 1)) {
  215. lastButtonState++;
  216. } else {
  217. lastButtonState = 0;
  218. }
  219. #ifdef DEBUG
  220. if (lastButtonState) {
  221. serialWriteString(getString(38));
  222. } else {
  223. serialWriteString(getString(39));
  224. }
  225. #endif
  226. }
  227. return lastButtonState;
  228. }
  229. #ifdef DEBUG
  230. uint8_t selfTest(void) {
  231. uint8_t result = NOERROR;
  232. uint8_t *data = getAudioData();
  233. if (data == NULL) {
  234. result |= AUDIOERROR;
  235. } else {
  236. free(data);
  237. }
  238. data = memGetBytes(0, 1);
  239. if (data == NULL) {
  240. result |= MEMORYERROR;
  241. } else {
  242. free(data);
  243. }
  244. setGeneralPurposeByte(0, 0x23);
  245. if (getGeneralPurposeByte(0) != 0x23) {
  246. result |= MEMORYWRITEERROR;
  247. }
  248. return result;
  249. }
  250. void printErrors(uint8_t e) {
  251. if (ISERROR(e, AUDIOERROR)) {
  252. serialWriteString(getString(3));
  253. }
  254. if (ISERROR(e, MEMORYERROR)) {
  255. serialWriteString(getString(4));
  256. }
  257. if (ISERROR(e, MEMORYWRITEERROR)) {
  258. serialWriteString(getString(5));
  259. }
  260. }
  261. void randomAnimation(void) {
  262. uint8_t *b = (uint8_t *)malloc(64);
  263. uint8_t x, y, z;
  264. if (b == NULL) {
  265. serialWriteString(getString(24));
  266. return;
  267. }
  268. for (x = 0; x < 64; x++) {
  269. b[x] = 0;
  270. }
  271. while(1) {
  272. setImage(b);
  273. while(isFinished() == 0);
  274. x = rand() / 4096;
  275. y = rand() / 4096;
  276. z = rand() / 4096;
  277. b[x + (8 * y)] ^= (1 << z);
  278. if (serialHasChar()) {
  279. serialWriteString(getString(25));
  280. free(b);
  281. serialHandler(serialGet());
  282. return;
  283. }
  284. }
  285. free(b);
  286. }
  287. #endif
  288. void serialHandler(char c) {
  289. // Used letters:
  290. // a, b, c, d, e, g, i, n, q, r, s, t, v, x, y, 0, 1, 2, 3, #
  291. #ifdef DEBUG
  292. uint8_t i, y, z;
  293. serialWrite(c);
  294. serialWriteString(": ");
  295. #endif
  296. switch(c) {
  297. case OK:
  298. serialWrite(OK);
  299. break;
  300. case 'h': case 'H': case '?':
  301. serialWriteString(getString(6));
  302. #ifdef DEBUG
  303. serialWriteString(getString(7));
  304. serialWriteString(getString(8));
  305. serialWriteString(getString(9));
  306. serialWriteString(getString(10));
  307. serialWriteString(getString(11));
  308. serialWriteString(getString(12));
  309. serialWriteString(getString(13));
  310. serialWriteString(getString(26));
  311. #endif
  312. break;
  313. case 'd': case 'D':
  314. clearMem();
  315. #ifndef DEBUG
  316. serialWrite(OK);
  317. #endif
  318. #ifdef DEBUG
  319. serialWriteString(getString(29));
  320. #endif
  321. break;
  322. #ifndef DEBUG
  323. case 'g': case 'G':
  324. transmitAnimations();
  325. break;
  326. case 's': case 'S':
  327. recieveAnimations();
  328. break;
  329. #endif
  330. case 'v': case 'V':
  331. serialWriteString(getString(0));
  332. break;
  333. #ifdef DEBUG
  334. case 'm': case 'M':
  335. lastButtonState = !lastButtonState;
  336. if (lastButtonState) {
  337. serialWriteString(getString(41));
  338. } else {
  339. serialWriteString(getString(40));
  340. }
  341. break;
  342. case 'q': case 'Q':
  343. shouldRestart = 1;
  344. serialWriteString(getString(30));
  345. break;
  346. case 'r': case 'R':
  347. randomAnimation();
  348. break;
  349. case 't': case 'T':
  350. printTime();
  351. break;
  352. case 'a': case 'A':
  353. sendAudioData();
  354. break;
  355. case 'c': case 'C':
  356. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  357. serialWriteString(getString(15));
  358. break;
  359. case 'x': case 'X':
  360. // Get byte, store as animation count
  361. serialWriteString(getString(16));
  362. while (!serialHasChar());
  363. c = serialGet();
  364. setAnimationCount(c);
  365. serialWriteString(itoa(c, buffer, 10));
  366. serialWriteString(getString(17));
  367. break;
  368. case 'y': case 'Y':
  369. setAnimationCount(0x2201);
  370. serialWriteString(getString(18));
  371. break;
  372. case 'e': case 'E':
  373. c = selfTest();
  374. serialWriteString(getString(19));
  375. serialWriteString(itoa(c, buffer, 2));
  376. serialWrite('\n');
  377. printErrors(c);
  378. break;
  379. case 'n': case 'N':
  380. snake();
  381. break;
  382. case '0':
  383. fillBuffer(0);
  384. DebugDone |= 4;
  385. break;
  386. case '1':
  387. fillBuffer(0xFF);
  388. DebugDone |= 4;
  389. break;
  390. case '3':
  391. setImage(defaultImageCube);
  392. DebugDone |= 4;
  393. break;
  394. case '2':
  395. DebugDone |= 4;
  396. fillBuffer(0);
  397. for (i = 0; i < 64; i++) {
  398. defaultImageA[i] = 0;
  399. }
  400. while(1) {
  401. for (i = 0; i < 8; i++) {
  402. for (y = 0; y < 8; y++) {
  403. defaultImageA[y + (i * 8)] = 0;
  404. for (z = 0; z < 8; z++) {
  405. defaultImageA[y + (i * 8)] |= (1 << z);
  406. setImage(defaultImageA);
  407. while (isFinished() == 0) {
  408. wdt_reset();
  409. if (serialHasChar()) {
  410. goto killMeForIt; // Yes I know...
  411. // But I need to break out of 2 while Loops...
  412. }
  413. }
  414. }
  415. defaultImageA[y + (i * 8)] = 0;
  416. }
  417. }
  418. }
  419. break;
  420. killMeForIt:
  421. serialGet();
  422. serialWriteString(getString(25));
  423. break;
  424. case 'I': case 'i':
  425. serialWriteString(ltoa(getTriggerCount(), buffer, 10));
  426. serialWrite('\n');
  427. break;
  428. #endif
  429. default:
  430. serialWrite(ERROR);
  431. break;
  432. }
  433. // c was used as temp var and does not contain the char anymore...!
  434. }
  435. #ifdef DEBUG
  436. void printTime(void) {
  437. serialWriteString(getString(14));
  438. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  439. serialWriteString("ms");
  440. if (getSystemTime() > 60000) {
  441. serialWriteString(" (");
  442. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  443. serialWriteString(" min)");
  444. }
  445. if (getSystemTime() > 1000) {
  446. serialWriteString(" (");
  447. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  448. itoa(getSystemTime() % 1000, buffer, 10);
  449. if (buffer[0] != '\0')
  450. serialWrite('.');
  451. if (buffer[2] == '\0')
  452. serialWrite('0');
  453. if (buffer[1] == '\0')
  454. serialWrite('0');
  455. if (buffer[0] != '\0')
  456. serialWriteString(buffer);
  457. serialWriteString("s)\n");
  458. } else {
  459. serialWrite('\n');
  460. }
  461. }
  462. #endif