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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. 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. // Should happen every half second
  177. setImage(idleAnimation[idleCounter]);
  178. if (idleCounter < (IDLEANIMATIONCOUNT - 1)) {
  179. idleCounter++;
  180. } else {
  181. idleCounter = 0;
  182. DebugDone |= 2; // Show animation
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. if (serialHasChar()) {
  190. serialHandler((char)(serialGet()));
  191. }
  192. #ifdef DEBUG
  193. // Print frames per second
  194. if ((getSystemTime() >= 1000) && ((DebugDone & 1) == 0)) {
  195. temp = getTriggerCount();
  196. serialWriteString(ltoa(temp, buffer, 10));
  197. serialWriteString(getString(27));
  198. serialWriteString(ltoa((temp / 8), buffer, 10));
  199. serialWriteString(getString(28));
  200. DebugDone |= 1;
  201. }
  202. // Show how stable we are running :)
  203. if (((getSystemTime() % 60000) == 0) && (getSystemTime() > 0)) {
  204. // serialWriteString(getString(37));
  205. printTime();
  206. }
  207. #endif
  208. if ((getSystemTime() - lastChecked) > 150) { // Check button state every 150ms
  209. lastMode = audioModeSelected();
  210. lastChecked = getSystemTime();
  211. }
  212. }
  213. close();
  214. return 0;
  215. }
  216. #ifdef DEBUG
  217. uint8_t selfTest(void) {
  218. uint8_t result = NOERROR;
  219. uint8_t *data = getAudioData();
  220. if (data == NULL) {
  221. result |= AUDIOERROR;
  222. } else {
  223. free(data);
  224. }
  225. data = memGetBytes(0, 1);
  226. if (data == NULL) {
  227. result |= MEMORYERROR;
  228. } else {
  229. free(data);
  230. }
  231. setGeneralPurposeByte(0, 0x23);
  232. if (getGeneralPurposeByte(0) != 0x23) {
  233. result |= MEMORYWRITEERROR;
  234. }
  235. return result;
  236. }
  237. void printErrors(uint8_t e) {
  238. if (ISERROR(e, AUDIOERROR)) {
  239. serialWriteString(getString(3));
  240. }
  241. if (ISERROR(e, MEMORYERROR)) {
  242. serialWriteString(getString(4));
  243. }
  244. if (ISERROR(e, MEMORYWRITEERROR)) {
  245. serialWriteString(getString(5));
  246. }
  247. }
  248. void randomAnimation(void) {
  249. uint8_t *b = (uint8_t *)malloc(64);
  250. uint8_t x, y, z;
  251. if (b == NULL) {
  252. serialWriteString(getString(24));
  253. return;
  254. }
  255. for (x = 0; x < 64; x++) {
  256. b[x] = 0;
  257. }
  258. while(1) {
  259. setImage(b);
  260. while(isFinished() == 0);
  261. x = rand() / 4096;
  262. y = rand() / 4096;
  263. z = rand() / 4096;
  264. b[x + (8 * y)] ^= (1 << z);
  265. if (serialHasChar()) {
  266. serialWriteString(getString(25));
  267. free(b);
  268. serialHandler(serialGet());
  269. return;
  270. }
  271. }
  272. free(b);
  273. }
  274. #endif
  275. void serialHandler(char c) {
  276. // Used letters:
  277. // a, b, c, d, e, g, i, n, q, r, s, t, v, x, y, 0, 1, 2, 3, #
  278. #ifdef DEBUG
  279. uint8_t i, y, z;
  280. serialWrite(c);
  281. serialWriteString(": ");
  282. #endif
  283. switch(c) {
  284. case OK:
  285. serialWrite(OK);
  286. break;
  287. case 'h': case 'H': case '?':
  288. serialWriteString(getString(6));
  289. #ifdef DEBUG
  290. serialWriteString(getString(7));
  291. serialWriteString(getString(8));
  292. serialWriteString(getString(9));
  293. serialWriteString(getString(10));
  294. serialWriteString(getString(11));
  295. serialWriteString(getString(12));
  296. serialWriteString(getString(13));
  297. serialWriteString(getString(26));
  298. #endif
  299. break;
  300. case 'd': case 'D':
  301. clearMem();
  302. #ifndef DEBUG
  303. serialWrite(OK);
  304. #endif
  305. #ifdef DEBUG
  306. serialWriteString(getString(29));
  307. #endif
  308. break;
  309. #ifndef DEBUG
  310. case 'g': case 'G':
  311. transmitAnimations();
  312. break;
  313. case 's': case 'S':
  314. recieveAnimations();
  315. break;
  316. #endif
  317. case 'v': case 'V':
  318. serialWriteString(getString(0));
  319. break;
  320. #ifdef DEBUG
  321. case 'm': case 'M':
  322. lastButtonState = !lastButtonState;
  323. if (lastButtonState) {
  324. serialWriteString(getString(41));
  325. } else {
  326. serialWriteString(getString(40));
  327. }
  328. break;
  329. case 'q': case 'Q':
  330. shouldRestart = 1;
  331. serialWriteString(getString(30));
  332. break;
  333. case 'r': case 'R':
  334. randomAnimation();
  335. break;
  336. case 't': case 'T':
  337. printTime();
  338. break;
  339. case 'a': case 'A':
  340. sendAudioData();
  341. break;
  342. case 'c': case 'C':
  343. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  344. serialWriteString(getString(15));
  345. break;
  346. case 'x': case 'X':
  347. // Get byte, store as animation count
  348. serialWriteString(getString(16));
  349. while (!serialHasChar());
  350. c = serialGet();
  351. setAnimationCount(c);
  352. serialWriteString(itoa(c, buffer, 10));
  353. serialWriteString(getString(17));
  354. break;
  355. case 'y': case 'Y':
  356. setAnimationCount(0x2201);
  357. serialWriteString(getString(18));
  358. break;
  359. case 'e': case 'E':
  360. c = selfTest();
  361. serialWriteString(getString(19));
  362. serialWriteString(itoa(c, buffer, 2));
  363. serialWrite('\n');
  364. printErrors(c);
  365. break;
  366. case 'n': case 'N':
  367. snake();
  368. break;
  369. case '0':
  370. fillBuffer(0);
  371. DebugDone |= 4;
  372. break;
  373. case '1':
  374. fillBuffer(0xFF);
  375. DebugDone |= 4;
  376. break;
  377. case '3':
  378. setImage(defaultImageCube);
  379. DebugDone |= 4;
  380. break;
  381. case '2':
  382. DebugDone |= 4;
  383. fillBuffer(0);
  384. for (i = 0; i < 64; i++) {
  385. defaultImageA[i] = 0;
  386. }
  387. while(1) {
  388. for (i = 0; i < 8; i++) {
  389. for (y = 0; y < 8; y++) {
  390. defaultImageA[y + (i * 8)] = 0;
  391. for (z = 0; z < 8; z++) {
  392. defaultImageA[y + (i * 8)] |= (1 << z);
  393. setImage(defaultImageA);
  394. while (isFinished() == 0) {
  395. if (serialHasChar()) {
  396. goto killMeForIt; // Yes I know...
  397. // But I need to break out of 2 while Loops...
  398. }
  399. }
  400. }
  401. defaultImageA[y + (i * 8)] = 0;
  402. }
  403. }
  404. }
  405. break;
  406. killMeForIt:
  407. serialGet();
  408. serialWriteString(getString(25));
  409. break;
  410. case 'I': case 'i':
  411. serialWriteString(ltoa(getTriggerCount(), buffer, 10));
  412. serialWrite('\n');
  413. break;
  414. #endif
  415. default:
  416. serialWrite(ERROR);
  417. break;
  418. }
  419. // c was used as temp var and does not contain the char anymore...!
  420. }
  421. #ifdef DEBUG
  422. void printTime(void) {
  423. serialWriteString(getString(14));
  424. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  425. serialWriteString("ms");
  426. if (getSystemTime() > 60000) {
  427. serialWriteString(" (");
  428. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  429. serialWriteString(" min)");
  430. }
  431. if (getSystemTime() > 1000) {
  432. serialWriteString(" (");
  433. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  434. itoa(getSystemTime() % 1000, buffer, 10);
  435. if (buffer[0] != '\0')
  436. serialWrite('.');
  437. if (buffer[2] == '\0')
  438. serialWrite('0');
  439. if (buffer[1] == '\0')
  440. serialWrite('0');
  441. if (buffer[0] != '\0')
  442. serialWriteString(buffer);
  443. serialWriteString("s)\n");
  444. } else {
  445. serialWrite('\n');
  446. }
  447. }
  448. #endif
  449. uint8_t audioModeSelected(void) {
  450. // Pushbutton: PB0, Low active
  451. if (!(PINB & (1 << PB0))) {
  452. // Button pushed
  453. if (lastButtonState == 0) {
  454. lastButtonState = 1;
  455. } else {
  456. lastButtonState = 0;
  457. }
  458. #ifdef DEBUG
  459. if (lastButtonState) {
  460. serialWriteString(getString(38));
  461. } else {
  462. serialWriteString(getString(39));
  463. }
  464. #endif
  465. }
  466. return lastButtonState;
  467. }