Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Commit 99e5998f authored by Mark Yoder's avatar Mark Yoder
Browse files

Switching to Python/Javascript tabs

parent 069b84c5
Branches
Tags
No related merge requests found
......@@ -970,12 +970,12 @@ Finally, add the code in :ref:`py_onewire__code` in to a
file named *w1.py*, edit the path assigned to *w1* so
that the path points to your device, and then run it.
.. _py_onewire__code:
.. tabs::
.. group-tab:: Python
.. _py_onewire__code:
.. literalinclude:: ../code/02sensors/w1.py
:caption: Reading a temperature with a DS18B20 (w1.py)
:language: python
......
.. _beaglebone-cookbook-displays:
Displays and Other Outputs
###########################
.. |I2C| replace:: I\ :sup:`2`\ C
.. |kohm| replace:: kΩ
.. |ohm| replace:: Ω
Displays and Other Outputs
###########################
.. |I2C| replace:: I\ :sup:`2`\ C
.. |kohm| replace:: kΩ
.. |ohm| replace:: Ω
In this chapter, you will learn how to control physical hardware via
BeagleBone Black's general-purpose input/output (GPIO) pins. The Bone has
65 GPIO pins that are brought out on two 46-pin headers, called
......@@ -14,7 +14,7 @@ BeagleBone Black's general-purpose input/output (GPIO) pins. The Bone has
.. note::
All the examples in the book assume you have cloned the Cookbook
repository on git.beagleboard.org. Go here :ref:`basics_repo` for instructions.
repository on git.beagleboard.org. Go here :ref:`basics_repo` for instructions.
.. _js_P8P9_fig:
......@@ -55,32 +55,40 @@ through *USR3*, but we'll refer to them as the *USER* LEDs.
The four *USER* LEDs
Place the code shown in :ref:`py_internLED_code` in a file called ``internLED.py``.
Place the code shown in :ref:`py_internLED_code` in a file called ``internLED.py``.
You can do this using VSC to edit files (as shown in :ref:`basics_vsc`) or with
a more traditional editor (as shown in :ref:`tips_editing_files`).
.. _py_internLED_code:
.. tabs::
.. literalinclude:: ../code/03displays/internLED.py
:caption: Using an internal LED (internLED.py)
:linenos:
.. group-tab:: Python
.. _py_internLED_code:
:download:`internLED.py <../code/03displays/internLED.py>`
.. literalinclude:: ../code/03displays/internLED.py
:caption: Using an internal LED (internLED.py)
:language: python
:linenos:
.. _c_internLED_code:
:download:`internLED.py <../code/03displays/internLED.py>`
.. literalinclude:: ../code/03displays/internLED.c
:caption: Using an internal LED (internLED.c)
:linenos:
.. group-tab:: C
.. _c_internLED_code:
:download:`internLED.c <../code/03displays/internLED.c>`
.. literalinclude:: ../code/03displays/internLED.c
:caption: Using an internal LED (internLED.c)
:language: c
:linenos:
:download:`internLED.c <../code/03displays/internLED.c>`
In the *bash* command window, enter the following commands:
.. code-block:: bash
bone$ cd ~/beaglebone-cookbook-code/03displays
bone$ ./internLED.py
bone$ ./internLED.py
The *USER0* LED should now be flashing.
......@@ -101,14 +109,14 @@ Connect an LED to one of the GPIO pins using a series resistor
to limit the current. To make this recipe, you will need:
* Breadboard and jumper wires.
* 220 |ohm| to 470 |ohm| resistor.
* 220 |ohm| to 470 |ohm| resistor.
* LED
.. WARNING::
The value of the current limiting resistor depends on the LED you are using.
The Bone can drive only 4 to 6 mA, so you might need a larger resistor to keep
from pulling too much current. A 330 |ohm| or 470 |ohm| resistor might be better.
from pulling too much current. A 330 |ohm| or 470 |ohm| resistor might be better.
:ref:`displays_externLED_fig` shows how you can wire the LED to pin 14 of
the *P9* header (*P9_14*). Every circuit in this book (:ref:`basics_wire_breadboard`)
......@@ -126,25 +134,33 @@ the LED. The _short_ lead always goes to ground.
After you've wired it, start VSC (see :ref:`basics_vsc`)
and find the code shown in :ref:`py_externLED_code`.
Notice that it looks very similar to the *internLED* code, in fact it only
differs in the line number (18 instead of 21). The built-in LEDs use the same
GPIO interface as the GPIO pins.
Notice that it looks very similar to the *internLED* code, in fact it only
differs in the line number (18 instead of 21). The built-in LEDs use the same
GPIO interface as the GPIO pins.
.. _py_externLED_code:
.. tabs::
.. literalinclude:: ../code/03displays/externLED.py
:caption: Code for using an external LED (externLED.py)
:linenos:
.. group-tab:: Python
:download:`externLED.py <../code/03displays/externLED.py>`
.. _py_externLED_code:
.. _c_externLED_code:
.. literalinclude:: ../code/03displays/externLED.py
:caption: Code for using an external LED (externLED.py)
:language: python
:linenos:
.. literalinclude:: ../code/03displays/externLED.c
:caption: Code for using an external LED (externLED.c)
:linenos:
:download:`externLED.py <../code/03displays/externLED.py>`
.. group-tab:: C
:download:`externLED.c <../code/03displays/externLED.c>`
.. _c_externLED_code:
.. literalinclude:: ../code/03displays/externLED.c
:caption: Code for using an external LED (externLED.c)
:language: c
:linenos:
:download:`externLED.c <../code/03displays/externLED.c>`
Save your file and run the code as before (:ref:`displays_onboardLED`).
......@@ -205,21 +221,29 @@ the same circuit as before (:ref:`displays_externLED_fig`). Find the code in
Then run it as before.
.. _py_fadeLED_code:
.. tabs::
.. literalinclude:: ../code/03displays/fadeLED.py
:caption: Code for using an external LED (fadeLED.py)
:linenos:
.. group-tab:: Python
:download:`fadeLED.py <../code/03displays/fadeLED.py>`
.. _py_fadeLED_code:
.. _js_fadeLED_code:
.. literalinclude:: ../code/03displays/fadeLED.py
:caption: Code for using an external LED (fadeLED.py)
:language: python
:linenos:
.. literalinclude:: ../code/03displays/fadeLED.js
:caption: Code for using an external LED (fadeLED.js)
:linenos:
:download:`fadeLED.py <../code/03displays/fadeLED.py>`
.. group-tab:: JavaScript
:download:`fadeLED.js <../code/03displays/fadeLED.js>`
.. _js_fadeLED_code:
.. literalinclude:: ../code/03displays/fadeLED.js
:caption: Code for using an external LED (fadeLED.js)
:language: JavaScript
:linenos:
:download:`fadeLED.js <../code/03displays/fadeLED.js>`
The Bone has several outputs that can be use as pwm's as shown in :ref:`cape-headers-pwm_fig`.
There are three *EHRPWM's* which each has a pair of pwm channels. Each pair must have the same period.
......@@ -243,7 +267,7 @@ The pwm's are accessed through */dev/bone/pwm*
bone$ ls
0 1 2
Here we see three pwmchips that can be used, each has two channels. Explore one.
Here we see three pwmchips that can be used, each has two channels. Explore one.
.. code-block:: bash
......@@ -254,9 +278,9 @@ Here we see three pwmchips that can be used, each has two channels. Explore one.
bone$ ls
capture duty_cycle enable period polarity power uevent
Here is where you can set the period and duty_cycle (in ns) and enable the pwm.
Attach in LED to P9_14 and if you set the period long enough you can see the LED flash.
Here is where you can set the period and duty_cycle (in ns) and enable the pwm.
Attach in LED to P9_14 and if you set the period long enough you can see the LED flash.
.. code-block:: bash
......@@ -307,7 +331,7 @@ This solution uses an `Adafruit Bicolor 8x8 LED Square Pixel Matrix w/|I2C| Back
To make this recipe, you will need:
* Breadboard and jumper wires
* Two 4.7 |kohm| resistors.
* Two 4.7 |kohm| resistors.
* |I2C| LED matrix
The LED matrix is a 5 V device, but you can drive it from 3.3 V. Wire, as shown in :ref:`displays_i2cMatrix_fig`.
......@@ -343,7 +367,7 @@ Using |I2C| command-line tools to discover the address of the display
70: 70 -- -- -- -- -- -- --
Here, you can see a device at *0x49* and *0x70*. I know I have a temperature
sensor at *0x49*, so the LED matrix must be at *0x70*.
sensor at *0x49*, so the LED matrix must be at *0x70*.
Find the code in :ref:`displays_matrix_i2c` and run it by using the following command:
......@@ -357,25 +381,25 @@ Find the code in :ref:`displays_matrix_i2c` and run it by using the following co
LED matrix display (matrixLEDi2c.py)
=====================================
.. literalinclude:: ../code/03displays/matrixLEDi2c.py
:caption: LED matrix display (matrixLEDi2c.py)
:linenos:
.. literalinclude:: ../code/03displays/matrixLEDi2c.py
:caption: LED matrix display (matrixLEDi2c.py)
:linenos:
:download:`matrixLEDi2c.py <../code/03displays/matrixLEDi2c.py>`
:download:`matrixLEDi2c.py <../code/03displays/matrixLEDi2c.py>`
.. annotations::
.. annotations::
<1> This line states which bus to use. The last digit gives the |I2C| bus number.
<1> This line states which bus to use. The last digit gives the |I2C| bus number.
<2> This specifies the address of the LED matrix, *0x70* in our case.
<2> This specifies the address of the LED matrix, *0x70* in our case.
<3> This indicates which LEDs to turn on. The first byte is for the first column of ``green`` LEDs. In this case, all are turned off. The next byte is for the first column of ``red`` LEDs. The hex *0x3c* number is *0b00111100* in binary. This means the first two red LEDs are off, the next four are on, and the last two are off. The next byte (*0x00*) says the second column of *green* LEDs are all off, the fourth byte (*0x42* = *0b01000010*) says just two *red* LEDs are on, and so on. Declarations define four different patterns to display on the LED matrix, the last being all turned off.
<3> This indicates which LEDs to turn on. The first byte is for the first column of ``green`` LEDs. In this case, all are turned off. The next byte is for the first column of ``red`` LEDs. The hex *0x3c* number is *0b00111100* in binary. This means the first two red LEDs are off, the next four are on, and the last two are off. The next byte (*0x00*) says the second column of *green* LEDs are all off, the fourth byte (*0x42* = *0b01000010*) says just two *red* LEDs are on, and so on. Declarations define four different patterns to display on the LED matrix, the last being all turned off.
<4> Send three commands to the matrix to get it ready to display.
<4> Send three commands to the matrix to get it ready to display.
<5> Now, we are ready to display the various patterns. After each pattern is displayed, we sleep a certain amount of time so that the pattern can be seen.
<5> Now, we are ready to display the various patterns. After each pattern is displayed, we sleep a certain amount of time so that the pattern can be seen.
<6> Finally, send commands to the LED matrix to set the brightness. This makes the display fade out and back in again.
<6> Finally, send commands to the LED matrix to set the brightness. This makes the display fade out and back in again.
.. _displays_drive5V:
......@@ -500,6 +524,7 @@ Then add the code from :ref:`speak_code` in a file called ``speak.js`` and run.
.. literalinclude:: ../code/03displays/speak.js
:caption: A program that talks (speak.js)
:language: JavaScript
:linenos:
:download:`speak.js <../code/03displays/speak.js>`
......
.. tabs::
.. group-tab:: Commands
.. code-block:: shell-session
.. group-tab:: Python
.. _py_internLED_code:
bone$ sudo apt update
bone$ sudo apt upgrade
bone$ sudo apt install -y \
make git wget \
doxygen graphviz librsvg2-bin\
texlive-latex-base texlive-latex-extra latexmk texlive-fonts-recommended \
python3 python3-pip \
python3-sphinx python3-sphinx-rtd-theme python3-sphinxcontrib.svg2pdfconverter \
python3-pil \
imagemagick-6.q16 librsvg2-bin webp \
texlive-full texlive-latex-extra texlive-fonts-extra \
fonts-freefont-otf fonts-dejavu fonts-dejavu-extra fonts-freefont-ttf
bone$ python3 -m pip install --upgrade pip
bone$ pip install -U sphinx_design
bone$ pip install -U sphinxcontrib-images
bone$ pip install -U sphinx-serve
.. literalinclude:: ../code/03displays/internLED.py
:caption: Using an internal LED (internLED.py)
:language: python
:linenos:
.. group-tab:: Shell Output
.. code-block:: shell-session
bone$ git remote add upstream https://git.beagleboard.org/docs/docs.beagleboard.io.git
bone$ git fetch upstream
bone$ git pull upstream main
\ No newline at end of file
:download:`internLED.py <../code/03displays/internLED.py>`
.. group-tab:: C
.. _c_internLED_code:
.. literalinclude:: ../code/03displays/internLED.c
:caption: Using an internal LED (internLED.c)
:language: c
:linenos:
:download:`internLED.c <../code/03displays/internLED.c>`
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment