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

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

Go to the source code of this file.

Data Structures

struct  tm1638_tag

Functions

static void tm1638_send_raw (const tm1638_p t, uint8_t x)
static uint8_t tm1638_receive_raw (const tm1638_p t)
static void tm1638_send_command (const tm1638_p t, uint8_t x)
static void tm1638_send_data (const tm1638_p t, uint8_t addr, uint8_t data)
static void tm1638_send_config (const tm1638_p t)
static uint8_t tm1638_calc_config (const tm1638_p t)
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.

Author:
Martin Oldfield ex-tm.nosp@m.1638.nosp@m.@mjo..nosp@m.tc
Version:
0.1

DESCRIPTION

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.

This is the C source file implementing the library, and so this documentation includes information only relevant to the implementation. If you're just using the library, read the tm1638.h documentation instead.

ISSUES

  1. The code makes the data line into an output by default, turning it into an input only when reading data. It should probably be the other way round: an input unless we're actually driving something.
  2. Some delays are needed or some pulses are too fast. The delays are all somewhat arbitrary, and whilst they work for me, I don't claim that they are optimal.
  3. The packaging is rather clunky: I don't grok autotools very well!

REFERENCES

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.

LICENSE

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.c.


Function Documentation

tm1638_p tm1638_alloc ( uint8_t  data,
uint8_t  clock,
uint8_t  strobe 
)

Allocation and initialization function, aka constructor.

NOTES:

  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:

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

Definition at line 103 of file tm1638.c.

static uint8_t tm1638_calc_config ( const tm1638_p  t)
static

Given settings in t, calculate the config byte to send.

Parameters:
tPointer to the tm1638 of interest.
Returns:
The byte of config data we need to send.

Definition at line 179 of file tm1638.c.

void tm1638_enable ( tm1638_p  t,
bool  enable 
)

Enable/disable the display

Parameters:
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.

Parameters:
cThe ASCII character of interest.
Returns:
The segments to set.

Definition at line 353 of file tm1638.c.

void tm1638_free ( tm1638_p t)

Freeing function aka destructor.

Parameters:
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.

Parameters:
tPointer to the tm1638 of interest.
Returns:
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.

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

Definition at line 378 of file tm1638.c.

static uint8_t tm1638_receive_raw ( const tm1638_p  t)
static

Low-level command primitive: read a byte from the hardware.

Parameters:
tPointer to the tm1638 of interest.
Returns:
The byte we read.

Definition at line 217 of file tm1638.c.

void tm1638_send_cls ( const tm1638_p  t)

Turn off all the LEDs

Parameters:
tPointer to the tm1638 of interest.

Definition at line 335 of file tm1638.c.

static void tm1638_send_command ( const tm1638_p  t,
uint8_t  x 
)
static

Medium-level command primitive: send a command to the hardware.

Parameters:
tPointer to the tm1638 of interest.
xThe command to send.

Definition at line 257 of file tm1638.c.

static void tm1638_send_config ( const tm1638_p  t)
static

Send the settings in t to the actual hardware.

Parameters:
tPointer to the tm1638 of interest.

Definition at line 166 of file tm1638.c.

static void tm1638_send_data ( const tm1638_p  t,
uint8_t  addr,
uint8_t  data 
)
static

Medium-level command primitive: write a data byte to the hardware.

Parameters:
tPointer to the tm1638 of interest.
addrThe address to write.
dataThe data to write.

Definition at line 278 of file tm1638.c.

static void tm1638_send_raw ( const tm1638_p  t,
uint8_t  x 
)
static

Low-level command primitive: send a byte to the hardware.

Parameters:
tPointer to the tm1638 of interest.
xThe byte to send.

Definition at line 191 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.

Parameters:
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.

Parameters:
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

Parameters:
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.

Parameters:
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

Parameters:
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.