Skip to content

Commit

Permalink
Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
AKJ7 committed Jan 21, 2021
1 parent 5254e93 commit c7f5489
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void loop()
delay(1000);

// Display float:
tm.display(2.65);
tm.display(29.65);
delay(1000);

// Display String:
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void loop()
delay(1000);

// Display float:
tm.display(2.65);
tm.display(29.65);
delay(1000);

// Display String:
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=TM1637 Driver
version=2.0.2
version=2.1.0
author=AKJ <akj123429@gmail.com>
maintainer=AKJ <akj123429@gmail.com>
sentence=Feature-full and simple TM1637 library with nonblocking animation support
Expand Down
25 changes: 22 additions & 3 deletions src/TM1637.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct DisplayDigit
DisplayDigit& setG() { value |= 0x40; return *this; }
DisplayDigit& setDot() { value |= 0x80; return *this; }
operator uint8_t() { return value; }
DisplayDigit& operator=(uint8_t rhs) { value = rhs; }
DisplayDigit& operator=(uint8_t rhs) { value = rhs; return *this; }

uint8_t value{};
};
Expand Down Expand Up @@ -60,7 +60,7 @@ class TM1637
Animator*>::type
display(const T value, bool overflow = true, bool pad = false, uint8_t offset = 0)
{
String temp(value);
String temp = stringer<T>(value);
if (temp == cache_)
return &animator_;
cache_ = temp;
Expand All @@ -85,7 +85,6 @@ class TM1637
animator_.buffer_ = "";
for (decltype(size) counter{}; counter < size; ++counter)
animator_.buffer_.concat(static_cast<char>(buffer[counter]));
animator_.resetAnimation();
animator_.refresh();
return &animator_; // TODO: Use weak_ptr
}
Expand All @@ -102,6 +101,26 @@ class TM1637
void setBrightness(uint8_t value) noexcept { animator_.brightness_ = Animator::fetchControl(value); };

private:
template <typename T>
typename type_traits::enable_if<type_traits::is_string<T>::value, String>::type stringer(T value)
{
return value;
}

template <typename T>
typename type_traits::enable_if<type_traits::is_integral<T>::value, String>::type stringer(T value)
{
return String(value);
}

template <typename T>
typename type_traits::enable_if<type_traits::is_floating_point<T>::value, String>::type stringer(T value)
{
return String(value, TOTAL_DIGITS);
}



Animator animator_;
String cache_ = "";
};
Expand Down
14 changes: 9 additions & 5 deletions src/animator.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ class Animator
sendToDisplay<DataCommand_e::FIXED_ADDRESS, AddressCommand_e::C6H>(brightness);
}

void reset(const String& value)
{
void reset(const String& value) {
buffer_ = "";
for (decltype(value.length()) counter{}; counter < value.length(); ++counter)
buffer_.concat(static_cast<char>(toDisplayDigit(value[counter])));
for (decltype(value.length()) counter{}; counter < value.length(); ++counter) {
auto d = toDisplayDigit(value[counter]);
if (d == 0x80u && buffer_.length() > 0) {
buffer_[buffer_.length() - 1] |= static_cast<char>(0x80u);
} else
buffer_.concat(static_cast<char>(d));
}
resetAnimation();
}

Expand All @@ -194,7 +198,7 @@ class Animator
private:
inline uint8_t toDisplayDigit(signed char c) noexcept
{
return c < 0 ? 0x00 : ascii[static_cast<int>(c)];
return c < 0 ? 0x00 : ascii[static_cast<unsigned>(c)];
}

static inline uint8_t control2Int(DisplayControl_e e) noexcept
Expand Down

0 comments on commit c7f5489

Please sign in to comment.