Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

animations.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * animations.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 <stdint.h>
  24. #include <stdlib.h>
  25. #include <avr/io.h>
  26. #include <avr/wdt.h>
  27. #include <buffhelp.h>
  28. #include <cube.h>
  29. /*
  30. HOW TO Write a new Animation
  31. A) Think of a name, create a function Prototype right below this comment.
  32. B) Increment NUMOFANIMATION by the number of animations you are going to add.
  33. C) Put a function pointer to your new animation into the animations[] Array.
  34. D) Implement your animation!
  35. --> Get a buffer with buffNew(); Display it with setImage(); Free Buffer after usage!
  36. --> Return desired duration until the next animation is called, in frames (24fps)
  37. */
  38. // Prototypes for all animations
  39. void upWave(uint8_t i);
  40. void downWave(uint8_t i);
  41. void xWave1(uint8_t i);
  42. void xWave2(uint8_t i);
  43. void zWave1(uint8_t i);
  44. void zWave2(uint8_t i);
  45. // Array of animation functions
  46. #define NUMOFANIMATIONS 0
  47. uint8_t (*animations[NUMOFANIMATIONS])(void) = { };
  48. #define WAVELENGTH 2
  49. uint8_t numOfAnimations(void) {
  50. return NUMOFANIMATIONS + 24;
  51. }
  52. uint8_t executeAnimation(uint8_t id) {
  53. if (id < (6*4)) {
  54. if (id < 4) {
  55. upWave(id);
  56. } else if (id < 8) {
  57. downWave(id - 4);
  58. } else if (id < 12) {
  59. xWave1(id - 8);
  60. } else if (id < 16) {
  61. xWave2(id - 12);
  62. } else if (id < 20) {
  63. zWave1(id - 16);
  64. } else {
  65. zWave2(id - 20);
  66. }
  67. return 1;
  68. } else if ((id - (6*4)) < NUMOFANIMATIONS) {
  69. return animations[id - (6*4)](); // Call animation
  70. }
  71. return 1;
  72. }
  73. void upWave(uint8_t i) {
  74. uint8_t *buff;
  75. int8_t x, y, z;
  76. buff = buffNew();
  77. // Up-wave
  78. for(y = (i * 2); y < ((i * 2) + 2); y++) {
  79. for(x = 0; x < 8; x++) {
  80. for(z = 0; z < 8; z++) {
  81. buffSetPixel(buff, x, y, z);
  82. }
  83. }
  84. setImage(buff);
  85. while(isFinished() < WAVELENGTH) {
  86. wdt_reset();
  87. }
  88. buffClearAllPixels(buff);
  89. }
  90. free(buff);
  91. }
  92. void downWave(uint8_t i) {
  93. uint8_t *buff;
  94. int8_t x, y, z;
  95. buff = buffNew();
  96. // Down-wave (Frames 9-15 of showoff.cube)
  97. for(y = (7 - (2 * i)); y >= ((7 - (2 * i)) - 1); y--) {
  98. for(x = 0; x < 8; x++) {
  99. for(z = 0; z < 8; z++) {
  100. buffSetPixel(buff, x, y, z);
  101. }
  102. }
  103. setImage(buff);
  104. while(isFinished() < WAVELENGTH) {
  105. wdt_reset();
  106. }
  107. buffClearAllPixels(buff);
  108. }
  109. free(buff);
  110. }
  111. void xWave1(uint8_t i) {
  112. uint8_t *buff;
  113. int8_t x, y, z;
  114. buff = buffNew();
  115. // x-axis wave
  116. for(x = (i * 2); x < ((i * 2) + 2); x++) {
  117. for(y = 0; y < 8; y++) {
  118. for(z = 0; z < 8; z++) {
  119. buffSetPixel(buff, x, y, z);
  120. }
  121. }
  122. setImage(buff);
  123. while(isFinished() < WAVELENGTH) {
  124. wdt_reset();
  125. }
  126. buffClearAllPixels(buff);
  127. }
  128. free(buff);
  129. }
  130. void xWave2(uint8_t i) {
  131. uint8_t *buff;
  132. int8_t x, y, z;
  133. buff = buffNew();
  134. for(x = (7 - (2 * i)); x >= ((7 - (2 * i)) - 1); x--) {
  135. for(y = 0; y < 8; y++) {
  136. for(z = 0; z < 8; z++) {
  137. buffSetPixel(buff, x, y, z);
  138. }
  139. }
  140. setImage(buff);
  141. while(isFinished() < WAVELENGTH) {
  142. wdt_reset();
  143. }
  144. buffClearAllPixels(buff);
  145. }
  146. free(buff);
  147. }
  148. void zWave1(uint8_t i) {
  149. uint8_t *buff;
  150. int8_t x, y, z;
  151. buff = buffNew();
  152. // z-axis-wave
  153. for(z = (i * 2); z < ((i * 2) + 2); z++) {
  154. for(y = 0; y < 8; y++) {
  155. for(x = 0; x < 8; x++) {
  156. buffSetPixel(buff, x, y, z);
  157. }
  158. }
  159. setImage(buff);
  160. while(isFinished() < WAVELENGTH) {
  161. wdt_reset();
  162. }
  163. buffClearAllPixels(buff);
  164. }
  165. free(buff);
  166. }
  167. void zWave2(uint8_t i) {
  168. uint8_t *buff;
  169. int8_t x, y, z;
  170. buff = buffNew();
  171. for(z = (7 - (2 * i)); z >= ((7 - (2 * i)) - 1); z--) {
  172. for(x = 0; x < 8; x++) {
  173. for(y = 0; y < 8; y++) {
  174. buffSetPixel(buff, x, y, z);
  175. }
  176. }
  177. setImage(buff);
  178. while(isFinished() < WAVELENGTH) {
  179. wdt_reset();
  180. }
  181. buffClearAllPixels(buff);
  182. }
  183. free(buff);
  184. }