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.

transmit.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * transmit.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. #include <avr/io.h>
  24. #include <util/delay.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <avr/wdt.h>
  28. #include "memLayer.h"
  29. #include "serial.h"
  30. #include "time.h"
  31. #include "strings.h"
  32. #include "audio.h"
  33. #define OK 0x42
  34. #define ERROR 0x23
  35. #define TRANSTIMEOUT 2500
  36. // These are global variables from main.c
  37. extern char buffer[11];
  38. extern uint8_t refreshAnimationCount;
  39. void recieveAnimations(void) {
  40. uint8_t animCount, a, frameCount, f, i, c;
  41. uint16_t completeCount = 0;
  42. uint8_t *frame = (uint8_t *)malloc(65);
  43. uint64_t timestamp = getSystemTime();
  44. serialWrite(OK); // We are ready...
  45. while (!serialHasChar()) { // Wait for answer
  46. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  47. wdt_reset();
  48. } else {
  49. setAnimationCount(0);
  50. return;
  51. }
  52. }
  53. c = serialGet();
  54. animCount = c; // Got animation count
  55. serialWrite(OK);
  56. for (a = 0; a < animCount; a++) {
  57. while (!serialHasChar()) { // Wait for answer
  58. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  59. wdt_reset();
  60. } else {
  61. setAnimationCount(0);
  62. return;
  63. }
  64. }
  65. c = serialGet();
  66. frameCount = c; // Got frame count
  67. serialWrite(OK);
  68. for (f = 0; f < frameCount; f++) {
  69. while (!serialHasChar()) { // Wait for answer
  70. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  71. wdt_reset();
  72. } else {
  73. setAnimationCount(0);
  74. return;
  75. }
  76. }
  77. c = serialGet();
  78. frame[64] = c; // Got duration
  79. serialWrite(OK);
  80. for (i = 0; i < 64; i++) {
  81. while (!serialHasChar()) { // Wait for answer
  82. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  83. wdt_reset();
  84. } else {
  85. setAnimationCount(0);
  86. return;
  87. }
  88. }
  89. c = serialGet();
  90. frame[i] = c; // Got data byte
  91. }
  92. serialWrite(OK);
  93. setFrame(completeCount++, frame);
  94. }
  95. }
  96. free(frame);
  97. while (!serialHasChar()) { // Wait for answer
  98. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  99. wdt_reset();
  100. } else {
  101. setAnimationCount(0);
  102. return;
  103. }
  104. }
  105. c = serialGet();
  106. while (!serialHasChar()) { // Wait for answer
  107. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  108. wdt_reset();
  109. } else {
  110. setAnimationCount(0);
  111. return;
  112. }
  113. }
  114. c = serialGet();
  115. while (!serialHasChar()) { // Wait for answer
  116. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  117. wdt_reset();
  118. } else {
  119. setAnimationCount(0);
  120. return;
  121. }
  122. }
  123. c = serialGet();
  124. while (!serialHasChar()) { // Wait for answer
  125. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  126. wdt_reset();
  127. } else {
  128. setAnimationCount(0);
  129. return;
  130. }
  131. }
  132. c = serialGet();
  133. serialWrite(OK);
  134. setAnimationCount(completeCount);
  135. refreshAnimationCount = 1;
  136. }
  137. // TODO: Rewrite for new Serial API
  138. void transmitAnimations(void) {
  139. // We store no animation information in here
  140. // So we have to place all frames in one or more
  141. // animations... We need 8 animations max...
  142. uint8_t animationsToGo;
  143. uint16_t framesToGo = getAnimationCount();
  144. uint16_t character;
  145. uint8_t a;
  146. uint8_t f, fMax, i;
  147. uint8_t *frame;
  148. if ((framesToGo % 255) == 0) {
  149. animationsToGo = framesToGo / 255;
  150. } else {
  151. animationsToGo = (framesToGo / 255) + 1;
  152. }
  153. serialWrite(OK);
  154. serialWrite(animationsToGo);
  155. while ((character = serialGet()) & 0xFF00); // Wait for answer
  156. if ((character & 0x00FF) != OK) { // Error code recieved
  157. return;
  158. }
  159. for (a = 0; a < animationsToGo; a++) {
  160. if (framesToGo > 255) {
  161. fMax = 255;
  162. } else {
  163. fMax = framesToGo;
  164. }
  165. serialWrite(fMax); // Number of Frames in current animation
  166. while ((character = serialGet()) & 0xFF00); // Wait for answer
  167. if ((character & 0x00FF) != OK) { // Error code recieved
  168. return;
  169. }
  170. for (f = 0; f < fMax; f++) {
  171. frame = getFrame(f + (255 * a));
  172. serialWrite(frame[64]); // frame duration
  173. while ((character = serialGet()) & 0xFF00); // Wait for answer
  174. if ((character & 0x00FF) != OK) { // Error code recieved
  175. free(frame);
  176. return;
  177. }
  178. for (i = 0; i < 64; i++) {
  179. serialWrite(frame[i]);
  180. }
  181. while ((character = serialGet()) & 0xFF00); // Wait for answer
  182. if ((character & 0x00FF) != OK) { // Error code recieved
  183. free(frame);
  184. return;
  185. }
  186. free(frame);
  187. }
  188. framesToGo -= fMax;
  189. }
  190. serialWrite(OK);
  191. serialWrite(OK);
  192. serialWrite(OK);
  193. serialWrite(OK);
  194. while ((character = serialGet()) & 0xFF00); // Wait for answer
  195. // Error code ignored...
  196. }
  197. void sendAudioData(void) {
  198. uint8_t i;
  199. uint8_t *audioData = getAudioData();
  200. if (audioData == NULL) {
  201. serialWriteString(getString(21));
  202. } else {
  203. serialWriteString(getString(22));
  204. for (i = 0; i < 7; i++) {
  205. serialWrite(i + '0');
  206. serialWriteString(": ");
  207. itoa(audioData[i], buffer, 10);
  208. serialWriteString(buffer);
  209. serialWrite('\n');
  210. }
  211. }
  212. }
  213. void printTime(void) {
  214. serialWriteString(getString(14));
  215. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  216. serialWriteString("ms");
  217. if (getSystemTime() > 60000) {
  218. serialWriteString(" (");
  219. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  220. serialWriteString(" min)");
  221. }
  222. if (getSystemTime() > 1000) {
  223. serialWriteString(" (");
  224. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  225. itoa(getSystemTime() % 1000, buffer, 10);
  226. if (buffer[0] != '\0')
  227. serialWrite('.');
  228. if (buffer[2] == '\0')
  229. serialWrite('0');
  230. if (buffer[1] == '\0')
  231. serialWrite('0');
  232. if (buffer[0] != '\0')
  233. serialWriteString(buffer);
  234. serialWriteString("s)\n");
  235. } else {
  236. serialWrite('\n');
  237. }
  238. }