epdif.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @filename : epdif.cpp
  3. * @brief : Implements EPD interface functions
  4. * Users have to implement all the functions in epdif.cpp
  5. * @author : Yehui from Waveshare
  6. *
  7. * Copyright (C) Waveshare August 10 2017
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documnetation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "epdif.h"
  28. #include <SPI.h>
  29. EpdIf::EpdIf() {
  30. };
  31. EpdIf::~EpdIf() {
  32. };
  33. void EpdIf::DigitalWrite(int pin, int value) {
  34. digitalWrite(pin, value);
  35. }
  36. int EpdIf::DigitalRead(int pin) {
  37. return digitalRead(pin);
  38. }
  39. void EpdIf::DelayMs(unsigned int delaytime) {
  40. delay(delaytime);
  41. }
  42. void EpdIf::SpiTransfer(unsigned char data) {
  43. digitalWrite(CS_PIN, LOW);
  44. SPI.transfer(data);
  45. digitalWrite(CS_PIN, HIGH);
  46. }
  47. void EpdIf::EPD_GPIO_Init()
  48. {
  49. SPI.end();
  50. }
  51. void EpdIf::EPD_SPI_Init()
  52. {
  53. SPI.begin();
  54. SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
  55. }
  56. void EpdIf::EPD_Mode(int pin, char mode) {
  57. if(mode == 0)
  58. pinMode(pin, INPUT);
  59. else
  60. pinMode(pin, OUTPUT);
  61. }
  62. void EpdIf::EPD_SendData(unsigned char data) {
  63. unsigned char i,j=data;
  64. EPD_Mode(MOSI_PIN, 1);
  65. EPD_Mode(SCLK_PIN, 1);
  66. digitalWrite(CS_PIN, 0);
  67. for(i = 0; i<8; i++)
  68. {
  69. digitalWrite(SCLK_PIN, 0);
  70. if (j & 0x80)
  71. {
  72. digitalWrite(MOSI_PIN, 1);
  73. }
  74. else
  75. {
  76. digitalWrite(MOSI_PIN, 0);
  77. }
  78. digitalWrite(SCLK_PIN, 1);
  79. j = j << 1;
  80. // DelayMs(1);
  81. }
  82. digitalWrite(SCLK_PIN, 0);
  83. digitalWrite(CS_PIN, 1);
  84. }
  85. unsigned char EpdIf::EPD_ReadData()
  86. {
  87. unsigned char i,j=0xff;
  88. EPD_Mode(MOSI_PIN, 0);
  89. EPD_Mode(SCLK_PIN, 1);
  90. digitalWrite(CS_PIN, 0);
  91. for(i = 0; i<8; i++)
  92. {
  93. digitalWrite(SCLK_PIN, 0);
  94. j = j << 1;
  95. if (DigitalRead(MOSI_PIN))
  96. {
  97. j = j | 0x01;
  98. }
  99. else
  100. {
  101. j= j & 0xfe;
  102. }
  103. digitalWrite(SCLK_PIN, 1);
  104. }
  105. digitalWrite(SCLK_PIN, 0);
  106. digitalWrite(CS_PIN, 1);
  107. return j;
  108. }
  109. int EpdIf::IfInit(void) {
  110. pinMode(CS_PIN, OUTPUT);
  111. pinMode(RST_PIN, OUTPUT);
  112. pinMode(DC_PIN, OUTPUT);
  113. pinMode(BUSY_PIN, INPUT);
  114. pinMode(PWR_PIN, OUTPUT);
  115. DigitalWrite(PWR_PIN, 1);
  116. SPI.begin();
  117. SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
  118. return 0;
  119. }