pretty_effect.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. This code generates an effect that should pass the 'fancy graphics' qualification
  3. as set in the comment in the spi_master code.
  4. This example code is in the Public Domain (or CC0 licensed, at your option.)
  5. Unless required by applicable law or agreed to in writing, this
  6. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7. CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <math.h>
  10. #include "pretty_effect.h"
  11. #include "sdkconfig.h"
  12. #ifdef CONFIG_IDF_TARGET_ESP32
  13. #include "decode_image.h"
  14. uint16_t **pixels;
  15. //Grab a rgb16 pixel from the esp32_tiles image
  16. static inline uint16_t get_bgnd_pixel(int x, int y)
  17. {
  18. //Image has an 8x8 pixel margin, so we can also resolve e.g. [-3, 243]
  19. x+=8;
  20. y+=8;
  21. return pixels[y][x];
  22. }
  23. #elif defined CONFIG_IDF_TARGET_ESP32S2BETA
  24. //esp32s2beta doesn't have enough memory to hold the decoded image, calculate instead
  25. static inline uint16_t get_bgnd_pixel(int x, int y)
  26. {
  27. return ((x<<3)^(y<<3)^(x*y));
  28. }
  29. #endif
  30. //This variable is used to detect the next frame.
  31. static int prev_frame=-1;
  32. //Instead of calculating the offsets for each pixel we grab, we pre-calculate the valueswhenever a frame changes, then re-use
  33. //these as we go through all the pixels in the frame. This is much, much faster.
  34. static int8_t xofs[320], yofs[240];
  35. static int8_t xcomp[320], ycomp[240];
  36. //Calculate the pixel data for a set of lines (with implied line size of 320). Pixels go in dest, line is the Y-coordinate of the
  37. //first line to be calculated, linect is the amount of lines to calculate. Frame increases by one every time the entire image
  38. //is displayed; this is used to go to the next frame of animation.
  39. void pretty_effect_calc_lines(uint16_t *dest, int line, int frame, int linect)
  40. {
  41. if (frame!=prev_frame) {
  42. //We need to calculate a new set of offset coefficients. Take some random sines as offsets to make everything
  43. //look pretty and fluid-y.
  44. for (int x=0; x<320; x++) xofs[x]=sin(frame*0.15+x*0.06)*4;
  45. for (int y=0; y<240; y++) yofs[y]=sin(frame*0.1+y*0.05)*4;
  46. for (int x=0; x<320; x++) xcomp[x]=sin(frame*0.11+x*0.12)*4;
  47. for (int y=0; y<240; y++) ycomp[y]=sin(frame*0.07+y*0.15)*4;
  48. prev_frame=frame;
  49. }
  50. for (int y=line; y<line+linect; y++) {
  51. for (int x=0; x<320; x++) {
  52. *dest++=get_bgnd_pixel(x+yofs[y]+xcomp[x], y+xofs[x]+ycomp[y]);
  53. }
  54. }
  55. }
  56. esp_err_t pretty_effect_init(void)
  57. {
  58. #ifdef CONFIG_IDF_TARGET_ESP32
  59. return decode_image(&pixels);
  60. #elif defined CONFIG_IDF_TARGET_ESP32S2BETA
  61. //esp32s2beta doesn't have enough memory to hold the decoded image, calculate instead
  62. return ESP_OK;
  63. #endif
  64. }