Skip to content
Snippets Groups Projects
Commit e3605d72 authored by squix78's avatar squix78
Browse files

Merge pull request #43 from squix78/implement-print-class

Implement basic print interface
parents 48226c9a 72ec74e9
No related branches found
No related tags found
No related merge requests found
...@@ -506,6 +506,111 @@ void OLEDDisplay::clear(void) { ...@@ -506,6 +506,111 @@ void OLEDDisplay::clear(void) {
memset(buffer, 0, DISPLAY_BUFFER_SIZE); memset(buffer, 0, DISPLAY_BUFFER_SIZE);
} }
void OLEDDisplay::drawLogBuffer(uint16_t xMove, uint16_t yMove) {
uint16_t lineHeight = pgm_read_byte(fontData + HEIGHT_POS);
// Always align left
setTextAlignment(TEXT_ALIGN_LEFT);
// State values
uint16_t length = 0;
uint16_t line = 0;
uint16_t lastPos = 0;
for (uint16_t i=0;i<this->logBufferFilled;i++){
// Everytime we have a \n print
if (this->logBuffer[i] == 10) {
length++;
// Draw string on line `line` from lastPos to length
// Passing 0 as the lenght because we are in TEXT_ALIGN_LEFT
drawStringInternal(xMove, yMove + (line++) * lineHeight, &this->logBuffer[lastPos], length, 0);
// Remember last pos
lastPos = i;
// Reset length
length = 0;
} else {
// Count chars until next linebreak
length++;
}
}
// Draw the remaining string
if (length > 0) {
drawStringInternal(xMove, yMove + line * lineHeight, &this->logBuffer[lastPos], length, 0);
}
}
bool OLEDDisplay::setLogBuffer(uint16_t lines, uint16_t chars){
if (logBuffer != NULL) free(logBuffer);
uint16_t size = lines * chars;
if (size > 0) {
this->logBufferLine = 0; // Lines printed
this->logBufferMaxLines = lines; // Lines max printable
this->logBufferSize = size; // Total number of characters the buffer can hold
this->logBuffer = (char *) malloc(size * sizeof(uint8_t));
if(!this->logBuffer) {
DEBUG_OLEDDISPLAY("[OLEDDISPLAY][setLogBuffer] Not enough memory to create log buffer\n");
return false;
}
}
return true;
}
size_t OLEDDisplay::write(uint8_t c) {
if (this->logBufferSize > 0) {
// Don't waste space on \r\n line endings, dropping \r
if (c == 13) return 1;
bool maxLineNotReached = this->logBufferLine < this->logBufferMaxLines;
bool bufferNotFull = this->logBufferFilled < this->logBufferSize;
// Can we write to the buffer?
if (bufferNotFull && maxLineNotReached) {
this->logBuffer[logBufferFilled] = utf8ascii(c);
this->logBufferFilled++;
// Keep track of lines written
if (c == 10) this->logBufferLine++;
} else {
// Max line number is reached
if (!maxLineNotReached) this->logBufferLine--;
// Find the end of the first line
uint16_t firstLineEnd = 0;
for (uint16_t i=0;i<this->logBufferFilled;i++) {
if (this->logBuffer[i] == 10){
// Include last char too
firstLineEnd = i + 1;
break;
}
}
// If there was a line ending
if (firstLineEnd > 0) {
// Calculate the new logBufferFilled value
this->logBufferFilled = logBufferFilled - firstLineEnd;
// Now we move the lines infront of the buffer
memcpy(this->logBuffer, &this->logBuffer[firstLineEnd], logBufferFilled);
} else {
// Let's reuse the buffer if it was full
if (!bufferNotFull) {
this->logBufferFilled = 0;
}// else {
// Nothing to do here
//}
}
write(c);
}
}
// We are always writing all uint8_t to the buffer
return 1;
}
size_t OLEDDisplay::write(const char* str) {
if (str == NULL) return 0;
size_t length = strlen(str);
for (size_t i = 0; i < length; i++) {
write(str[i]);
}
return length;
}
// Private functions // Private functions
void OLEDDisplay::sendInitCommands(void) { void OLEDDisplay::sendInitCommands(void) {
sendCommand(DISPLAYOFF); sendCommand(DISPLAYOFF);
......
...@@ -108,7 +108,7 @@ enum OLEDDISPLAY_TEXT_ALIGNMENT { ...@@ -108,7 +108,7 @@ enum OLEDDISPLAY_TEXT_ALIGNMENT {
}; };
class OLEDDisplay { class OLEDDisplay : public Print {
public: public:
// Initialize the display // Initialize the display
bool init(); bool init();
...@@ -209,6 +209,20 @@ class OLEDDisplay { ...@@ -209,6 +209,20 @@ class OLEDDisplay {
// Clear the local pixel buffer // Clear the local pixel buffer
void clear(void); void clear(void);
// Log buffer implementation
// This will define the lines and characters you can
// print to the screen. When you exeed the buffer size (lines * chars)
// the output may be truncated due to the size constraint.
bool setLogBuffer(uint16_t lines, uint16_t chars);
// Draw the log buffer at position (x, y)
void drawLogBuffer(uint16_t x, uint16_t y);
// Implementent needed function to be compatible with Print class
size_t write(uint8_t c);
size_t write(const char* s);
uint8_t *buffer; uint8_t *buffer;
#ifdef OLEDDISPLAY_DOUBLE_BUFFER #ifdef OLEDDISPLAY_DOUBLE_BUFFER
...@@ -216,10 +230,18 @@ class OLEDDisplay { ...@@ -216,10 +230,18 @@ class OLEDDisplay {
#endif #endif
protected: protected:
OLEDDISPLAY_TEXT_ALIGNMENT textAlignment = TEXT_ALIGN_LEFT; OLEDDISPLAY_TEXT_ALIGNMENT textAlignment = TEXT_ALIGN_LEFT;
OLEDDISPLAY_COLOR color = WHITE; OLEDDISPLAY_COLOR color = WHITE;
const char *fontData = ArialMT_Plain_10; const char *fontData = ArialMT_Plain_10;
// State values for logBuffer
uint16_t logBufferSize = 0;
uint16_t logBufferFilled = 0;
uint16_t logBufferLine = 0;
uint16_t logBufferMaxLines = 0;
char *logBuffer = NULL;
// Send a command to the display (low level function) // Send a command to the display (low level function)
virtual void sendCommand(uint8_t com); virtual void sendCommand(uint8_t com);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment