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.

88 lines
2.6KB

  1. /**
  2. * SyncArduinoClock.
  3. *
  4. * SyncArduinoClock is a Processing sketch that responds to Arduino
  5. * requests for time synchronization messages. Run this in the
  6. * Processing environment (not in Arduino) on your PC or Mac.
  7. *
  8. * Download TimeSerial onto Arduino and you should see the time
  9. * message displayed when you run SyncArduinoClock in Processing.
  10. * The Arduino time is set from the time on your computer through the
  11. * Processing sketch.
  12. *
  13. * portIndex must be set to the port connected to the Arduino
  14. *
  15. * The current time is sent in response to request message from Arduino
  16. * or by clicking the display window
  17. *
  18. * The time message is 11 ASCII text characters; a header (the letter 'T')
  19. * followed by the ten digit system time (unix time)
  20. */
  21. import processing.serial.*;
  22. import java.util.Date;
  23. import java.util.Calendar;
  24. import java.util.GregorianCalendar;
  25. public static final short portIndex = 0; // select the com port, 0 is the first port
  26. public static final String TIME_HEADER = "T"; //header for arduino serial time message
  27. public static final char TIME_REQUEST = 7; // ASCII bell character
  28. public static final char LF = 10; // ASCII linefeed
  29. public static final char CR = 13; // ASCII linefeed
  30. Serial myPort; // Create object from Serial class
  31. void setup() {
  32. size(200, 200);
  33. println(Serial.list());
  34. println(" Connecting to -> " + Serial.list()[portIndex]);
  35. myPort = new Serial(this,Serial.list()[portIndex], 9600);
  36. println(getTimeNow());
  37. }
  38. void draw()
  39. {
  40. textSize(20);
  41. textAlign(CENTER);
  42. fill(0);
  43. text("Click to send\nTime Sync", 0, 75, 200, 175);
  44. if ( myPort.available() > 0) { // If data is available,
  45. char val = char(myPort.read()); // read it and store it in val
  46. if(val == TIME_REQUEST){
  47. long t = getTimeNow();
  48. sendTimeMessage(TIME_HEADER, t);
  49. }
  50. else
  51. {
  52. if(val == LF)
  53. ; //igonore
  54. else if(val == CR)
  55. println();
  56. else
  57. print(val); // echo everying but time request
  58. }
  59. }
  60. }
  61. void mousePressed() {
  62. sendTimeMessage( TIME_HEADER, getTimeNow());
  63. }
  64. void sendTimeMessage(String header, long time) {
  65. String timeStr = String.valueOf(time);
  66. myPort.write(header); // send header and time to arduino
  67. myPort.write(timeStr);
  68. myPort.write('\n');
  69. }
  70. long getTimeNow(){
  71. // java time is in ms, we want secs
  72. Date d = new Date();
  73. Calendar cal = new GregorianCalendar();
  74. long current = d.getTime()/1000;
  75. long timezone = cal.get(cal.ZONE_OFFSET)/1000;
  76. long daylight = cal.get(cal.DST_OFFSET)/1000;
  77. return current + timezone + daylight;
  78. }