-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,114 @@ | ||||||||
/* | ||||||||
* Copyright (C) 2024 Nico Behrens <[email protected]> | ||||||||
* | ||||||||
* This file is subject to the terms and conditions of the GNU Lesser | ||||||||
* General Public License v2.1. See the file LICENSE in the top level | ||||||||
* directory for more details. | ||||||||
*/ | ||||||||
|
||||||||
/** | ||||||||
* @defgroup drivers_tm1637 TM1637 display | ||||||||
* @ingroup drivers_display | ||||||||
* @brief Driver for the TM1637 4-digit 7-segment display | ||||||||
* | ||||||||
* @{ | ||||||||
* @file | ||||||||
* @brief Interface definition for the TM1637 4-digit 7-segment display driver | ||||||||
* | ||||||||
* @author Nico Behrens <[email protected]> | ||||||||
* | ||||||||
*/ | ||||||||
|
||||||||
#ifndef TM1637_H | ||||||||
#define TM1637_H | ||||||||
|
||||||||
#include "board.h" | ||||||||
#include "periph/gpio.h" | ||||||||
|
||||||||
#ifdef __cplusplus | ||||||||
extern "C" | ||||||||
{ | ||||||||
#endif | ||||||||
|
||||||||
/** | ||||||||
* @brief Pin configuration parameters for the tm1637 display | ||||||||
*/ | ||||||||
typedef struct { | ||||||||
/** | ||||||||
* @brief GPIO for clock | ||||||||
*/ | ||||||||
gpio_t clk; | ||||||||
|
||||||||
/** | ||||||||
* @brief GPIO for data input/output | ||||||||
*/ | ||||||||
gpio_t dio; | ||||||||
} tm1637_params_t; | ||||||||
|
||||||||
/** | ||||||||
* @brief tm1637 driver descriptor | ||||||||
*/ | ||||||||
typedef struct { | ||||||||
/** | ||||||||
* @brief Configuration parameters | ||||||||
* | ||||||||
*/ | ||||||||
tm1637_params_t params; | ||||||||
} tm1637_t; | ||||||||
|
||||||||
/** | ||||||||
* @brief Brightness level enum for the display | ||||||||
* | ||||||||
* @note The brightness must be one of the given values. | ||||||||
*/ | ||||||||
typedef enum { | ||||||||
TM1637_PW_1_16 = 0x00, | ||||||||
TM1637_PW_2_16 = 0x01, | ||||||||
TM1637_PW_4_16 = 0x02, | ||||||||
TM1637_PW_10_16 = 0x03, | ||||||||
TM1637_PW_11_16 = 0x04, | ||||||||
TM1637_PW_12_16 = 0x05, | ||||||||
TM1637_PW_13_16 = 0x06, | ||||||||
TM1637_PW_14_16 = 0x07 | ||||||||
} tm1637_brightness_t; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The meaning of these values is unclear to me. I assume they are fractions of full brightness, but the doc does not say this. If they are fractions, why are some values skipped? |
||||||||
|
||||||||
/** | ||||||||
* @brief Initializes the tm1637 device | ||||||||
* | ||||||||
* @param[out] dev device descriptor of the display | ||||||||
* @param[in] params configuration parameters | ||||||||
* @return 0 on success, error otherwise | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Even better than |
||||||||
*/ | ||||||||
int tm1637_init(tm1637_t *dev, const tm1637_params_t *params); | ||||||||
|
||||||||
/** | ||||||||
* @brief Writes an integer to the display | ||||||||
* | ||||||||
* @note The integer can't be bigger than 9999 or smaller than | ||||||||
* -999 as only 4 digits can be displayed at a time. | ||||||||
* When leading zeros are enabled, the display is padded with zeros. | ||||||||
* For negative integers the leading zeros are added between the minus sign | ||||||||
* and the number. | ||||||||
* | ||||||||
* @param[in] dev device descriptor of the display | ||||||||
* @param[in] number number to write, in the range of -999 to 9999 | ||||||||
* @param[in] brightness brightness of the display according to @ref tm1637_brightness_t | ||||||||
* @param[in] colon If enabled, displays a colon in the middle | ||||||||
* @param[in] leading_zeros If enabled, displays leading zeros | ||||||||
*/ | ||||||||
void tm1637_write_number(const tm1637_t *dev, int16_t number, tm1637_brightness_t brightness, | ||||||||
bool colon, bool leading_zeros); | ||||||||
|
||||||||
/** | ||||||||
* @brief Clear the display | ||||||||
* | ||||||||
* @param[in] dev device descriptor of the display | ||||||||
*/ | ||||||||
void tm1637_clear(const tm1637_t *dev); | ||||||||
|
||||||||
#ifdef __cplusplus | ||||||||
} | ||||||||
#endif | ||||||||
|
||||||||
#endif /* TM1637_H */ | ||||||||
/** @} */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MODULE = tm1637 | ||
|
||
include $(RIOTBASE)/Makefile.base |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FEATURES_REQUIRED += periph_gpio | ||
USEMODULE += ztimer | ||
USEMODULE += ztimer_msec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
USEMODULE_INCLUDES_tm1637 := $(LAST_MAKEFILEDIR)/include | ||
USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_tm1637) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (C) 2024 Nico Behrens <[email protected]> | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup drivers_tm1637 | ||
* | ||
* @{ | ||
* @file | ||
* @brief Config for the TM1637 display | ||
* | ||
* @author Nico Behrens <[email protected]> | ||
*/ | ||
#ifndef TM1637_PARAMS_H | ||
#define TM1637_PARAMS_H | ||
|
||
#include "board.h" | ||
#include "periph/gpio.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef TM1637_PARAM_CLK | ||
/** | ||
* @brief see @ref tm1637_params_t | ||
*/ | ||
#define TM1637_PARAM_CLK GPIO_PIN(0, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to have default values here, or should we rather error out if the user hasn't provided them? Not sure how this is done in other places in RIOT. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest setting the default to |
||
#endif | ||
|
||
#ifndef TM1637_PARAM_DIO | ||
/** | ||
* @brief see @ref tm1637_params_t | ||
*/ | ||
#define TM1637_PARAM_DIO GPIO_PIN(0, 1) | ||
#endif | ||
|
||
#ifndef TM1637_PARAMS | ||
/** | ||
* @brief see @ref tm1637_params_t | ||
*/ | ||
#define TM1637_PARAMS { .clk = TM1637_PARAM_CLK, \ | ||
.dio = TM1637_PARAM_DIO } | ||
#endif | ||
|
||
/** | ||
* @brief see @ref tm1637_params_t | ||
*/ | ||
static const tm1637_params_t tm1637_params[] = { | ||
TM1637_PARAMS | ||
}; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* TM1637_PARAMS_H */ | ||
/** @} */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The driver should hold a pointer to
tm1637_params_t
. Not a copy.