epdconfig.py 5.1 KB

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