Skip to content

Commit e484faf

Browse files
committed
doc/guides/tutorial: Introduce code_folder for all tutorials
1 parent a4e6749 commit e484faf

File tree

8 files changed

+55
-25
lines changed

8 files changed

+55
-25
lines changed

doc/guides/c_tutorials/create_project.mdx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Creating a Project
33
description: This tutorial will guide you through creating a new project with a simple hello world program.
4+
code_folder: examples/guides/creating_project/
45
---
56
import Contact from '@components/contact.astro';
67
import GitSetup from '@components/gitsetup.mdx';
@@ -19,12 +20,9 @@ Now that Visual Studio Code is open, we create a new file called `main.c` and ad
1920

2021
```c
2122
#include <stdio.h>
22-
#include "ztimer.h"
2323

2424
int main(void)
2525
{
26-
ztimer_sleep(ZTIMER_SEC, 3);
27-
2826
puts("Hello World!");
2927

3028
return 0;
@@ -33,7 +31,7 @@ int main(void)
3331
3432
![The hello world program in Visual Studio Code](img/create_project/03_main_c.png)
3533
36-
This program will print "Hello World!" to the console after sleeping for 3 seconds. We use the `ztimer_sleep` function to sleep for 3 seconds otherwise we will most likely miss the output before connecting to our device. This function is part of the `ztimer` module, which is part of RIOT. The `puts` function is part of the standard C library and is used to print a string to the console.
34+
This program will print "Hello World!" to the console when it is run. The `#include <stdio.h>` line includes the standard input/output library, which allows us to use the `puts` function to print to the console.
3735
3836
## Step 3: Creating the Makefile
3937
@@ -54,9 +52,6 @@ RIOTBASE ?= $(CURDIR)/RIOT
5452
# development process:
5553
DEVELHELP ?= 1
5654
57-
# This board requires a start sleep to actually catch the printed output
58-
USEMODULE += ztimer_sec
59-
6055
# Change this to 0 show compiler invocation lines by default:
6156
QUIET ?= 1
6257

doc/guides/c_tutorials/gpio.mdx

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: GPIO & Real Boards
33
description: This tutorial explains how to use GPIO in RIOT to control LEDs or read button presses.
4+
code_folder: examples/guides/gpio/
45
---
56

67
import Contact from '@components/contact.astro';
@@ -100,6 +101,7 @@ gpio_mode_t led0_mode = GPIO_OUT;
100101

101102
Now we can control the LED by first initializing the pin and then clearing or setting it.
102103

104+
{/*<!--skip ci-->*/}
103105
```c
104106
int main(void) {
105107
// Initialize the LED0 pin
@@ -117,10 +119,10 @@ This code will turn off the LED when the board starts which is quite boring,
117119
so let's make it blink by adding a delay and toggling the LED.
118120
119121
```c
120-
while (1) {
122+
while (1) {
121123
gpio_toggle(led0);
122124
ztimer_sleep(ZTIMER_MSEC, 500);
123-
}
125+
}
124126
```
125127

126128
![The Code in Visual Studio Code](img/gpio/04_code.png)
@@ -144,11 +146,12 @@ that way we can drastically reduce the power consumption of the device.
144146

145147
First we need to define the callback function that will be called when the button is pressed.
146148

147-
```c
148-
// Define the LED0 pin and mode
149+
```c title="Define the LED0 pin and mode"
149150
gpio_t led1 = GPIO_PIN(1, 10);
150151
gpio_mode_t led1_mode = GPIO_OUT;
152+
```
151153

154+
```c title="Define the button callback function"
152155
void button_callback (void *arg)
153156
{
154157
(void) arg; /* the argument is not used */
@@ -163,12 +166,39 @@ void button_callback (void *arg)
163166
164167
Now we need to define the button pin and mode and initialize it.
165168
166-
```c
167-
// Define the button pin and callback
169+
```c {1-2, 26-27}
170+
// Define the button pin
168171
gpio_t button = GPIO_PIN(1, 2);
169172
173+
void button_callback (void *arg)
174+
{
175+
(void) arg; /* the argument is not used */
176+
if (!gpio_read(button)) {
177+
gpio_set(led1);
178+
}
179+
else {
180+
gpio_clear(led1);
181+
}
182+
}
183+
170184
int main(void) {
171-
gpio_init_int(button, GPIO_IN_PU, GPIO_BOTH, button_callback, NULL);
185+
// Initialize the LED0 pin
186+
gpio_init(led0, led0_mode);
187+
// Turn off the LED0 pin
188+
gpio_clear(led0);
189+
190+
// Initialize the LED1 pin
191+
gpio_init(led1, led1_mode);
192+
// Turn off the LED1 pin
193+
gpio_clear(led1);
194+
195+
// Initialize the button pin
196+
gpio_init_int(button, GPIO_IN_PU, GPIO_BOTH, button_callback, NULL);
197+
198+
while (1) {
199+
gpio_toggle(led0);
200+
ztimer_sleep(ZTIMER_MSEC, 500);
201+
}
172202
}
173203
```
174204

