Simple single-color 8x8x8 LED Cube with AVRs
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

main.c 11KB

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