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.

simulator.h 1.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // simulator.h
  2. // Lets Arduino RadioHead sketches run within a simulator on Linux as a single process
  3. // Copyright (C) 2014 Mike McCauley
  4. // $Id: simulator.h,v 1.4 2015/08/13 02:45:47 mikem Exp mikem $
  5. #ifndef simulator_h
  6. #define simulator_h
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdint.h>
  11. // Equivalent types for common Arduino types like uint8_t are in stdint.h
  12. // Access to some globals
  13. // Command line args passed to the process.
  14. extern int _simulator_argc;
  15. extern char** _simulator_argv;
  16. // Definitions for various Arduino functions
  17. extern void delay(unsigned long ms);
  18. extern unsigned long millis();
  19. extern long random(long to);
  20. extern long random(long from, long to);
  21. // Equavalent to HardwareSerial in Arduino
  22. // but outputs to stdout
  23. class SerialSimulator
  24. {
  25. public:
  26. #define DEC 10
  27. #define HEX 16
  28. #define OCT 8
  29. #define BIN 2
  30. // TODO: move these from being inlined
  31. void begin(int baud) {}
  32. size_t println(const char* s)
  33. {
  34. print(s);
  35. return printf("\n");
  36. }
  37. size_t print(const char* s)
  38. {
  39. return printf("%s", s); // This style prevent warnings from [-Wformat-security]
  40. }
  41. size_t print(unsigned int n, int base = DEC)
  42. {
  43. if (base == DEC)
  44. return printf("%d", n);
  45. else if (base == HEX)
  46. return printf("%02x", n);
  47. else if (base == OCT)
  48. return printf("%o", n);
  49. // TODO: BIN
  50. else
  51. return 0;
  52. }
  53. size_t print(char ch)
  54. {
  55. return printf("%c", ch);
  56. }
  57. size_t println(char ch)
  58. {
  59. return printf("%c\n", ch);
  60. }
  61. size_t print(unsigned char ch, int base = DEC)
  62. {
  63. return print((unsigned int)ch, base);
  64. }
  65. size_t println(unsigned char ch, int base = DEC)
  66. {
  67. print((unsigned int)ch, base);
  68. return printf("\n");
  69. }
  70. };
  71. // Global instance of the Serial output
  72. extern SerialSimulator Serial;
  73. #endif