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

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