printf / scanf functions in Imagecraft compiler

Question: Are printf / scanf functions supported in Imagecraft compiler?

 

Answer:

Yes, Imagecraft compiler version 7.02.004 supports the printf/scanf functions. The compiler is available in PSoC Designer 5.0 SP6 and later.  Due to code size requirement, the printf/scanf functions are supported in the following three forms.

1. Basic with no long / floating point, or modifier,
2. Support for long
3. Support for floating point.

By default the option 1 is supported. To use option 2 or 3 the following changes are required in the compiler settings.

Support for float:
1. Open local.mk file from "Projects" tab in PSoC Designer. If local.mk file is not already available, create a text file called local.mk in the project directory.
2. At the bottom of local.mk, add CODECOMPRESSOR:=$(CODECOMPRESSOR) -lfpm8c  (this will link the libfpm8c.a, which contains the floating point capability of printf/scanf with modifier support)
3. Save local.mk file and build the project.

More code and data/stack space are needed for the fully featured versions (for the support of long and float).

Support for long:
1. Open local.mk file from "Projects" tab in PSoC Designer. If local.mk file does not already exist, create a text file called local.mk in the project directory.
2. At the bottom of local.mk, add CODECOMPRESSOR:=$(CODECOMPRESSOR) -llpm8c  (this will link the liblpm8c.a , which contains the long data type capability of printf/scanf)
3. Save local.mk file and build the project.

Example code for csprintf:

The following sample code shows the usage of csprintf() function with float support.

#include "m8c.h"        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "stdio.h"             //definitions for all standard io functions

float result = 100.235641;

char str[];                        //Character array that holds the formatted string

void main(void)

{

    //Start LCD module
    LCD_Start();

    //LCD starting position is (0,0)
    LCD_Position(0,0);
        
    //Get the formatted string
    csprintf(str,"%f",result);

    //Display the string on LCD
    LCD_PrString(str);

    while(1);
}

The above code will display 100.235641 on LCD.

Example code for cprintf

cprintf() function writes the formatted string to a console.  This function uses “int putchar(char)”  function to send the characters to console.  User has to define this putchar function inside which the character is converted into formatted string and send to output.  The following sample code shows the usage of cprintf() function.

#include "m8c.h"        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "stdio.h"             //definitions for all standard io functions

float result=100.235641;

char str[]; //Character array to hold the formatted string

int putchar(char c)
{
    // Copy the character passed by cprintf function to formatted string "str"
    strncpy( str , &c , 1 );


    //Print the formatted string on LCD
    LCD_PrString( str );          

    //This return statement can be used to return the size of the string.  In this code it is not used.
    return c;                         
}

void main(void)
{
    //Start LCD module
    LCD_Start();
        
    //LCD starting position is (0,0)
    LCD_Position(0,0);

    //Get the formatted string
    cprintf("%f",result);

    while(1);
}

The above code will display 100.235641 on LCD.