Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The library provides a single class named TM1637Display. An instance of this cla

* `setSegments` - Set the raw value of the segments of each digit
* `showNumberDec` - Display a decimal number
* `showNumberFloatColon` - Display a floating point number using a colon for the decimal point
* `showNumberDecEx` - Display a decimal number with decimal points or colon
* `setBrightness` - Sets the brightness of the display

Expand Down
27 changes: 18 additions & 9 deletions TM1637Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ void TM1637Display::showNumberDec(int num, bool leading_zero, uint8_t length, ui
showNumberDecEx(num, 0, leading_zero, length, pos);
}

void TM1637Display::showNumberFloatColon(float num, bool leading_zero) {
const int COLON_SHIFT = 100;

int32_t num_long = round(num * COLON_SHIFT); // Need int32_t to store 700.0 shifted
if (num_long < 10000 && num_long > -1000)
{
// In this case a int16_t could store the number but no need to explicitly convert it
bool use_leading_zero = leading_zero || (-COLON_SHIFT < num_long && num_long < COLON_SHIFT);
showNumberDecEx(num_long, 0b01000000, use_leading_zero);
}
else
{
showNumberDec(round(num), leading_zero);
}
}

void TM1637Display::showNumberDecEx(int num, uint8_t dots, bool leading_zero,
uint8_t length, uint8_t pos)
{
Expand Down Expand Up @@ -143,24 +159,17 @@ void TM1637Display::showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots, bo
digits[length-1] = encodeDigit(0);
}
else {
//uint8_t i = length-1;
//if (negative) {
// // Negative number, show the minus sign
// digits[i] = minusSegments;
// i--;
//}

for(int i = length-1; i >= 0; --i)
{
uint8_t digit = num % base;

if (digit == 0 && num == 0 && leading_zero == false)
if (digit == 0 && num == 0 && !leading_zero)
// Leading zero is blank
digits[i] = 0;
else
digits[i] = encodeDigit(digit);

if (digit == 0 && num == 0 && negative) {
if (digit == 0 && num == 0 && negative && (!leading_zero || i == 0)) {
digits[i] = minusSegments;
negative = false;
}
Expand Down
17 changes: 15 additions & 2 deletions TM1637Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,26 @@ class TM1637Display {
//!
//! @param num The number to be shown
//! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
//! blank. NOTE: leading zero is not supported with negative numbers.
//! blank.
//! @param length The number of digits to set. The user must ensure that the number to be shown
//! fits to the number of digits requested (for example, if two digits are to be displayed,
//! the number must be between 0 to 99)
//! @param pos The position of the most significant digit (0 - leftmost, 3 - rightmost)
void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);

//! Display a floating point number using a colon for the decimal point
//!
//! Display the given argument as a floating point number, displaying digits to the right of the
//! decimal point only when possible while prioritizing all digits to the left of the decimal
//! point, and given the limitations of 4 digit 7 segment displays.
//!
//! @param num The number to be shown
//! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
//! blank.
//! Note: When `-1 < num < 1` and `leading_zero` is false, superfluous leading zeros may be
//! shown. This behavior may change in the future.
void showNumberFloatColon(float num, bool leading_zero = false);

//! Display a decimal number, with dot control
//!
//! Display the given argument as a decimal number. The dots between the digits (or colon)
Expand All @@ -99,7 +112,7 @@ class TM1637Display {
//! For displays with dots and colons colon:
//! * 0.0:0.0 (0b11100000)
//! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
//! blank. NOTE: leading zero is not supported with negative numbers.
//! blank.
//! @param length The number of digits to set. The user must ensure that the number to be shown
//! fits to the number of digits requested (for example, if two digits are to be displayed,
//! the number must be between 0 to 99)
Expand Down
42 changes: 41 additions & 1 deletion examples/TM1637Test/TM1637Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ void loop()
delay(TEST_DELAY);
display.showNumberDec(301, true); // Expect: 0301
delay(TEST_DELAY);
display.showNumberDec(-23, true); // Expect: -023
delay(TEST_DELAY);
display.clear();
display.showNumberDec(14, false, 2, 1); // Expect: _14_
delay(TEST_DELAY);
Expand All @@ -88,6 +90,34 @@ void loop()
display.clear();
display.showNumberDec(-5, false, 3, 0); // Expect: _-5_
delay(TEST_DELAY);

// Show floating point numbers using the colon for a decimal point when possible
display.showNumberFloatColon(12.34); // Expect: 12:34
delay(TEST_DELAY);
display.showNumberFloatColon(-12.34); // Expect: _-12
delay(TEST_DELAY);
display.showNumberFloatColon(99.996); // Expect: _100
delay(TEST_DELAY);
display.showNumberFloatColon(-9.996); // Expect: _-10
delay(TEST_DELAY);
display.showNumberFloatColon(700.6); // Expect: _701
delay(TEST_DELAY);
display.showNumberFloatColon(0.996); // Expect: _1:00
delay(TEST_DELAY);
display.showNumberFloatColon(2.34, false); // Expect: _2:34
delay(TEST_DELAY);
display.showNumberFloatColon(2.34, true); // Expect: 02:34
delay(TEST_DELAY);
display.showNumberFloatColon(0.01, true); // Expect: 00:01
delay(TEST_DELAY);
display.showNumberFloatColon(0.02, false); // Expect: 00:02 - Future: _0:02
delay(TEST_DELAY);
display.showNumberFloatColon(-0.01, true); // Expect: -0.01
delay(TEST_DELAY);
display.showNumberFloatColon(-0.02, false); // Expect: -0.02
delay(TEST_DELAY);

// Hexadecimal
display.showNumberHexEx(0xf1af); // Expect: f1Af
delay(TEST_DELAY);
display.showNumberHexEx(0x2c); // Expect: __2C
Expand Down Expand Up @@ -126,6 +156,16 @@ void loop()

// Done!
display.setSegments(SEG_DONE);
delay(TEST_DELAY);

while(1);
// Voltmeter
while(true) {
// Constants for Arduino Uno
const float RESOLUTION = 1023.0; // Max value for resolution
const float OPERATING_VOLTAGE = 5.0;
// If analog input pin is not connected, reading will fluctuate
float reading = (analogRead(0) / RESOLUTION) * OPERATING_VOLTAGE;
display.showNumberFloatColon(reading);
delay(100);
}
}