epdconfig.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # /*****************************************************************************
  2. # * | File : epdconfig.py
  3. # * | Author : Waveshare team
  4. # * | Function : Hardware underlying interface
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.2
  8. # * | Date : 2022-10-29
  9. # * | Info :
  10. # ******************************************************************************
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. import os
  30. import logging
  31. import sys
  32. import time
  33. import subprocess
  34. logger = logging.getLogger(__name__)
  35. class RaspberryPi:
  36. # Pin definition
  37. RST_PIN = 17
  38. DC_PIN = 25
  39. CS_PIN = 8
  40. BUSY_PIN = 24
  41. PWR_PIN = 18
  42. def __init__(self):
  43. import spidev
  44. import gpiozero
  45. self.SPI = spidev.SpiDev()
  46. self.GPIO_RST_PIN = gpiozero.LED(self.RST_PIN)
  47. self.GPIO_DC_PIN = gpiozero.LED(self.DC_PIN)
  48. # self.GPIO_CS_PIN = gpiozero.LED(self.CS_PIN)
  49. self.GPIO_PWR_PIN = gpiozero.LED(self.PWR_PIN)
  50. self.GPIO_BUSY_PIN = gpiozero.Button(self.BUSY_PIN, pull_up = False)
  51. def digital_write(self, pin, value):
  52. if pin == self.RST_PIN:
  53. if value:
  54. self.GPIO_RST_PIN.on()
  55. else:
  56. self.GPIO_RST_PIN.off()
  57. elif pin == self.DC_PIN:
  58. if value:
  59. self.GPIO_DC_PIN.on()
  60. else:
  61. self.GPIO_DC_PIN.off()
  62. # elif pin == self.CS_PIN:
  63. # if value:
  64. # self.GPIO_CS_PIN.on()
  65. # else:
  66. # self.GPIO_CS_PIN.off()
  67. elif pin == self.PWR_PIN:
  68. if value:
  69. self.GPIO_PWR_PIN.on()
  70. else:
  71. self.GPIO_PWR_PIN.off()
  72. def digital_read(self, pin):
  73. if pin == self.BUSY_PIN:
  74. return self.GPIO_BUSY_PIN.value
  75. elif pin == self.RST_PIN:
  76. return self.RST_PIN.value
  77. elif pin == self.DC_PIN:
  78. return self.DC_PIN.value
  79. # elif pin == self.CS_PIN:
  80. # return self.CS_PIN.value
  81. elif pin == self.PWR_PIN:
  82. return self.PWR_PIN.value
  83. def delay_ms(self, delaytime):
  84. time.sleep(delaytime / 1000.0)
  85. def spi_writebyte(self, data):
  86. self.SPI.writebytes(data)
  87. def spi_writebyte2(self, data):
  88. self.SPI.writebytes2(data)
  89. def module_init(self):
  90. self.GPIO_PWR_PIN.on()
  91. # SPI device, bus = 0, device = 0
  92. self.SPI.open(0, 0)
  93. self.SPI.max_speed_hz = 4000000
  94. self.SPI.mode = 0b00
  95. return 0
  96. def module_exit(self):
  97. logger.debug("spi end")
  98. self.SPI.close()
  99. self.GPIO_RST_PIN.off()
  100. self.GPIO_DC_PIN.off()
  101. self.GPIO_PWR_PIN.off()
  102. self.GPIO_RST_PIN.close()
  103. self.GPIO_DC_PIN.close()
  104. # self.GPIO_CS_PIN.close()
  105. self.GPIO_PWR_PIN.close()
  106. self.GPIO_BUSY_PIN.close()
  107. logger.debug("close 5V, Module enters 0 power consumption ...")
  108. class JetsonNano:
  109. # Pin definition
  110. RST_PIN = 17
  111. DC_PIN = 25
  112. CS_PIN = 8
  113. BUSY_PIN = 24
  114. PWR_PIN = 18
  115. def __init__(self):
  116. import ctypes
  117. find_dirs = [
  118. os.path.dirname(os.path.realpath(__file__)),
  119. '/usr/local/lib',
  120. '/usr/lib',
  121. ]
  122. self.SPI = None
  123. for find_dir in find_dirs:
  124. so_filename = os.path.join(find_dir, 'sysfs_software_spi.so')
  125. if os.path.exists(so_filename):
  126. self.SPI = ctypes.cdll.LoadLibrary(so_filename)
  127. break
  128. if self.SPI is None:
  129. raise RuntimeError('Cannot find sysfs_software_spi.so')
  130. import Jetson.GPIO
  131. self.GPIO = Jetson.GPIO
  132. def digital_write(self, pin, value):
  133. self.GPIO.output(pin, value)
  134. def digital_read(self, pin):
  135. return self.GPIO.input(self.BUSY_PIN)
  136. def delay_ms(self, delaytime):
  137. time.sleep(delaytime / 1000.0)
  138. def spi_writebyte(self, data):
  139. self.SPI.SYSFS_software_spi_transfer(data[0])
  140. def spi_writebyte2(self, data):
  141. for i in range(len(data)):
  142. self.SPI.SYSFS_software_spi_transfer(data[i])
  143. def module_init(self):
  144. self.GPIO.setmode(self.GPIO.BCM)
  145. self.GPIO.setwarnings(False)
  146. self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
  147. self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
  148. self.GPIO.setup(self.CS_PIN, self.GPIO.OUT)
  149. self.GPIO.setup(self.PWR_PIN, self.GPIO.OUT)
  150. self.GPIO.setup(self.BUSY_PIN, self.GPIO.IN)
  151. self.GPIO.output(self.PWR_PIN, 1)
  152. self.SPI.SYSFS_software_spi_begin()
  153. return 0
  154. def module_exit(self):
  155. logger.debug("spi end")
  156. self.SPI.SYSFS_software_spi_end()
  157. logger.debug("close 5V, Module enters 0 power consumption ...")
  158. self.GPIO.output(self.RST_PIN, 0)
  159. self.GPIO.output(self.DC_PIN, 0)
  160. self.GPIO.output(self.PWR_PIN, 0)
  161. self.GPIO.cleanup([self.RST_PIN, self.DC_PIN, self.CS_PIN, self.BUSY_PIN, self.PWR_PIN])
  162. class SunriseX3:
  163. # Pin definition
  164. RST_PIN = 17
  165. DC_PIN = 25
  166. CS_PIN = 8
  167. BUSY_PIN = 24
  168. PWR_PIN = 18
  169. Flag = 0
  170. def __init__(self):
  171. import spidev
  172. import Hobot.GPIO
  173. self.GPIO = Hobot.GPIO
  174. self.SPI = spidev.SpiDev()
  175. def digital_write(self, pin, value):
  176. self.GPIO.output(pin, value)
  177. def digital_read(self, pin):
  178. return self.GPIO.input(pin)
  179. def delay_ms(self, delaytime):
  180. time.sleep(delaytime / 1000.0)
  181. def spi_writebyte(self, data):
  182. self.SPI.writebytes(data)
  183. def spi_writebyte2(self, data):
  184. # for i in range(len(data)):
  185. # self.SPI.writebytes([data[i]])
  186. self.SPI.xfer3(data)
  187. def module_init(self):
  188. if self.Flag == 0:
  189. self.Flag = 1
  190. self.GPIO.setmode(self.GPIO.BCM)
  191. self.GPIO.setwarnings(False)
  192. self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
  193. self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
  194. self.GPIO.setup(self.CS_PIN, self.GPIO.OUT)
  195. self.GPIO.setup(self.PWR_PIN, self.GPIO.OUT)
  196. self.GPIO.setup(self.BUSY_PIN, self.GPIO.IN)
  197. self.GPIO.output(self.PWR_PIN, 1)
  198. # SPI device, bus = 0, device = 0
  199. self.SPI.open(2, 0)
  200. self.SPI.max_speed_hz = 4000000
  201. self.SPI.mode = 0b00
  202. return 0
  203. else:
  204. return 0
  205. def module_exit(self):
  206. logger.debug("spi end")
  207. self.SPI.close()
  208. logger.debug("close 5V, Module enters 0 power consumption ...")
  209. self.Flag = 0
  210. self.GPIO.output(self.RST_PIN, 0)
  211. self.GPIO.output(self.DC_PIN, 0)
  212. self.GPIO.output(self.PWR_PIN, 0)
  213. self.GPIO.cleanup([self.RST_PIN, self.DC_PIN, self.CS_PIN, self.BUSY_PIN], self.PWR_PIN)
  214. if sys.version_info[0] == 2:
  215. process = subprocess.Popen("cat /proc/cpuinfo | grep Raspberry", shell=True, stdout=subprocess.PIPE)
  216. else:
  217. process = subprocess.Popen("cat /proc/cpuinfo | grep Raspberry", shell=True, stdout=subprocess.PIPE, text=True)
  218. output, _ = process.communicate()
  219. if sys.version_info[0] == 2:
  220. output = output.decode(sys.stdout.encoding)
  221. if "Raspberry" in output:
  222. implementation = RaspberryPi()
  223. elif os.path.exists('/sys/bus/platform/drivers/gpio-x3'):
  224. implementation = SunriseX3()
  225. else:
  226. implementation = JetsonNano()
  227. for func in [x for x in dir(implementation) if not x.startswith('_')]:
  228. setattr(sys.modules[__name__], func, getattr(implementation, func))
  229. ### END OF FILE ###