pi-tm1638  1.0
TM1638 library for Raspberry Pi
 All Data Structures Files Functions Variables Typedefs
tm1638.h File Reference

A simple interface to TM1638 based displays for the Raspberry Pi. More...

Go to the source code of this file.


typedef struct tm1638_tag tm1638
typedef tm1638tm1638_p


tm1638_p tm1638_alloc (uint8_t data, uint8_t clock, uint8_t strobe)
void tm1638_free (tm1638_p *t)
void tm1638_enable (tm1638_p t, bool enable)
void tm1638_set_intensity (tm1638_p t, uint8_t intensity)
void tm1638_set_7seg_raw (const tm1638_p t, uint8_t digit, uint8_t n)
void tm1638_set_7seg_text (const tm1638_p t, const char *str, uint8_t dots)
void tm1638_set_led (const tm1638_p t, uint8_t led, uint8_t cols)
void tm1638_set_8leds (const tm1638_p t, uint8_t red, uint8_t green)
void tm1638_send_cls (const tm1638_p t)
uint8_t tm1638_font (char c)
uint32_t tm1638_read_buttons (const tm1638_p t)
uint8_t tm1638_read_8buttons (const tm1638_p t)

Detailed Description

A simple interface to TM1638 based displays for the Raspberry Pi.

Martin Oldfield ex-tm.nosp@m.1638.nosp@m.@mjo..nosp@m.tc


A simple interface to the TM1638 based displays for the Raspberry Pi.

Dealextreme, doubtless amongst others, sell small boards with eight seven-segment displays, eight red-green LEDs and eight push buttons for less than $10.

The boards are basically just the LEDs and switches, and a TM1638 driver chip. This sits on a two-wire serial bus which makes it fairly easy to connect the boards to a computer/microcontroller of your choice. Of course, one needs a little bit of software. This is such a library for the Raspberry Pi.


#include <bcm2835.h>
#include <tm1638.h>


if (!bcm2835_init())
  { ... }

tm1638_p t = tm1638_alloc(17, 21, 22);
if (!t)
  { ... }

tm1638_set_7seg_text(t, "Hello!", 0xc0);

    uint8_t  x = tm1638_read_8buttons(t);
    tm1638_set_8leds(t, 0, x);



All of the hardware interfacing is done via Mike McCauley's excellent bcm2835 library, so you'll need to install that first. Get it from http://www.open.com.au/mikem/bcm2835/


Inevitably people have already done all this for with an Arduino, and that made it easier to write this:

  1. John Boxall wrote a blog about it.
  2. Ricardo Batista wrote a library to do it.
  3. Marc (via John above) found a datasheet.

This isn't really a port of Ricardo's code: I wanted a different API. However, I did copy his nice 7-segment font, and his code was very helpful when it came to understanding the data-sheet.

Thank you to everyone.


This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details at http://www.gnu.org/copyleft/gpl.html

Definition in file tm1638.h.

Typedef Documentation

typedef struct tm1638_tag tm1638

Forward declaration of the tm1638 structure. Users of the library should treat this as an opaque object and all interaction with the struct will be indirect.

That is, the library will allocate and free all the structs, so your code should only contain pointers to tm1638 structs, not the structs themselves.

Definition at line 96 of file tm1638.h.

typedef tm1638* tm1638_p

With this in mind, here's a pointer!

Definition at line 101 of file tm1638.h.

Function Documentation

tm1638_p tm1638_alloc ( uint8_t  data,
uint8_t  clock,
uint8_t  strobe 

Allocation and initialization function, aka constructor.


  1. You must initialize the bcm2825 library first!
  2. You must pair every tm1638_alloc with a tm1638_free!

All the parameters specify the pins we've connected to the TM1638 board:

dataPin used for data.
clockPin used for clock.
strobePin used for strobe.
Pointer to tm1638 struct, or NULL if failure!

Definition at line 103 of file tm1638.c.

void tm1638_enable ( tm1638_p  t,
bool  enable 

Enable/disable the display

tPointer to the tm1638 of interest.
enableEnable (true) or disable (false).

Definition at line 142 of file tm1638.c.

uint8_t tm1638_font ( char  c)

A simple 7-segment font.

cThe ASCII character of interest.
The segments to set.

Definition at line 353 of file tm1638.c.

void tm1638_free ( tm1638_p t)

Freeing function aka destructor.

tPointer to tm1638 pointer, set to NULL when freed.

Definition at line 135 of file tm1638.c.

uint8_t tm1638_read_8buttons ( const tm1638_p  t)

Read the state of the eight buttons on the standard board.

tPointer to the tm1638 of interest.
8-bit uint of button states. MSB is leftmost.

Definition at line 401 of file tm1638.c.

uint32_t tm1638_read_buttons ( const tm1638_p  t)

Read the 32-bit button input register. The bit order in here isn't helpful for the standard boards with eight buttons on them: see tm1638_read_8buttons() instead.

tPointer to the tm1638 of interest.
32-bit uint of button states.

Definition at line 378 of file tm1638.c.

void tm1638_send_cls ( const tm1638_p  t)

Turn off all the LEDs

tPointer to the tm1638 of interest.

Definition at line 335 of file tm1638.c.

void tm1638_set_7seg_raw ( const tm1638_p  t,
uint8_t  digit,
uint8_t  n 

Set segments in a particular digit.

tPointer to the tm1638 of interest.
digitThe digit of interest (0 is left-most).
nThe segments to set: (1 is top central, 128 is the point).

Definition at line 295 of file tm1638.c.

void tm1638_set_7seg_text ( const tm1638_p  t,
const char *  str,
uint8_t  dots 

Display some text on the display.

tPointer to the tm1638 of interest.
strThe text to display.
dotsThe 8 bits correspond to the decimal points, LSB = leftmost.

Definition at line 301 of file tm1638.c.

void tm1638_set_8leds ( const tm1638_p  t,
uint8_t  red,
uint8_t  green 

Set the status of all eight LEDs at once

tPointer to the tm1638 of interest.
redA byte's worth of red data (MSB is leftmost).
greenA byte's worth of green data (MSB is leftmost).

The ordering might seem odd, but makes the display a sensible binary rendition of red & green.

Definition at line 328 of file tm1638.c.

void tm1638_set_intensity ( tm1638_p  t,
uint8_t  intensity 

Set the display intensity.

tPointer to the tm1638 of interest.
intensityThe desired intensity (0-7).

Definition at line 149 of file tm1638.c.

void tm1638_set_led ( const tm1638_p  t,
uint8_t  led,
uint8_t  cols 

Set the status of one LED

tPointer to the tm1638 of interest.
ledThe LED in question.
colsThe colour to which it should be set.

Definition at line 322 of file tm1638.c.