Skip to content

Commit 105c675

Browse files
committed
Implement basic Print interface #35
1 parent 001b13b commit 105c675

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

OLEDDisplay.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,62 @@ void OLEDDisplay::clear(void) {
506506
memset(buffer, 0, DISPLAY_BUFFER_SIZE);
507507
}
508508

509+
void OLEDDisplay::drawLogBuffer(uint16_t xMove, uint16_t yMove) {
510+
setTextAlignment(TEXT_ALIGN_LEFT);
511+
uint16_t lineHeight = pgm_read_byte(fontData + HEIGHT_POS);
512+
uint16_t length = 0;
513+
uint16_t line = 0;
514+
uint16_t lastPos = 0;
515+
for (uint16_t i=0;i<this->logBufferFilled;i++){
516+
if (this->logBuffer[i] == 10) {
517+
drawStringInternal(xMove, yMove + (line++) * lineHeight, &this->logBuffer[lastPos], length, 0);
518+
lastPos = i;
519+
length = 0;
520+
} else {
521+
length++;
522+
}
523+
}
524+
drawStringInternal(xMove, yMove + (line++) * lineHeight, &this->logBuffer[lastPos], length, 0);
525+
}
526+
527+
bool OLEDDisplay::setLogBuffer(uint16_t lines, uint16_t chars){
528+
if (logBuffer != NULL) free(logBuffer);
529+
uint16_t size = lines * chars;
530+
if (size > 0) {
531+
this->logBufferMaxLines = lines;
532+
this->logBufferLine = 0;
533+
this->logBufferSize = size;
534+
this->logBuffer = (char *) malloc(size * sizeof(uint8_t));
535+
if(!this->logBuffer) {
536+
DEBUG_OLEDDISPLAY("[OLEDDISPLAY][setLogBuffer] Not enough memory to create log buffer\n");
537+
return false;
538+
}
539+
}
540+
return true;
541+
}
542+
543+
size_t OLEDDisplay::write(uint8_t c) {
544+
if (this->logBufferSize > 0) {
545+
if (this->logBufferFilled < this->logBufferSize && this->logBufferLine < this->logBufferMaxLines) {
546+
this->logBuffer[logBufferFilled] = utf8ascii(c);
547+
this->logBufferFilled++;
548+
if (c == 10) this->logBufferLine++;
549+
} else {
550+
if (this->logBufferLine <= this->logBufferMaxLines) this->logBufferLine = 0;
551+
this->logBufferFilled = 0;
552+
write(c);
553+
}
554+
}
555+
556+
}
557+
558+
size_t OLEDDisplay::write(const char* str) {
559+
size_t length = strlen(str);
560+
for (size_t i = 0; i < length; i++) {
561+
write(str[i]);
562+
}
563+
}
564+
509565
// Private functions
510566
void OLEDDisplay::sendInitCommands(void) {
511567
sendCommand(DISPLAYOFF);

OLEDDisplay.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ enum OLEDDISPLAY_TEXT_ALIGNMENT {
108108
};
109109

110110

111-
class OLEDDisplay {
111+
class OLEDDisplay : public Print {
112112
public:
113113
// Initialize the display
114114
bool init();
@@ -210,17 +210,39 @@ class OLEDDisplay {
210210
// Clear the local pixel buffer
211211
void clear(void);
212212

213+
// Log buffer implementation
214+
215+
// This will define the lines and characters you can
216+
// print to the screen. When you exeed the buffer size (lines * chars)
217+
// the output may be truncated due to the size constraint.
218+
bool setLogBuffer(uint16_t lines, uint16_t chars);
219+
220+
// Draw the log buffer at position (x, y)
221+
void drawLogBuffer(uint16_t x, uint16_t y);
222+
223+
// Implementent needed function to be compatible with Print class
224+
size_t write(uint8_t c);
225+
size_t write(const char* s);
226+
213227
uint8_t *buffer;
214228

215229
#ifdef OLEDDISPLAY_DOUBLE_BUFFER
216230
uint8_t *buffer_back;
217231
#endif
218232

219233
protected:
234+
220235
OLEDDISPLAY_TEXT_ALIGNMENT textAlignment = TEXT_ALIGN_LEFT;
221236
OLEDDISPLAY_COLOR color = WHITE;
222237

223-
const char *fontData = ArialMT_Plain_10;
238+
const char *fontData = ArialMT_Plain_10;
239+
240+
// State values for logBuffer
241+
uint16_t logBufferSize = 0;
242+
uint16_t logBufferFilled = 0;
243+
uint16_t logBufferLine = 0;
244+
uint16_t logBufferMaxLines = 0;
245+
char *logBuffer = NULL;
224246

225247
// Send a command to the display (low level function)
226248
virtual void sendCommand(uint8_t com);

0 commit comments

Comments
 (0)