gpt4 book ai didi

c++ - 实现 TimeLib.h 的 Arduino 类

转载 作者:行者123 更新时间:2023-11-30 05:21:20 33 4
gpt4 key购买 nike

我正在为使用 TimeLib.h 库的 ESP8266-Arduino 创建这个库。

在我的标题中我有:

WiFiUDP *_UdpNTP = NULL;
void setupTime();
time_t getNtpTime();
void sendNTPpacket(WiFiUDP *u);

这些是我的功能:

void Konnec::setupTime() {
setSyncProvider(getNtpTime);
setSyncInterval(NTP_POL_TIME);
}


time_t Konnec::getNtpTime() {
//WiFiUDP udp;
//udp.begin(localPort);

_UdpNTP = new WiFiUDP();
_UdpNTP->begin(localPort);

while (_UdpNTP->parsePacket() > 0); // discard any previously received packets
for (int i = 0; i < 5; i++) { // 5 retries.
sendNTPpacket(_UdpNTP);
uint32_t beginWait = millis();
while (millis() - beginWait < 1000) {
if (_UdpNTP->parsePacket()) {
Serial.println("");
Serial.println("Receive NTP Response");
_UdpNTP->read(packetBuffer, NTP_PACKET_SIZE);
// Extract seconds portion.
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secSince1900 = highWord << 16 | lowWord;
_UdpNTP->flush();
return secSince1900 - 2208988800UL + TIMEZONE * SECS_PER_HOUR;
}
delay(10);
}
}
Serial.println("");
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}

void Konnec::sendNTPpacket(WiFiUDP *u) {
// Zeroise the buffer.
memset(packetBuffer, 0, NTP_PACKET_SIZE);
memcpy(packetBuffer, sendBuffer, 16);

if (u->beginPacket(timeServer, 123)) {
u->write(packetBuffer, NTP_PACKET_SIZE);
u->endPacket();
}
}

这是我得到的错误:

konnec.cpp: 991:28: error: cannot convert 'Konnec::getNtpTime' from type 'time_t (Konnec::)() {aka long int (Konnec::)()}' to type 'getExternalTime {aka long int (*)()}
setSyncProvider(getNtpTime)
Error compiling libraries

有人知道解决方案是什么吗?

最佳答案

问题似乎是 Konnec::getNtpTime 是一个类方法,而 setSyncProvider 需要一个函数。请记住,函数和方法是不同的东西,因为方法还需要知道它正在操作的特定类实例。

虽然查看您的代码,但看起来 getNtpTime 不依赖于您的类的任何特定内容,因此您可以将其声明为:

static time_t Konnec::getNtpTime();

这将使它被用作一个函数,因为它在那个时候是有效的。

关于c++ - 实现 TimeLib.h 的 Arduino 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40198412/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com