Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 11 години
преди 11 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stdint.h>
  2. static uint32_t seed;
  3. void randomSeed(uint32_t newseed)
  4. {
  5. if (newseed > 0) seed = newseed;
  6. }
  7. void srandom(uint32_t newseed)
  8. {
  9. seed = newseed;
  10. }
  11. int32_t random(void)
  12. {
  13. int32_t hi, lo, x;
  14. // the algorithm used in avr-libc 1.6.4
  15. x = seed;
  16. if (x == 0) x = 123459876;
  17. hi = x / 127773;
  18. lo = x % 127773;
  19. x = 16807 * lo - 2836 * hi;
  20. if (x < 0) x += 0x7FFFFFFF;
  21. seed = x;
  22. return x;
  23. }
  24. uint32_t random(uint32_t howbig)
  25. {
  26. if (howbig == 0) return 0;
  27. return random() % howbig;
  28. }
  29. int32_t random(int32_t howsmall, int32_t howbig)
  30. {
  31. if (howsmall >= howbig) return howsmall;
  32. int32_t diff = howbig - howsmall;
  33. return random(diff) + howsmall;
  34. }
  35. long map(long x, long in_min, long in_max, long out_min, long out_max)
  36. {
  37. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  38. }
  39. unsigned int makeWord(unsigned int w) { return w; }
  40. unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }