@@ -506,6 +506,111 @@ void OLEDDisplay::clear(void) {
506506 memset (buffer, 0 , DISPLAY_BUFFER_SIZE);
507507}
508508
509+ void OLEDDisplay::drawLogBuffer (uint16_t xMove, uint16_t yMove) {
510+ uint16_t lineHeight = pgm_read_byte (fontData + HEIGHT_POS);
511+ // Always align left
512+ setTextAlignment (TEXT_ALIGN_LEFT);
513+
514+ // State values
515+ uint16_t length = 0 ;
516+ uint16_t line = 0 ;
517+ uint16_t lastPos = 0 ;
518+
519+ for (uint16_t i=0 ;i<this ->logBufferFilled ;i++){
520+ // Everytime we have a \n print
521+ if (this ->logBuffer [i] == 10 ) {
522+ length++;
523+ // Draw string on line `line` from lastPos to length
524+ // Passing 0 as the lenght because we are in TEXT_ALIGN_LEFT
525+ drawStringInternal (xMove, yMove + (line++) * lineHeight, &this ->logBuffer [lastPos], length, 0 );
526+ // Remember last pos
527+ lastPos = i;
528+ // Reset length
529+ length = 0 ;
530+ } else {
531+ // Count chars until next linebreak
532+ length++;
533+ }
534+ }
535+ // Draw the remaining string
536+ if (length > 0 ) {
537+ drawStringInternal (xMove, yMove + line * lineHeight, &this ->logBuffer [lastPos], length, 0 );
538+ }
539+ }
540+
541+ bool OLEDDisplay::setLogBuffer (uint16_t lines, uint16_t chars){
542+ if (logBuffer != NULL ) free (logBuffer);
543+ uint16_t size = lines * chars;
544+ if (size > 0 ) {
545+ this ->logBufferLine = 0 ; // Lines printed
546+ this ->logBufferMaxLines = lines; // Lines max printable
547+ this ->logBufferSize = size; // Total number of characters the buffer can hold
548+ this ->logBuffer = (char *) malloc (size * sizeof (uint8_t ));
549+ if (!this ->logBuffer ) {
550+ DEBUG_OLEDDISPLAY (" [OLEDDISPLAY][setLogBuffer] Not enough memory to create log buffer\n " );
551+ return false ;
552+ }
553+ }
554+ return true ;
555+ }
556+
557+ size_t OLEDDisplay::write (uint8_t c) {
558+ if (this ->logBufferSize > 0 ) {
559+ // Don't waste space on \r\n line endings, dropping \r
560+ if (c == 13 ) return 1 ;
561+
562+ bool maxLineNotReached = this ->logBufferLine < this ->logBufferMaxLines ;
563+ bool bufferNotFull = this ->logBufferFilled < this ->logBufferSize ;
564+
565+ // Can we write to the buffer?
566+ if (bufferNotFull && maxLineNotReached) {
567+ this ->logBuffer [logBufferFilled] = utf8ascii (c);
568+ this ->logBufferFilled ++;
569+ // Keep track of lines written
570+ if (c == 10 ) this ->logBufferLine ++;
571+ } else {
572+ // Max line number is reached
573+ if (!maxLineNotReached) this ->logBufferLine --;
574+
575+ // Find the end of the first line
576+ uint16_t firstLineEnd = 0 ;
577+ for (uint16_t i=0 ;i<this ->logBufferFilled ;i++) {
578+ if (this ->logBuffer [i] == 10 ){
579+ // Include last char too
580+ firstLineEnd = i + 1 ;
581+ break ;
582+ }
583+ }
584+ // If there was a line ending
585+ if (firstLineEnd > 0 ) {
586+ // Calculate the new logBufferFilled value
587+ this ->logBufferFilled = logBufferFilled - firstLineEnd;
588+ // Now we move the lines infront of the buffer
589+ memcpy (this ->logBuffer , &this ->logBuffer [firstLineEnd], logBufferFilled);
590+ } else {
591+ // Let's reuse the buffer if it was full
592+ if (!bufferNotFull) {
593+ this ->logBufferFilled = 0 ;
594+ }// else {
595+ // Nothing to do here
596+ // }
597+ }
598+ write (c);
599+ }
600+ }
601+ // We are always writing all uint8_t to the buffer
602+ return 1 ;
603+ }
604+
605+ size_t OLEDDisplay::write (const char * str) {
606+ if (str == NULL ) return 0 ;
607+ size_t length = strlen (str);
608+ for (size_t i = 0 ; i < length; i++) {
609+ write (str[i]);
610+ }
611+ return length;
612+ }
613+
509614// Private functions
510615void OLEDDisplay::sendInitCommands (void ) {
511616 sendCommand (DISPLAYOFF);
0 commit comments