PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

109 lines
3.0KB

  1. /*
  2. * TimeSerialDateStrings.pde
  3. * example code illustrating Time library date strings
  4. *
  5. * This sketch adds date string functionality to TimeSerial sketch
  6. * Also shows how to handle different messages
  7. *
  8. * A message starting with a time header sets the time
  9. * A Processing example sketch to automatically send the messages is inclided in the download
  10. * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
  11. *
  12. * A message starting with a format header sets the date format
  13. * send: Fs\n for short date format
  14. * send: Fl\n for long date format
  15. */
  16. #include <TimeLib.h>
  17. // single character message tags
  18. #define TIME_HEADER 'T' // Header tag for serial time sync message
  19. #define FORMAT_HEADER 'F' // Header tag indicating a date format message
  20. #define FORMAT_SHORT 's' // short month and day strings
  21. #define FORMAT_LONG 'l' // (lower case l) long month and day strings
  22. #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
  23. static boolean isLongFormat = true;
  24. void setup() {
  25. Serial.begin(9600);
  26. while (!Serial) ; // Needed for Leonardo only
  27. setSyncProvider( requestSync); //set function to call when sync required
  28. Serial.println("Waiting for sync message");
  29. }
  30. void loop(){
  31. if (Serial.available() > 1) { // wait for at least two characters
  32. char c = Serial.read();
  33. if( c == TIME_HEADER) {
  34. processSyncMessage();
  35. }
  36. else if( c== FORMAT_HEADER) {
  37. processFormatMessage();
  38. }
  39. }
  40. if (timeStatus()!= timeNotSet) {
  41. digitalClockDisplay();
  42. }
  43. delay(1000);
  44. }
  45. void digitalClockDisplay() {
  46. // digital clock display of the time
  47. Serial.print(hour());
  48. printDigits(minute());
  49. printDigits(second());
  50. Serial.print(" ");
  51. if(isLongFormat)
  52. Serial.print(dayStr(weekday()));
  53. else
  54. Serial.print(dayShortStr(weekday()));
  55. Serial.print(" ");
  56. Serial.print(day());
  57. Serial.print(" ");
  58. if(isLongFormat)
  59. Serial.print(monthStr(month()));
  60. else
  61. Serial.print(monthShortStr(month()));
  62. Serial.print(" ");
  63. Serial.print(year());
  64. Serial.println();
  65. }
  66. void printDigits(int digits) {
  67. // utility function for digital clock display: prints preceding colon and leading 0
  68. Serial.print(":");
  69. if(digits < 10)
  70. Serial.print('0');
  71. Serial.print(digits);
  72. }
  73. void processFormatMessage() {
  74. char c = Serial.read();
  75. if( c == FORMAT_LONG){
  76. isLongFormat = true;
  77. Serial.println(F("Setting long format"));
  78. }
  79. else if( c == FORMAT_SHORT) {
  80. isLongFormat = false;
  81. Serial.println(F("Setting short format"));
  82. }
  83. }
  84. void processSyncMessage() {
  85. unsigned long pctime;
  86. const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - paul, perhaps we define in time.h?
  87. pctime = Serial.parseInt();
  88. if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
  89. setTime(pctime); // Sync Arduino clock to the time received on the serial port
  90. }
  91. }
  92. time_t requestSync() {
  93. Serial.write(TIME_REQUEST);
  94. return 0; // the time will be sent later in response to serial mesg
  95. }