CheeseBoard
A library for use with the CheeseBoard Cheddar platform
CbOledDisplay.cpp
1 #include <Arduino.h>
2 #include <MutilaDebug.h>
3 #include "CbOledDisplay.h"
4 #include "CheeseboardConfig.h"
5 
6 CbOledDisplayClass CbOledDisplay(U8G2_R0, CBOLED_SCK_PIN, CBOLED_SDA_PIN, A0);
7 
8 CbOledDisplayClass::CbOledDisplayClass(const u8g2_cb_t *rotation, uint8_t clock, uint8_t data, uint8_t reset) :
9  U8G2_SSD1306_128X64_NONAME_F_SW_I2C(rotation, clock, data, reset)
10 {
11 }
12 
14 {
15  CBOLED_CLASS::begin();
16  setFontPosBottom();
17 }
18 
20 {
21  char buf[2];
22  buf[0] = c;
23  buf[1] = 0;
24  return CbOledDisplay.getStrWidth(buf);
25 }
26 
27 u8g2_uint_t CbOledDisplayClass::drawStrR(u8g2_uint_t x, u8g2_uint_t y, const char *s)
28 {
29  return drawStr(x-getStrWidth(s), y, s);
30 }
31 
32 void CbOledDisplayClass::drawText(const char* text, char hAlign, char vAlign)
33 {
34  _DB(F("CbOledDisplay::drawText: "));
35  _DB(text);
36  _DB(F(" hAl="));
37  _DB(hAlign);
38  _DB(F(" vAl="));
39  _DBLN(vAlign);
40  setFont(CBOLED_MESSAGE_FONT); // choose a suitable font
41 
42  const uint8_t maxLines = getDisplayHeight() / (CBOLED_MESSAGE_FONT_HEIGHT+CBOLED_MESSAGE_FONT_VSEP);
43  String lineText[maxLines];
44  uint8_t lines = 0;
45  int8_t lastSpace = -1;
46  for (uint8_t i=0; text[i]!=0; i++) {
47  if (text[i] == '\r' || text[i] == '\n' || text[i] == '|') {
48  // explicit line break
49  if (lines >= maxLines-1) {
50  // if we're out of lines, stop adding more letters
51  break;
52  } else {
53  // continue on next line
54  lines++;
55  lastSpace = -1;
56  continue;
57  }
58  } else if (getStrWidth(lineText[lines].c_str()) + getCharWidth(text[i]) >= getDisplayWidth()) {
59  // word wrap
60  if (lines >= maxLines-1) {
61  // if we're out of lines, stop adding more letters
62  break;
63  }
64  if (lastSpace>=0) {
65  // 1. copy last partial word from the end of this line to the next line
66  lineText[lines+1] = lineText[lines].substring(lastSpace+1);
67  // 2. remove last partial word from end of this line
68  lineText[lines].remove(lastSpace);
69  lastSpace = -1;
70  }
71  // 3. text from here goes on next line
72  lines++;
73  }
74  lineText[lines] += text[i];
75  if (text[i] == ' ') {
76  lastSpace = lineText[lines].length() - 1;
77  }
78  }
79  lines++;
80 
81  uint8_t blockHeight = (CBOLED_MESSAGE_FONT_HEIGHT+CBOLED_MESSAGE_FONT_VSEP)*lines;
82  uint8_t vOffset = 0;
83  switch (vAlign) {
84  case 'M':
85  vOffset = (getDisplayHeight() - blockHeight) / 2;
86  break;
87  case 'B':
88  vOffset = getDisplayHeight() - blockHeight;
89  break;
90  }
91 
92  // Work out where to put it
93  for (uint8_t i=0; i<lines; i++) {
94  uint8_t ypos = vOffset + CBOLED_MESSAGE_FONT_HEIGHT + ((CBOLED_MESSAGE_FONT_HEIGHT+CBOLED_MESSAGE_FONT_VSEP)*i);
95  uint8_t xpos = 0;
96  switch (hAlign) {
97  case 'C':
98  xpos = (getDisplayWidth() - getStrWidth(lineText[i].c_str())) / 2;
99  break;
100  case 'R':
101  xpos = getDisplayWidth() - getStrWidth(lineText[i].c_str());
102  break;
103  }
104  drawStr(xpos, ypos, lineText[i].c_str());
105  }
106 }
107 
108 
u8g2_uint_t drawStrR(u8g2_uint_t x, u8g2_uint_t y, const char *s)
u8g2_uint_t getCharWidth(char c)
CbOledDisplayClass(const u8g2_cb_t *rotation, uint8_t clock, uint8_t data, uint8_t reset=U8X8_PIN_NONE)
void drawText(const char *text, char hAlign='L', char vAlign='M')