Skip to content

The library controls TM1638 controller for 7-segment digital tubes, two-color LEDs and 24 switch keyboards.

License

Notifications You must be signed in to change notification settings

mrkaleArduinoLib/gbj_tm1638

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gbjTM1638

Library for utilizing display modules with TM1638 controller. Those modules are available in various hardware equipment as follows. However each variant has 8 digital 7-segment tubes.

  1. 8 digital 7-segment 0.56" tubes with decimal dots, 8 two-color LEDs red & green, keypad with 8 keys, two 10 pin connectors (usually marked as JY-LKM1368). This module can be cascaded by 10 pin cable, where output connector of a modules is tied with input connector of the next module in a chain. The entire chain is controlled through the input connector of the first module in a chain and particular modules is selected by a corresponding STB pin of that connector.

  2. 8 digital 7-segment 0.56" tubes with decimal dots, two 10 pin connectors (usually unmarked). This module can be considered as a extension digital tubes only module of the previous one and can be cascaded.

  3. 8 digital 7-segment 0.36" tubes with decimal dots, 8 red LEDs, keypad with 8 keys, one 5 pin connector (usually marked as LED--KEY, LED&KEY).

The library controls the controller as a state machine with screen buffer in the microcontroller's operating memory, which is transmitted to the controller for displaying.

  • Screen buffer is considered as an image of controller's graphical memory.
  • Graphical library methods (prefixed with "print") performs all graphical manipulations in the screen buffer, which state reflects the desired image for display.
  • Finally the dedicated method display() transmits the content of the screen buffer to the controller and it causes to display the image on the attached display (digital tubes) and LEDs.
  • The controller TM1638 can control up to 8 digital tubes each with radix (decimal dot), 8 two-color LEDs, and 24 keys of the keypad, although display modules usually have just 8 or 16 keys in the keypad implemented.
  • The library controls 7-segment glyphs (digits) mutual independently from radix 8th segments of digital tubes and LEDs.
  • The library implements key scan capabilities of the controller as well for all 24 keys.
  • The library inherits from the system library Print, so that all system print operations are available.

Dependency

  • Arduino.h: Main include file for the Arduino SDK version greater or equal to 100.
  • WProgram.h: Main include file for the Arduino SDK version less than 100.
  • inttypes.h: Integer type conversions. This header file includes the exact-width integer definitions and extends them with additional facilities provided by the implementation.
  • Print.h: System library for printing.

Fonts

The font is an assignment of a glyph definition to particular ASCII code.

  • A 7-segment display glyph is defined by a segment mask of the controller.
  • Every font is defined as one-dimensional array with the same name gbjFont7segTable, stored in a separate include file with the naming convention font7seg_variant.h in the subfolder extras. Font variants differentiate from each other by length and content of that array.
  • The library contains those fonts:
    • font7seg_basic.h: Alphanumeric glyphs reasonably recognizable and readable on 7-segment displays.
    • font7seg_decnums.h: Decimal digits, space, and minus glyph.
    • font7seg_hexnums.h: Hexadecimal digits, space, and minus glyph.
  • Despite the font array is one-dimensional one, glyphs are defined by logical group of two bytes in it.
    • The first byte of the glyph pair is an ASCII code of a glyph. Usually it is a 7-bit code, but it might be 8-bit one as well, if appropriate segment mask is provided, which can be reasonably displayed on the 7-segment display.
    • The second byte of the glyph pair is a segment mask of a glyph with least significant bit (LSB) corresponding to the segment A. The 8th, most significant bit (MSB) corresponding to the decimal point (DP) is ignored if set, because the library controls radix segments separately, not by fonts.
  • Involving ASCII codes to the font definition enables to define just recognizable glyphs by the 7-segment displays or needed by a project and not to waste memory by definition contiguous set of ASCII codes with unused glyphs, although not starting from 0.
  • After including a font include file into a sketch, the font is stored in the flash memory of a microcontroller in order to save operational SRAM.
  • The library can utilize just one font at a time.

Constants

