ST7735Display.cpp 3.1 KB

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