seeed_tft.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # This file is part of the MicroPython project, http://micropython.org/
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) 2016 Glenn Ruben Bakke
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. """
  25. MicroPython Seeedstudio TFT Shield V2 driver, SPI interfaces, Analog GPIO
  26. Contains SD-card reader, LCD and Touch sensor
  27. The pca10040 pin layout is used as reference.
  28. Example usage of LCD:
  29. from seeedstudio_tft_shield_v2 import ILI9341
  30. lcd = ILI9341(240, 320)
  31. lcd.text("Hello World!, 32, 32)
  32. lcd.show()
  33. Example usage of SD card reader:
  34. import os
  35. from seeedstudio_tft_shield_v2 import mount_tf
  36. tf = mount_tf()
  37. os.listdir()
  38. """
  39. import os
  40. import time
  41. import framebuf
  42. from machine import SPI, Pin
  43. from sdcard import SDCard
  44. def mount_tf(self, mount_point="/"):
  45. sd = SDCard(SPI(0), Pin("P15", mode=Pin.OUT))
  46. os.mount(sd, mount_point)
  47. class ILI9341:
  48. def __init__(self, width, height):
  49. self.width = width
  50. self.height = height
  51. self.pages = self.height // 8
  52. self.buffer = bytearray(self.pages * self.width)
  53. self.framebuf = framebuf.FrameBuffer(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
  54. self.spi = SPI(0)
  55. # chip select
  56. self.cs = Pin("P16", mode=Pin.OUT, pull=Pin.PULL_UP)
  57. # command
  58. self.dc = Pin("P17", mode=Pin.OUT, pull=Pin.PULL_UP)
  59. # initialize all pins high
  60. self.cs.high()
  61. self.dc.high()
  62. self.spi.init(baudrate=8000000, phase=0, polarity=0)
  63. self.init_display()
  64. def init_display(self):
  65. time.sleep_ms(500)
  66. self.write_cmd(0x01)
  67. time.sleep_ms(200)
  68. self.write_cmd(0xCF)
  69. self.write_data(bytearray([0x00, 0x8B, 0x30]))
  70. self.write_cmd(0xED)
  71. self.write_data(bytearray([0x67, 0x03, 0x12, 0x81]))
  72. self.write_cmd(0xE8)
  73. self.write_data(bytearray([0x85, 0x10, 0x7A]))
  74. self.write_cmd(0xCB)
  75. self.write_data(bytearray([0x39, 0x2C, 0x00, 0x34, 0x02]))
  76. self.write_cmd(0xF7)
  77. self.write_data(bytearray([0x20]))
  78. self.write_cmd(0xEA)
  79. self.write_data(bytearray([0x00, 0x00]))
  80. # Power control
  81. self.write_cmd(0xC0)
  82. # VRH[5:0]
  83. self.write_data(bytearray([0x1B]))
  84. # Power control
  85. self.write_cmd(0xC1)
  86. # SAP[2:0];BT[3:0]
  87. self.write_data(bytearray([0x10]))
  88. # VCM control
  89. self.write_cmd(0xC5)
  90. self.write_data(bytearray([0x3F, 0x3C]))
  91. # VCM control2
  92. self.write_cmd(0xC7)
  93. self.write_data(bytearray([0xB7]))
  94. # Memory Access Control
  95. self.write_cmd(0x36)
  96. self.write_data(bytearray([0x08]))
  97. self.write_cmd(0x3A)
  98. self.write_data(bytearray([0x55]))
  99. self.write_cmd(0xB1)
  100. self.write_data(bytearray([0x00, 0x1B]))
  101. # Display Function Control
  102. self.write_cmd(0xB6)
  103. self.write_data(bytearray([0x0A, 0xA2]))
  104. # 3Gamma Function Disable
  105. self.write_cmd(0xF2)
  106. self.write_data(bytearray([0x00]))
  107. # Gamma curve selected
  108. self.write_cmd(0x26)
  109. self.write_data(bytearray([0x01]))
  110. # Set Gamma
  111. self.write_cmd(0xE0)
  112. self.write_data(bytearray([0x0F, 0x2A, 0x28, 0x08, 0x0E, 0x08, 0x54, 0XA9, 0x43, 0x0A, 0x0F, 0x00, 0x00, 0x00, 0x00]))
  113. # Set Gamma
  114. self.write_cmd(0XE1)
  115. self.write_data(bytearray([0x00, 0x15, 0x17, 0x07, 0x11, 0x06, 0x2B, 0x56, 0x3C, 0x05, 0x10, 0x0F, 0x3F, 0x3F, 0x0F]))
  116. # Exit Sleep
  117. self.write_cmd(0x11)
  118. time.sleep_ms(120)
  119. # Display on
  120. self.write_cmd(0x29)
  121. time.sleep_ms(500)
  122. self.fill(0)
  123. def show(self):
  124. # set col
  125. self.write_cmd(0x2A)
  126. self.write_data(bytearray([0x00, 0x00]))
  127. self.write_data(bytearray([0x00, 0xef]))
  128. # set page
  129. self.write_cmd(0x2B)
  130. self.write_data(bytearray([0x00, 0x00]))
  131. self.write_data(bytearray([0x01, 0x3f]))
  132. self.write_cmd(0x2c);
  133. num_of_pixels = self.height * self.width
  134. for row in range(0, self.pages):
  135. for pixel_pos in range(0, 8):
  136. for col in range(0, self.width):
  137. compressed_pixel = self.buffer[row * 240 + col]
  138. if ((compressed_pixel >> pixel_pos) & 0x1) == 0:
  139. self.write_data(bytearray([0x00, 0x00]))
  140. else:
  141. self.write_data(bytearray([0xFF, 0xFF]))
  142. def fill(self, col):
  143. self.framebuf.fill(col)
  144. def pixel(self, x, y, col):
  145. self.framebuf.pixel(x, y, col)
  146. def scroll(self, dx, dy):
  147. self.framebuf.scroll(dx, dy)
  148. def text(self, string, x, y, col=1):
  149. self.framebuf.text(string, x, y, col)
  150. def write_cmd(self, cmd):
  151. self.dc.low()
  152. self.cs.low()
  153. self.spi.write(bytearray([cmd]))
  154. self.cs.high()
  155. def write_data(self, buf):
  156. self.dc.high()
  157. self.cs.low()
  158. self.spi.write(buf)
  159. self.cs.high()