-10.9 KB
Loading
-3.2 KB
Loading

doc/guides/c_tutorials/saul.mdx renamed to doc/guides/c_tutorials/saul.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Sensors/Actuators using SAUL
33
description: Learn how to use sensors and actuators with RIOT
4+
code_folder: examples/guides/saul/
45
---
56

67
In the previous chapter we learned how to interact with the GPIO directly, but RIOT provides a more abstract way to interact with sensors and actuators. RIOT calls this the SAUL (Sensors/Actuators Abstraction Layer) system.
@@ -87,7 +88,6 @@ which then stores the result in a `phydat_t` struct we provide.
8788
// and store the result in the temperature variable
8889
// saul_reg_read returns the dimension of the data read (1 in this case)
8990
int dimension = saul_reg_read(temperature_sensor, &temperature);
90-
}
9191
```
9292

9393
Once again, since C doesn't have exceptions, we need to check if the sensor was read correctly.

doc/guides/c_tutorials/shell.mdx renamed to doc/guides/c_tutorials/shell.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Shell Commands
33
description: This tutorial explains how to use the RIOT shell.
4+
code_folder: examples/guides/shell/
45
---
56

67
The shell is a powerful tool in RIOT that allows you to interact with your application at runtime.
@@ -22,9 +23,11 @@ This line tells the build system to include the shell module in our application.
2223

2324
Now we can start the shell in our main function by calling the `shell_init` function. Go into your `main.c` file and include the following code:
2425

25-
```c
26+
```c title="The include so we can use the shell"
2627
#include "shell.h"
28+
```
2729

30+
```c title="The main function"
2831
int main(void)
2932
{
3033
// Buffer to store command line input
@@ -57,9 +60,11 @@ RIOT provides a set of macros to make this process easier.
5760
5861
Let's create a simple shell command that echoes back the input. Go into your `main.c` file and include the following code:
5962
60-
```c
63+
```c title="The include so we can print to the console"
6164
#include <stdio.h>
65+
```
6266

67+
```c title="The function that implements the echo command"
6368
int echo_command(int argc, char **argv)
6469
{
6570
for (int i = 1; i < argc; i++) {
@@ -75,10 +80,8 @@ This function takes two arguments: `argc` and `argv`. `argc` is the number of ar
7580
7681
Now all that is left is to register the command with the shell. Go back to your `main.c` file and include the following code _outside_ of the `main` function:
7782
78-
```c
79-
SHELL_COMMAND(echo,
80-
"Echoes back the input",
81-
echo_command);
83+
```c title="Register the command with the shell"
84+
SHELL_COMMAND(echo,"Echo a message", echo_command);
8285
```
8386

8487
This macro takes three arguments: the name of the command, a short description of the command, and the function that implements the command.

doc/guides/c_tutorials/threads.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Threads
33
description: This tutorial explains how to use threads in RIOT.
4+
code_folder: examples/guides/threads/
45
---
56

67
import Contact from '@components/contact.astro';
@@ -53,7 +54,7 @@ Lastly, we need to actually start the thread. Go into your `main` function and i
5354
int main(void) {
5455
thread_create(my_thread_stack, sizeof(my_thread_stack),
5556
THREAD_PRIORITY_MAIN - 1, 0, my_first_thread, NULL,
56-
"My first thread");
57+
"My first thread");
5758

5859
puts("Hello, from the main thread!");
5960
}

doc/guides/c_tutorials/timers.mdx renamed to doc/guides/c_tutorials/timers.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Timers and Callbacks
33
description: This tutorial explains how to use timers and callbacs in RIOT.
4+
code_folder: examples/guides/timers/
45
---
56

67
Timers and interrupts are essential concepts in embedded systems programming. In this tutorial, we will take a look at how to use timers and interrupts in RIOT.
@@ -47,7 +48,6 @@ int main(void)
4748
.callback = timer_callback,
4849
.arg = "3 seconds have passed!"
4950
};
50-
}
5151
```
5252

5353
This code creates a timer and initializes it with the callback function we created earlier and a message that will be printed when the timer expires.
@@ -71,9 +71,10 @@ instead of constantly checking if the timer has expired. For example, lets use o
7171
![The output](img/timers/01_output.png)
7272
7373
```c
74-
ztimer_sleep(ZTIMER_SEC, 5);
74+
// Sleep for 5 seconds
75+
ztimer_sleep(ZTIMER_SEC, 5);
7576
76-
puts("5 seconds have passed!");
77+
puts("5 seconds have passed!");
7778
```
7879

7980
## Conclusion

0 commit comments

Comments
 (0)