led_tm1637: allow number to be displayed without leading 0

This commit is contained in:
King Kévin 2020-02-18 17:28:34 +01:00
parent afd5bef309
commit c30b3ecb48
2 changed files with 34 additions and 12 deletions

View File

@ -255,7 +255,7 @@ bool led_tm1637_on(void)
{
uint8_t data[] = { 0x88 + display_brightness }; // command to turn display on (use set brightness)
bool to_return = false; // result to return
if (led_tm1637_write(data,LENGTH(data))) { // send command
if (led_tm1637_write(data, LENGTH(data))) { // send command
display_on = true; // remember display is on
to_return = true; // command succeeded
}
@ -265,7 +265,7 @@ bool led_tm1637_on(void)
bool led_tm1637_off(void)
{
uint8_t data[] = { 0x80 + display_brightness }; // command to turn display off (use set brightness)
if (led_tm1637_write(data,LENGTH(data))) { // send command
if (led_tm1637_write(data, LENGTH(data))) { // send command
display_on = false; // remember display is off
return true; // command succeeded
}
@ -283,15 +283,36 @@ bool led_tm1637_brightness(enum led_tm1637_brightness_t brightness)
return false;
}
bool led_tm1637_number(uint16_t number)
bool led_tm1637_number(uint16_t number, bool zero)
{
uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
uint8_t data[] = { 0xc0, ascii_7segments[((number / 1000) % 10) + '0' - ' '], ascii_7segments[((number / 100) % 10) + '0' - ' '], ascii_7segments[((number / 10) % 10) + '0' - ' '], ascii_7segments[((number / 1) % 10) + '0' - ' '] }; // set address C0H and add data
if (led_tm1637_write(write_data,LENGTH(write_data)) && led_tm1637_write(data,LENGTH(data))) { // send commands
return true;
const uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
const uint8_t digits[] = { // digits to display
(number / 1000) % 10,
(number / 100) % 10,
(number / 10) % 10,
(number / 1) % 10,
};
uint8_t data[] = { 0xc0, 0, 0, 0, 0}; // number to be displayed
// convert digits to text to be displayed
if (0 == digits[0] && !zero) {
data[1] = ascii_7segments[' ' - ' '];
} else {
data[1] = ascii_7segments[digits[0] + '0' - ' '];
}
return false;
if (0 == digits[0] && 0 == digits[1] && !zero) {
data[2] = ascii_7segments[' ' - ' '];
} else {
data[2] = ascii_7segments[digits[1] + '0' - ' '];
}
if (0 == digits[0] && 0 == digits[1] && 0 == digits[2] && !zero) {
data[3] = ascii_7segments[' ' - ' '];
} else {
data[3] = ascii_7segments[digits[2] + '0' - ' '];
}
data[4] = ascii_7segments[digits[3] + '0' - ' '];
// display number
return led_tm1637_write(write_data, LENGTH(write_data)) && led_tm1637_write(data, LENGTH(data)); // send commands
}
bool led_tm1637_time(uint8_t hours, uint8_t minutes)
@ -299,7 +320,7 @@ bool led_tm1637_time(uint8_t hours, uint8_t minutes)
uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
uint8_t data[] = { 0xc0, ascii_7segments[((hours / 10) % 10) + '0' - ' '], ascii_7segments[((hours / 1) % 10) + '0' - ' '] | 0x80, ascii_7segments[((minutes / 10) % 10) + '0' - ' '], ascii_7segments[((minutes / 1) % 10) + '0' - ' '] }; // set address C0H and add data
if (led_tm1637_write(write_data,LENGTH(write_data)) && led_tm1637_write(data,LENGTH(data))) { // send commands
if (led_tm1637_write(write_data, LENGTH(write_data)) && led_tm1637_write(data, LENGTH(data))) { // send commands
return true;
}
return false;
@ -318,7 +339,7 @@ bool led_tm1637_text(char* text)
uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
uint8_t data[] = { 0xc0, ascii_7segments[(text[0] & 0x7f) - ' '] | (text[0] & 0x80), ascii_7segments[(text[1] & 0x7f) - ' ']|(text[1] & 0x80), ascii_7segments[(text[2] & 0x7f) - ' ']|(text[2] & 0x80), ascii_7segments[(text[3] & 0x7f) - ' ']|(text[3] & 0x80) }; // set address C0H and add data
if (led_tm1637_write(write_data,LENGTH(write_data)) && led_tm1637_write(data,LENGTH(data))) { // send commands
if (led_tm1637_write(write_data, LENGTH(write_data)) && led_tm1637_write(data, LENGTH(data))) { // send commands
return true;
}
return false;

View File

@ -52,9 +52,10 @@ bool led_tm1637_off(void);
bool led_tm1637_brightness(enum led_tm1637_brightness_t brightness);
/** display number
* @param[in] number number to display (0-9999)
* @param[in] zero pad number with zero on the left
* @return if transmission succeeded
*/
bool led_tm1637_number(uint16_t number);
bool led_tm1637_number(uint16_t number, bool zero);
/** display time
* @param[in] hours hours to display (0-99)
* @param[in] minutes minutes to display (0-99)