Skip to content

Commit 7d08bc2

Browse files
committed
docs/pyboard/tutorial: Add tutorial for LCD160CR.
1 parent cffe00d commit 7d08bc2

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

docs/pyboard/tutorial/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Tutorials requiring extra components
3535
fading_led.rst
3636
lcd_skin.rst
3737
amp_skin.rst
38+
lcd160cr_skin.rst
3839

3940
Tips, tricks and useful things to know
4041
--------------------------------------
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
The LCD160CR skin
2+
=================
3+
4+
This tutorial shows how to get started using the LCD160CR skin.
5+
6+
.. image:: http://micropython.org/resources/LCD160CRv10-persp.jpg
7+
:alt: LCD160CRv1.0 picture
8+
:width: 640px
9+
10+
For detailed documentation of the driver for the display see the
11+
:mod:`lcd160cr` module.
12+
13+
Plugging in the display
14+
-----------------------
15+
16+
The display can be plugged directly into a pyboard (all pyboard versions
17+
are supported). You plug the display onto the top of the pyboard either
18+
in the X or Y positions. The display should cover half of the pyboard.
19+
20+
Getting the driver
21+
------------------
22+
23+
You can control the display directly using a power/enable pin and an I2C
24+
bus, but it is much more convenient to use the driver provided by the
25+
:mod:`lcd160cr` module. This driver is included in recent version of the
26+
pyboard firmware (see `here <http://micropython.org/download>`__). You
27+
can also find the driver in the GitHub repository
28+
`here <https://github.com/micropython/micropython/blob/master/drivers/display/lcd160cr.py>`__, and to use this version you will need to copy the file to your
29+
board, into a directory that is searched by import (usually the lib/
30+
directory).
31+
32+
Once you have the driver installed you need to import it to use it::
33+
34+
import lcd160cr
35+
36+
Testing the display
37+
-------------------
38+
39+
There is a test program which you can use to test the features of the display,
40+
and which also serves as a basis to start creating your own code that uses the
41+
LCD. This test program is included in recent versions of the pyboard firmware
42+
and is also available on GitHub
43+
`here <https://github.com/micropython/micropython/blob/master/drivers/display/lcd160cr_test.py>`__.
44+
45+
To run the test from the MicroPython prompt do::
46+
47+
>>> import lcd160cr_test
48+
49+
It will then print some brief instructions. You will need to know which
50+
position your display is connected to (X or Y) and then you can run (assuming
51+
you have the display on position X)::
52+
53+
>>> test_all('X')
54+
55+
Drawing some graphics
56+
---------------------
57+
58+
You must first create an LCD160CR object which will control the display. Do this
59+
using::
60+
61+
>>> import lcd160cr
62+
>>> lcd = lcd160cr.LCD160CR('X')
63+
64+
This assumes your display is connected in the X position. If it's in the Y
65+
position then use ``lcd = lcd160cr.LCD160CR('Y')`` instead.
66+
67+
To erase the screen and draw a line, try::
68+
69+
>>> lcd.set_pen(lcd.rgb(255, 0, 0), lcd.rgb(64, 64, 128))
70+
>>> lcd.erase()
71+
>>> lcd.line(10, 10, 50, 80)
72+
73+
The next example draws random rectangles on the screen. You can copy-and-paste it
74+
into the MicroPython prompt by first pressing "Ctrl-E" at the prompt, then "Ctrl-D"
75+
once you have pasted the text. ::
76+
77+
from random import randint
78+
for i in range(1000):
79+
fg = lcd.rgb(randint(128, 255), randint(128, 255), randint(128, 255))
80+
bg = lcd.rgb(randint(0, 128), randint(0, 128), randint(0, 128))
81+
lcd.set_pen(fg, bg)
82+
lcd.rect(randint(0, lcd.w), randint(0, lcd.h), randint(10, 40), randint(10, 40))
83+
84+
Using the touch sensor
85+
----------------------
86+
87+
The display includes a resistive touch sensor that can report the position (in
88+
pixels) of a single force-based touch on the screen. To see if there is a touch
89+
on the screen use::
90+
91+
>>> lcd.is_touched()
92+
93+
This will return either ``False`` or ``True``. Run the above command while touching
94+
the screen to see the result.
95+
96+
To get the location of the touch you can use the method::
97+
98+
>>> lcd.get_touched()
99+
100+
This will return a 3-tuple, with the first entry being 0 or 1 depending on whether
101+
there is currently anything touching the screen (1 if there is), and the second and
102+
third entries in the tuple being the x and y coordinates of the current (or most
103+
recent) touch.
104+
105+
Directing the MicroPython output to the display
106+
-----------------------------------------------
107+
108+
The display supports input from a UART and implements basic VT100 commands, which
109+
means it can be used as a simple, general purpose terminal. Let's set up the
110+
pyboard to redirect its output to the display.
111+
112+
First you need to create a UART object::
113+
114+
>>> import pyb
115+
>>> uart = pyb.UART('XA', 115200)
116+
117+
This assumes your display is connected to position X. If it's on position Y then
118+
use ``uart = pyb.UART('YA', 115200)`` instead.
119+
120+
Now, connect the REPL output to this UART::
121+
122+
>>> pyb.repl_uart(uart)
123+
124+
From now on anything you type at the MicroPython prompt, and any output you
125+
receive, will appear on the display.
126+
127+
No set-up commands are required for this mode to work and you can use the display
128+
to monitor the output of any UART, not just from the pyboard. All that is needed
129+
is for the display to have power, ground and the power/enable pin driven high.
130+
Then any characters on the display's UART input will be printed to the screen.
131+
You can adjust the UART baudrate from the default of 115200 using the
132+
`set_uart_baudrate` method.

0 commit comments

Comments
 (0)