123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
-
- import processing.serial.*;
- import java.util.Date;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- public static final short portIndex = 0;
- public static final String TIME_HEADER = "T";
- public static final char TIME_REQUEST = 7;
- public static final char LF = 10;
- public static final char CR = 13;
- Serial myPort;
- void setup() {
- size(200, 200);
- println(Serial.list());
- println(" Connecting to -> " + Serial.list()[portIndex]);
- myPort = new Serial(this,Serial.list()[portIndex], 9600);
- println(getTimeNow());
- }
- void draw()
- {
- textSize(20);
- textAlign(CENTER);
- fill(0);
- text("Click to send\nTime Sync", 0, 75, 200, 175);
- if ( myPort.available() > 0) {
- char val = char(myPort.read());
- if(val == TIME_REQUEST){
- long t = getTimeNow();
- sendTimeMessage(TIME_HEADER, t);
- }
- else
- {
- if(val == LF)
- ;
- else if(val == CR)
- println();
- else
- print(val);
- }
- }
- }
- void mousePressed() {
- sendTimeMessage( TIME_HEADER, getTimeNow());
- }
- void sendTimeMessage(String header, long time) {
- String timeStr = String.valueOf(time);
- myPort.write(header);
- myPort.write(timeStr);
- myPort.write('\n');
- }
- long getTimeNow(){
-
- Date d = new Date();
- Calendar cal = new GregorianCalendar();
- long current = d.getTime()/1000;
- long timezone = cal.get(cal.ZONE_OFFSET)/1000;
- long daylight = cal.get(cal.DST_OFFSET)/1000;
- return current + timezone + daylight;
- }
|