epdconfig.py 4.8 KB

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