ssd1306-i2c.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * lws abstract display implementation for ssd1306 on i2c
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <private-lib-core.h>
  25. #include <drivers/devices/display/ssd1306.h>
  26. static uint8_t ssd1306_128x64_init[] = {
  27. SSD1306_DISPLAYOFF,
  28. SSD1306_SETDISPLAYCLOCKDIV, 0xf0,
  29. SSD1306_SETMULTIPLEX, 64 - 1,
  30. SSD1306_SETDISPLAYOFFSET, 0,
  31. SSD1306_CHARGEPUMP, 0x14,
  32. SSD1306_MEMORYMODE, 0,
  33. SSD1306_SEGREMAP | (0 << 0),
  34. SSD1306_COMSCANDEC,
  35. SSD1306_SETCOMPINS, (1 << 4) | 0x02,
  36. SSD1306_SETCONTRAST, 0, /* start at lowest */
  37. SSD1306_SETPRECHARGE, (0xf << 4) | (1 << 0),
  38. SSD1306_SETVCOMDESELECT, (4 << 4),
  39. SSD1306_DEACTIVATE_SCROLL,
  40. SSD1306_DISPLAYALLON_RESUME,
  41. SSD1306_NORMALDISPLAY,
  42. SSD1306_DISPLAYON
  43. };
  44. int
  45. lws_display_ssd1306_i2c_init(const struct lws_display *disp)
  46. {
  47. const lws_display_ssd1306_t *si = (const lws_display_ssd1306_t *)disp;
  48. si->i2c->init(si->i2c);
  49. if (si->gpio) {
  50. si->gpio->mode(si->reset_gpio, LWSGGPIO_FL_WRITE |
  51. LWSGGPIO_FL_PULLUP);
  52. si->gpio->set(si->reset_gpio, 0);
  53. lws_msleep(1);
  54. si->gpio->set(si->reset_gpio, 1);
  55. lws_msleep(1);
  56. }
  57. if (lws_i2c_command_list(si->i2c, si->i2c7_address,
  58. ssd1306_128x64_init,
  59. LWS_ARRAY_SIZE(ssd1306_128x64_init))) {
  60. lwsl_err("%s: fail\n", __func__);
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. int
  66. lws_display_ssd1306_i2c_contrast(const struct lws_display *disp, uint8_t b)
  67. {
  68. const lws_display_ssd1306_t *si = (const lws_display_ssd1306_t *)disp;
  69. uint8_t ba[2];
  70. ba[0] = SSD1306_SETCONTRAST;
  71. ba[1] = b;
  72. return lws_i2c_command_list(si->i2c, si->i2c7_address,
  73. ba, LWS_ARRAY_SIZE(ba));
  74. }
  75. int
  76. lws_display_ssd1306_i2c_blit(const struct lws_display *disp, const uint8_t *src,
  77. lws_display_scalar x, lws_display_scalar y,
  78. lws_display_scalar w, lws_display_scalar h)
  79. {
  80. const lws_display_ssd1306_t *si = (const lws_display_ssd1306_t *)disp;
  81. uint8_t ba[6];
  82. int n, m;
  83. /*
  84. * The display is arranged in 128x8 bands, with one byte containing
  85. * the 8 vertical pixels of the band.
  86. */
  87. if (h < 8)
  88. h = 8;
  89. ba[0] = SSD1306_COLUMNADDR;
  90. ba[1] = x;
  91. ba[2] = x + w - 1;
  92. ba[3] = SSD1306_PAGEADDR;
  93. ba[4] = y / 8;
  94. ba[5] = ba[4] + (h / 8) - 1;
  95. if (lws_i2c_command_list(si->i2c, si->i2c7_address,
  96. ba, LWS_ARRAY_SIZE(ba))) {
  97. lwsl_err("%s: fail\n", __func__);
  98. return 1;
  99. }
  100. for (n = 0; n < (w * h) / 8;) {
  101. lws_bb_i2c_start(si->i2c);
  102. lws_bb_i2c_write(si->i2c, si->i2c7_address << 1);
  103. lws_bb_i2c_write(si->i2c, SSD1306_SETSTARTLINE | y);
  104. for (m = 0; m < w; m++)
  105. lws_bb_i2c_write(si->i2c, src[n++]);
  106. lws_bb_i2c_stop(si->i2c);
  107. y += 8;
  108. }
  109. return 0;
  110. }
  111. int
  112. lws_display_ssd1306_i2c_power(const struct lws_display *disp, int state)
  113. {
  114. const lws_display_ssd1306_t *si = (const lws_display_ssd1306_t *)disp;
  115. if (!state)
  116. return lws_i2c_command(si->i2c, si->i2c7_address,
  117. SSD1306_DISPLAYOFF | !!state);
  118. return lws_display_ssd1306_i2c_init(disp);
  119. }