lcd160cr_skin.rst 4.9 KB

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