Skip to content

Commit

Permalink
Display negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
wahlencraft committed Apr 11, 2021
1 parent d6519ab commit 1c67fda
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
21 changes: 17 additions & 4 deletions PicoTM1637.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void TM1637_put_4_bytes(uint start_pos, uint data) {
/* Convert a number to something readable for the 'put bytes' functions.
*
* Warning, input must not be more than 4 digits. */
unsigned int num_to_hex(int num) {
unsigned int num_to_hex(int num) {
unsigned int hex = 0x0, seg;
while (num) {
seg = digitToSegment[num % 10]; // extract last digit as 7 segment byte
Expand All @@ -91,21 +91,34 @@ unsigned int num_to_hex(int num) {
}

void TM1637_display(int number, bool leadingZeros) {
// Is number positive or negative?
int isPositive;
int isNegative;
if (number >= 0) {
isPositive = 1;
} else {
isPositive = 0;
number = -1*number;
}
// Determine length of number
int len = 0;
int numberCopy = number;
while (numberCopy) {
len++;
numberCopy /= 10;
}
if (len > 4) {
if (len > 3 + isPositive) {
printf("Warning number %d too long\n", number);
len = 4;
// least signigicant digits will be cut of in num_to_hex
len = 3 + isPositive;
// least signigicant digits will be lost
}

// Get hex
unsigned int hex = num_to_hex(number);
if (!isPositive) {
hex = (hex << 8) + 0x40; // add a negative sign
len++; // count negative sign in length
}
unsigned int startPos = 0;
if (leadingZeros && (len < MAX_DIGITS)) {
for (int i=len; i<MAX_DIGITS; i++) {
Expand Down
15 changes: 8 additions & 7 deletions PicoTM1637.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,27 @@ void TM1637_put_2_bytes(uint start_pos, uint data);
* to the left */
void TM1637_put_4_bytes(uint start_pos, uint data);

/* Display a number with 4 digits.
/* Display a positive number with 4 digits or a negative number with 3 digits.
*
* The least significat digit will be put to the right.
* Arguments:
* int number: The number to display (currently only positve)
* int number: The number to display.
* bool leadingZeros: If leading Zeros should be displayed or not. */
void TM1637_display(int number, bool leadingZeros);

/* Display a number on the 2 leftmost digits on the display. A colon is by
* default shown, to turn this off use TM1637_set_colon(false)
/* Display a posetive number on the 2 leftmost digits on the display. A colon is
* by default shown, to turn this off use TM1637_set_colon(false)
*
* !!! Avoid using this function since it will cause the right side to flicker.
* Instead use TM1637_display_both. */
void TM1637_display_left(int number, bool leadingZeros);

/* Display a number on the 2 rightmost digits on the display. */
/* Display a positive number on the 2 rightmost digits on the display. */
void TM1637_display_right(int number, bool leadingZeros);

/* Display two (two digit) numbers on the display. By default there will be a
* colon between them. Disable this behaviour with TM1637_set_colon(false); */
/* Display two (two digit positive) numbers on the display. By default there
* will be a colon between them. Disable this behaviour with
* TM1637_set_colon(false); */
void TM1637_display_both(int leftNumber, int rightNumber, bool leadingZeros);

/* Turn the colon led on or off. Default is on.
Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TM1637-pico
#### Control TM1637 based 7-segment display from raspberry pi pico.

## Features
* Display a positive 4 digit number
* Display a positive 4 digit number or a negative 3 digit number.
* Display two positive 2 digit numbers with (or without) a colon in between.
* You can choose if you want leading zeros in both of the above cases.
* Display 4 or 2 digits of raw data.
Expand All @@ -12,15 +12,14 @@ TM1637-pico
The actual communication with the display is handled by the picos pio
functionality. When a display function is called, some data is very quickly put
into a buffer that the pio has access to. The pio will then, one bit at a time
send send the data to the TM1637 display. If to much data is sent to the display
in a short period of time, the buffer will become full.
This will block further execution in the main program if even more data is sent
to the display.
send send the data to the TM1637 display. If you attempt to display too much
in a short period of time, the buffer will become full. This will block further
execution in the main program if even more data is sent to the display.

This library will only work with the 4 digit version of the display.

To get started have a look at the `examples/demo.c` file. For a full list of functions
please refer to `PicoTM1637.h`
To get started have a look at the `examples/demo.c` file. For a full list of
functions please refer to `PicoTM1637.h`

## Setup
1. Download the files from this repository and put them in a folder somewhere.
Expand Down
12 changes: 8 additions & 4 deletions examples/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ int main()
TM1637_clear();
sleep_ms(500);

// Count up from 0
int count = 0;
while(count <= 200) {
// Count down from 150 to -50
int count = 150;
TM1637_display(count, false);
sleep_ms(500);
while(count >= -50) {
TM1637_display(count, false);
count++;
count--;
// The display can not update too often. So even though there is no
// sleep, this will take a couple of moments.
}

sleep_ms(1000);
TM1637_clear();
sleep_ms(500);

// Demo a clock, by default there will be a colon between the numbers.
int seconds = 0;
Expand Down

0 comments on commit 1c67fda

Please sign in to comment.