All constants are embedded into the class as static ones including result and error codes except constant defining hardware keypad equipment.

  • gbj_tm1638:VERSION: Name and semantic version of the library.

  • gbj_tm1638::SUCCESS: Result code for successful processing.

  • GBJ_TM1638_KEYS_PRESENT: Really implemented keys in the keypad of a display module. The constant defines the dimension of keys presses history array. Define it in your sketch right before including header file of this library according to your display module, if number of its hardware keys differs from default value of the constant. Redefinition of the constant is enabled in order not to waist memory for not implemented keys and in order to manage different keypads. Default value is 8 keys.

Errors

  • gbj_tm1638::ERROR_PINS: Error code for incorrectly assigned microcontroller's pins to controller's pins, usually some o them are duplicated.
  • gbj_tm1638::ERROR_ACK: Error code for not acknowledged transmission by the controller.

Key actions

  • gbj_tm1638::KEY_CLICK: A key has been clicked once. The delay of key released after its pressing should be a bit longer than at the double click after the first click in order to distinguish between them.
  • gbj_tm1638::KEY_CLICK_DOUBLE: A key has been clicked twice, i.e., double clicked with short delay between key presses.
  • gbj_tm1638::KEY_HOLD: A key has been clicked and keep pressed a while, then released.
  • gbj_tm1638::KEY_HOLD_DOUBLE: A key has been double clicked and keep pressed a while at the second press, then released.

Interface

The methods in bold return result or error codes and communicate with the controller directly. The methods for screen buffer manipulation return nothing, just update the content of the screen buffer as an image of controller registers, and must be followed by display() method in order to display the content of the screen buffer.

It is possible to use functions from the parent library Print, which is extended by this library.

Custom data types

Initialization

Display manipulation

Screen buffer manipulation

Setters

Getters

gbj_tm1638_handler()

Description

Custom data type determining the template for key action handler procedures.

  • The handler method is called then particular action with any key of the keypad is detected. It allows to process and execute some code dedicated to particular key and its action.
  • The handler method is registered to the library by the method registerHandler() and should have appropriate input parameters defined.

Syntax

    void (*gbj_tm1638_handler)(uint8_t key, uint8_t action);

Parameters

  • key: Number of a keypad's key counting from 0, for which action the handler is called.

  • action: The action with a key, which has been executed recently.

    • Valid values: key actions defined by constants for key actions
    • Default value: none

Returns

None

See also

registerHandler()

Back to interface

gbj_tm1638()

Description

The constructor method sanitizes and stores physical features of the display and limited ones for the sake of a sketch to the class instance object.

Syntax

gbj_tm1638(uint8_t pinClk, uint8_t pinDio, uint8_t pinStb, uint8_t digits, uint8_t leds, uint8_t keys);

Parameters

  • pinClk: Microcontroller pin's number utilized as a serial clock.

    • Valid values: non-negative integer (according to a microcontroller datasheet)
    • Default value: 2
  • pinDio: Microcontroller pin's number utilized as a data input and output.

    • Valid values: non-negative integer (according to a microcontroller datasheet)
    • Default value: 3

  • digits: Number of 7-segment digital tubes to be controlled.

  • leds: Number of LEDs to be controlled.

  • keys: Number of keypad's keys to be controlled. Default value is aimed for regular display modules.

Returns

The library instance object for a display module.

Back to interface

begin()

Description

The method checks the microcontroller's pins defined in the constructor and performs initial sequence recommended by the data sheet for the controller.

  • The method clears all digital tubes including radixes and turns off all LEDs.
  • The method sets a display module to the normal operating mode.
  • The method checks whether some two pins set by constructor are not mutually equal.

Syntax

uint8_t begin();

Parameters

None

Returns

Some of result or error codes.

Back to interface

display()

Description

The method transmits current content of the screen buffer to the controller, so that its content is displayed immediately and stays unchanged until another transmission.

  • The method utilizes automatic addressing mode of the controller.

Syntax

uint8_t display();

Parameters

None

Returns

Some of result or error codes.

