main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. test procedure for a draw arc procedure
  3. screenshot conversion:
  4. convert -flip -resize 256x128 screenshot.tga screenshot.png
  5. */
  6. #include "u8g2.h"
  7. #include <stdio.h>
  8. #ifndef NO_SDL
  9. #include "SDL.h"
  10. #endif
  11. u8g2_t u8g2;
  12. // #define HIDE_INSTRUCTIONS
  13. void spinner_animation(uint8_t animation_enabled, uint8_t* start, uint8_t* end)
  14. {
  15. const uint8_t big_increment = 10;
  16. const uint8_t small_increment = 5;
  17. static uint8_t grow = 0, initialize = 1;
  18. static uint8_t prev_arc_length;
  19. if(!animation_enabled)
  20. {
  21. grow = 0;
  22. initialize = 1;
  23. }
  24. else
  25. {
  26. do
  27. {
  28. // Decrement angles
  29. *start = (256 + *start - (grow ? big_increment : small_increment)) % 256;
  30. *end = (256 + *end - (grow ? small_increment : big_increment)) % 256;
  31. // Get arc length
  32. uint8_t arc_length = (256 + *end - *start) % 256;
  33. if(initialize)
  34. {
  35. prev_arc_length = arc_length;
  36. initialize = 0;
  37. }
  38. // Check if arc overflows (grown above 255 or shrunk below 0)
  39. if((grow && arc_length < prev_arc_length) || (!grow && arc_length > prev_arc_length))
  40. {
  41. // invert start and end angles, and invert animation
  42. uint8_t tmp = *start;
  43. *start = *end;
  44. *end = tmp;
  45. grow = !grow;
  46. arc_length = grow ? 0 : 255;
  47. }
  48. prev_arc_length = arc_length; // update arc length for next iteration
  49. } while (*start == *end); // prevent drawing when arc length is 0 (arc function would draw a full circle)
  50. }
  51. }
  52. int main(void)
  53. {
  54. int k;
  55. char buffer[256];
  56. uint8_t animation_enabled = 1;
  57. const uint8_t desired_fps = 30;
  58. uint32_t last_ticks = 0;
  59. u8g2_uint_t x = 30;
  60. u8g2_uint_t y = 32;
  61. uint8_t rad_in = 18;
  62. uint8_t rad_out = 20;
  63. uint8_t start = 0;
  64. uint8_t end = 220;
  65. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  66. u8x8_ConnectBitmapToU8x8(u8g2_GetU8x8(&u8g2)); /* connect to bitmap */
  67. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  68. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  69. u8g2_SetFont(&u8g2, u8g2_font_tiny5_tf);
  70. u8g2_SetFontPosCenter(&u8g2);
  71. u8g2_SetFontMode(&u8g2, 1);
  72. for(;;)
  73. {
  74. #ifndef NO_SDL
  75. // Wait for next frame with constant FPS
  76. if (SDL_GetTicks() - last_ticks < 1000/desired_fps) continue;
  77. last_ticks = SDL_GetTicks();
  78. // Get current key press
  79. k = u8g_sdl_get_key();
  80. switch(k) {
  81. case ' ': animation_enabled = !animation_enabled; break;
  82. case 'y': rad_in -= 1; break;
  83. case 'u': rad_in += 1; break;
  84. case 'h': rad_out -= 1; break;
  85. case 'j': rad_out += 1; break;
  86. case 'i': start -= 1; break;
  87. case 'o': start += 1; break;
  88. case 'k': end -= 1; break;
  89. case 'l': end += 1; break;
  90. case 'e': case 273: y -= 1; break;
  91. case 'x': case 274: y += 1; break;
  92. case 's': case 276: x -= 1; break;
  93. case 'd': case 275: x += 1; break;
  94. case 'q': return 0;
  95. case 't': u8x8_SaveBitmapTGA(u8g2_GetU8x8(&u8g2), "screenshot.tga"); break;
  96. }
  97. if(k == ' ') printf(animation_enabled ? "play\n" : "pause\n");
  98. else if(k > 0) printf("x=%d, y=%d, rad_in=%d, rad_out=%d, start=%d, end=%d\n", x, y, rad_in, rad_out, start, end);
  99. // Animate if enabled
  100. spinner_animation(animation_enabled, &start, &end);
  101. #endif
  102. // Draw elements
  103. u8g2_FirstPage(&u8g2);
  104. do {
  105. // Draw arc for each radius
  106. for(u8g2_uint_t rad = rad_in; rad <= rad_out; rad++)
  107. {
  108. u8g2_DrawArc(&u8g2, x, y, rad, start, end);
  109. }
  110. // Draw instructions
  111. #ifndef HIDE_INSTRUCTIONS
  112. // separator
  113. u8g2_DrawVLine(&u8g2, 62, 0, 64);
  114. // anim
  115. u8g2_DrawTriangle(&u8g2, 66, 2, 66, 8, 71, 5);
  116. u8g2_DrawLine(&u8g2, 72, 8, 76, 2);
  117. u8g2_DrawBox(&u8g2, 79, 3, 2, 5);
  118. u8g2_DrawBox(&u8g2, 82, 3, 2, 5);
  119. u8g2_DrawUTF8(&u8g2, 87, 6, ":");
  120. (k == ' ' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 90, 1, 35, 9);
  121. u8g2_DrawUTF8(&u8g2, 95, 6, "SPACE");
  122. // + / - icons
  123. u8g2_DrawHLine(&u8g2, 66, 17, 9);
  124. u8g2_DrawHLine(&u8g2, 117, 17, 9);
  125. u8g2_DrawVLine(&u8g2, 121, 13, 9);
  126. // rad_in
  127. (k == 'y' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 66, 25, 9, 9);
  128. u8g2_DrawUTF8(&u8g2, 69, 30, "Y");
  129. (k == 'u' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 117, 25, 9, 9);
  130. u8g2_DrawUTF8(&u8g2, 120, 30, "U");
  131. snprintf(buffer, sizeof(buffer), "IN:%d", rad_in);
  132. u8g2_DrawUTF8(&u8g2, 78, 30, buffer);
  133. // rad_out
  134. (k == 'h' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 66, 35, 9, 9);
  135. u8g2_DrawUTF8(&u8g2, 69, 40, "H");
  136. (k == 'j' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 117, 35, 9, 9);
  137. u8g2_DrawUTF8(&u8g2, 120, 40, "J");
  138. snprintf(buffer, sizeof(buffer), "OUT:%d", rad_out);
  139. u8g2_DrawUTF8(&u8g2, 78, 40, buffer);
  140. // start
  141. (k == 'i' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 66, 45, 9, 9);
  142. u8g2_DrawUTF8(&u8g2, 69, 50, "I");
  143. (k == 'o' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 117, 45, 9, 9);
  144. u8g2_DrawUTF8(&u8g2, 120, 50, "O");
  145. snprintf(buffer, sizeof(buffer), "START:%d", start);
  146. u8g2_DrawUTF8(&u8g2, 78, 50, buffer);
  147. // end
  148. (k == 'k' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 66, 55, 9, 9);
  149. u8g2_DrawUTF8(&u8g2, 69, 60, "K");
  150. (k == 'l' ? u8g2_DrawBox : u8g2_DrawFrame)(&u8g2, 117, 55, 9, 9);
  151. u8g2_DrawUTF8(&u8g2, 120, 60, "L");
  152. snprintf(buffer, sizeof(buffer), "END:%d", end);
  153. u8g2_DrawUTF8(&u8g2, 78, 60, buffer);
  154. #endif
  155. } while( u8g2_NextPage(&u8g2) );
  156. }
  157. return 0;
  158. }