epdconfig.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # /*****************************************************************************
  2. # * | File : epdconfig.py
  3. # * | Author : Waveshare team
  4. # * | Function : Hardware underlying interface
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2019-06-21
  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. class RaspberryPi:
  34. # Pin definition
  35. RST_PIN = 17
  36. DC_PIN = 25
  37. CS_PIN = 8
  38. BUSY_PIN = 24
  39. def __init__(self):
  40. import spidev
  41. import RPi.GPIO
  42. self.GPIO = RPi.GPIO
  43. # SPI device, bus = 0, device = 0
  44. self.SPI = spidev.SpiDev(0, 0)
  45. def digital_write(self, pin, value):
  46. self.GPIO.output(pin, value)
  47. def digital_read(self, pin):
  48. return self.GPIO.input(pin)
  49. def delay_ms(self, delaytime):
  50. time.sleep(delaytime / 1000.0)
  51. def spi_writebyte(self, data):
  52. self.SPI.writebytes(data)
  53. def module_init(self):
  54. self.GPIO.setmode(self.GPIO.BCM)
  55. self.GPIO.setwarnings(False)
  56. self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
  57. self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
  58. self.GPIO.setup(self.CS_PIN, self.GPIO.OUT)
  59. self.GPIO.setup(self.BUSY_PIN, self.GPIO.IN)
  60. self.SPI.max_speed_hz = 4000000
  61. self.SPI.mode = 0b00
  62. return 0
  63. def module_exit(self):
  64. logging.debug("spi end")
  65. self.SPI.close()
  66. logging.debug("close 5V, Module enters 0 power consumption ...")
  67. self.GPIO.output(self.RST_PIN, 0)
  68. self.GPIO.output(self.DC_PIN, 0)
  69. self.GPIO.cleanup()
  70. class JetsonNano:
  71. # Pin definition
  72. RST_PIN = 17
  73. DC_PIN = 25
  74. CS_PIN = 8
  75. BUSY_PIN = 24
  76. def __init__(self):
  77. import ctypes
  78. find_dirs = [
  79. os.path.dirname(os.path.realpath(__file__)),
  80. '/usr/local/lib',
  81. '/usr/lib',
  82. ]
  83. self.SPI = None
  84. for find_dir in find_dirs:
  85. so_filename = os.path.join(find_dir, 'sysfs_software_spi.so')
  86. if os.path.exists(so_filename):
  87. self.SPI = ctypes.cdll.LoadLibrary(so_filename)
  88. break
  89. if self.SPI is None:
  90. raise RuntimeError('Cannot find sysfs_software_spi.so')
  91. import Jetson.GPIO
  92. self.GPIO = Jetson.GPIO
  93. def digital_write(self, pin, value):
  94. self.GPIO.output(pin, value)
  95. def digital_read(self, pin):
  96. return self.GPIO.input(self.BUSY_PIN)
  97. def delay_ms(self, delaytime):
  98. time.sleep(delaytime / 1000.0)
  99. def spi_writebyte(self, data):
  100. self.SPI.SYSFS_software_spi_transfer(data[0])
  101. def module_init(self):
  102. self.GPIO.setmode(self.GPIO.BCM)
  103. self.GPIO.setwarnings(False)
  104. self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
  105. self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
  106. self.GPIO.setup(self.CS_PIN, self.GPIO.OUT)
  107. self.GPIO.setup(self.BUSY_PIN, self.GPIO.IN)
  108. self.SPI.SYSFS_software_spi_begin()
  109. return 0
  110. def module_exit(self):
  111. logging.debug("spi end")
  112. self.SPI.SYSFS_software_spi_end()
  113. logging.debug("close 5V, Module enters 0 power consumption ...")
  114. self.GPIO.output(self.RST_PIN, 0)
  115. self.GPIO.output(self.DC_PIN, 0)
  116. self.GPIO.cleanup()
  117. if os.path.exists('/sys/bus/platform/drivers/gpiomem-bcm2835'):
  118. implementation = RaspberryPi()
  119. else:
  120. implementation = JetsonNano()
  121. for func in [x for x in dir(implementation) if not x.startswith('_')]:
  122. setattr(sys.modules[__name__], func, getattr(implementation, func))
  123. ### END OF FILE ###