See also

displayOn()

displayOff()

Back to interface

displayOn(), displayOff()

Description

Particular method either turns on or off the entire display module including digital tubes and LEDs without changing current contrast level.

  • Both methods are suitable for making a display module blinking.

Syntax

uint8_t displayOn();
uint8_t displayOff();

Parameters

None

Returns

Some of result or error codes.

See also

display()

Back to interface

displayClear()

Description

The method turns off all segments including for radixes of all digital tubes and then sets the printing position for subsequent printing.

Syntax

void displayClear(uint8_t digit);

Parameters

  • digit: Number of digital tube counting from 0 where the printing should start after display clearing.
    • Valid values: 0 ~ digits - 1 (from constructor)
    • Default value: 0

Returns

None

See also

printDigitOff()

printRadixOff()

placePrint()

Back to interface

moduleClear()

Description

The method turns off all segments including for radixes of all digital tubes as well as all LEDs and then sets the printing position for subsequent printing.

  • It is a wrapper method for subsequent calling methods printLedOff() and [displayClear()].

Syntax

void moduleClear(uint8_t digit);

Parameters

  • digit: Number of digital tube counting from 0 where the printing should start after display clearing.
    • Valid values: 0 ~ digits - 1 (from constructor)
    • Default value: 0

Returns

None

See also

printLedOff()

displayClear()

Back to interface

printRadixOn(), printRadixOff(), printRadixToggle()

Description

The particular method performs corresponding manipulation with radix segment (usually 8th one) of particular glyph without influence on its glyph segments (first 7 segments) in the screen buffer.

  • Each method is overloaded. If there is no input parameter provided, the method performs appropriate action on all controlled digital tubes.

Syntax

void printRadixOn(uint8_t digit);
void printRadixOn();
void printRadixOff(uint8_t digit);
void printRadixOff();
void printRadixToggle(uint8_t digit);
void printRadixToggle();

Parameters

  • digit: controller's digit tube number counting from 0, which radix segment should be manipulated.
    • Valid values: 0 ~ digits - 1 (from constructor)
    • Default value: none

Returns

None

See also

printDigitOn()

printDigitOff()

Back to interface

printDigit()

Description

The method sets glyph segments (first 7 ones) of particular digital tube without influence on its radix segment in the screen buffer.

  • The method is overloaded. If there is one input parameter provided, it is considered as a segment mask for current print position set at recent printing action.
  • The method is useful for writing to the display without any font used.

Syntax

void printDigit(uint8_t digit, uint8_t segmentMask);
void printDigit(uint8_t segmentMask);

Parameters

  • digit: controller's digital tube number counting from 0, which glyph segments should be manipulated.

    • Valid values: 0 ~ digits - 1 (from constructor)
    • Default value: none
  • segmentMask: Bit mask defining what segments should be turned on. Segments marks starting from A to G relate to mask bits 0 to 6 counting from the least significant bit. The 7th bit relates to radix segment and therefore it is ignored.

    • Valid values: 0 ~ 127
    • Default value: none

Returns

None

See also

printDigitOn()

printDigitOff()

Back to interface

printDigitOn(), printDigitOff()

Description

The particular method performs corresponding manipulation turning on or off with all glyph segments at once of the display without changing glyph radix segments.

  • Each method is overloaded. If there is no input parameter provided, the method performs appropriate action on all controlled digital tubes.

Syntax

void printDigitOn(uint8_t digit);
void printDigitOn();
void printDigitOff(uint8_t digit);
void printDigitOff();

Parameters

  • digit: controller's digit tube number counting from 0, which glyph segments should be manipulated.
    • Valid values: 0 ~ digits - 1 (from constructor)
    • Default value: none

Returns

None

See also

printDigit()

Back to interface

printText()

Description

The method prints text starting from provided or default position on digital tubes.

  • The method clears the display right before printing.
  • It is a wrapper method for subsequent calling methods displayClear() and system method print().

Syntax

void printText(const char* text, uint8_t digit);
void printText(String text, uint8_t digit);

Parameters

  • text: Pointer to a text that should be printed.

    • Valid values: microcontroller's addressing range
    • Default value: none
  • digit: controller's digit tube number counting from 0, where printing should start.

    • Valid values: 0 ~ digits - 1 (from constructor)
    • Default value: 0

Returns

None

See also

printDigit()

Back to interface

printGlyphs()

Description

The method prints text starting from provided or default position on digital tubes without impact on radixes.

  • The method clears digits right before printing leaving radixes intact.
  • The method is suitable for displaying data, where radixes are independent of them and are used for another purpose.
  • It is a wrapper method for subsequent calling methods printDigitOff(), placePrint(), and system method print().

Syntax

void printGlyphs(const char* text, uint8_t digit);
void printGlyphs(String text, uint8_t digit);

Parameters

  • text: Pointer to a text that should be printed.

    • Valid values: microcontroller's addressing range
    • Default value: none
  • digit: controller's digit tube number counting from 0, where printing should start.

    • Valid values: 0 ~ digits - 1 (from constructor)
    • Default value: 0

Returns

None

See also

printText()

Back to interface

placePrint()

Description

The method stores desired position of a digital tube where the subsequent print should start.

  • The method should be call right before any printing method, which does not have its input parameter for setting printing position.

Syntax

void placePrint(uint8_t digit);

Parameters

  • digit: Printing position for starting a print action.
    • Valid values: 0 ~ digits - 1 (from constructor)
    • Default value: 0

Returns

None

See also

printDigit()

Back to interface

printLedOnRed(), printLedToggleRed(), printLedOnGreen(), printLedToggleGreen()

Description

The particular method turns on or toggles red or green LEDs without influence on digital tubes in the screen buffer.

  • If there is no input LED number provided, the methods manipulates all module's controlled LEDs of corresponding color at once.
  • If a display module does not have two-color LEDs implemented, manipulation with green LEDs has no effect, just on red ones has.

Syntax

void printLedOnRed(uint8_t led);
void printLedOnRed();
void printLedToggleRed(uint8_t led);
void printLedToggleRed();
void printLedOnGreen(uint8_t led);
void printLedOnGreen();
void printLedToggleGreen(uint8_t led);
void printLedToggleGreen();

Parameters

  • led: controller's LED number counting from 0.
    • Valid values: 0 ~ leds - 1 (from constructor)
    • Default value: none

Returns

None

See also

printLedOff()

printLedSwap()

Back to interface

printLedOff()

Description

The method turns off LEDs without influence on digital tubes in the screen buffer, i.e., in case of two-color LEDs both colors.

  • If there is no input LED number provided, the methods turns off all module's controlled LEDs at once.

Syntax

void printLedOff(uint8_t led);
void printLedOff();

Parameters

  • led: controller's LED number counting from 0.
    • Valid values: 0 ~ leds - 1 (from constructor)
    • Default value: none

Returns

None

See also

printLedOnRed()

printLedOnGreen()

Back to interface

printLedSwap()

Description

The method swaps colors of a turned on two-color LEDs without influence on digital tubes in the screen buffer, i.e., changes red to green and vice versa.

  • The color swapping is realized by negating LED value in the screen buffer.
  • The color swapping occurs only if some of colors was turned on right before swapping.
  • If a LED was turned off before swapping, both colors are turned on; however, the red has precedence because color mixture is not supported by a display module.
  • If a display module has just red LEDs implemented, the swapping has effect of toggling a red LED.
  • If there is no input LED number provided, the method swaps all module's controlled LEDs at once.

Syntax

void printLedSwap(uint8_t led);
void printLedSwap();

Parameters

  • led: controller's LED number counting from 0.
    • Valid values: 0 ~ leds - 1 (from constructor)
    • Default value: none

Returns

None

See also

printLedOnRed()

printLedToggleRed()

printLedOnGreen()

printLedOff()

Back to interface

write()

Description

The library inherits the system Print class, so that all regular print functions can be used.

  • Actually all print functions eventually call one of listed write methods, so that all of them should be implemented.
  • If some character (ASCII) code is not present in the font table, i.e., it is unknown for the library, that character is ignored and not displayed.
  • If unknown character has ASCII code of comma, dot, or colon, the library turns on the radix segments of the recently displayed digit. Thus, the decimal points or colon can be present in displayed string at proper position and does not need to be control separately.

Syntax

size_t write(uint8_t ascii);
size_t write(const char* text);
size_t write(const uint8_t* buffer, size_t size);

Parameters

  • ascii: ASCII code of a character that should be displayed at the current print position. The methods is usually utilized internally by system prints.

    • Valid values: 0 ~ 255
    • Default value: none
  • text: Pointer to a null terminated string that should be displayed from the current print position.

    • Valid values: microcontroller's addressing range
    • Default value: none
  • buffer: Pointer to a string, which part should be displayed from the current print position.

    • Valid values: microcontroller's addressing range
    • Default value: none
  • size: Number of characters that should be displayed from the current print position.

    • Valid values: microcontroller's addressing range
    • Default value: none

Returns

None

See also

printText()

placePrint()

Back to interface

registerHandler()

Description

The method registers a procedure, which is called when particular action with a key of module's keypad has been performed.

Syntax

void registerHandler(gbj_tm1638_handler handler);

Parameters

  • handler: Pointer to a handler procedure of type gbj_tm1638_handler.
    • Valid values: microcontroller's addressing range
    • Default value: none

Returns

None

Example

Handler is defined in a sketch as a standalone procedure. Only registering that method makes a handler from it.

gbj_tm1638 Sled = gbj_tm1638();

void keyHandler(uint8_t key, uint8_t action)
{
  switch (action)
  {
    case gbj_tm1638::KEY_CLICK:
      Sled.printLedOnRed(key);
      break;

    case gbj_tm1638::KEY_HOLD:
      Sled.printDigitOn(key);
      break;

    case gbj_tm1638::KEY_CLICK_DOUBLE:
      Sled.printLedOff(key);
      break;

    case gbj_tm1638::KEY_HOLD_DOUBLE:
      Sled.printDigitOff(key);
      break;
  }
}

setup()
{
 Sled.begin();
 Sled.registerHandler(keyHandler);
}

See also

gbj_tm1638_handler

Back to interface

run()

Description

The method processes timing and catches keypad's keys presses and calls a handler if particular action is detected and if some handler is registered.

  • The method should be call very often. The best place is in the loop() function of a sketch, which should be without delay() function or other blocking activities.

Syntax

void run();

Parameters

None

Returns

None

Example

gbj_tm1638 Sled = gbj_tm1638();

setup()
{
 Sled.begin();
 Sled.registerHandler(keyHandler);
}

loop()
{
 Sled.run();
}

See also

registerHandler()

Back to interface

initLastResult()

Description

The method sets internal status of recent processing of a controller code to success with value of constant gbj_tm1638::SUCCESS. It is usually called right before any operation with the controller in order to reset the internal status.

Syntax

void initLastResult();

Parameters

None

Returns

None

See also

getLastResult()

setLastResult()

Back to interface

setLastResult()

Description

The method sets the internal status of recent processing with controller to input value. Without input parameter it is equivalent to the method initLastResult().

Syntax

uint8_t setLastResult(uint8_t lastResult);

Parameters

Returns

New (actual) result code of recent operation.

See also

initLastResult()

getLastResult()

Back to interface

setContrast()

Description

The method sets the level of the display contrast.

  • The contrast is perceived as the brightness of the display.
  • The brightness is technically implemented with PWM of segments power supply.

Syntax

uint8_t setContrast(uint8_t contrast);

Parameters

  • contrast: Level of contrast/brightness.

Returns

Some of result or error codes.

Back to interface

setFont()

Description

The method gathers font parameters for printing characters on 7-segment displays.

  • Font definition is usually included to an application sketch from particular include file, while the font table resides in programmatic (flash) memory of a microcontroller in order to save operational memory (SRAM).
  • Each glyph of a font consists of the pair of bytes. The first byte determines ASCII code of a glyph and second byte determines segment mask of a glyph. It allows to defined only displayable glyphs on 7-segment displays and suppress need to waste memory for useless characters.

Syntax

void setFont(const uint8_t* fontTable, uint8_t fontTableSize);

Parameters

  • fontTable: Pointer to constant byte array with font characters definitions. Because the font table resides in flash memory, it has to be constant.

    • Valid values: microcontroller addressing range
    • Default value: none
  • fontTableSize: The number of bytes that should be utilized from the font table.

    • Because the font table is referenced by a pointer and not as an array, the table size cannot be calculated internally, but has to be defined externally usually by the function sizeof.
    • The table size in conjunction with font character pair of bytes determines the number of characters used for printing.
    • The size can be smaller than the real size of the table, however, the size should be a multiple of 2.
      • Valid values: 0 ~ 255 (maximal 127 different characters)
      • Default value: none

Returns

None

Example

#include "font7seg_basic.h"
gbj_tm1638 Sled = gbj_tm1638();
setup()
{
 Sled.begin();
 Sled.setFont(gbjFont7segTable, sizeof(gbjFont7segTable));
}

See also

Fonts

Back to interface

getLastResult()

Description

The method returns a result code of the recent operation with controller. It is usually called for error handling in a sketch.

Syntax

uint8_t getLastResult();

Parameters

None

Returns

Current result code. It is some of expected result or error codes.

See also

setLastResult()

initLastResult()

Back to interface

getLastCommand()

Description

The method returns the command code used at recent communication with controller. In conjunction with returned result or error code of particular method it is possible to detect the source or reason of a communication error.

Syntax

uint8_t getLastCommand();

Parameters

None

Returns

Recently used command code.

Back to interface

getDigits(), getLeds(), getKeys()

Description

The corresponding method returns number of particular controlled equipment items of a display module as it was defined in the corresponding constructor's parameter.

Syntax

uint8_t getDigits();
uint8_t getLeds();
uint8_t getKeys();

Parameters

None

Returns

Current number of controlled items of corresponding type by a library instance object.

See also

gbj_tm1638()

Back to interface

getDigitsMax(), getLedsMax(), getKeysMax()

Description

The method returns maximal number of equipment items of a display module that the controller supports.

Syntax

uint8_t getDigitsMax();
uint8_t getLedsMax();
uint8_t getKeysMax();

Parameters

None

Returns

Maximal number of equipment items of a display module supported by the controller.

See also

getDigits()

getLeds()

getKeys()

Back to interface

getContrast()

Description

The method returns the current contrast/brightness level store in the library instance object.

Syntax

uint8_t getContrast();

Parameters

None

Returns

Current contrast level counting up to getContrastMax().

See also

setContrast()

Back to interface

getContrastMax()

Description

The method returns the maximal contrast/brightness level supported by the controller.

Syntax

uint8_t getContrastMax();

Parameters

None

Returns

Maximal contrast level supported by the controller, which is 7.

See also

getContrast()

Back to interface

getPrint()

Description

The method returns the current print position set by recent print activity.

Syntax

uint8_t getPrint();

Parameters

None

Returns

Current printing position counting from 0. It may get beyond the maximal controlled or implemented digital tube.

See also

placePrint()

Back to interface

isSuccess()

Description

The method returns a flag whether the recent operation with controller was successful.

Syntax

bool isSuccess();

Parameters

None

Returns

Flag about successful recent processing.

See also

getLastResult()

isError()

Back to interface

isError()

Description

The method returns a flag whether the recent operation with controller failed. The corresponding error code can be obtained by the method [getLastResult()]((#getLastResult), which is one of the error constants.

Syntax

bool isError();

Parameters

None

Returns

Flag about failing of the recent operation.

See also

getLastResult()

isSuccess()

Back to interface

About

The library controls TM1638 controller for 7-segment digital tubes, two-color LEDs and 24 switch keyboards.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published