main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "u8g2.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. u8g2_t u8g2;
  5. /*
  6. Linear Congruential Generator (LCG)
  7. z = (a*z + c) % m;
  8. m = 256*256 (16 Bit)
  9. for period:
  10. a-1: dividable by 2
  11. a-1: multiple of 4
  12. c: not dividable by 2
  13. c = 17
  14. a-1 = 64 --> a = 65
  15. */
  16. uint16_t rndz = 127; // start value
  17. uint16_t lcg(void) {
  18. rndz = ((uint16_t)65*(uint16_t)rndz + (uint16_t)17);
  19. return rndz;
  20. }
  21. uint16_t get_counter_y_pos(uint16_t x)
  22. {
  23. #define ELEMENTS 7
  24. /*
  25. wolframalpha.com: plot ((0,0), (25, 10), (50,38), (80, 100), (100,128), (114, 137), (127, 128))
  26. */
  27. static const int16_t xa[ELEMENTS] = { 0, 25, 50, 80, 100, 114, 127 } ;
  28. static const int16_t ya[ELEMENTS] = { 0, 10, 38, 100 , 128, 137, 128};
  29. int i = 1;
  30. if ( x > xa[ELEMENTS-1] )
  31. return ya[ELEMENTS-1];
  32. while( xa[i] < x )
  33. i++;
  34. return ((x-xa[i-1]) * (ya[i]-ya[i-1])) / (xa[i]-xa[i-1]) + ya[i-1];
  35. }
  36. void draw_mech_counter(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, int16_t step, const char *prev, const char *next)
  37. {
  38. u8g2_uint_t h = u8g2_GetAscent(u8g2) - u8g2_GetDescent(u8g2);
  39. u8g2_uint_t shift = 0;
  40. if ( step >= 0 )
  41. {
  42. shift = (get_counter_y_pos(step)*h)/128;
  43. }
  44. y += shift;
  45. if ( prev[0] == '1' )
  46. u8g2_DrawStr(u8g2, x+4, y, prev);
  47. else
  48. u8g2_DrawStr(u8g2, x, y, prev);
  49. y -= h;
  50. if ( next[0] == '1' )
  51. u8g2_DrawStr(u8g2, x+4, y, next);
  52. else
  53. u8g2_DrawStr(u8g2, x, y, next);
  54. }
  55. void draw_4_counter(u8g2_t *u8g2, uint16_t rnd, u8g2_uint_t x, u8g2_uint_t dx, u8g2_uint_t y, uint8_t step, const char *prev, const char *next)
  56. {
  57. u8g2_uint_t ex = 3;
  58. u8g2_uint_t h = u8g2_GetAscent(u8g2);
  59. uint8_t i;
  60. char p[2] = "0";
  61. char n[2] = "0";
  62. u8g2_DrawFrame(u8g2, x-1, y-h-ex-1, 4*dx+3, h+2*ex+2);
  63. u8g2_SetClipWindow(u8g2, x, y-h-ex, x+4*dx, y+ex);
  64. for( i = 0; i < 4; i++ )
  65. {
  66. p[0] = prev[i];
  67. n[0] = next[i];
  68. draw_mech_counter(u8g2, x, y, step + (rnd & 31), p, n);
  69. rnd >>= 2;
  70. x += dx;
  71. }
  72. u8g2_SetMaxClipWindow(u8g2);
  73. }
  74. char prev_counter[] = " ";
  75. char next_counter[] = " ";
  76. uint16_t rnd_counter = 0;
  77. void init_counter(uint16_t value)
  78. {
  79. strcpy(prev_counter, next_counter);
  80. strcpy(next_counter, u8x8_u16toa(value, 4));
  81. rnd_counter = lcg();
  82. }
  83. void draw_counter(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t dx, u8g2_uint_t y, uint8_t step)
  84. {
  85. draw_4_counter(u8g2, rnd_counter, x, dx, y, step, prev_counter, next_counter);
  86. }
  87. int main(void)
  88. {
  89. int x, y;
  90. int k;
  91. int i;
  92. int step = 0;
  93. //uint16_t rnd;
  94. char prev[5];
  95. char next[5];
  96. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  97. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  98. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  99. u8g2_SetFont(&u8g2, u8g2_font_helvB18_tn);
  100. strcpy(prev, u8x8_u16toa(lcg(), 4));
  101. strcpy(next, u8x8_u16toa(lcg(), 4));
  102. //rnd = lcg();
  103. init_counter(lcg());
  104. x = 2;
  105. y = 32;
  106. for(;;)
  107. {
  108. #ifdef U8G2_WITH_HVLINE_COUNT
  109. u8g2.hv_cnt = 0UL;
  110. #endif /* U8G2_WITH_HVLINE_COUNT */
  111. /*
  112. u8g2_ClearBuffer(&u8g2);
  113. u8g2_SetFontDirection(&u8g2, 0);
  114. u8g2_DrawStr(&u8g2, x, y, "ABC");
  115. u8g2_SetFontDirection(&u8g2, 1);
  116. u8g2_DrawStr(&u8g2, x, y, "abc");
  117. u8g2_SetFontDirection(&u8g2, 2);
  118. u8g2_DrawStr(&u8g2, x, y, "abc");
  119. u8g2_SetFontDirection(&u8g2, 3);
  120. u8g2_DrawStr(&u8g2, x, y, "abc");
  121. u8g2_SendBuffer(&u8g2);
  122. */
  123. u8g2_FirstPage(&u8g2);
  124. i = 0;
  125. do
  126. {
  127. u8g2_SetFont(&u8g2, u8g2_font_mystery_quest_32_tr);
  128. // u8g2_draw_counter(&u8g2, rnd, x, 16, y, step, prev, next);
  129. draw_counter(&u8g2, x, 16, y, step);
  130. u8g2_SetFont(&u8g2, u8g2_font_mystery_quest_24_tr);
  131. u8g2_DrawStr(&u8g2, x+74,y-2, "RPM");
  132. //u8g2_SetFont(&u8g2, u8g2_font_mystery_quest_32_tr);
  133. //u8g2_draw_mech_counter(&u8g2, x+48, y, step, "5","6");
  134. // u8g2_SetFont(&u8g2, u8g2_font_mystery_quest_32_tr);
  135. //u8g2_DrawStr(&u8g2, x,y+32, "3210");
  136. //u8g2_SetFont(&u8g2, u8g2_font_mystery_quest_24_tr);
  137. //u8g2_DrawStr(&u8g2, x+74,y+30, "mV");
  138. i++;
  139. } while( u8g2_NextPage(&u8g2) );
  140. #ifdef U8G2_WITH_HVLINE_COUNT
  141. printf("hv cnt: %ld\n", u8g2.hv_cnt);
  142. #endif /* U8G2_WITH_HVLINE_COUNT */
  143. do
  144. {
  145. k = u8g_sdl_get_key();
  146. } while( k < 0 );
  147. if ( k == 273 ) y -= 7;
  148. if ( k == 274 ) y += 7;
  149. if ( k == 276 ) x -= 7;
  150. if ( k == 275 ) x += 7;
  151. if ( k == 'e' ) y -= 1;
  152. if ( k == 'x' ) y += 1;
  153. if ( k == 's' ) x -= 1;
  154. if ( k == 'd' ) x += 1;
  155. if ( k == ' ' ) step += 3;
  156. if ( step > 160 )
  157. {
  158. init_counter(lcg());
  159. /*
  160. rnd = lcg();
  161. strcpy(prev, next);
  162. strcpy(next, u8x8_u16toa(lcg(), 4));
  163. */
  164. step = 0;
  165. }
  166. if ( k == 'q' ) break;
  167. //printf("%u\n", lcg());
  168. }
  169. //u8x8_Set8x8Font(u8g2_GetU8x8(&u8g2), bdf_font);
  170. //u8x8_Draw8x8String(u8g2_GetU8x8(&u8g2), 0, 0, "Hello World!");
  171. return 0;
  172. }