#include <TimeLib.h>
uint32_t dtg=0;
uint16_t MM=0;
uint16_t DD=0;
uint16_t HH=0;
//ag fokkit kom ons add die jaar ook.
//we MUST cast the YY to a 32 bit value here
//so that it all shifts left properly because if it stays a 16bit value
//the left few bits will dissappear
uint32_t YY=0;
#define BUFLEN 40
#define YEAROFFSET 2023
#define RECORDLENGTH 10
char buf[BUFLEN];
//create array for month names-not gna use waste of space
/*
char const months[12][4]=
{
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC",
};
*/
void setup()
{
Serial.begin(115200);
//change these values to suit you
//syntax:setTime(int hr,int min,int sec,int day, int month, int yr);
//set time doesnt seem to work after year 2054
setTime(19,45,00,24,11,2043); //these values will actually come from the RTC
}
void loop()
{
MM=month(); //we using the month() from the timelib.h library but it will come from RTC
DD=day();
HH=hour();
YY=year()-YEAROFFSET; //this year is year zero
snprintf(buf,BUFLEN,"Y:%lu M:%i D:%i H:%i",YY,MM,DD,HH);//u must learn the use of sprintf and snprintf ok!
Serial.println(buf);
//create the 32 bit variable 'dtg' that holds all three values YY MM DD HH
//the year then means our DTG has to be an uint32_t with place for 32 bits
//which means we have 10 bytes per record which is perfectly ok
//the YYYYY is 5 bits which =32 so we start at year 2023 as value 0 and add 1 for each year thereafter
//thus we can hold 32 years worth
//our dtg BITS will look like this then:
//YYYYYMMMMDDDDDHHHHH
dtg=dtg|(YY<<14);
dtg=dtg|(MM<<10); //OR saves AND destroys
dtg=dtg|(DD<<5);
dtg=dtg|(HH<<0);
Serial.println(dtg,BIN);//BIN prints it in binary format-arduino lekka thingy
//user sends command #pr11 for 11th month
printReport(11);
delay(20000);
}
/*
So in the eeprom for an example the very first record will be at address zero.
So byte address:
0-3 Will hold the DTG
4-5 Will hold the Car Count Total 16 bits is 2^16 = 65536 max cars per hour
6-7 Will hold the fast car count
8 Will hold the avg speed. It only needs 1 byte . i.e.: 0-255
9 Will hold the overspeed avg
Then the second record will start at address 10, and so on.
When you have completed a record write, we write the new position of the cursor to:
Eeprom address 130 000. This EEPROM holds 131072 bytes of data.
And 365 days *24 hrs * 10 bytes per record
is a total bytes needed of 87600 bytes. so anything after address 87600 is never used.
So we can write to the end of the EEPROM addresses to store stuff securely.
Such as the cursorvalue.
But the cursorvalue needs 4 bytes because it goes to address 87600 which is just outside the 65536 limit of 2 bytes.
So the cursor is at 130000 to address 130004.
So, the process of storing the data is
1. create the ten byte record
2. save the record at the current cursorvalue found at address 130000
3. if successfully saved
4. increment cursorvalue=cursorvalue + 8.
5. if cursorvalue >= 87600 then make cursorvalue=0
5. save cursorvalue to eeprom address 130000-130004.
*/
void MDHtoDTG(int mnth,int dy,int hr)
{
}
void DTGtoMDH(uint16_t dtg)
{
}
void printReport(int monthNr)
{
//this is how we will show it to the clients serial printout
//lets make some random bullshit car counts,etc.
int cc,ovspd,avg,overavg;
for(uint32_t i=0;i<8760;i=i+RECORDLENGTH)
{
//TODO:read the data in from the EEPROM
//convert from dtg back to MM DD HH
int HHH=(dtg>>0) & 0b00011111; //this 0b00011111 is called a mask.
int DDD=(dtg>>5) & 0b00011111;
int MMM=(dtg>>10) & 0b00001111;
int YYY=(dtg>>14) & 0b00011111;
if(MMM==monthNr)
{
cc=random(2500);
ovspd=random(1200);
avg=random(85);
overavg=random(78);
snprintf(buf,BUFLEN,"%lu,%i/%i/%i,%i:00,%i,%i,%i,%i",i,YYY+YEAROFFSET,MMM,DDD,HHH+i,cc,ovspd,avg,overavg);
Serial.println(buf);
}
}
Serial.println("========================");
}