PlatformIO package of the Teensy core framework compatible with GCC 10 & 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.

72 lines
1.5KB

  1. // main.cpp
  2. // Lets Arduino RadioHead sketches run within a simulator on Linux as a single process
  3. // Copyright (C) 2014 Mike McCauley
  4. // $Id: simMain.cpp,v 1.2 2014/05/09 05:30:03 mikem Exp mikem $
  5. #include <RadioHead.h>
  6. #if (RH_PLATFORM == RH_PLATFORM_UNIX)
  7. #include <stdio.h>
  8. #include <RHutil/simulator.h>
  9. #include <sys/time.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12. SerialSimulator Serial;
  13. // Functions we expect to find in the sketch
  14. extern void setup();
  15. extern void loop();
  16. // Millis at the start of the process
  17. unsigned long start_millis;
  18. int _simulator_argc;
  19. char** _simulator_argv;
  20. // Returns milliseconds since beginning of day
  21. unsigned long time_in_millis()
  22. {
  23. struct timeval te;
  24. gettimeofday(&te, NULL); // get current time
  25. unsigned long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caclulate milliseconds
  26. return milliseconds;
  27. }
  28. // Run the Arduino standard functions in the main loop
  29. int main(int argc, char** argv)
  30. {
  31. // Let simulated program have access to argc and argv
  32. _simulator_argc = argc;
  33. _simulator_argv = argv;
  34. start_millis = time_in_millis();
  35. // Seed the random number generator
  36. srand(getpid() ^ (unsigned) time(NULL)/2);
  37. setup();
  38. while (1)
  39. loop();
  40. }
  41. void delay(unsigned long ms)
  42. {
  43. usleep(ms * 1000);
  44. }
  45. // Arduino equivalent, milliseconds since process start
  46. unsigned long millis()
  47. {
  48. return time_in_millis() - start_millis;
  49. }
  50. long random(long from, long to)
  51. {
  52. return from + (random() % (to - from));
  53. }
  54. long random(long to)
  55. {
  56. return random(0, to);
  57. }
  58. #endif