add toggleButton example
This commit is contained in:
@@ -2,7 +2,7 @@ Add table of contents
|
||||
|
||||
## Blinky
|
||||
|
||||
The classic, helloWorld of embedded programming. This program turns the LED on and off periodically.
|
||||
The classic, helloWorld of embedded programming. This program turns the onboard LED on and off periodically.
|
||||
|
||||
### Initial steps:
|
||||
- Create new project with project generator.
|
||||
@@ -12,6 +12,7 @@ The classic, helloWorld of embedded programming. This program turns the LED on a
|
||||
|
||||
<img src="https://c.l3n.co/i/OW219o.png" width="300">
|
||||
|
||||
Copy the below code into the project, build and upload to the pico.
|
||||
### Basic Code:
|
||||
|
||||
``` C
|
||||
@@ -71,7 +72,7 @@ sleep_ms(500);
|
||||
|
||||
This program blinks an LED connected to one of the GPIO(General Purpose Input/Output) ports.
|
||||
|
||||
For this example, I used Pin 20 == GP15. I connected a resistor to this pin and connected an LED in series with the resistor to the ground, as shown in the image below. This resistor limits the amount of current drawn by the LED. There are other ways to limit the current, which are discussed in this document later.
|
||||
For this example, I used Pin 20(GP15). I connected a resistor to this pin and connected an LED in series with the resistor to the ground, as shown in the image below. This resistor limits the amount of current drawn by the LED. There are other ways to limit the current, which are discussed in this document later.
|
||||
|
||||
```c
|
||||
const uint LEDPin = 15;
|
||||
@@ -90,13 +91,13 @@ while (true)
|
||||
}
|
||||
```
|
||||
|
||||
The only change here is in line 1, `PICO_DEFAULT_LED_PIN` is changed to `15`, to represent GP15. Remember: in the program, a GPIO pin is represented by the GP number and NOT by the pin number, as shown in the [Pico's Pinout](https://pico.pinout.xyz/).
|
||||
The only change here is in line 1, `PICO_DEFAULT_LED_PIN` is changed to `15`, to represent GP15. Remember: in the program, a GPIO pin in the program is represented by the GP number and NOT by the pin number, as shown in the [Pico's Pinout](https://pico.pinout.xyz/).
|
||||
|
||||
---
|
||||
|
||||
## Multiple Blinky
|
||||
|
||||
This program controls multiple LED connected to various GPIO ports. Here, the program is written such that it looks like a loading bar. For this setup, internal current limits are used. They are toggled programatically as shown in the code below.
|
||||
This program controls multiple LED connected to various GPIO ports. Here, the program is written such that it resembles a loading bar. For this setup, internal current limits are used. They are toggled programatically as shown in the code below.
|
||||
|
||||
```c
|
||||
|
||||
@@ -134,9 +135,11 @@ sleep_ms(500);
|
||||
|
||||
The rest of the code is similar to the prior examples, which toggles the LEDS one by one, in series and turns them off, creating a loading effect. Visual output can be seen below.
|
||||
|
||||
add gif of above
|
||||
|
||||
## ToggleBlinky
|
||||
|
||||
This example uses `gpio_get_out_level` to to get the output level for gicen given gpio port. The above example can be simplified by using this function call, as below. This function will be covered in DigitalIn.md as well.
|
||||
This example uses `gpio_get_out_level` to to get the output level for given given gpio port. The above example can be simplified using this function call, as below. This function will be covered in DigitalIn.md as well.
|
||||
|
||||
```c
|
||||
int LED1 = 11;
|
||||
@@ -166,7 +169,7 @@ This example uses `gpio_get_out_level` to to get the output level for gicen give
|
||||
|
||||
## ParallelBlinky
|
||||
|
||||
In this example, the init and set dir operations are performed simultaneously using `gpio_init_mask` and `gpio_set_dir_masked` calls. These take a integer value called mask, where each bit represents one GPIO pin. The above example can be simplified and replicated with the code below.
|
||||
In this example, the init and set dir operations are performed simultaneously using `gpio_init_mask` and `gpio_set_dir_masked` calls. These take a integer value called mask, where each bit of that integer represents one GPIO pin. The above example can be simplified and replicated with the code below.
|
||||
|
||||
```c
|
||||
int LED1 = 11;
|
||||
@@ -190,11 +193,14 @@ while (true)
|
||||
sleep_ms(500);
|
||||
```
|
||||
|
||||
Here, the `pinMask` variable represents the gpio pins. The position from the right represents the the gpio pin with that number and putting a `1` in that position means it is highlighted. So the 0th bit maps to GP0, 1st bit maps to GP1 and so on. We need GP11 to GP15, so the bits in 11th to 15th positions are set to `1`, denoting they are highlighted.
|
||||
`gpio_init_mask` will initialize only the pins highlighted in the function's input, which is its first argument `pinMask`.
|
||||
`gpio_set_dir_masked` will set the IO direction of only the highlighted pins in its first argument `pinMask` to the direction specified in its second argument `dirMask`. In dirMask, `1` denoted output and `0` denotes input.
|
||||
Here, the `pinMask` variable represents the gpio pins. The position number from the right represents GP# with that number. A `1` in a position means the pin represented by that position is highlighted. So the 0th bit maps to GP0, 1st bit maps to GP1 and so on. We need GP11 to GP15, so the bits in 11th to 15th positions are set to `1`, making them highlighted.
|
||||
|
||||
For this example, the init mask functions will initialize GP11 to GP15, because they are highlighted by `pinMask`. The set dir mask function will focus on GP11 to GP15, because they are highlighted by `pinMask`, take the bits in highlighted locations from `dirMask` and set the direction if they are 1 or 0.
|
||||
`gpio_init_mask` will initialize only the pins highlighted in the function's input.
|
||||
|
||||
|
||||
`gpio_set_dir_masked` will set the IO direction of only the highlighted pins in its first argument to the direction specified in its second argument `dirMask`. In dirMask, `1` denoted output and `0` denotes input.
|
||||
|
||||
For this example, the init mask functions will initialize GP11 to GP15, because they are highlighted by `pinMask`. The set dir mask function will focus on GP11 to GP15, because they are highlighted by `pinMask`, and set the direction if they are 1 or 0 based on `dirMask`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user