animation.ino 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Animation Rendering Logics */
  2. int frameCount = 0;
  3. char previousAnicode = 'a';
  4. //Handle animation rendering
  5. void handleAnimationRendering(char anicode) {
  6. if (previousAnicode != anicode){
  7. previousAnicode = anicode;
  8. frameCount = 0;
  9. }
  10. //Check if the target animation frame is static
  11. String targetFrame = "/" + String(anicode) + ".bin";
  12. if (SD.exists(targetFrame)) {
  13. //This is a static frame. Load and render it
  14. loadFrameAndRender(targetFrame);
  15. delay(getFrameDuration(anicode,0));
  16. return;
  17. }
  18. //It is not a static frame. Check if x0.bin exists
  19. targetFrame = "/" + String(anicode) + String(frameCount) + ".bin";
  20. if (SD.exists(targetFrame)) {
  21. loadFrameAndRender(targetFrame);
  22. frameCount++;
  23. delay(getFrameDuration(anicode,frameCount));
  24. return;
  25. } else {
  26. //Not found.
  27. if (frameCount != 0) {
  28. loadFrameAndRender("/" + String(anicode) + "0.bin");
  29. delay(getFrameDuration(anicode,0));
  30. frameCount = 1;
  31. return;
  32. } else {
  33. //frameCount = 0. x0.bin not exists. Report error
  34. Serial.println("Target frame buffer not found: " + String(anicode) + ".bin");
  35. setAnimationCode('a');
  36. return;
  37. }
  38. }
  39. }
  40. //Get how long the frame shd last.
  41. //Default 500 unless specially programmed
  42. int getFrameDuration(char anicode, int framecount){
  43. if ((anicode == 'a' || anicode == 'b') && framecount == 0){
  44. return 4000;
  45. }
  46. return 500;
  47. }