animation.ino 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Animation Rendering Logics */
  2. int frameCount = 0;
  3. char previousAnicode = 'a';
  4. const String animationFolder = "/anime/"; //Need tailing slash
  5. //delay with early breakout if current animation code changed
  6. void delayWithEarlyBreakout(int delayDuration) {
  7. delayDuration = delayDuration / 100;
  8. for (int i = 0; i < delayDuration; i++) {
  9. delay(100);
  10. if (getAnimationCode() != previousAnicode) {
  11. break;
  12. }
  13. }
  14. }
  15. //Handle animation rendering
  16. void handleAnimationRendering(char anicode) {
  17. if (!SD_exists){
  18. //SD card not exists. Use what is inside the buffer.
  19. Serial.println("SD card not exists");
  20. renderFrame();
  21. delay(5000);
  22. return;
  23. }
  24. if (previousAnicode != anicode) {
  25. previousAnicode = anicode;
  26. frameCount = 0;
  27. }
  28. //Check if the target animation frame is static
  29. String targetFrame = animationFolder + String(anicode) + ".bin";
  30. if (SD.exists(targetFrame)) {
  31. //This is a static frame. Load and render it
  32. loadFrameAndRender(targetFrame);
  33. int delayDuration = getFrameDuration(anicode, 0);
  34. delayWithEarlyBreakout(delayDuration);
  35. return;
  36. }
  37. //It is not a static frame. Check if x0.bin exists
  38. targetFrame = animationFolder + String(anicode) + String(frameCount) + ".bin";
  39. if (SD.exists(targetFrame)) {
  40. loadFrameAndRender(targetFrame);
  41. frameCount++;
  42. int delayDuration = getFrameDuration(anicode, frameCount);
  43. delayWithEarlyBreakout(delayDuration);
  44. return;
  45. } else {
  46. //Not found.
  47. if (frameCount != 0) {
  48. loadFrameAndRender(animationFolder + String(anicode) + "0.bin");
  49. int delayDuration = getFrameDuration(anicode, 0);
  50. delayWithEarlyBreakout(delayDuration);
  51. frameCount = 1;
  52. return;
  53. } else {
  54. //frameCount = 0. x0.bin not exists. Report error
  55. Serial.println("Target frame buffer not found: " + String(anicode) + ".bin");
  56. setAnimationCode('a');
  57. return;
  58. }
  59. }
  60. }
  61. //Get how long the frame shd last.
  62. //Default 500 unless specially programmed
  63. int getFrameDuration(char anicode, int framecount) {
  64. if ((anicode == 'a' || anicode == 'b' || anicode == 'g')) {
  65. //Blinking
  66. if (framecount == 0) {
  67. return 3000;
  68. }
  69. return 300;
  70. }
  71. return 500;
  72. }