config.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # /*****************************************************************************
  2. # * | File : config.py
  3. # * | Author : Waveshare team
  4. # * | Function : Hardware underlying interface,for Raspberry pi
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2020-06-17
  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 time
  30. from smbus import SMBus
  31. import spidev
  32. import ctypes
  33. from gpiozero import *
  34. Device_SPI = 1
  35. Device_I2C = 0
  36. class RaspberryPi:
  37. def __init__(self,spi=spidev.SpiDev(0,0),spi_freq=10000000,rst = 27,dc = 25,bl = 18,bl_freq=1000,i2c=None):
  38. self.INPUT = False
  39. self.OUTPUT = True
  40. self.SPEED =spi_freq
  41. if(Device_SPI == 1):
  42. self.Device = Device_SPI
  43. self.spi = spi
  44. else :
  45. self.Device = Device_I2C
  46. self.address = 0x3c
  47. self.bus = SMBus(1)
  48. self.RST_PIN = self.gpio_mode(rst,self.OUTPUT)
  49. self.DC_PIN = self.gpio_mode(dc,self.OUTPUT)
  50. def delay_ms(self,delaytime):
  51. time.sleep(delaytime / 1000.0)
  52. def gpio_mode(self,Pin,Mode,pull_up = None,active_state = True):
  53. if Mode:
  54. return DigitalOutputDevice(Pin,active_high = True,initial_value =False)
  55. else:
  56. return DigitalInputDevice(Pin,pull_up=pull_up,active_state=active_state)
  57. def digital_write(self, Pin, value):
  58. if value:
  59. Pin.on()
  60. else:
  61. Pin.off()
  62. def digital_read(self, Pin):
  63. return Pin.value
  64. def spi_writebyte(self,data):
  65. self.spi.writebytes([data[0]])
  66. def i2c_writebyte(self,reg, value):
  67. self.bus.write_byte_data(self.address, reg, value)
  68. def module_init(self):
  69. self.digital_write(self.RST_PIN,False)
  70. if(self.Device == Device_SPI):
  71. self.spi.max_speed_hz = self.SPEED
  72. self.spi.mode = 0b11
  73. self.digital_write(self.DC_PIN,False)
  74. return 0
  75. def module_exit(self):
  76. if(self.Device == Device_SPI):
  77. self.spi.close()
  78. else :
  79. self.bus.close()
  80. self.digital_write(self.RST_PIN,False)
  81. self.digital_write(self.DC_PIN,False)
  82. ### END OF FILE ###