Skip to content

Commit 26e16bd

Browse files
First version
1 parent da89d3a commit 26e16bd

File tree

5 files changed

+156
-31
lines changed

5 files changed

+156
-31
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1-
# Pico-TM1637
1+
# Raspberry Pi Pico - TM1637 7-Segment Displays Library
2+
3+
This repository contains a Raspberry Pi Pico library using its SDK writen for the 7-Degment Displays that are equiped with an TM1637 module. It also contains one example. Below are the instructions on how to build it.
4+
5+
## Example
6+
7+
After you clone the repo you run the following commands to build it. You have to have installed *CMake* and *Make*. Also you need to have the SDK on your system and point `PICO_SDK_PATH` to it.
8+
```sh
9+
export PICO_SDK_PATH='/Path/to/SDK'
10+
```
11+
```sh
12+
cd Pico-TM1637/example
13+
```
14+
```sh
15+
mkdir build
16+
```
17+
```sh
18+
cd build
19+
```
20+
```sh
21+
cmake ..
22+
```
23+
```sh
24+
make -j4
25+
```
26+
### How to connect the LCD to the board
27+
![Fritzing drawing](img/fritzing.png)

TM1637.cpp

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,7 @@ auto TM1637::Two_Digits_To_Segment(data number, bool hex, bool leading_zeros) no
131131
return segments;
132132
}
133133

134-
void TM1637::SetBrightness(byte brightness_level) noexcept
135-
{
136-
brightness = std::min(brightness_level, MAX_BRIGHTNESS);
137-
Send_4_Bytes(current_segments);
138-
}
139-
140-
void TM1637::Display(int16_t number, bool hex, bool leading_zeros) noexcept
134+
void TM1637::Internal_Display(int16_t number, bool hex, bool leading_zeros) noexcept
141135
{
142136
static constexpr byte DASH = 0x40;
143137

@@ -208,7 +202,7 @@ void TM1637::Display(int16_t number, bool hex, bool leading_zeros) noexcept
208202
Send_4_Bytes(current_segments);
209203
}
210204

211-
void TM1637::DisplayLeft(data number, bool hex, bool leading_zeros) noexcept
205+
void TM1637::Internal_Display_Left(data number, bool hex, bool leading_zeros) noexcept
212206
{
213207
static constexpr size_t LEFT_BYTE_MASK = 0xFFFF0000;
214208

@@ -220,7 +214,7 @@ void TM1637::DisplayLeft(data number, bool hex, bool leading_zeros) noexcept
220214
Send_4_Bytes(current_segments);
221215
}
222216

223-
void TM1637::DisplayRight(data number, bool hex, bool leading_zeros) noexcept
217+
void TM1637::Internal_Display_Right(data number, bool hex, bool leading_zeros) noexcept
224218
{
225219
static constexpr size_t RIGHT_BYTE_MASK = 0x0000FFFF;
226220

@@ -233,6 +227,42 @@ void TM1637::DisplayRight(data number, bool hex, bool leading_zeros) noexcept
233227
Send_4_Bytes(current_segments);
234228
}
235229

230+
void TM1637::Display(int16_t number, bool leading_zeros) noexcept
231+
{
232+
Internal_Display(number, false, leading_zeros);
233+
}
234+
235+
void TM1637::DisplayHex(int16_t number, bool leading_zeros) noexcept
236+
{
237+
Internal_Display(number, true, leading_zeros);
238+
}
239+
240+
void TM1637::DisplayLeft(data number, bool leading_zeros) noexcept
241+
{
242+
Internal_Display_Left(number, false, leading_zeros);
243+
}
244+
245+
void TM1637::DisplayLeftHex(data number, bool leading_zeros) noexcept
246+
{
247+
Internal_Display_Left(number, true, leading_zeros);
248+
}
249+
250+
void TM1637::DisplayRight(data number, bool leading_zeros) noexcept
251+
{
252+
Internal_Display_Right(number, false, leading_zeros);
253+
}
254+
255+
void TM1637::DisplayRightHex(data number, bool leading_zeros) noexcept
256+
{
257+
Internal_Display_Right(number, true, leading_zeros);
258+
}
259+
260+
void TM1637::SetBrightness(byte brightness_level) noexcept
261+
{
262+
brightness = std::min(brightness_level, MAX_BRIGHTNESS);
263+
Send_4_Bytes(current_segments);
264+
}
265+
236266
void TM1637::ColonOn() noexcept
237267
{
238268
static constexpr size_t CONTROL_DISPLAY = 0x8000;

TM1637.hpp

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ class TM1637
9191
*/
9292
static auto Two_Digits_To_Segment(data number, bool hex = false, bool leading_zeros = false) noexcept -> data;
9393

94+
/**
95+
* Internal function to display a number on all four digits.
96+
*
97+
* @param number The number to be displayed
98+
* @param hex The hex display option
99+
* @param leading_zeros Optional leading zeros
100+
*/
101+
void Internal_Display(int16_t number, bool hex = false, bool leading_zeros = false) noexcept;
102+
103+
/**
104+
* Internal function to display a number on the most significant two digits.
105+
*
106+
* @param number The number to be displayed
107+
* @param hex The hex display option
108+
* @param leading_zeros Optional leading zeros
109+
*/
110+
void Internal_Display_Left(data number, bool hex = false, bool leading_zeros = false) noexcept;
111+
112+
/**
113+
* Internal function to display a number on the least significant two digits.
114+
*
115+
* @param number The number to be displayed
116+
* @param hex The hex display option
117+
* @param leading_zeros Optional leading zeros
118+
*/
119+
void Internal_Display_Right(data number, bool hex = false, bool leading_zeros = false) noexcept;
120+
94121
public:
95122

96123
/**
@@ -103,39 +130,60 @@ class TM1637
103130
TM1637(byte DIO, byte CLK, PIO pio) noexcept;
104131

105132
/**
106-
* Sets the brightness brightness_level. The minimum brightness_level is 0
107-
* and the maximum is 7.
133+
* Display a number on all four digits.
108134
*
109-
* @param brightness_level The desired brightness brightness_level
135+
* @param number
136+
* @param leading_zeros
110137
*/
111-
void SetBrightness(byte brightness_level) noexcept;
138+
void Display(int16_t number, bool leading_zeros = false) noexcept;
112139

113140
/**
114-
* Display a number on all four digits.
141+
* Display a number on all four digits in hex format.
115142
*
116-
* @param number The number to be displayed
117-
* @param hex The hex display option
118-
* @param leading_zeros Optional leading zeros
143+
* @param number
144+
* @param leading_zeros
119145
*/
120-
void Display(int16_t number, bool hex = false, bool leading_zeros = false) noexcept;
146+
void DisplayHex(int16_t number, bool leading_zeros = false) noexcept;
121147

122148
/**
123149
* Display a number on the most significant two digits.
124150
*
125-
* @param number The number to be displayed
126-
* @param hex The hex display option
127-
* @param leading_zeros Optional leading zeros
151+
* @param number
152+
* @param leading_zeros
128153
*/
129-
void DisplayLeft(data number, bool hex = false, bool leading_zeros = false) noexcept;
154+
void DisplayLeft(data number, bool leading_zeros = false) noexcept;
155+
156+
/**
157+
* Display a number on the most significant two digits in hex format.
158+
*
159+
* @param number
160+
* @param leading_zeros
161+
*/
162+
void DisplayLeftHex(data number, bool leading_zeros = false) noexcept;
130163

131164
/**
132165
* Display a number on the least significant two digits.
133166
*
134-
* @param number The number to be displayed
135-
* @param hex The hex display option
136-
* @param leading_zeros Optional leading zeros
167+
* @param number
168+
* @param leading_zeros
137169
*/
138-
void DisplayRight(data number, bool hex = false, bool leading_zeros = false) noexcept;
170+
void DisplayRight(data number, bool leading_zeros = false) noexcept;
171+
172+
/**
173+
* Display a number on the least significant two digits in hex format.
174+
*
175+
* @param number
176+
* @param leading_zeros
177+
*/
178+
void DisplayRightHex(data number, bool leading_zeros = false) noexcept;
179+
180+
/**
181+
* Sets the brightness brightness_level. The minimum brightness_level is 0
182+
* and the maximum is 7.
183+
*
184+
* @param brightness_level The desired brightness brightness_level
185+
*/
186+
void SetBrightness(byte brightness_level) noexcept;
139187

140188
/**
141189
* Turns on the middle colon.

example/main.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <pico/binary_info/code.h>
2+
#include <pico/time.h>
23
#include "../TM1637.hpp"
34
#include <memory>
4-
#include <pico/time.h>
55

66
int main()
77
{
@@ -10,10 +10,31 @@ int main()
1010
constexpr auto CLK = 28;
1111
auto * pio = pio0;
1212

13-
bi_decl(bi_1pin_with_name(DIO, "[DIO] Scoreboard data pin"))
14-
bi_decl(bi_1pin_with_name(CLK, "[CLK] Scoreboard clock pin"))
13+
bi_decl(bi_1pin_with_name(DIO, "[DIO] LED segments data pin"))
14+
bi_decl(bi_1pin_with_name(CLK, "[CLK] LED segments clock pin"))
1515

1616
auto led_segments = std::make_unique<TM1637>(DIO, CLK, pio);
17-
led_segments->Display(-612, false, true);
17+
18+
static constexpr size_t PAUSE_MS = 5000;
19+
20+
for (size_t i = 0; i <= 100; ++i)
21+
{
22+
led_segments->Display(i, true);
23+
sleep_ms(100);
24+
}
25+
26+
sleep_ms(PAUSE_MS);
27+
28+
for (size_t i = 0; i <= 0x100; ++i)
29+
{
30+
led_segments->DisplayHex(i, true);
31+
sleep_ms(100);
32+
}
33+
34+
sleep_ms(PAUSE_MS);
35+
36+
led_segments->ColonOn();
37+
led_segments->DisplayLeft(15);
38+
led_segments->DisplayRight(45);
1839
}
1940

img/fritzing.png

41.3 KB
Loading

0 commit comments

Comments
 (0)