Skip to content

Uploaded 4.3 version with TT-GO 1.14 display #1816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
106 changes: 53 additions & 53 deletions ESP_Code/Counter.h
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
#ifndef _COUNTER_H_
#define _COUNTER_H_

#include <Arduino.h>
#include <string.h>

template <unsigned int max_digits>
class Counter {

public:
Counter() { reset(); }

void reset() {
memset(buffer, '0', max_digits);
buffer[max_digits] = '\0';
val = 0;
len = 1;
}

inline Counter &operator++() {
inc_string(buffer + max_digits - 1);
++val;
return *this;
}

inline operator unsigned int() const { return val; }
inline const char *c_str() const { return buffer + max_digits - len; }
inline size_t strlen() const { return len; }

protected:
inline void inc_string(char *c) {
// In theory, the line below should be uncommented to avoid writing outside the buffer. In practice however,
// with max_digits set to 10 or more, we can fit all possible unsigned 32-bit integers in the buffer.
// The check is skipped to gain a small extra speed improvement.
// if (c >= buffer) return;

if (*c < '9') {
*c += 1;
}
else {
*c = '0';
inc_string(c - 1);
len = max(max_digits - (c - buffer) + 1, len);
}
}

protected:
char buffer[max_digits + 1];
unsigned int val;
size_t len;
};

#endif
#ifndef _COUNTER_H_
#define _COUNTER_H_
#include <Arduino.h>
#include <string.h>
template <unsigned int max_digits>
class Counter {
public:
Counter() { reset(); }
void reset() {
memset(buffer, '0', max_digits);
buffer[max_digits] = '\0';
val = 0;
len = 1;
}
inline Counter &operator++() {
inc_string(buffer + max_digits - 1);
++val;
return *this;
}
inline operator unsigned int() const { return val; }
inline const char *c_str() const { return buffer + max_digits - len; }
inline size_t strlen() const { return len; }
protected:
inline void inc_string(char *c) {
// In theory, the line below should be uncommented to avoid writing outside the buffer. In practice however,
// with max_digits set to 10 or more, we can fit all possible unsigned 32-bit integers in the buffer.
// The check is skipped to gain a small extra speed improvement.
// if (c >= buffer) return;
if (*c < '9') {
*c += 1;
}
else {
*c = '0';
inc_string(c - 1);
len = max(max_digits - (c - buffer) + 1, len);
}
}
protected:
char buffer[max_digits + 1];
unsigned int val;
size_t len;
};
#endif
Loading