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.

main.c 11KB

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