lcd160cr.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. :mod:`lcd160cr` --- control of LCD160CR display
  2. ===============================================
  3. .. module:: lcd160cr
  4. :synopsis: control of LCD160CR display
  5. This module provides control of the MicroPython LCD160CR display.
  6. .. image:: http://micropython.org/resources/LCD160CRv10-persp.jpg
  7. :alt: LCD160CRv1.0 picture
  8. :width: 640px
  9. Further resources are available via the following links:
  10. * `LCD160CRv1.0 reference manual <http://micropython.org/resources/LCD160CRv10-refmanual.pdf>`_ (100KiB PDF)
  11. * `LCD160CRv1.0 schematics <http://micropython.org/resources/LCD160CRv10-schematics.pdf>`_ (1.6MiB PDF)
  12. class LCD160CR
  13. --------------
  14. The LCD160CR class provides an interface to the display. Create an
  15. instance of this class and use its methods to draw to the LCD and get
  16. the status of the touch panel.
  17. For example::
  18. import lcd160cr
  19. lcd = lcd160cr.LCD160CR('X')
  20. lcd.set_orient(lcd160cr.PORTRAIT)
  21. lcd.set_pos(0, 0)
  22. lcd.set_text_color(lcd.rgb(255, 0, 0), lcd.rgb(0, 0, 0))
  23. lcd.set_font(1)
  24. lcd.write('Hello MicroPython!')
  25. print('touch:', lcd.get_touch())
  26. Constructors
  27. ------------
  28. .. class:: LCD160CR(connect=None, \*, pwr=None, i2c=None, spi=None, i2c_addr=98)
  29. Construct an LCD160CR object. The parameters are:
  30. - *connect* is a string specifying the physical connection of the LCD
  31. display to the board; valid values are "X", "Y", "XY", "YX".
  32. Use "X" when the display is connected to a pyboard in the X-skin
  33. position, and "Y" when connected in the Y-skin position. "XY"
  34. and "YX" are used when the display is connected to the right or
  35. left side of the pyboard, respectively.
  36. - *pwr* is a Pin object connected to the LCD's power/enabled pin.
  37. - *i2c* is an I2C object connected to the LCD's I2C interface.
  38. - *spi* is an SPI object connected to the LCD's SPI interface.
  39. - *i2c_addr* is the I2C address of the display.
  40. One must specify either a valid *connect* or all of *pwr*, *i2c* and *spi*.
  41. If a valid *connect* is given then any of *pwr*, *i2c* or *spi* which are
  42. not passed as parameters (i.e. they are ``None``) will be created based on the
  43. value of *connect*. This allows to override the default interface to the
  44. display if needed.
  45. The default values are:
  46. - "X" is for the X-skin and uses:
  47. ``pwr=Pin("X4")``, ``i2c=I2C("X")``, ``spi=SPI("X")``
  48. - "Y" is for the Y-skin and uses:
  49. ``pwr=Pin("Y4")``, ``i2c=I2C("Y")``, ``spi=SPI("Y")``
  50. - "XY" is for the right-side and uses:
  51. ``pwr=Pin("X4")``, ``i2c=I2C("Y")``, ``spi=SPI("X")``
  52. - "YX" is for the left-side and uses:
  53. ``pwr=Pin("Y4")``, ``i2c=I2C("X")``, ``spi=SPI("Y")``
  54. See `this image <http://micropython.org/resources/LCD160CRv10-positions.jpg>`_
  55. for how the display can be connected to the pyboard.
  56. Static methods
  57. --------------
  58. .. staticmethod:: LCD160CR.rgb(r, g, b)
  59. Return a 16-bit integer representing the given rgb color values. The
  60. 16-bit value can be used to set the font color (see
  61. :meth:`LCD160CR.set_text_color`) pen color (see :meth:`LCD160CR.set_pen`)
  62. and draw individual pixels.
  63. .. staticmethod:: LCD160CR.clip_line(data, w, h):
  64. Clip the given line data. This is for internal use.
  65. Instance members
  66. ----------------
  67. The following instance members are publicly accessible.
  68. .. data:: LCD160CR.w
  69. .. data:: LCD160CR.h
  70. The width and height of the display, respectively, in pixels. These
  71. members are updated when calling :meth:`LCD160CR.set_orient` and should
  72. be considered read-only.
  73. Setup commands
  74. --------------
  75. .. method:: LCD160CR.set_power(on)
  76. Turn the display on or off, depending on the given value of *on*: 0 or ``False``
  77. will turn the display off, and 1 or ``True`` will turn it on.
  78. .. method:: LCD160CR.set_orient(orient)
  79. Set the orientation of the display. The *orient* parameter can be one
  80. of `PORTRAIT`, `LANDSCAPE`, `PORTRAIT_UPSIDEDOWN`, `LANDSCAPE_UPSIDEDOWN`.
  81. .. method:: LCD160CR.set_brightness(value)
  82. Set the brightness of the display, between 0 and 31.
  83. .. method:: LCD160CR.set_i2c_addr(addr)
  84. Set the I2C address of the display. The *addr* value must have the
  85. lower 2 bits cleared.
  86. .. method:: LCD160CR.set_uart_baudrate(baudrate)
  87. Set the baudrate of the UART interface.
  88. .. method:: LCD160CR.set_startup_deco(value)
  89. Set the start-up decoration of the display. The *value* parameter can be a
  90. logical or of `STARTUP_DECO_NONE`, `STARTUP_DECO_MLOGO`, `STARTUP_DECO_INFO`.
  91. .. method:: LCD160CR.save_to_flash()
  92. Save the following parameters to flash so they persist on restart and power up:
  93. initial decoration, orientation, brightness, UART baud rate, I2C address.
  94. Pixel access methods
  95. --------------------
  96. The following methods manipulate individual pixels on the display.
  97. .. method:: LCD160CR.set_pixel(x, y, c)
  98. Set the specified pixel to the given color. The color should be a 16-bit
  99. integer and can be created by :meth:`LCD160CR.rgb`.
  100. .. method:: LCD160CR.get_pixel(x, y)
  101. Get the 16-bit value of the specified pixel.
  102. .. method:: LCD160CR.get_line(x, y, buf)
  103. Low-level method to get a line of pixels into the given buffer.
  104. To read *n* pixels *buf* should be *2*n+1* bytes in length. The first byte
  105. is a dummy byte and should be ignored, and subsequent bytes represent the
  106. pixels in the line starting at coordinate *(x, y)*.
  107. .. method:: LCD160CR.screen_dump(buf, x=0, y=0, w=None, h=None)
  108. Dump the contents of the screen to the given buffer. The parameters *x* and *y*
  109. specify the starting coordinate, and *w* and *h* the size of the region. If *w*
  110. or *h* are ``None`` then they will take on their maximum values, set by the size
  111. of the screen minus the given *x* and *y* values. *buf* should be large enough
  112. to hold ``2*w*h`` bytes. If it's smaller then only the initial horizontal lines
  113. will be stored.
  114. .. method:: LCD160CR.screen_load(buf)
  115. Load the entire screen from the given buffer.
  116. Drawing text
  117. ------------
  118. To draw text one sets the position, color and font, and then uses
  119. `LCD160CR.write` to draw the text.
  120. .. method:: LCD160CR.set_pos(x, y)
  121. Set the position for text output using :meth:`LCD160CR.write`. The position
  122. is the upper-left corner of the text.
  123. .. method:: LCD160CR.set_text_color(fg, bg)
  124. Set the foreground and background color of the text.
  125. .. method:: LCD160CR.set_font(font, scale=0, bold=0, trans=0, scroll=0)
  126. Set the font for the text. Subsequent calls to `write` will use the newly
  127. configured font. The parameters are:
  128. - *font* is the font family to use, valid values are 0, 1, 2, 3.
  129. - *scale* is a scaling value for each character pixel, where the pixels
  130. are drawn as a square with side length equal to *scale + 1*. The value
  131. can be between 0 and 63.
  132. - *bold* controls the number of pixels to overdraw each character pixel,
  133. making a bold effect. The lower 2 bits of *bold* are the number of
  134. pixels to overdraw in the horizontal direction, and the next 2 bits are
  135. for the vertical direction. For example, a *bold* value of 5 will
  136. overdraw 1 pixel in both the horizontal and vertical directions.
  137. - *trans* can be either 0 or 1 and if set to 1 the characters will be
  138. drawn with a transparent background.
  139. - *scroll* can be either 0 or 1 and if set to 1 the display will do a
  140. soft scroll if the text moves to the next line.
  141. .. method:: LCD160CR.write(s)
  142. Write text to the display, using the current position, color and font.
  143. As text is written the position is automatically incremented. The
  144. display supports basic VT100 control codes such as newline and backspace.
  145. Drawing primitive shapes
  146. ------------------------
  147. Primitive drawing commands use a foreground and background color set by the
  148. `set_pen` method.
  149. .. method:: LCD160CR.set_pen(line, fill)
  150. Set the line and fill color for primitive shapes.
  151. .. method:: LCD160CR.erase()
  152. Erase the entire display to the pen fill color.
  153. .. method:: LCD160CR.dot(x, y)
  154. Draw a single pixel at the given location using the pen line color.
  155. .. method:: LCD160CR.rect(x, y, w, h)
  156. .. method:: LCD160CR.rect_outline(x, y, w, h)
  157. .. method:: LCD160CR.rect_interior(x, y, w, h)
  158. Draw a rectangle at the given location and size using the pen line
  159. color for the outline, and the pen fill color for the interior.
  160. The `rect` method draws the outline and interior, while the other methods
  161. just draw one or the other.
  162. .. method:: LCD160CR.line(x1, y1, x2, y2)
  163. Draw a line between the given coordinates using the pen line color.
  164. .. method:: LCD160CR.dot_no_clip(x, y)
  165. .. method:: LCD160CR.rect_no_clip(x, y, w, h)
  166. .. method:: LCD160CR.rect_outline_no_clip(x, y, w, h)
  167. .. method:: LCD160CR.rect_interior_no_clip(x, y, w, h)
  168. .. method:: LCD160CR.line_no_clip(x1, y1, x2, y2)
  169. These methods are as above but don't do any clipping on the input
  170. coordinates. They are faster than the clipping versions and can be
  171. used when you know that the coordinates are within the display.
  172. .. method:: LCD160CR.poly_dot(data)
  173. Draw a sequence of dots using the pen line color.
  174. The *data* should be a buffer of bytes, with each successive pair of
  175. bytes corresponding to coordinate pairs (x, y).
  176. .. method:: LCD160CR.poly_line(data)
  177. Similar to :meth:`LCD160CR.poly_dot` but draws lines between the dots.
  178. Touch screen methods
  179. --------------------
  180. .. method:: LCD160CR.touch_config(calib=False, save=False, irq=None)
  181. Configure the touch panel:
  182. - If *calib* is ``True`` then the call will trigger a touch calibration of
  183. the resistive touch sensor. This requires the user to touch various
  184. parts of the screen.
  185. - If *save* is ``True`` then the touch parameters will be saved to NVRAM
  186. to persist across reset/power up.
  187. - If *irq* is ``True`` then the display will be configured to pull the IRQ
  188. line low when a touch force is detected. If *irq* is ``False`` then this
  189. feature is disabled. If *irq* is ``None`` (the default value) then no
  190. change is made to this setting.
  191. .. method:: LCD160CR.is_touched()
  192. Returns a boolean: ``True`` if there is currently a touch force on the screen,
  193. ``False`` otherwise.
  194. .. method:: LCD160CR.get_touch()
  195. Returns a 3-tuple of: *(active, x, y)*. If there is currently a touch force
  196. on the screen then *active* is 1, otherwise it is 0. The *x* and *y* values
  197. indicate the position of the current or most recent touch.
  198. Advanced commands
  199. -----------------
  200. .. method:: LCD160CR.set_spi_win(x, y, w, h)
  201. Set the window that SPI data is written to.
  202. .. method:: LCD160CR.fast_spi(flush=True)
  203. Ready the display to accept RGB pixel data on the SPI bus, resetting the location
  204. of the first byte to go to the top-left corner of the window set by
  205. :meth:`LCD160CR.set_spi_win`.
  206. The method returns an SPI object which can be used to write the pixel data.
  207. Pixels should be sent as 16-bit RGB values in the 5-6-5 format. The destination
  208. counter will increase as data is sent, and data can be sent in arbitrary sized
  209. chunks. Once the destination counter reaches the end of the window specified by
  210. :meth:`LCD160CR.set_spi_win` it will wrap around to the top-left corner of that window.
  211. .. method:: LCD160CR.show_framebuf(buf)
  212. Show the given buffer on the display. *buf* should be an array of bytes containing
  213. the 16-bit RGB values for the pixels, and they will be written to the area
  214. specified by :meth:`LCD160CR.set_spi_win`, starting from the top-left corner.
  215. The `framebuf <framebuf.html>`_ module can be used to construct frame buffers
  216. and provides drawing primitives. Using a frame buffer will improve
  217. performance of animations when compared to drawing directly to the screen.
  218. .. method:: LCD160CR.set_scroll(on)
  219. Turn scrolling on or off. This controls globally whether any window regions will
  220. scroll.
  221. .. method:: LCD160CR.set_scroll_win(win, x=-1, y=0, w=0, h=0, vec=0, pat=0, fill=0x07e0, color=0)
  222. Configure a window region for scrolling:
  223. - *win* is the window id to configure. There are 0..7 standard windows for
  224. general purpose use. Window 8 is the text scroll window (the ticker).
  225. - *x*, *y*, *w*, *h* specify the location of the window in the display.
  226. - *vec* specifies the direction and speed of scroll: it is a 16-bit value
  227. of the form ``0bF.ddSSSSSSSSSSSS``. *dd* is 0, 1, 2, 3 for +x, +y, -x,
  228. -y scrolling. *F* sets the speed format, with 0 meaning that the window
  229. is shifted *S % 256* pixel every frame, and 1 meaning that the window
  230. is shifted 1 pixel every *S* frames.
  231. - *pat* is a 16-bit pattern mask for the background.
  232. - *fill* is the fill color.
  233. - *color* is the extra color, either of the text or pattern foreground.
  234. .. method:: LCD160CR.set_scroll_win_param(win, param, value)
  235. Set a single parameter of a scrolling window region:
  236. - *win* is the window id, 0..8.
  237. - *param* is the parameter number to configure, 0..7, and corresponds
  238. to the parameters in the `set_scroll_win` method.
  239. - *value* is the value to set.
  240. .. method:: LCD160CR.set_scroll_buf(s)
  241. Set the string for scrolling in window 8. The parameter *s* must be a string
  242. with length 32 or less.
  243. .. method:: LCD160CR.jpeg(buf)
  244. Display a JPEG. *buf* should contain the entire JPEG data. JPEG data should
  245. not include EXIF information. The following encodings are supported: Baseline
  246. DCT, Huffman coding, 8 bits per sample, 3 color components, YCbCr4:2:2.
  247. The origin of the JPEG is set by :meth:`LCD160CR.set_pos`.
  248. .. method:: LCD160CR.jpeg_start(total_len)
  249. .. method:: LCD160CR.jpeg_data(buf)
  250. Display a JPEG with the data split across multiple buffers. There must be
  251. a single call to `jpeg_start` to begin with, specifying the total number of
  252. bytes in the JPEG. Then this number of bytes must be transferred to the
  253. display using one or more calls to the `jpeg_data` command.
  254. .. method:: LCD160CR.feed_wdt()
  255. The first call to this method will start the display's internal watchdog
  256. timer. Subsequent calls will feed the watchdog. The timeout is roughly 30
  257. seconds.
  258. .. method:: LCD160CR.reset()
  259. Reset the display.
  260. Constants
  261. ---------
  262. .. data:: lcd160cr.PORTRAIT
  263. lcd160cr.LANDSCAPE
  264. lcd160cr.PORTRAIT_UPSIDEDOWN
  265. lcd160cr.LANDSCAPE_UPSIDEDOWN
  266. Orientations of the display, used by :meth:`LCD160CR.set_orient`.
  267. .. data:: lcd160cr.STARTUP_DECO_NONE
  268. lcd160cr.STARTUP_DECO_MLOGO
  269. lcd160cr.STARTUP_DECO_INFO
  270. Types of start-up decoration, can be OR'ed together, used by
  271. :meth:`LCD160CR.set_startup_deco`.