Skip to content

Commit

Permalink
Update Time library to latest version
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
petewill committed Oct 13, 2016
1 parent 4a645c3 commit 74d966d
Show file tree
Hide file tree
Showing 24 changed files with 1,054 additions and 600 deletions.
37 changes: 27 additions & 10 deletions libraries/Time/DateStrings.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
/* DateStrings.cpp
* Definitions for date strings for use with the Time library
*
* Updated for Arduino 1.5.7 18 July 2014
*
* No memory is consumed in the sketch if your code does not call any of the string methods
* You can change the text of the strings, make sure the short strings are each exactly 3 characters
* the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in Time.h
* the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in TimeLib.h
*
*/

#include <avr/pgmspace.h>
#include "Time.h"

#if defined(__AVR__)
#include <avr/pgmspace.h>
#else
// for compatiblity with Arduino Due and Teensy 3.0 and maybe others?
#define PROGMEM
#define PGM_P const char *
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#define pgm_read_word(addr) (*(const unsigned char **)(addr))
#define strcpy_P(dest, src) strcpy((dest), (src))
#endif
#include <string.h> // for strcpy_P or strcpy
#include "TimeLib.h"

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

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

const char monthStr0[] PROGMEM = "";
const char monthStr1[] PROGMEM = "January";
const char monthStr2[] PROGMEM = "February";
const char monthStr3[] PROGMEM = "March";
Expand All @@ -28,10 +41,10 @@ const char monthStr10[] PROGMEM = "October";
const char monthStr11[] PROGMEM = "November";
const char monthStr12[] PROGMEM = "December";

PGM_P const monthNames_P[] PROGMEM =
const PROGMEM char * const PROGMEM monthNames_P[] =
{
"",monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,monthStr6,
monthStr7,monthStr8,monthStr9,monthStr10,monthStr11,monthStr12
monthStr0,monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,monthStr6,
monthStr7,monthStr8,monthStr9,monthStr10,monthStr11,monthStr12
};

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

PGM_P const dayNames_P[] PROGMEM = { dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7};
const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThrFriSat";
const PROGMEM char * const PROGMEM dayNames_P[] =
{
dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7
};

const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThuFriSat";

/* functions to return date strings */

char* monthStr(uint8_t month)
{
strcpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[month])));
return buffer;
return buffer;
}

char* monthShortStr(uint8_t month)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@


import processing.serial.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

public static final short portIndex = 1; // select the com port, 0 is the first port
public static final char TIME_HEADER = 'T'; //header byte for arduino serial time message
public static final short portIndex = 0; // select the com port, 0 is the first port
public static final String TIME_HEADER = "T"; //header for arduino serial time message
public static final char TIME_REQUEST = 7; // ASCII bell character
public static final char LF = 10; // ASCII linefeed
public static final char CR = 13; // ASCII linefeed
Expand All @@ -25,10 +28,15 @@ void setup() {
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) { // If data is available,
char val = char(myPort.read()); // read it and store it in val
if(val == TIME_REQUEST){
Expand All @@ -52,19 +60,19 @@ void mousePressed() {
}


void sendTimeMessage(char header, long time) {
void sendTimeMessage(String header, long time) {
String timeStr = String.valueOf(time);
myPort.write(header); // send header and time to arduino
myPort.write(timeStr);
myPort.write(timeStr);
myPort.write('\n');
}

long getTimeNow(){
// java time is in ms, we want secs
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
int tzo = cal.get(Calendar.ZONE_OFFSET);
int dst = cal.get(Calendar.DST_OFFSET);
long now = (cal.getTimeInMillis() / 1000) ;
now = now + (tzo/1000) + (dst/1000);
return now;
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ The portIndex must be set the Serial port connected to Arduino.
Download TimeSerial.pde onto Arduino and you should see the time
message displayed when you run SyncArduinoClock in Processing.
The Arduino time is set from the time on your computer through the
Processing sketch.
Processing sketch.
82 changes: 0 additions & 82 deletions libraries/Time/Examples/TimeGPS/TimeGPS.pde

This file was deleted.

120 changes: 0 additions & 120 deletions libraries/Time/Examples/TimeNTP/TimeNTP.pde

This file was deleted.

Loading

0 comments on commit 74d966d

Please sign in to comment.