Skip to content

Commit 74d966d

Browse files
committed
Update Time library to latest version
The old time library is not compatible with the newer version of the Arduino IDE (see PaulStoffregen/Time#32). This updated library should be backwards compatible with code that still uses the Time.h reference on an older Arduino IDE.
1 parent 4a645c3 commit 74d966d

File tree

24 files changed

+1054
-600
lines changed

24 files changed

+1054
-600
lines changed

libraries/Time/DateStrings.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
/* DateStrings.cpp
22
* Definitions for date strings for use with the Time library
33
*
4+
* Updated for Arduino 1.5.7 18 July 2014
5+
*
46
* No memory is consumed in the sketch if your code does not call any of the string methods
57
* You can change the text of the strings, make sure the short strings are each exactly 3 characters
6-
* the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in Time.h
8+
* the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in TimeLib.h
79
*
810
*/
9-
10-
#include <avr/pgmspace.h>
11-
#include "Time.h"
11+
12+
#if defined(__AVR__)
13+
#include <avr/pgmspace.h>
14+
#else
15+
// for compatiblity with Arduino Due and Teensy 3.0 and maybe others?
16+
#define PROGMEM
17+
#define PGM_P const char *
18+
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
19+
#define pgm_read_word(addr) (*(const unsigned char **)(addr))
20+
#define strcpy_P(dest, src) strcpy((dest), (src))
21+
#endif
22+
#include <string.h> // for strcpy_P or strcpy
23+
#include "TimeLib.h"
1224

1325
// the short strings for each day or month must be exactly dt_SHORT_STR_LEN
1426
#define dt_SHORT_STR_LEN 3 // the length of short strings
1527

1628
static char buffer[dt_MAX_STRING_LEN+1]; // must be big enough for longest string and the terminating null
1729

30+
const char monthStr0[] PROGMEM = "";
1831
const char monthStr1[] PROGMEM = "January";
1932
const char monthStr2[] PROGMEM = "February";
2033
const char monthStr3[] PROGMEM = "March";
@@ -28,10 +41,10 @@ const char monthStr10[] PROGMEM = "October";
2841
const char monthStr11[] PROGMEM = "November";
2942
const char monthStr12[] PROGMEM = "December";
3043

31-
PGM_P const monthNames_P[] PROGMEM =
44+
const PROGMEM char * const PROGMEM monthNames_P[] =
3245
{
33-
"",monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,monthStr6,
34-
monthStr7,monthStr8,monthStr9,monthStr10,monthStr11,monthStr12
46+
monthStr0,monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,monthStr6,
47+
monthStr7,monthStr8,monthStr9,monthStr10,monthStr11,monthStr12
3548
};
3649

3750
const char monthShortNames_P[] PROGMEM = "ErrJanFebMarAprMayJunJulAugSepOctNovDec";
@@ -45,15 +58,19 @@ const char dayStr5[] PROGMEM = "Thursday";
4558
const char dayStr6[] PROGMEM = "Friday";
4659
const char dayStr7[] PROGMEM = "Saturday";
4760

48-
PGM_P const dayNames_P[] PROGMEM = { dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7};
49-
const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThrFriSat";
61+
const PROGMEM char * const PROGMEM dayNames_P[] =
62+
{
63+
dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7
64+
};
65+
66+
const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThuFriSat";
5067

5168
/* functions to return date strings */
5269

5370
char* monthStr(uint8_t month)
5471
{
5572
strcpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[month])));
56-
return buffer;
73+
return buffer;
5774
}
5875

5976
char* monthShortStr(uint8_t month)

libraries/Time/Examples/Processing/SyncArduinoClock/SyncArduinoClock.pde

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212

1313

1414
import processing.serial.*;
15+
import java.util.Date;
16+
import java.util.Calendar;
17+
import java.util.GregorianCalendar;
1518

16-
public static final short portIndex = 1; // select the com port, 0 is the first port
17-
public static final char TIME_HEADER = 'T'; //header byte for arduino serial time message
19+
public static final short portIndex = 0; // select the com port, 0 is the first port
20+
public static final String TIME_HEADER = "T"; //header for arduino serial time message
1821
public static final char TIME_REQUEST = 7; // ASCII bell character
1922
public static final char LF = 10; // ASCII linefeed
2023
public static final char CR = 13; // ASCII linefeed
@@ -25,10 +28,15 @@ void setup() {
2528
println(Serial.list());
2629
println(" Connecting to -> " + Serial.list()[portIndex]);
2730
myPort = new Serial(this,Serial.list()[portIndex], 9600);
31+
println(getTimeNow());
2832
}
2933

3034
void draw()
3135
{
36+
textSize(20);
37+
textAlign(CENTER);
38+
fill(0);
39+
text("Click to send\nTime Sync", 0, 75, 200, 175);
3240
if ( myPort.available() > 0) { // If data is available,
3341
char val = char(myPort.read()); // read it and store it in val
3442
if(val == TIME_REQUEST){
@@ -52,19 +60,19 @@ void mousePressed() {
5260
}
5361

5462

55-
void sendTimeMessage(char header, long time) {
63+
void sendTimeMessage(String header, long time) {
5664
String timeStr = String.valueOf(time);
5765
myPort.write(header); // send header and time to arduino
58-
myPort.write(timeStr);
66+
myPort.write(timeStr);
67+
myPort.write('\n');
5968
}
6069

6170
long getTimeNow(){
6271
// java time is in ms, we want secs
63-
GregorianCalendar cal = new GregorianCalendar();
64-
cal.setTime(new Date());
65-
int tzo = cal.get(Calendar.ZONE_OFFSET);
66-
int dst = cal.get(Calendar.DST_OFFSET);
67-
long now = (cal.getTimeInMillis() / 1000) ;
68-
now = now + (tzo/1000) + (dst/1000);
69-
return now;
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;
7078
}

libraries/Time/Examples/Processing/SyncArduinoClock/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ The portIndex must be set the Serial port connected to Arduino.
66
Download TimeSerial.pde onto Arduino and you should see the time
77
message displayed when you run SyncArduinoClock in Processing.
88
The Arduino time is set from the time on your computer through the
9-
Processing sketch.
9+
Processing sketch.

libraries/Time/Examples/TimeGPS/TimeGPS.pde

Lines changed: 0 additions & 82 deletions
This file was deleted.

libraries/Time/Examples/TimeNTP/TimeNTP.pde

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)