 You are currently not logged in and your progress will not be saved. Register or Log in

# Exercise 1

## Printing to the console

In this exercise, we will practice using the user-friendly `printk()` function to print strings to the console.

We will modify Lesson 2 Exercise 2 so that when Button 1 is pressed, the factorials of the numbers from 1 to 10 are calculated and printed on the console as shown below. We are using the UART console, which is the default set in the board configuration file.

``````*** Booting Zephyr OS build v2.7.0-ncs1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Calculating the factorials of numbers from 1 to 10:
The factorial of  1 = 1
The factorial of  2 = 2
The factorial of  3 = 6
The factorial of  4 = 24
The factorial of  5 = 120
The factorial of  6 = 720
The factorial of  7 = 5040
The factorial of  8 = 40320
The factorial of  9 = 362880
The factorial of 10 = 3628800

_______________________________________________________
``````

Exercise Steps:

2. Make sure that your development kit is powered on and connected to your computer.

3. Add this application as an existing application in nRF Connect for VS Code, as we have done in lesson 2 and lesson 3.

4. Configure a terminal emulator on your machine. This has already been covered in Lesson 3 Exercise 1 step 8.

#### Note

The nRF Connect for VS code extension pack comes with a built-in terminal emulator called nRF Terminal. You can use it instead of PuTTY by simply clicking on the terminal symbol next to the COM port in the connected devices window:

5. Open the` main.c` file inside fund_less4_exer1/src.

6. Include the header file of the `printk()` function, by adding the following line under STEP 6:

``#include <sys/printk.h>``

#### Note

The console driver (`CONFIG_CONSOLE`) and console type (e.g. `CONFIG_UART_CONSOLE`) configuration options are already enabled in the board configuration file. Hence, we do not have to do anything in the application configuration file (`prj.conf`).

7. Let’s print out our first message, a simple banner that says `nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1`. Search for STEP 7 and add the following line

``printk("nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1\n\r");``

Build and flash the application to your board as we have done in previous lessons, and you should see this message is printed on the console.

``````*** Booting Zephyr OS build v2.6.99-ncs1-1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1``````

8. Now let’s replace the button callback function (ISR) with a code that generates numbers’ factorials:

8.1 Define the macro `MAX_NUMBER_FACT` that represents the maximum number to calculate its factorial and set its value to `10`.

``#define MAX_NUMBER_FACT 10``

Since code in ISR runs at a high priority (higher than all system and user threads), it should be written with timing in mind. Too lengthy or too complex tasks should not be performed by an ISR, they should be deferred to a thread. This will be covered in-depth in Lesson 7.

8.2 In `main.c`, replace the callback function `button_pressed()` with the following

``````void button_pressed(const struct device *dev, struct gpio_callback *cb,
uint32_t pins)
{
int i;
int j;
long int factorial;
printk("Calculating the factorials of numbers from 1 to %d:\n\r",MAX_NUMBER_FACT);
for (i=1;i<=MAX_NUMBER_FACT;i++){
factorial =1;
for (j=1;j<=i;j++){
factorial = factorial*j;
}
printk("The factorial of %2d = %ld\n\r",i,factorial);
}
printk("_______________________________________________________\n\r");
/*Important note!
Code in ISR runs at a high priority, therefore, it should be written with timing in mind.
Too lengthy or too complex tasks should not be performed by an ISR, they should be deferred to a thread.
*/
}
``````

Copy the code above and replace with the one in` main.c` as shown in the screenshot below:

In this code, we are using `printk()` in three different locations:

The first call is to print a formatted string that contains one integer` %d`.

``printk("Calculating the factorials of numbers from 1 to %d:\n\r",MAX_NUMBER_FACT);``

The second call is to print a formatted string containing an integer with a width specifier of `%2d`, and a long integer `%ld`. This is placed inside a loop, so it will be executed multiple times.

``printk("The factorial of %2d = %ld\n\r",i,factorial);``

The third and last call to `printk()` is to print a simple string that contains one long underline.

``printk("_______________________________________________________\n\r");``

9. Build the exercise and flash it to the board.

Observe that when button 1 on the board is pressed, the factorials of the numbers from 1 to 10 are calculated and printed on the console as shown below

``````*** Booting Zephyr OS build v2.6.99-ncs1-1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Calculating the factorials of numbers from 1 to 10:
The factorial of  1 = 1
The factorial of  2 = 2
The factorial of  3 = 6
The factorial of  4 = 24
The factorial of  5 = 120
The factorial of  6 = 720
The factorial of  7 = 5040
The factorial of  8 = 40320
The factorial of  9 = 362880
The factorial of 10 = 3628800
_______________________________________________________
``````