Skip to content
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

TM1637 7-segment display driver #21112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

KSKNico
Copy link
Contributor

@KSKNico KSKNico commented Dec 27, 2024

Contribution description

This is a 4 digit display driver for the TM1637 7-segment display. It can display base 10 integers on a range from 9999 to -999, with or without leading zeros and variable brightness. The display can be cleared, too.

The driver was implemented according to a subset of all functionality and adheres to the specification .

Testing procedure

A test can be found in tests/drivers/tm1637/. The test covers all possible configurations for the driver and contains default pin configuration in tm1637_params.h.

Issues/PRs references

A similar display driver is still in review at PR #20981, but isn't compatible with this 4 pin display.

Images

20241227_184429

@github-actions github-actions bot added Area: doc Area: Documentation Area: tests Area: tests and testing framework Area: drivers Area: Device drivers labels Dec 27, 2024
Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, this looks quite good to me.

The CI has a few comments (check in the code view here):

  • One line is longer than 100 chars, please add a line break
  • A few files have no terminating newline. (Btw: What editor are you using? Maybe we can add a config to our repo that your editor will pick up. IMO it is better to automate such tedious style things and concentrate.)

Btw: I think negative numbers and leading zeros will result in the minus sign to be overwritten by a zero, or did I read the code incorrectly?

Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, great code quality! I have mostly nits below.

You've rightfully already mentioned #20981. IIRC, the idea was there to provide a unified driver for different kinds of 7-segment displays. How does yours differ and could it be integrated somehow? Otherwise, we should rename the driver in the other PR back to something more specific.

* @brief Brightness level enum for the display
*
* @note The brightness is defined as a fraction of
* the pulse width and must be on of the given values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* the pulse width and must be on of the given values.
* the pulse width and must be one of the given values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, I don't fully understand the sentence here. The brightness being a fraction of the pulse width is an implementation detail not really interesting to the user, right?

*
* @note The integer can't be bigger than 9999 or smaller than
* -999 as only 4 digits can be displayed at a time.
* With the leading zeros enabled, the display is padded with zeros.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* With the leading zeros enabled, the display is padded with zeros.
* When leading zeros are enabled, the display is padded with zeros.

Comment on lines +79 to +81
* @param[out] dev device descriptor of the display
* @param[in] params configuration parameters
* @return 0 on success, error otherwise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be slightly easier to read if the params are aligned, as in

Suggested change
* @param[out] dev device descriptor of the display
* @param[in] params configuration parameters
* @return 0 on success, error otherwise
* @param[out] dev device descriptor of the display
* @param[in] params configuration parameters
* @return 0 on success, error otherwise

It is apparently not explicitly mandated by the coding convention, but that's how I've seen it everywhere across RIOT until now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for the rest of this file, obviously :)

* and the number.
*
* @param[in] dev device descriptor of the display
* @param[in] number number to write, in the range of 9999 to -999
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param[in] number number to write, in the range of 9999 to -999
* @param[in] number number to write, in the range of -999 to 9999

feels a bit more natural to me

Comment on lines 3 to 6
USEMODULE += ztimer
USEMODULE += ztimer_msec
USEMODULE += periph_gpio

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
USEMODULE += ztimer
USEMODULE += ztimer_msec
USEMODULE += periph_gpio

those should go (and mostly are already) in Makefile.dep. Specifying periph_gpio in FEATURES_REQUIRED should also be enough, I think.

*
* @param[in,out] segments segments to enable the colon on
*/
static void enable_colon(uint8_t *segments)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static void enable_colon(uint8_t *segments)
inline static void enable_colon(uint8_t *segments)

void tm1637_clear(const tm1637_t *dev)
{
uint8_t segments[DIGIT_COUNT] = { 0, 0, 0, 0 };
transmit_segments(dev, segments, TM1637_PW_1_16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this specific brightness?

assert(number <= 9999);
assert(number >= -999);

uint8_t segments[DIGIT_COUNT] = { 0, 0, 0, 0 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint8_t segments[DIGIT_COUNT] = { 0, 0, 0, 0 };
uint8_t segments[DIGIT_COUNT] = { 0 };

does the same, and doesn't hard-code DIGIT_COUNT ;-)

Comment on lines +266 to +272
if (leading_zeros) {
for (int i = 0; i < DIGIT_COUNT; ++i) {
if (segments[i] == 0) {
segments[i] = segments_array[0];
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this overwrite the minus sign set above in line 251?

Comment on lines +258 to +261
if (number != 0) {
segments[DIGIT_COUNT - 1 - i] = segments_array[number % 10];
number /= 10;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an exact copy of lines 242-245, you could consolidate and reduce the levels of indentation :)

@mguetschow mguetschow added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 3, 2025
@riot-ci
Copy link

riot-ci commented Jan 3, 2025

Murdock results

✔️ PASSED

72455c5 removed modules that are already dependencies

Success Failures Total Runtime
10267 0 10267 19m:09s

Artifacts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: doc Area: Documentation Area: drivers Area: Device drivers Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants