Help w/ RTCLib, Chronodot, and a calendar.

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Help w/ RTCLib, Chronodot, and a calendar.

Post by 1chicagodave »

I have a chronodot, using RTCLib (from Adafruit GitHub repo), and time.h. I've already got a pretty decent NeoPixel Ring "analog" clock working with it all. Now...I want to add a calendar feature.

You know those whiteboard calendars with just a bunch of rows of seven boxes, and you get to fill in the dates for each new month? Well, I have a few 5x7 Bi-color LED matrix displays....and figured one could work in a similar way. I have the HT16K33 LED backpack working with it. I already made bmp's of all possible 'month-shapes' and can call/display them at will. I can already draw a single pixel of a different color to mark "Today" on the matrix. And, I can easily pull the day-of-week from RTCLib.

What I need (hope someone will help with) is using the RTC, RTClib, time library, and maybe some fun code to figure out the following —
• What day of week the current month begins with (Sunday, Mon, etc...)
• How many days in the current month
• The current week of the month (1st week, 2nd week....5th week?)

I need this in order to call the appropriate month bmp, and correctly place my "Today" pixel.

I've been digesting perpetual calendar formulas for the past day or so...and think there may be something there. I also think if I can maybe access some of the libraries' private variables, it may be easier....since it's already coming close to what I need.

Any help, code, formulae, links, etc....very appreciated.

Thanks!

1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Re: Help w/ RTCLib, Chronodot, and a calendar.

Post by 1chicagodave »

In case anyone is a bit more visual or has no idea what I'm talking about...here's a small demo.

I just 'cheated' and programmed in the next four months & have them rotating through the display. Which may be the best way to do the months...just have a look-up table or array? I'd rather do it with code....if it'll save any memory. I feel I'm gonna be pretty close to the limit by the time I'm done with everything. Either way, I still need to calculate which week it currently is.

http://youtube.com/watch?v=RBBwjlNioQg

It's a pretty quick little demo.

Thanks!

User avatar
jigsawnz
 
Posts: 180
Joined: Mon Mar 12, 2012 10:17 pm

Re: Help w/ RTCLib, Chronodot, and a calendar.

Post by jigsawnz »

Here I have some code for you to check the days in a month. Wrote it for a project a year ago.

Code: Select all

//---------------------------------------------------------------------------
//void validateDay()
//Purpose:  Check if it is a 30 day, 31 day, 28 day or 29 day month.
//Inputs:   Integer, integer
//Outputs:	Integer
//---------------------------------------------------------------------------
int validateDay( int year, int month )
{
	int day;

	if ( month == 4 || month == 6 || month == 9 || month == 11 )
		day = 30;

	if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8||
		  month == 10 || month == 12)
		day = 31;

	if ( month == 2 )
		day = daysInFebruary( year);

	return day;
}
//---------------------------------------------------------------------------
//void daysInFebruary()
//Purpose: 	Check if it is a leap year or not and check if the amount of days
//				are within the range.
//Inputs:
//Outputs:  Integer
//---------------------------------------------------------------------------
int daysInFebruary( int year)
{
	int maxDays;

	if ( year % 4 == 0 )
		if ( year % 100 == 0 )
			if ( year % 400 == 0 )
				maxDays = 29;
			else
				maxDays = 28;
		else
			maxDays = 29;
	else
		maxDays = 28;

	return maxDays;
}
//---------------------------------------------------------------------------

1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Re: Help w/ RTCLib, Chronodot, and a calendar.

Post by 1chicagodave »

Sweet....thanks!

1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Re: Help w/ RTCLib, Chronodot, and a calendar.

Post by 1chicagodave »

***SOLVED!***

Figured it out. I'll post my code in case anyone else ever needs something like this.

One by one:

• How many days in the current month
This is actually in the time libraries already, so it knows when to start a new month.
• What day of week the current month begins with (Sunday, Mon, etc...)
Beginning with what day of the week today is, this can be calculated with functions from time.h library & my new favorite, "%".

Code: Select all


int firstWeekdayOfMonth = weekday() - (day()%7) + 1;

// weekday() returns 1-7 for Sunday - Saturday
// day() returns today's date
// Example:
// Thursday, Sept 12, 2013
// weekday() returns '5'
// day() returns '12'
// 12 / 7 = 1 w/ remainder of 5
// 5 - 5 + 1 = 1 = Sunday was first weekday of the month

From that we can find the last bit -
• The current week of the month (1st week, 2nd week....5th week?)

Code: Select all

// Using same example date

int firstWeekEnds = 8 - firstWeekdayOfMonth;

// 8 - 1 = 7
// The 7th is Saturday, the last day of the first week in Sept 2013
// So...

int weekOne = firstWeekEnds;
int weekTwo = firstWeekEnds + 7;    // 2nd week ends 7 days later
int weekThree = weekTwo + 7;
int weekFour = weekThree + 7;
int weekFive = weekFour + 7;
int weekSix = weekFive + 7;

// then compare to today's date

if (day() <= weekOne) thisWeek = 0
else if (day() <= weekTwo && day() > weekOne) thisWeek = 1;
else if (day() <= weekThree && day() > weekTwo) thisWeek = 2;
else if (day() <= weekFour && day() > weekThree) thisWeek = 3;
else if (day() <= weekFive && day() > weekFour) thisWeek = 4;
else if (day() <= weekSix && day() > weekFive) thisWeek = 5;

// Using 0 - 5 since that's what I need for my matrix y-coordinate. 

And to light up that day in my LED matrix....

Code: Select all


int dayX = weekday() - 1;      // since matrix starts with (0,0) 
                                            // and weekday() starts with '1'

int dayY = thisWeek;           // from previous section of code

matrix.drawPixel(dayX, dayY, LED_RED);      // Turns on red LED at correct point in matrix
matrix.writeDisplay();

The code could be made a bit simpler. But, I kept it like this to keep it very understandable.
Hope it helps someone!

Thanks again jigsawnz for posting a piece to the puzzle. It did help!

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”