Simple single-color 8x8x8 LED Cube with AVRs
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

transmit.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 1500
  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. }
  49. }
  50. c = serialGet();
  51. animCount = c; // Got animation count
  52. serialWrite(OK);
  53. for (a = 0; a < animCount; a++) {
  54. while (!serialHasChar()) { // Wait for answer
  55. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  56. wdt_reset();
  57. }
  58. }
  59. c = serialGet();
  60. frameCount = c; // Got frame count
  61. serialWrite(OK);
  62. for (f = 0; f < frameCount; f++) {
  63. while (!serialHasChar()) { // Wait for answer
  64. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  65. wdt_reset();
  66. }
  67. }
  68. c = serialGet();
  69. frame[64] = c; // Got duration
  70. serialWrite(OK);
  71. for (i = 0; i < 64; i++) {
  72. while (!serialHasChar()) { // Wait for answer
  73. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  74. wdt_reset();
  75. }
  76. }
  77. c = serialGet();
  78. frame[i] = c; // Got data byte
  79. }
  80. serialWrite(OK);
  81. setFrame(completeCount++, frame);
  82. }
  83. }
  84. free(frame);
  85. while (!serialHasChar()) { // Wait for answer
  86. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  87. wdt_reset();
  88. }
  89. }
  90. c = serialGet();
  91. while (!serialHasChar()) { // Wait for answer
  92. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  93. wdt_reset();
  94. }
  95. }
  96. c = serialGet();
  97. while (!serialHasChar()) { // Wait for answer
  98. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  99. wdt_reset();
  100. }
  101. }
  102. c = serialGet();
  103. while (!serialHasChar()) { // Wait for answer
  104. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  105. wdt_reset();
  106. }
  107. }
  108. c = serialGet();
  109. serialWrite(OK);
  110. setAnimationCount(completeCount);
  111. refreshAnimationCount = 1;
  112. }
  113. // TODO: Rewrite for new Serial API
  114. void transmitAnimations(void) {
  115. // We store no animation information in here
  116. // So we have to place all frames in one or more
  117. // animations... We need 8 animations max...
  118. uint8_t animationsToGo;
  119. uint16_t framesToGo = getAnimationCount();
  120. uint16_t character;
  121. uint8_t a;
  122. uint8_t f, fMax, i;
  123. uint8_t *frame;
  124. if ((framesToGo % 255) == 0) {
  125. animationsToGo = framesToGo / 255;
  126. } else {
  127. animationsToGo = (framesToGo / 255) + 1;
  128. }
  129. serialWrite(OK);
  130. serialWrite(animationsToGo);
  131. while ((character = serialGet()) & 0xFF00); // Wait for answer
  132. if ((character & 0x00FF) != OK) { // Error code recieved
  133. return;
  134. }
  135. for (a = 0; a < animationsToGo; a++) {
  136. if (framesToGo > 255) {
  137. fMax = 255;
  138. } else {
  139. fMax = framesToGo;
  140. }
  141. serialWrite(fMax); // Number of Frames in current animation
  142. while ((character = serialGet()) & 0xFF00); // Wait for answer
  143. if ((character & 0x00FF) != OK) { // Error code recieved
  144. return;
  145. }
  146. for (f = 0; f < fMax; f++) {
  147. frame = getFrame(f + (255 * a));
  148. serialWrite(frame[64]); // frame duration
  149. while ((character = serialGet()) & 0xFF00); // Wait for answer
  150. if ((character & 0x00FF) != OK) { // Error code recieved
  151. free(frame);
  152. return;
  153. }
  154. for (i = 0; i < 64; i++) {
  155. serialWrite(frame[i]);
  156. }
  157. while ((character = serialGet()) & 0xFF00); // Wait for answer
  158. if ((character & 0x00FF) != OK) { // Error code recieved
  159. free(frame);
  160. return;
  161. }
  162. free(frame);
  163. }
  164. framesToGo -= fMax;
  165. }
  166. serialWrite(OK);
  167. serialWrite(OK);
  168. serialWrite(OK);
  169. serialWrite(OK);
  170. while ((character = serialGet()) & 0xFF00); // Wait for answer
  171. // Error code ignored...
  172. }
  173. void sendAudioData(void) {
  174. uint8_t i;
  175. uint8_t *audioData = getAudioData();
  176. if (audioData == NULL) {
  177. serialWriteString(getString(21));
  178. } else {
  179. serialWriteString(getString(22));
  180. for (i = 0; i < 7; i++) {
  181. serialWrite(i + '0');
  182. serialWriteString(": ");
  183. itoa(audioData[i], buffer, 10);
  184. serialWriteString(buffer);
  185. serialWrite('\n');
  186. }
  187. }
  188. }