Note that there are some explanatory texts on larger screens.

plurals
  1. POstatic functions
    primarykey
    data
    text
    <p>well i have made an ntp server lib for an arduino program.I want to make the functions i made in the ntp lib static so i can call them from other classes without making objects of that ntp server class.</p> <p>My problem is that as i make one by one the functions and the variables of the class static (since some functions use some of the class variables) i find myself making a certain function static and then the whole library gives error. The whole library can not understand any variable.I get error that variables can not be resolved also i get error that functions of the NTP class can not bet used since there is not object for them( but it is the functions of the class and there are not implemented in to another class). i will give you my code.</p> <pre><code>#ifndef NTPLIB_H_ #define NTPLIB_H_ #include "Arduino.h" #include "SPI.h" #include "IPAddress.h" #include "EthernetUdp.h" #include "Ethernet.h" #include "DayNumber.h" class NTPlib { private: public: static EthernetUDP Udp; static DayNumber DN; static const int Gmt=2; static const unsigned int localPort = 8888; static const int NTP_PACKET_SIZE=48; static byte packetBuffer[NTP_PACKET_SIZE ]; unsigned long secsSince1900; unsigned long UnixTime; static unsigned long daysPassedInYear; static int utchour; static int lcthour; static int min; static int sec; static int year; static int month; static int date; static int dayOfWeek; NTPlib(); static NTPlib getTime(); static bool testNtpServer(); void startEthernetAndUdp(); unsigned long static sendNTPpacket(IPAddress&amp; address); virtual ~NTPlib(); }; #endif /* NTPLIB_H_ */ </code></pre> <p>this was the header file and here is the .cpp file</p> <pre><code>#include "NTPlib.h" NTPlib::NTPlib() { // TODO Auto-generated constructor stub } NTPlib NTPlib::getTime(){ if (testNtpServer()){ // We've received a packet, read the data from it Udp.read((unsigned char*)packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer //the timestamp starts at byte 40 of the received packet and is four bytes, // or two words, long. First, extract the two words: unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); // combine the four bytes (two words) into a long integer // this is NTP time (seconds since Jan 1 1900): unsigned long secsSince1900 = highWord &lt;&lt; 16 | lowWord; Serial.print("Seconds since Jan 1 1900 = " ); Serial.println(secsSince1900); // now convert NTP time into everyday time: // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: const unsigned long seventyYears = 2208988800UL; // subtract seventy years: unsigned long UnixTime = secsSince1900 - seventyYears; sec = UnixTime % 60; //calc min min = (UnixTime/60)%60; //calc hour utchour = (UnixTime/3600)%24; lcthour = utchour + Gmt; //day of the week dayOfWeek = (((UnixTime/86400UL) + 3) % 7) + 1;//setting first day Sunday=1 // print Unix time: Serial.print("Unix time = "); Serial.println(UnixTime); Serial.print("the day is : "); Serial.println(dayOfWeek); Serial.print("The LTC time is "); Serial.println(lcthour); unsigned long UnixTimeToDays=UnixTime/86400UL; Serial.println(UnixTimeToDays); unsigned long calcDaysInYears=0; int calcYear=1970; while((calcDaysInYears+= (DN.checkLeapYear(calcYear)? 366:365)) &lt;= UnixTimeToDays){ calcYear++; } //setting year year = calcYear; Serial.print("the year is :"); Serial.println(year); //calculating days in this year calcDaysInYears -= (DN.checkLeapYear(calcYear)? 366:365); daysPassedInYear = UnixTimeToDays - calcDaysInYears; Serial.println(daysPassedInYear); //set Daynumber one year loop DN.Days1YearLoop = daysPassedInYear+1; //calculating date and month static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; int calcMonth; int monthLength; for (calcMonth=0; calcMonth&lt;12;calcMonth++){ if (DN.checkLeapYear(year)){ monthLength = (calcMonth==1) ? 29: 28; }else{ monthLength = monthDays[calcMonth]; } if ( daysPassedInYear &gt; monthLength){ daysPassedInYear -= monthLength; }else{ break; } } month = ++calcMonth; date = ++daysPassedInYear; Serial.print("Month is : "); Serial.print(month); Serial.print(" Date is: "); Serial.println(date); // print the hour, minute and second: Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) Serial.print(utchour); // print the hour (86400 equals secs per day) Serial.print(':'); if ( min &lt; 10 ) { // In the first 10 minutes of each hour, we'll want a leading '0' Serial.print('0'); } Serial.print(min); // print the minute (3600 equals secs per minute) Serial.print(':'); if ( sec &lt; 10 ) { // In the first 10 seconds of each minute, we'll want a leading '0' Serial.print('0'); } Serial.println(sec); // print the seconds return NTPlib(); }else{ //error ntp time from DS1307 Serial.println("RTC"); return NTPlib(); } } unsigned long NTPlib::sendNTPpacket(IPAddress&amp; address){ // set all bytes in the buffer to 0 memset(packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request // (see URL above for details on the packets) packetBuffer[0] = 0b11100011; // LI, Version, Mode packetBuffer[1] = 0; // Stratum, or type of clock packetBuffer[2] = 6; // Polling Interval packetBuffer[3] = 0xEC; // Peer Clock Precision // 8 bytes of zero for Root Delay &amp; Root Dispersion packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; // all NTP fields have been given values, now // you can send a packet requesting a timestamp: Udp.beginPacket(address, 123); //NTP requests are to port 123 Udp.write(packetBuffer,NTP_PACKET_SIZE); Udp.endPacket(); } void NTPlib::startEthernetAndUdp(){ //declaration of the mac address of ethernet shield byte mac[]= {0x00,0xAA,0xBB,0xCC,0xDE,0x02}; if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: //for(;;) // ; } Udp.begin(localPort); } bool NTPlib::testNtpServer(){ byte serverslist[4][4]={ 193,93,167,241, 129,215,160,240, 138,195,130,71, 132,163,4,101 } ; IPAddress ntpServers(serverslist[0]); sendNTPpacket(ntpServers); int x=0; delay(1000); while(!Udp.parsePacket() &amp;&amp; x&lt;=3){ x++; IPAddress ntpServers(serverslist[x]); Serial.println("ton pairneis"); sendNTPpacket(ntpServers); delay(1000); } if (x&lt;=3){ Serial.print("leitoyrgei o server "); Serial.println(++x); return true; }else{ return false; } } NTPlib::~NTPlib() { // TODO Auto-generated destructor stub } </code></pre> <p>well at first i made gettime function static and ofcourse all the related variables.After that i had to make testNTPServer function static and from i had only 1 error. The error was </p> <blockquote> <p>Description Resource Path Location Type cannot call member function 'long unsigned int NTPlib::sendNTPpacket(IPAddress&amp;)' without object NTPlib.cpp /NTP/lib line 191 C/C++ Problem</p> </blockquote> <p>This function is a function of the class it shouldn't need object. also when i do this function static all cpp give Undefined Reference to all of the variables of the class.One example is this:</p> <blockquote> <p>Description Resource Path Location Type more undefined references to `NTPlib::packetBuffer' follow NTPlib.cpp /NTP/lib line 149 C/C++ Problem</p> </blockquote> <p>Any help?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload