Search code examples
avratmega

Using of variable instead of hard-coded string


I plan to to use led display containing TM1637.
Programming language: avr-gcc
I've found this library:
https://github.com/project37cat/TM1637-lib-AVR/blob/master/tm1637.h
and it works for me by using of function led_print in this example: https://github.com/project37cat/TM1637-lib-AVR/blob/master/main.c
However I cannot figure out how to use a variable for it instead of hard-coded string (Integer value is planned to be used but I'm not sure whether it's even possible as library expects string).


Solution

  • Programming language: avr-gcc

    GCC is a cross-platform compiler, not a programming language. You are programming in "C" in this case, but it could as well be C++.

    Integer value is planned to be used but I'm not sure whether it's even possible as library expects string

    You have to convert the desired integer value to a string (char-array) representation. There are several ways to achieve that. A common one would be to use snprintf(). Also take a look at format strings.

    #include <stdio.h>   // library that contains snprintf()
    
    char buffer[10];   // this is the char-buffer where your "string" will be stored
    int value = 234452;   // this is the value you want to convert
    snprintf(buffer, 10, "%d", value);  // this coverts "value" to a decimal representation as specified by the format string "%d" and copies it to "buffer"
    

    Then you should be able to use

    led_print(0, buffer);