Advertisement
LeventeDaradici

displaySSD1306.cpp - new bootlogo for SSD1306 display for yoRadio!

Jun 24th, 2024
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.85 KB | Source Code | 0 0
  1. #include "../core/options.h"
  2. #if DSP_MODEL==DSP_SSD1306 || DSP_MODEL==DSP_SSD1306x32
  3.  
  4. #include "displaySSD1306.h"
  5. #include <Wire.h>
  6. #include "../core/player.h"
  7. #include "../core/config.h"
  8. #include "../core/network.h"
  9.  
  10. #ifndef SCREEN_ADDRESS
  11. #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 or scan it https://create.arduino.cc/projecthub/abdularbi17/how-to-scan-i2c-address-in-arduino-eaadda
  12. #endif
  13.  
  14. #if DSP_MODEL==DSP_SSD1306
  15.  
  16. //#define LOGO_WIDTH 21
  17. //#define LOGO_HEIGHT 32
  18.  
  19. //const unsigned char logo [] PROGMEM=
  20. //{
  21. // 0x06, 0x03, 0x00, 0x0f, 0x07, 0x80, 0x1f, 0x8f, 0xc0, 0x1f, 0x8f, 0xc0,
  22. // 0x0f, 0x07, 0x80, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23. // 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x03, 0xff, 0x00, 0x0f, 0xff, 0x80,
  24. // 0x1f, 0xff, 0xc0, 0x1f, 0xff, 0xc0, 0x3f, 0x8f, 0xe0, 0x7e, 0x03, 0xf0,
  25. // 0x7c, 0x01, 0xf0, 0x7c, 0x01, 0xf0, 0x7f, 0xff, 0xf0, 0xff, 0xff, 0xf8,
  26. // 0xff, 0xff, 0xf8, 0xff, 0xff, 0xf8, 0x7c, 0x00, 0x00, 0x7c, 0x00, 0x00,
  27. // 0x7e, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x3f, 0xc0, 0xe0, 0x3f, 0xff, 0xe0,
  28. // 0x1f, 0xff, 0xe0, 0x0f, 0xff, 0xe0, 0x03, 0xff, 0xc0, 0x00, 0xfe, 0x00
  29. //};
  30. //#endif
  31.  
  32. #define LOGO_WIDTH 90
  33. #define LOGO_HEIGHT 64
  34.  
  35. const unsigned char logo [] PROGMEM = {
  36. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  37. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  38. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  39. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  40. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  41. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  42. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  43. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  44. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  45. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  46. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  47. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  48. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  49. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  50. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  51. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  52. B00000000,B00000000,B00000000,B00011111,B01111000,B00000000,B00000000,B01111001,B11100000,B00000000,B11111000,B00111111,
  53. B00000000,B00000000,B00000000,B00011111,B00111100,B00000000,B00000000,B01111001,B11100000,B00000000,B11111000,B00111111,
  54. B00000000,B00000000,B00000000,B00011111,B00111100,B00000000,B00000000,B01111000,B11000000,B00000000,B11111000,B00111111,
  55. B00000000,B00000000,B00000000,B00011111,B00111110,B00000000,B00000000,B01111000,B00000000,B00000000,B01110000,B00111111,
  56. B00000011,B11000110,B00111100,B00011111,B01111110,B00111111,B10000111,B11111001,B11100001,B11110000,B01110000,B00111111,
  57. B00000011,B11000110,B11101111,B00011111,B01111110,B01110011,B11001110,B01111001,B11100011,B10111000,B01110000,B00111111,
  58. B00000001,B11100100,B11101111,B00011111,B00111100,B00100011,B11011100,B01111001,B11100111,B01111100,B01110000,B00111111,
  59. B00000001,B11100101,B11100111,B10011111,B00111000,B00010111,B11011100,B01111001,B11100111,B00111100,B01100000,B00111111,
  60. B00000000,B11111001,B11100111,B10011111,B11110000,B01110111,B11011110,B01111001,B11101111,B00111110,B00100000,B00111111,
  61. B00000000,B11111001,B11110011,B10011111,B01111110,B11110011,B11011110,B01111001,B11101111,B10011110,B00000000,B00111111,
  62. B00000000,B01111000,B11110011,B10011111,B01111110,B11110011,B11011111,B01111001,B11100111,B10011100,B11110000,B00111111,
  63. B00000000,B01110000,B11110011,B10011111,B00111110,B11111011,B11001111,B01111001,B11100111,B10011100,B11110000,B00111111,
  64. B00000001,B00110000,B01110111,B00011111,B00111110,B01111011,B11000111,B01111001,B11100011,B10111000,B11110000,B00111111,
  65. B00000011,B00100000,B00011100,B00011111,B00011100,B00111111,B11000011,B11111001,B11100000,B11110000,B01110000,B00111111,
  66. B00000111,B11000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  67. B00000011,B11000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  68. B00000001,B10000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  69. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  70. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  71. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  72. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  73. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  74. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  75. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  76. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  77. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  78. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  79. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  80. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  81. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  82. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  83. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  84. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  85. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  86. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  87. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  88. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  89. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  90. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  91. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  92. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  93. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  94. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  95. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  96. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  97. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  98. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111,
  99. B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00111111
  100. };
  101. #endif
  102.  
  103.  
  104. #ifndef I2CFREQ_HZ
  105. #define I2CFREQ_HZ 4000000UL
  106. #endif
  107.  
  108. TwoWire I2CSSD1306 = TwoWire(0);
  109.  
  110. DspCore::DspCore(): Adafruit_SSD1306(128, ((DSP_MODEL==DSP_SSD1306)?64:32), &I2CSSD1306, I2C_RST, I2CFREQ_HZ) { }
  111.  
  112. #include "tools/utf8RusGFX.h"
  113.  
  114. void DspCore::initDisplay() {
  115. I2CSSD1306.begin(I2C_SDA, I2C_SCL);
  116. if (!begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  117. Serial.println(F("SSD1306 allocation failed"));
  118. for (;;); // Don't proceed, loop forever
  119. }
  120. #include "tools/oledcolorfix.h"
  121. cp437(true);
  122. flip();
  123. invert();
  124. setTextWrap(false);
  125.  
  126. plItemHeight = playlistConf.widget.textsize*(CHARHEIGHT-1)+playlistConf.widget.textsize*4;
  127. plTtemsCount = round((float)height()/plItemHeight);
  128. if(plTtemsCount%2==0) plTtemsCount++;
  129. plCurrentPos = plTtemsCount/2;
  130. plYStart = (height() / 2 - plItemHeight / 2) - plItemHeight * (plTtemsCount - 1) / 2 + playlistConf.widget.textsize*2;
  131. }
  132.  
  133. void DspCore::drawLogo(uint16_t top) {
  134. #if DSP_MODEL==DSP_SSD1306
  135. drawBitmap((width() - LOGO_WIDTH ) / 2, 8, logo, LOGO_WIDTH, LOGO_HEIGHT, 1);
  136. #else
  137. setTextSize(1);
  138. setCursor((width() - 6*CHARWIDTH) / 2, 0);
  139. setTextColor(TFT_FG, TFT_BG);
  140. print(utf8Rus("ёRadio", false));
  141. setTextSize(1);
  142. #endif
  143. display();
  144. }
  145.  
  146. void DspCore::printPLitem(uint8_t pos, const char* item, ScrollWidget& current){
  147. setTextSize(playlistConf.widget.textsize);
  148. if (pos == plCurrentPos) {
  149. current.setText(item);
  150. } else {
  151. uint8_t plColor = (abs(pos - plCurrentPos)-1)>4?4:abs(pos - plCurrentPos)-1;
  152. setTextColor(config.theme.playlist[plColor], config.theme.background);
  153. setCursor(TFT_FRAMEWDT, plYStart + pos * plItemHeight);
  154. fillRect(0, plYStart + pos * plItemHeight - 1, width(), plItemHeight - 2, config.theme.background);
  155. print(utf8Rus(item, true));
  156. }
  157. }
  158.  
  159. void DspCore::drawPlaylist(uint16_t currentItem) {
  160. uint8_t lastPos = config.fillPlMenu(currentItem - plCurrentPos, plTtemsCount);
  161. if(lastPos<plTtemsCount){
  162. fillRect(0, lastPos*plItemHeight+plYStart, width(), height()/2, config.theme.background);
  163. }
  164. }
  165.  
  166. void DspCore::clearDsp(bool black) {
  167. fillScreen(TFT_BG);
  168. }
  169.  
  170. uint8_t DspCore::_charWidth(unsigned char c){
  171. return CHARWIDTH*clockTimeHeight;
  172. }
  173.  
  174. uint16_t DspCore::textWidth(const char *txt){
  175. uint16_t w = 0, l=strlen(txt);
  176. for(uint16_t c=0;c<l;c++) w+=_charWidth(txt[c]);
  177. return w;
  178. }
  179.  
  180. void DspCore::_getTimeBounds() {
  181. _timewidth = textWidth(_timeBuf);
  182. char buf[4];
  183. strftime(buf, 4, "%H", &network.timeinfo);
  184. _dotsLeft=textWidth(buf);
  185. }
  186.  
  187. void DspCore::_clockSeconds(){
  188. setTextSize(clockTimeHeight);
  189. #if DSP_MODEL!=DSP_SSD1306x32
  190. setTextColor((network.timeinfo.tm_sec % 2 == 0) ? config.theme.clock : config.theme.background, config.theme.background);
  191. #else
  192. setTextColor((network.timeinfo.tm_sec % 2 == 0) ? 0 : 1, 1);
  193. #endif
  194. setCursor(_timeleft+_dotsLeft, clockTop);
  195. print(":"); /* print dots */
  196. #if DSP_MODEL!=DSP_SSD1306x32
  197. setTextSize(1);
  198. setCursor(_timeleft+_timewidth+1, clockTop);
  199. setTextColor(config.theme.clock, config.theme.background);
  200. sprintf(_bufforseconds, "%02d", network.timeinfo.tm_sec);
  201. print(_bufforseconds);
  202. #endif
  203. //setFont();
  204. }
  205.  
  206. void DspCore::_clockDate(){ }
  207.  
  208. void DspCore::_clockTime(){
  209. if(_oldtimeleft>0) dsp.fillRect(_oldtimeleft, clockTop, _oldtimewidth, clockTimeHeight*CHARHEIGHT, config.theme.background);
  210. #if DSP_MODEL!=DSP_SSD1306x32
  211. _timeleft = (width()/2 - _timewidth/2)+clockRightSpace;
  212. setTextColor(config.theme.clock, config.theme.background);
  213. #else
  214. _timeleft = (width() - _timewidth)-clockRightSpace;
  215. setTextColor(0, 1);
  216. #endif
  217. setTextSize(clockTimeHeight);
  218.  
  219. setCursor(_timeleft, clockTop);
  220. print(_timeBuf);
  221. //setFont();
  222. strlcpy(_oldTimeBuf, _timeBuf, sizeof(_timeBuf));
  223. _oldtimewidth = _timewidth;
  224. _oldtimeleft = _timeleft;
  225. }
  226.  
  227. void DspCore::printClock(uint16_t top, uint16_t rightspace, uint16_t timeheight, bool redraw){
  228. clockTop = top;
  229. clockRightSpace = rightspace;
  230. clockTimeHeight = timeheight;
  231. strftime(_timeBuf, sizeof(_timeBuf), "%H:%M", &network.timeinfo);
  232. if(strcmp(_oldTimeBuf, _timeBuf)!=0 || redraw){
  233. _getTimeBounds();
  234. _clockTime();
  235. }
  236. _clockSeconds();
  237. }
  238.  
  239. void DspCore::clearClock(){
  240. dsp.fillRect(_timeleft, clockTop, _timewidth, clockTimeHeight*CHARHEIGHT, config.theme.background);
  241. }
  242.  
  243. void DspCore::startWrite(void) { }
  244.  
  245. void DspCore::endWrite(void) { }
  246.  
  247. void DspCore::loop(bool force) {
  248. display();
  249. delay(0);
  250. }
  251.  
  252. void DspCore::charSize(uint8_t textsize, uint8_t& width, uint16_t& height){
  253. width = textsize * CHARWIDTH;
  254. height = textsize * CHARHEIGHT;
  255. }
  256.  
  257. void DspCore::setTextSize(uint8_t s){
  258. Adafruit_GFX::setTextSize(s);
  259. }
  260.  
  261. void DspCore::flip(){
  262. setRotation(config.store.flipscreen?2:0);
  263. }
  264.  
  265. void DspCore::invert(){
  266. invertDisplay(config.store.invertdisplay);
  267. }
  268.  
  269. void DspCore::sleep(void) { ssd1306_command(SSD1306_DISPLAYOFF); }
  270. void DspCore::wake(void) { ssd1306_command(SSD1306_DISPLAYON); }
  271.  
  272. void DspCore::writePixel(int16_t x, int16_t y, uint16_t color) {
  273. if(_clipping){
  274. if ((x < _cliparea.left) || (x > _cliparea.left+_cliparea.width) || (y < _cliparea.top) || (y > _cliparea.top + _cliparea.height)) return;
  275. }
  276. Adafruit_SSD1306::writePixel(x, y, color);
  277. }
  278.  
  279. void DspCore::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
  280. if(_clipping){
  281. if ((x < _cliparea.left) || (x >= _cliparea.left+_cliparea.width) || (y < _cliparea.top) || (y > _cliparea.top + _cliparea.height)) return;
  282. }
  283. Adafruit_SSD1306::writeFillRect(x, y, w, h, color);
  284. }
  285.  
  286. void DspCore::setClipping(clipArea ca){
  287. _cliparea = ca;
  288. _clipping = true;
  289. }
  290.  
  291. void DspCore::clearClipping(){
  292. _clipping = false;
  293. }
  294.  
  295. void DspCore::setNumFont(){
  296. setTextSize(2);
  297. }
  298.  
  299. #endif
  300.  
Tags: yoRadio eRadio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement