From d7eed7535542d67284a1731dfd7096c4851b4f2d Mon Sep 17 00:00:00 2001 From: David Refoua Date: Fri, 9 Feb 2024 22:44:16 +0330 Subject: [PATCH] fix indentation --- README.md | 6 +- TM1637Display.cpp | 201 ++++++++++++++--------------- TM1637Display.h | 21 ++- examples/TM1637Test/TM1637Test.ino | 12 +- keywords.txt | 1 - library.properties | 3 +- release_notes.md | 8 +- 7 files changed, 124 insertions(+), 128 deletions(-) diff --git a/README.md b/README.md index 403028a..93a0c1d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ TM1637 ====== -Arduino library for TM1637 (LED Driver) +Arduino library for TM1637 (LED Driver). Description @@ -9,7 +9,7 @@ An Arduino library for 7-segment display modules based on the TM1637 chip, such Hardware Connection ------------------- -The display modules has two signal connection (and two power connections) which are CLK and DIO. These pins can be connected to any pair of digital pins on the Arduino. When an object is created, the pins should be configured. There is no limitation on the number of instances used concurrently (as long as each instance has a pin pair of its own) +The display modules has two signal connection, CLK and DIO, as well as two power connections, VCC and GND. The signal pins can be connected to any pair of digital pins on the Arduino. When an object is created, the pins should be configured. There is no limitation on the number of instances used concurrently (as long as each instance has a pin pair of its own). Installation ------------ @@ -24,4 +24,4 @@ The library provides a single class named TM1637Display. An instance of this cla * `showNumberDecEx` - Display a decimal number with decimal points or colon * `setBrightness` - Sets the brightness of the display -The information given above is only a summary. Please refer to TM1637Display.h for more information. An example is included, demonstrating the operation of most of the functions. +The information given above is only a summary. Please refer to `TM1637Display.h` for more information. An example is included, demonstrating the operation of most of the functions. diff --git a/TM1637Display.cpp b/TM1637Display.cpp index eada692..9f08bdb 100644 --- a/TM1637Display.cpp +++ b/TM1637Display.cpp @@ -16,9 +16,9 @@ // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA extern "C" { - #include - #include - #include + #include + #include + #include } #include @@ -37,24 +37,24 @@ extern "C" { // --- // D const uint8_t digitToSegment[] = { - // XGFEDCBA - 0b00111111, // 0 - 0b00000110, // 1 - 0b01011011, // 2 - 0b01001111, // 3 - 0b01100110, // 4 - 0b01101101, // 5 - 0b01111101, // 6 - 0b00000111, // 7 - 0b01111111, // 8 - 0b01101111, // 9 - 0b01110111, // A - 0b01111100, // b - 0b00111001, // C - 0b01011110, // d - 0b01111001, // E - 0b01110001 // F - }; + // XGFEDCBA + 0b00111111, // 0 + 0b00000110, // 1 + 0b01011011, // 2 + 0b01001111, // 3 + 0b01100110, // 4 + 0b01101101, // 5 + 0b01111101, // 6 + 0b00000111, // 7 + 0b01111111, // 8 + 0b01101111, // 9 + 0b01110111, // A + 0b01111100, // b + 0b00111001, // C + 0b01011110, // d + 0b01111001, // E + 0b01110001 // F +}; static const uint8_t minusSegments = 0b01000000; @@ -67,8 +67,8 @@ TM1637Display::TM1637Display(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDel // Set the pin direction and default value. // Both pins are set as inputs, allowing the pull-up resistors to pull them up - pinMode(m_pinClk, INPUT); - pinMode(m_pinDIO,INPUT); + pinMode(m_pinClk, INPUT); + pinMode(m_pinDIO, INPUT); digitalWrite(m_pinClk, LOW); digitalWrite(m_pinDIO, LOW); } @@ -80,7 +80,7 @@ void TM1637Display::setBrightness(uint8_t brightness, bool on) void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos) { - // Write COMM1 + // Write COMM1 start(); writeByte(TM1637_I2C_COMM1); stop(); @@ -91,7 +91,7 @@ void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_ // Write the data bytes for (uint8_t k=0; k < length; k++) - writeByte(segments[k]); + writeByte(segments[k]); stop(); @@ -103,78 +103,78 @@ void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_ void TM1637Display::clear() { - uint8_t data[] = { 0, 0, 0, 0 }; + uint8_t data[] = { 0, 0, 0, 0 }; setSegments(data); } void TM1637Display::showNumberDec(int num, bool leading_zero, uint8_t length, uint8_t pos) { - showNumberDecEx(num, 0, leading_zero, length, pos); + showNumberDecEx(num, 0, leading_zero, length, pos); } void TM1637Display::showNumberDecEx(int num, uint8_t dots, bool leading_zero, uint8_t length, uint8_t pos) { - showNumberBaseEx(num < 0? -10 : 10, num < 0? -num : num, dots, leading_zero, length, pos); + showNumberBaseEx(num < 0? -10 : 10, num < 0? -num : num, dots, leading_zero, length, pos); } void TM1637Display::showNumberHexEx(uint16_t num, uint8_t dots, bool leading_zero, uint8_t length, uint8_t pos) { - showNumberBaseEx(16, num, dots, leading_zero, length, pos); + showNumberBaseEx(16, num, dots, leading_zero, length, pos); } void TM1637Display::showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots, bool leading_zero, uint8_t length, uint8_t pos) { - bool negative = false; + bool negative = false; + if (base < 0) { - base = -base; + base = -base; negative = true; } - - uint8_t digits[4]; + uint8_t digits[4]; if (num == 0 && !leading_zero) { // Singular case - take care separately - for(uint8_t i = 0; i < (length-1); i++) + for (uint8_t i = 0; i < (length-1); i++) digits[i] = 0; 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 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; - + uint8_t digit = num % base; + if (digit == 0 && num == 0 && leading_zero == false) - // Leading zero is blank + // Leading zero is blank digits[i] = 0; else - digits[i] = encodeDigit(digit); - + digits[i] = encodeDigit(digit); + if (digit == 0 && num == 0 && negative) { - digits[i] = minusSegments; + digits[i] = minusSegments; negative = false; } num /= base; } - } - - if(dots != 0) + } + + if (dots != 0) { showDots(dots, digits); } - - setSegments(digits, length, pos); + + setSegments(digits, length, pos); } void TM1637Display::bitDelay() @@ -184,8 +184,8 @@ void TM1637Display::bitDelay() void TM1637Display::start() { - pinMode(m_pinDIO, OUTPUT); - bitDelay(); + pinMode(m_pinDIO, OUTPUT); + bitDelay(); } void TM1637Display::stop() @@ -200,56 +200,55 @@ void TM1637Display::stop() bool TM1637Display::writeByte(uint8_t b) { - uint8_t data = b; - - // 8 Data Bits - for(uint8_t i = 0; i < 8; i++) { - // CLK low - pinMode(m_pinClk, OUTPUT); - bitDelay(); - - // Set data bit - if (data & 0x01) - pinMode(m_pinDIO, INPUT); - else - pinMode(m_pinDIO, OUTPUT); - - bitDelay(); - - // CLK high - pinMode(m_pinClk, INPUT); - bitDelay(); - data = data >> 1; - } - - // Wait for acknowledge - // CLK to zero - pinMode(m_pinClk, OUTPUT); - pinMode(m_pinDIO, INPUT); - bitDelay(); - - // CLK to high - pinMode(m_pinClk, INPUT); - bitDelay(); - uint8_t ack = digitalRead(m_pinDIO); - if (ack == 0) - pinMode(m_pinDIO, OUTPUT); - - - bitDelay(); - pinMode(m_pinClk, OUTPUT); - bitDelay(); - - return ack; + uint8_t data = b; + + // 8 Data Bits + for(uint8_t i = 0; i < 8; i++) { + // CLK low + pinMode(m_pinClk, OUTPUT); + bitDelay(); + + // Set data bit + if (data & 0x01) + pinMode(m_pinDIO, INPUT); + else + pinMode(m_pinDIO, OUTPUT); + + bitDelay(); + + // CLK high + pinMode(m_pinClk, INPUT); + bitDelay(); + data = data >> 1; + } + + // Wait for acknowledge + // CLK to zero + pinMode(m_pinClk, OUTPUT); + pinMode(m_pinDIO, INPUT); + bitDelay(); + + // CLK to high + pinMode(m_pinClk, INPUT); + bitDelay(); + uint8_t ack = digitalRead(m_pinDIO); + if (ack == 0) + pinMode(m_pinDIO, OUTPUT); + + bitDelay(); + pinMode(m_pinClk, OUTPUT); + bitDelay(); + + return ack; } void TM1637Display::showDots(uint8_t dots, uint8_t* digits) { - for(int i = 0; i < 4; ++i) - { - digits[i] |= (dots & 0x80); - dots <<= 1; - } + for (int i = 0; i < 4; ++i) + { + digits[i] |= (dots & 0x80); + dots <<= 1; + } } uint8_t TM1637Display::encodeDigit(uint8_t digit) diff --git a/TM1637Display.h b/TM1637Display.h index f586c78..084e238 100644 --- a/TM1637Display.h +++ b/TM1637Display.h @@ -143,24 +143,23 @@ class TM1637Display { static uint8_t encodeDigit(uint8_t digit); protected: - void bitDelay(); + void bitDelay(); - void start(); + void start(); - void stop(); + void stop(); - bool writeByte(uint8_t b); + bool writeByte(uint8_t b); - void showDots(uint8_t dots, uint8_t* digits); - - void showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0); + void showDots(uint8_t dots, uint8_t* digits); + void showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0); private: - uint8_t m_pinClk; - uint8_t m_pinDIO; - uint8_t m_brightness; - unsigned int m_bitDelay; + uint8_t m_pinClk; + uint8_t m_pinDIO; + uint8_t m_brightness; + unsigned int m_bitDelay; }; #endif // __TM1637DISPLAY__ diff --git a/examples/TM1637Test/TM1637Test.ino b/examples/TM1637Test/TM1637Test.ino index d826d1a..031d0d0 100644 --- a/examples/TM1637Test/TM1637Test.ino +++ b/examples/TM1637Test/TM1637Test.ino @@ -42,8 +42,8 @@ void loop() /* for(k = 3; k >= 0; k--) { - display.setSegments(data, 1, k); - delay(TEST_DELAY); + display.setSegments(data, 1, k); + delay(TEST_DELAY); } */ @@ -59,7 +59,6 @@ void loop() display.setSegments(data+1, 3, 1); delay(TEST_DELAY); - // Show decimal numbers with/without leading zeros display.showNumberDec(0, false); // Expect: ___0 delay(TEST_DELAY); @@ -97,7 +96,7 @@ void loop() display.clear(); display.showNumberHexEx(0xd1, 0, true, 2); // Expect: d1__ delay(TEST_DELAY); - + // Run through all the dots for(k=0; k <= 4; k++) { display.showNumberDecEx(0, (0x80 >> k), true); @@ -112,7 +111,7 @@ void loop() display.setSegments(data); delay(TEST_DELAY); } - + // On/Off test for(k = 0; k < 4; k++) { display.setBrightness(7, false); // Turn off @@ -120,10 +119,9 @@ void loop() delay(TEST_DELAY); display.setBrightness(7, true); // Turn on display.setSegments(data); - delay(TEST_DELAY); + delay(TEST_DELAY); } - // Done! display.setSegments(SEG_DONE); diff --git a/keywords.txt b/keywords.txt index 8a43483..a58e817 100644 --- a/keywords.txt +++ b/keywords.txt @@ -28,4 +28,3 @@ SEG_E LITERAL1 SEG_F LITERAL1 SEG_G LITERAL1 SEG_DP LITERAL1 - diff --git a/library.properties b/library.properties index 98554b0..129ad8b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TM1637 -version=1.2.0 +version=1.2.1 author=Avishay Orpaz maintainer=Avishay Orpaz sentence=Driver for 4 digit 7-segment display modules, based on the TM1637 chip. @@ -8,4 +8,3 @@ category=Display url=https://github.com/avishorp/TM1637 architectures=* includes=TM1637Display.h - diff --git a/release_notes.md b/release_notes.md index c10f725..a5afec2 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,13 +1,15 @@ +- V1.2.1 + * Minor formatting improvements + - V1.2.0 * Add support for negative numbers * Add support for hexadeciaml number * Bump default bit delay to 100us, make it adjustable * Add Arduino library metadata - + - V1.1.0 * Add support for decimal points/colon * Minor fixes - + - V1.0.0 * Initial release -