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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. // Length of an idle animation frame, 24 -> 1 second
  47. #define IDLELENGTH 48
  48. void init(void);
  49. uint8_t audioModeSelected(void);
  50. uint8_t selfTest(void);
  51. void serialHandler(char c);
  52. uint8_t defaultImageCube[64] = { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  60. #define NOERROR 0
  61. // Audio does not answer
  62. #define AUDIOERROR 1
  63. // Memory does not answer
  64. #define MEMORYERROR 2
  65. // Memory not writeable
  66. #define MEMORYWRITEERROR 4
  67. // x = errorcode, e = error definition, not NOERROR
  68. #define ISERROR(x, e) ((x) & (e))
  69. uint8_t shouldRestart = 0;
  70. uint8_t refreshAnimationCount = 0;
  71. uint8_t lastButtonState = 0;
  72. uint8_t maxButtonState = 1; // No audio, get's checked later
  73. uint8_t disableAudioData = 0;
  74. uint8_t disableMemory = 0;
  75. uint8_t disableAnim = 0;
  76. char buffer[11];
  77. int main(void) {
  78. /*
  79. Quick Summary of the jobs main has to do:
  80. - Initialize Cube
  81. - Regularly check the buttonstate
  82. - Check for incoming serial transmission and run serialHandler
  83. - Check if animations are stored
  84. --> If yes, display them
  85. --> If no, display our idle animations
  86. */
  87. uint8_t i;
  88. uint8_t imageIndex = 0, imageCount = 0;
  89. uint8_t idleIndex = 0, idleCount = 0;
  90. uint8_t lastButtonCheck, fpsWasSent = 0;
  91. uint32_t temp;
  92. uint8_t *imageData = NULL, *audioData = NULL;
  93. uint8_t duration = 0;
  94. // Initialization:
  95. MCUCSR = 0; // Reset Watchdog
  96. wdt_disable();
  97. init(); // Initialize all subsystems, set port directions, enable interrupts
  98. wdt_enable(WDTO_1S); // Watchdog reset after 1 second
  99. i = selfTest(); // Run selftest, print errors
  100. // Disable subsystems if they are unavailable
  101. if (ISERROR(i, AUDIOERROR))
  102. disableAudioData = 1;
  103. if (ISERROR(i, MEMORYERROR) || ISERROR(i, MEMORYWRITEERROR))
  104. disableMemory = 1;
  105. serialWriteString(getString(0)); // Print Version
  106. audioModeSelected(); // Initial button state check
  107. lastButtonCheck = getSystemTime(); // Time we checked
  108. if (disableMemory == 0)
  109. imageCount = getAnimationCount(); // Retrieve image count from memory
  110. idleCount = numOfAnimations();
  111. if (disableAudioData == 0)
  112. maxButtonState = numberOfVisualizations() + 1; // Number of toggle steps for button
  113. while(1) { // Our Mainloop
  114. if (!shouldRestart) { // A flag to trigger a watchdog reset
  115. wdt_reset();
  116. }
  117. if (refreshAnimationCount) {
  118. refreshAnimationCount = 0;
  119. if (disableMemory == 0)
  120. imageCount = getAnimationCount();
  121. }
  122. if (serialHasChar()) { // Run serialHandler
  123. serialHandler((char)(serialGet()));
  124. }
  125. if ((getSystemTime() - lastButtonCheck) > 150) { // Check button state every 150ms
  126. audioModeSelected();
  127. lastButtonCheck = getSystemTime();
  128. }
  129. if (lastButtonState == 0) {
  130. // Display animations, stored or built-in
  131. if (disableAnim == 0) {
  132. if ((imageCount > 0) && (disableMemory == 0)) {
  133. // Memory frames
  134. if (isFinished() > duration) {
  135. // Last frame was displayed long enough
  136. imageIndex = (imageIndex < (imageCount - 1)) ? (imageIndex + 1) : 0;
  137. imageData = getFrame(i);
  138. if (imageData == NULL) {
  139. duration = 24;
  140. setImage(defaultImageCube);
  141. } else {
  142. duration = imageData[64];
  143. setImage(imageData);
  144. free(imageData);
  145. }
  146. }
  147. } else {
  148. // Built-In Frames
  149. if (isFinished()) {
  150. idleIndex = (idleIndex < (idleCount - 1)) ? (idleIndex + 1) : 0;
  151. executeAnimation(idleIndex);
  152. }
  153. }
  154. }
  155. } else {
  156. // An audiomode is selected
  157. }
  158. // Print fps after one second
  159. if ((getSystemTime() >= 1000) && (fpsWasSent == 0)) {
  160. temp = getTriggerCount();
  161. serialWriteString(ltoa(temp, buffer, 10));
  162. serialWriteString(getString(27));
  163. serialWriteString(ltoa((temp / 8), buffer, 10));
  164. serialWriteString(getString(28));
  165. fpsWasSent = 1;
  166. }
  167. }
  168. close(); // This is of course unneccessary. We never reach this point.
  169. return 0;
  170. }
  171. void init(void) {
  172. DDRA = 0xFF; // Latch Data Bus as Output
  173. DDRD = 0xFC; DDRB = 24; // Mosfets as Output
  174. DDRC = 0xFC; DDRB |= 6; // Latch Enable as Output
  175. DDRB &= ~(1 << PB0); // Pushbutton as Input
  176. initCube();
  177. serialInit(25, 8, NONE, 1);
  178. i2c_init();
  179. initSystemTimer();
  180. sei(); // Enable Interrupts
  181. setImage(defaultImageCube); // Display something
  182. }
  183. uint8_t audioModeSelected(void) {
  184. // Pushbutton: PB0, Low active
  185. if (!(PINB & (1 << PB0))) {
  186. // Button pushed
  187. if (lastButtonState < (maxButtonState - 1)) {
  188. lastButtonState++;
  189. } else {
  190. lastButtonState = 0;
  191. }
  192. }
  193. return lastButtonState;
  194. }
  195. uint8_t selfTest(void) {
  196. uint8_t result = NOERROR;
  197. uint8_t *data = getAudioData();
  198. if (data == NULL) {
  199. result |= AUDIOERROR;
  200. } else {
  201. free(data);
  202. }
  203. data = memGetBytes(0, 1);
  204. if (data == NULL) {
  205. result |= MEMORYERROR;
  206. } else {
  207. free(data);
  208. }
  209. setGeneralPurposeByte(0, 0x23);
  210. if (getGeneralPurposeByte(0) != 0x23) {
  211. result |= MEMORYWRITEERROR;
  212. }
  213. if (result) { // Error in Selftest
  214. serialWriteString(getString(1));
  215. serialWriteString(itoa(result, buffer, 2));
  216. serialWrite('\n');
  217. if (ISERROR(result, AUDIOERROR)) {
  218. serialWriteString(getString(3));
  219. }
  220. if (ISERROR(result, MEMORYERROR)) {
  221. serialWriteString(getString(4));
  222. }
  223. if (ISERROR(result, MEMORYWRITEERROR)) {
  224. serialWriteString(getString(5));
  225. }
  226. }
  227. return result;
  228. }
  229. void randomAnimation(void) {
  230. uint8_t *b = (uint8_t *)malloc(64);
  231. uint8_t x, y, z;
  232. if (b == NULL) {
  233. serialWriteString(getString(24));
  234. return;
  235. }
  236. for (x = 0; x < 64; x++) {
  237. b[x] = 0;
  238. }
  239. while(1) {
  240. wdt_reset();
  241. setImage(b);
  242. while(isFinished() == 0);
  243. x = rand() / 4096;
  244. y = rand() / 4096;
  245. z = rand() / 4096;
  246. b[x + (8 * y)] ^= (1 << z);
  247. if (serialHasChar()) {
  248. serialWriteString(getString(25));
  249. free(b);
  250. serialGet();
  251. return;
  252. }
  253. }
  254. free(b);
  255. }
  256. void serialHandler(char c) {
  257. // Used letters:
  258. // a, b, c, d, e, g, i, n, q, r, s, t, v, x, y, 0, 1, 2, 3, #
  259. uint8_t i, y, z;
  260. switch(c) {
  261. case OK:
  262. serialWrite(OK);
  263. break;
  264. case 'h': case 'H': case '?':
  265. serialWriteString(getString(6));
  266. serialWriteString(getString(7));
  267. serialWriteString(getString(8));
  268. serialWriteString(getString(9));
  269. serialWriteString(getString(10));
  270. serialWriteString(getString(11));
  271. serialWriteString(getString(12));
  272. serialWriteString(getString(13));
  273. serialWriteString(getString(26));
  274. break;
  275. case 'd': case 'D':
  276. // clearMem(); // Much too invasive
  277. setAnimationCount(0);
  278. serialWrite(OK);
  279. refreshAnimationCount = 1;
  280. break;
  281. case 'g': case 'G':
  282. transmitAnimations();
  283. break;
  284. case 's': case 'S':
  285. recieveAnimations();
  286. break;
  287. case 'v': case 'V':
  288. serialWriteString(getString(0));
  289. break;
  290. case 'm': case 'M':
  291. lastButtonState = !lastButtonState;
  292. if (lastButtonState) {
  293. serialWriteString(getString(41));
  294. } else {
  295. serialWriteString(getString(40));
  296. }
  297. break;
  298. case 'q': case 'Q':
  299. shouldRestart = 1;
  300. serialWriteString(getString(30));
  301. break;
  302. case 'r': case 'R':
  303. randomAnimation();
  304. break;
  305. case 't': case 'T':
  306. printTime();
  307. break;
  308. case 'a': case 'A':
  309. sendAudioData();
  310. break;
  311. case 'c': case 'C':
  312. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  313. serialWriteString(getString(15));
  314. break;
  315. case 'x': case 'X':
  316. // Get byte, store as animation count
  317. serialWriteString(getString(16));
  318. while (!serialHasChar());
  319. c = serialGet();
  320. setAnimationCount(c);
  321. serialWriteString(itoa(c, buffer, 10));
  322. serialWriteString(getString(17));
  323. break;
  324. case 'y': case 'Y':
  325. setAnimationCount(0x2201);
  326. serialWriteString(getString(18));
  327. break;
  328. case 'e': case 'E':
  329. selfTest();
  330. break;
  331. case 'n': case 'N':
  332. // snake();
  333. break;
  334. case '0':
  335. fillBuffer(0);
  336. disableAnim = 1;
  337. break;
  338. case '1':
  339. fillBuffer(0xFF);
  340. disableAnim = 1;
  341. break;
  342. case '3':
  343. setImage(defaultImageCube);
  344. disableAnim = 1;
  345. break;
  346. case '2':
  347. fillBuffer(0);
  348. while(1) {
  349. for (i = 0; i < 8; i++) {
  350. for (y = 0; y < 8; y++) {
  351. defaultImageCube[y + (i * 8)] = 0;
  352. for (z = 0; z < 8; z++) {
  353. defaultImageCube[y + (i * 8)] |= (1 << z);
  354. setImage(defaultImageCube);
  355. while (isFinished() == 0) {
  356. wdt_reset();
  357. if (serialHasChar()) {
  358. goto killMeForIt; // Yes I know...
  359. // But I need to break out of 2 while Loops...
  360. }
  361. }
  362. }
  363. // defaultImageCube[y + (i * 8)] = 0;
  364. }
  365. }
  366. }
  367. break;
  368. killMeForIt:
  369. serialGet(); // Killed because we got a serial char. Remove it from buffer.
  370. serialWriteString(getString(25));
  371. break;
  372. case 'I': case 'i':
  373. serialWriteString(ltoa(getTriggerCount(), buffer, 10));
  374. serialWrite('\n');
  375. break;
  376. default:
  377. serialWrite(ERROR);
  378. break;
  379. }
  380. // c was used as temp var and does not contain the char anymore...!
  381. }