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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * main.c
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef F_CPU
  22. #define F_CPU 16000000L
  23. #endif
  24. #define OK 0x42
  25. #define ERROR 0x23
  26. #include <avr/io.h>
  27. #include <util/delay.h>
  28. #include <avr/interrupt.h>
  29. #include <avr/pgmspace.h>
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <avr/wdt.h>
  33. #include "serial.h"
  34. #include "cube.h"
  35. #include "time.h"
  36. #include "audio.h"
  37. #include "mem.h"
  38. #include "memLayer.h"
  39. #include "twi.h"
  40. #include "strings.h"
  41. #include "visualizer.h"
  42. #include "animations.h"
  43. #include "transmit.h"
  44. #include "font.h"
  45. #include "buffhelp.h"
  46. // Length of an idle animation frame, 24 -> 1 second
  47. #define IDLELENGTH 48
  48. void init(void);
  49. uint8_t selfTest(void);
  50. void serialHandler(char c);
  51. uint8_t defaultImageCube[64] = { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81,
  55. 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 };
  59. #define NOERROR 0
  60. // Audio does not answer
  61. #define AUDIOERROR 1
  62. // Memory does not answer
  63. #define MEMORYERROR 2
  64. // Memory not writeable
  65. #define MEMORYWRITEERROR 4
  66. // x = errorcode, e = error definition, not NOERROR
  67. #define ISERROR(x, e) ((x) & (e))
  68. uint8_t shouldRestart = 0;
  69. uint8_t refreshAnimationCount = 0;
  70. uint8_t lastButtonState = 0;
  71. uint8_t maxButtonState = 1; // No audio, get's checked later
  72. uint8_t disableAudioData = 0;
  73. uint8_t disableMemory = 0;
  74. uint8_t disableAnim = 0;
  75. /* Not so powerful Debouncing Example
  76. * No Interrupt needed
  77. * Author: Peter Dannegger
  78. */
  79. #define debounce(port, pin) ({ \
  80. static uint8_t flag = 0; /* new variable on every macro usage */ \
  81. uint8_t i = 0; \
  82. if (flag) { /* check for key release: */ \
  83. for(;;) { /* loop ... */ \
  84. if(!(port & 1 << pin)) { /* ... until key pressed or ... */ \
  85. i = 0; \
  86. break; \
  87. } \
  88. _delay_us(98); /* * 256 = 25ms */ \
  89. if(--i == 0) { /* ... until key >25ms released */ \
  90. flag = 0; /* clear press flag */ \
  91. i = 0; /* 0 = key release debounced */ \
  92. break; \
  93. } \
  94. } \
  95. } else { /* else check for key press: */ \
  96. for(;;) { /* loop ... */ \
  97. if ((port & 1<<pin)) { /* ... until key released or ... */ \
  98. i = 0; \
  99. break; \
  100. } \
  101. _delay_us( 98 ); /* * 256 = 25ms */ \
  102. if (--i == 0) { /* ... until key >25ms pressed */ \
  103. flag = 1; /* set press flag */ \
  104. i = 1; /* 1 = key press debounced */ \
  105. break; \
  106. } \
  107. } \
  108. } \
  109. i; /* return value of Macro */ \
  110. })
  111. int main(void) {
  112. /*
  113. Quick Summary of the jobs main has to do:
  114. - Initialize Cube
  115. - Regularly check the buttonstate
  116. - Check for incoming serial transmission and run serialHandler
  117. - Check if animations are stored
  118. --> If yes, display them
  119. --> If no, display our idle animations
  120. */
  121. uint8_t i;
  122. uint8_t imageIndex = 0, imageCount = 0;
  123. uint8_t idleIndex = 0, idleCount = 0;
  124. uint8_t *imageData = NULL, *audioData = NULL;
  125. uint8_t duration = 0;
  126. // Initialization:
  127. MCUCSR = 0; // Reset Watchdog
  128. wdt_disable();
  129. init(); // Initialize all subsystems, set port directions, enable interrupts
  130. wdt_enable(WDTO_1S); // Watchdog reset after 1 second
  131. i = selfTest(); // Run selftest, print errors
  132. // Disable subsystems if they are unavailable
  133. if (ISERROR(i, AUDIOERROR))
  134. disableAudioData = 1;
  135. if (ISERROR(i, MEMORYERROR) || ISERROR(i, MEMORYWRITEERROR))
  136. disableMemory = 1;
  137. serialWriteString(getString(0)); // Print Version
  138. if (disableMemory == 0)
  139. imageCount = getAnimationCount(); // Retrieve image count from memory
  140. idleCount = numOfAnimations();
  141. if (disableAudioData == 0)
  142. maxButtonState = numberOfVisualizations() + 1; // Number of toggle steps for button
  143. lastButtonState = 1;
  144. serialWriteString(getString(2));
  145. while(1) { // Our Mainloop
  146. if (!shouldRestart) { // A flag to trigger a watchdog reset
  147. wdt_reset();
  148. }
  149. if (refreshAnimationCount) {
  150. refreshAnimationCount = 0;
  151. if (disableMemory == 0)
  152. imageCount = getAnimationCount();
  153. }
  154. if (serialHasChar()) { // Run serialHandler
  155. serialHandler((char)(serialGet()));
  156. }
  157. if (debounce(PINB, PB0)) {
  158. if (lastButtonState > 0) {
  159. lastButtonState--;
  160. } else {
  161. lastButtonState = maxButtonState - 1;
  162. }
  163. }
  164. if (lastButtonState == 0) {
  165. // Display animations, stored or built-in
  166. if (disableAnim == 0) {
  167. if ((imageCount > 0) && (disableMemory == 0)) {
  168. // Memory frames
  169. if (isFinished() > duration) {
  170. // Last frame was displayed long enough
  171. imageIndex = (imageIndex < (imageCount - 1)) ? (imageIndex + 1) : 0;
  172. imageData = getFrame(imageIndex);
  173. if (imageData == NULL) {
  174. duration = 24;
  175. setImage(defaultImageCube);
  176. } else {
  177. duration = imageData[64];
  178. setImage(imageData);
  179. free(imageData);
  180. }
  181. }
  182. } else {
  183. // Built-In Anims
  184. if (isFinished() > duration) {
  185. idleIndex = (idleIndex < (idleCount - 1)) ? (idleIndex + 1) : 0;
  186. duration = executeAnimation(idleIndex);
  187. }
  188. }
  189. }
  190. } else {
  191. // An audiomode is selected
  192. if (disableAudioData == 0) {
  193. if (isFinished()) {
  194. audioData = getAudioData();
  195. if (audioData != NULL) {
  196. runVisualization(audioData, (lastButtonState - 1));
  197. } else {
  198. lastButtonState = 0;
  199. duration = 24;
  200. setImage(defaultImageCube); // Quasi Error Screen
  201. }
  202. }
  203. } else {
  204. lastButtonState = 0;
  205. }
  206. }
  207. }
  208. return 0; // We never reach this point, of course!
  209. }
  210. void init(void) {
  211. DDRA = 0xFF; // Latch Data Bus as Output
  212. DDRD = 0xFC; DDRB = 24; // Mosfets as Output
  213. DDRC = 0xFC; DDRB |= 6; // Latch Enable as Output
  214. DDRB &= ~(1 << PB0); // Pushbutton as Input
  215. initCube();
  216. serialInit(25, 8, NONE, 1);
  217. i2c_init();
  218. initSystemTimer();
  219. sei(); // Enable Interrupts
  220. setImage(defaultImageCube); // Display something
  221. }
  222. uint8_t selfTest(void) {
  223. uint8_t result = NOERROR;
  224. uint8_t *data = getAudioData();
  225. if (data == NULL) {
  226. result |= AUDIOERROR;
  227. } else {
  228. }
  229. data = memGetBytes(0, 1);
  230. if (data == NULL) {
  231. result |= MEMORYERROR;
  232. } else {
  233. free(data);
  234. }
  235. setGeneralPurposeByte(0, 0x23);
  236. if (getGeneralPurposeByte(0) != 0x23) {
  237. result |= MEMORYWRITEERROR;
  238. }
  239. if (result) { // Error in Selftest
  240. serialWriteString(getString(1));
  241. serialWriteString(itoa(result, buffer, 2));
  242. serialWrite('\n');
  243. if (ISERROR(result, AUDIOERROR)) {
  244. serialWriteString(getString(3));
  245. }
  246. if (ISERROR(result, MEMORYERROR)) {
  247. serialWriteString(getString(4));
  248. }
  249. if (ISERROR(result, MEMORYWRITEERROR)) {
  250. serialWriteString(getString(5));
  251. }
  252. }
  253. return result;
  254. }
  255. void randomAnimation(void) {
  256. uint8_t *b = (uint8_t *)malloc(64);
  257. uint8_t x, y, z;
  258. if (b == NULL) {
  259. serialWriteString(getString(24));
  260. return;
  261. }
  262. for (x = 0; x < 64; x++) {
  263. b[x] = 0;
  264. }
  265. while(1) {
  266. wdt_reset();
  267. setImage(b);
  268. while(isFinished() == 0);
  269. x = rand() / 4096;
  270. y = rand() / 4096;
  271. z = rand() / 4096;
  272. b[x + (8 * y)] ^= (1 << z);
  273. if (serialHasChar()) {
  274. serialWriteString(getString(25));
  275. free(b);
  276. serialGet();
  277. return;
  278. }
  279. }
  280. free(b);
  281. }
  282. void serialHandler(char c) {
  283. // Used letters:
  284. // a, b, c, d, e, f, g, h, i, m, n, o, p, q, r, s, t, u, v, w, x, y, 0, 1, 2, 3, #
  285. uint8_t i, y, z;
  286. uint8_t *tmp;
  287. switch(c) {
  288. case OK:
  289. serialWrite(OK);
  290. break;
  291. case 'h': case 'H': case '?':
  292. serialWriteString(getString(6));
  293. serialWriteString(getString(7));
  294. serialWriteString(getString(8));
  295. serialWriteString(getString(9));
  296. serialWriteString(getString(10));
  297. serialWriteString(getString(26));
  298. serialWriteString(getString(34));
  299. serialWriteString(getString(17));
  300. serialWriteString(getString(11));
  301. serialWriteString(getString(12));
  302. serialWriteString(getString(13));
  303. break;
  304. case 'd': case 'D':
  305. setAnimationCount(0);
  306. serialWrite(OK);
  307. refreshAnimationCount = 1;
  308. break;
  309. case 'f': case 'F':
  310. serialWriteString(getString(32));
  311. clearMem();
  312. serialWriteString(getString(33));
  313. refreshAnimationCount = 1;
  314. break;
  315. case 'g': case 'G':
  316. transmitAnimations();
  317. break;
  318. case 's': case 'S':
  319. recieveAnimations();
  320. refreshAnimationCount = 1;
  321. break;
  322. case 'v': case 'V':
  323. serialWriteString(getString(0));
  324. break;
  325. case 'm': case 'M':
  326. if (lastButtonState > 0) {
  327. lastButtonState--;
  328. } else {
  329. lastButtonState = maxButtonState - 1;
  330. }
  331. if (lastButtonState) {
  332. writeNumber(lastButtonState, 10);
  333. serialWrite(' ');
  334. serialWriteString(getString(41));
  335. } else {
  336. serialWriteString(getString(40));
  337. }
  338. break;
  339. case 'q': case 'Q':
  340. shouldRestart = 1;
  341. serialWriteString(getString(30));
  342. break;
  343. case 'r': case 'R':
  344. randomAnimation();
  345. break;
  346. case 't': case 'T':
  347. printTime();
  348. break;
  349. case 'a': case 'A':
  350. sendAudioData();
  351. break;
  352. case 'c': case 'C':
  353. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  354. serialWriteString(" (");
  355. serialWriteString(itoa(refreshAnimationCount, buffer, 10));
  356. serialWriteString(")");
  357. serialWriteString(getString(15));
  358. break;
  359. case 'u': case 'U':
  360. serialWriteString(getString(31));
  361. tmp = getFrame(readNumber(10));
  362. dumpFrame(tmp);
  363. free(tmp);
  364. break;
  365. case 'o': case 'O':
  366. serialWriteString(getString(36)); // Start:
  367. i = readNumber(10);
  368. serialWriteString(getString(39)); // Ende:
  369. y = readNumber(10);
  370. serialWriteString(getString(35)); // Duration:
  371. z = readNumber(10);
  372. for (c = i; c <= y; c++) {
  373. setDuration(c, z);
  374. }
  375. serialWriteString(getString(33)); // Done
  376. break;
  377. case 'w': case 'W':
  378. serialWriteString(getString(42)); // Text:
  379. textRenderInput();
  380. break;
  381. case 'x': case 'X':
  382. // Get byte, store as animation count
  383. serialWriteString(getString(16)); // "New animation count: "
  384. setAnimationCount(readNumber(10));
  385. refreshAnimationCount = 1;
  386. break;
  387. case 'p': case 'P':
  388. simpleAnimationInput();
  389. break;
  390. case 'y': case 'Y':
  391. serialWriteString(getString(18)); // "Frame to change: "
  392. i = readNumber(10);
  393. tmp = readAFrame();
  394. setFrame(i, tmp);
  395. free(tmp);
  396. break;
  397. case 'e': case 'E':
  398. selfTest();
  399. break;
  400. case 'n': case 'N':
  401. // snake();
  402. break;
  403. case '0':
  404. fillBuffer(0);
  405. disableAnim = 1;
  406. break;
  407. case '1':
  408. fillBuffer(0xFF);
  409. disableAnim = 1;
  410. break;
  411. case '2':
  412. buffClearAllPixels(defaultImageCube);
  413. while(1) {
  414. for (i = 0; i < 8; i++) {
  415. for (y = 0; y < 8; y++) {
  416. defaultImageCube[y + (i * 8)] = 0;
  417. for (z = 0; z < 8; z++) {
  418. defaultImageCube[y + (i * 8)] |= (1 << z);
  419. setImage(defaultImageCube);
  420. while (isFinished() == 0) {
  421. wdt_reset();
  422. if (serialHasChar()) {
  423. goto killMeForIt; // Yes I know...
  424. // But I need to break out of 2 while Loops...
  425. }
  426. }
  427. }
  428. // defaultImageCube[y + (i * 8)] = 0;
  429. }
  430. }
  431. }
  432. break;
  433. killMeForIt:
  434. serialGet(); // Killed because we got a serial char. Remove it from buffer.
  435. serialWriteString(getString(25));
  436. break;
  437. case '3':
  438. buffClearAllPixels(defaultImageCube);
  439. y = 0;
  440. while(1) {
  441. tmp = getFont(y);
  442. if (y < 255) {
  443. y++;
  444. } else {
  445. y = 0;
  446. }
  447. for (i = 0; i < 8; i++) {
  448. defaultImageCube[i] = tmp[i];
  449. }
  450. setImage(defaultImageCube);
  451. while (isFinished() < 12) {
  452. wdt_reset();
  453. if (serialHasChar()) {
  454. goto killMeForIt; // Yes, yes. But i already need it for '2'
  455. // So why not do it again... Please, no raptors :)
  456. // http://xkcd.com/292/
  457. }
  458. }
  459. }
  460. break;
  461. case 'I': case 'i':
  462. serialWriteString(ltoa(getTriggerCount(), buffer, 10));
  463. serialWrite('\n');
  464. break;
  465. default:
  466. serialWrite(ERROR);
  467. break;
  468. }
  469. // c was used as temp var and does not contain the char anymore...!
  470. }