Permalink
Browse files

Fixed millis() in interface for Windows and Linux

It did not reach UINT32_MAX before rolling over. Also added delay().
  • Loading branch information...
fredilarsen committed Jan 29, 2018
1 parent a74ee5c commit 7a3fa29d05d2c07b8192c24b95784b345e5c0895
@@ -45,6 +45,7 @@
};
auto start_ts = std::chrono::high_resolution_clock::now();
auto start_ts_ms = std::chrono::high_resolution_clock::now();
uint32_t micros() {
auto elapsed_usec =
@@ -59,7 +60,10 @@
};
uint32_t millis() {
return (micros() / 1000);
return (uint32_t)
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - start_ts_ms
).count();
};
void delayMicroseconds(uint32_t delay_value) {
@@ -74,6 +78,10 @@
}
};
void delay(uint32_t delay_value_ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(delay_value_ms));
}
#ifndef A0
#define A0 0
#endif
@@ -26,8 +26,6 @@
#define constrain(x, l, u) (x<l?l:(x>u?u:x))
#endif
void delay(uint32_t ms) { PJON_DELAY_MICROSECONDS(ms*1000ul); }
class TCPHelperClient {
#ifdef _WIN32
SOCKET _fd = -1;
@@ -275,6 +273,8 @@ class TCPHelperClient {
}
bool operator!=(const TCPHelperClient& rhs) { return !this->operator==(rhs); }
uint8_t getSocketNumber() { return _fd; }
int print(const char *msg) { return write((const uint8_t*) msg, strlen(msg)); }
};
@@ -352,7 +352,11 @@ class TCPHelperServer {
#ifdef _WIN32
unsigned long ul = 1;
int flags = ioctlsocket(_fd, FIONBIO, (unsigned long *)&ul); //Set into non blocking mode.
if (flags != SOCKET_ERROR); //Failed to set.
if (flags != NO_ERROR) { // Failed to set.
#ifdef ETCP_ERROR_PRINT
printf("ioctlsocket FIONBIO failed.");
#endif
}
#else
int flags = fcntl(_fd, F_GETFL, 0);
if (flags != -1) fcntl(_fd, F_SETFL, flags | O_NONBLOCK);
@@ -42,6 +42,7 @@
};
auto start_ts = std::chrono::high_resolution_clock::now();
auto start_ts_ms = std::chrono::high_resolution_clock::now();
uint32_t micros() {
auto elapsed_usec =
@@ -55,6 +56,13 @@
} else return (uint32_t) elapsed_usec;
};
uint32_t millis() {
return (uint32_t)
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - start_ts_ms
).count();
};
void delayMicroseconds(uint32_t delay_value) {
auto begin_ts = std::chrono::high_resolution_clock::now();
@@ -68,6 +76,10 @@
}
};
void delay(uint32_t delay_value_ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(delay_value_ms));
}
#ifndef A0
#define A0 0
#endif
@@ -141,6 +153,6 @@
#endif
#ifndef PJON_MILLIS
#define PJON_MILLIS() (PJON_MICROS() / 1000)
#define PJON_MILLIS millis
#endif
#endif
@@ -140,10 +140,6 @@
static void println(const char *s) { printf("%s\n", s); }
static void println(int n) { printf("%d\n", n); }
} DummyPrint;
#ifdef _WIN32
#define millis() PJON_MILLIS()
#endif
#endif
// Constants

0 comments on commit 7a3fa29

Please sign in to comment.