ST7735Display.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifdef ST7735
  2. #include "ST7735Display.h"
  3. #ifndef DISPLAY_ROTATION
  4. #define DISPLAY_ROTATION 2
  5. #endif
  6. #define SCALE_X 1.25f // 160 / 128
  7. #define SCALE_Y 1.25f // 80 / 64
  8. bool ST7735Display::i2c_probe(TwoWire& wire, uint8_t addr) {
  9. return true;
  10. /*
  11. wire.beginTransmission(addr);
  12. uint8_t error = wire.endTransmission();
  13. return (error == 0);
  14. */
  15. }
  16. bool ST7735Display::begin() {
  17. if (!_isOn) {
  18. if (_peripher_power) _peripher_power->claim();
  19. pinMode(PIN_TFT_LEDA_CTL, OUTPUT);
  20. digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
  21. digitalWrite(PIN_TFT_RST, HIGH);
  22. display.initR(INITR_MINI160x80_PLUGIN);
  23. display.setRotation(DISPLAY_ROTATION);
  24. display.setSPISpeed(40000000);
  25. display.fillScreen(ST77XX_BLACK);
  26. display.setTextColor(ST77XX_WHITE);
  27. display.setTextSize(2);
  28. display.cp437(true); // Use full 256 char 'Code Page 437' font
  29. _isOn = true;
  30. }
  31. return true;
  32. }
  33. void ST7735Display::turnOn() {
  34. ST7735Display::begin();
  35. }
  36. void ST7735Display::turnOff() {
  37. if (_isOn) {
  38. digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
  39. digitalWrite(PIN_TFT_RST, LOW);
  40. digitalWrite(PIN_TFT_LEDA_CTL, LOW);
  41. _isOn = false;
  42. if (_peripher_power) _peripher_power->release();
  43. }
  44. }
  45. void ST7735Display::clear() {
  46. //Serial.println("DBG: display.Clear");
  47. display.fillScreen(ST77XX_BLACK);
  48. }
  49. void ST7735Display::startFrame(Color bkg) {
  50. display.fillScreen(0x00);
  51. display.setTextColor(ST77XX_WHITE);
  52. display.setTextSize(1); // This one affects size of Please wait... message
  53. display.cp437(true); // Use full 256 char 'Code Page 437' font
  54. }
  55. void ST7735Display::setTextSize(int sz) {
  56. display.setTextSize(sz);
  57. }
  58. void ST7735Display::setColor(Color c) {
  59. switch (c) {
  60. case DisplayDriver::DARK :
  61. _color = ST77XX_BLACK;
  62. break;
  63. case DisplayDriver::LIGHT :
  64. _color = ST77XX_WHITE;
  65. break;
  66. case DisplayDriver::RED :
  67. _color = ST77XX_RED;
  68. break;
  69. case DisplayDriver::GREEN :
  70. _color = ST77XX_GREEN;
  71. break;
  72. case DisplayDriver::BLUE :
  73. _color = ST77XX_BLUE;
  74. break;
  75. case DisplayDriver::YELLOW :
  76. _color = ST77XX_YELLOW;
  77. break;
  78. case DisplayDriver::ORANGE :
  79. _color = ST77XX_ORANGE;
  80. break;
  81. default:
  82. _color = ST77XX_WHITE;
  83. break;
  84. }
  85. display.setTextColor(_color);
  86. }
  87. void ST7735Display::setCursor(int x, int y) {
  88. display.setCursor(x*SCALE_X, y*SCALE_Y);
  89. }
  90. void ST7735Display::print(const char* str) {
  91. display.print(str);
  92. }
  93. void ST7735Display::fillRect(int x, int y, int w, int h) {
  94. display.fillRect(x*SCALE_X, y*SCALE_Y, w*SCALE_X, h*SCALE_Y, _color);
  95. }
  96. void ST7735Display::drawRect(int x, int y, int w, int h) {
  97. display.drawRect(x*SCALE_X, y*SCALE_Y, w*SCALE_X, h*SCALE_Y, _color);
  98. }
  99. void ST7735Display::drawXbm(int x, int y, const uint8_t* bits, int w, int h) {
  100. display.drawBitmap(x*SCALE_X, y*SCALE_Y, bits, w, h, _color);
  101. }
  102. uint16_t ST7735Display::getTextWidth(const char* str) {
  103. int16_t x1, y1;
  104. uint16_t w, h;
  105. display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
  106. return w / SCALE_X;
  107. }
  108. void ST7735Display::endFrame() {
  109. // display.display();
  110. }
  111. #endif