Skip to content

Commit 0244efe

Browse files
committed
Adding enableDebugging. Corrected line (missing last pixel)
1 parent d4b3bf8 commit 0244efe

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ MicroOLED KEYWORD1
1414

1515
begin KEYWORD2
1616
invert KEYWORD2
17+
enableDebugging KEYWORD2
1718
clear KEYWORD2
1819
invert KEYWORD2
1920
contrast KEYWORD2

src/SFE_MicroOLED.cpp

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
6969
#define TOTALFONTS 4
7070
#endif
7171

72-
#define swapOLED(a, b) \
73-
{ \
74-
uint8_t t = a; \
75-
a = b; \
76-
b = t; \
77-
}
78-
7972
// Add the font name as declared in the header file. Remove as many as possible to conserve FLASH memory.
8073
const unsigned char *MicroOLED::fontsPointer[] = {
8174
font5x7, font8x16, sevensegment, fontlargenumber
@@ -272,6 +265,14 @@ void MicroOLED::begin()
272265
clear(ALL); // Erase hardware memory inside the OLED controller to avoid random data in memory.
273266
}
274267

268+
//Calling this function with nothing sets the debug port to Serial
269+
//You can also call it with other streams like Serial1, SerialUSB, etc.
270+
void MicroOLED::enableDebugging(Stream &debugPort)
271+
{
272+
_debugPort = &debugPort;
273+
_printDebug = true;
274+
}
275+
275276
/** \brief Send the display a command byte
276277
277278
Send a command via SPI, I2C or parallel to SSD1306 controller.
@@ -556,26 +557,70 @@ Draw line using color and mode from x0,y0 to x1,y1 of the screen buffer.
556557
*/
557558
void MicroOLED::line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t color, uint8_t mode)
558559
{
560+
if (_printDebug == true)
561+
{
562+
_debugPort->print(F("line: line coords: ("));
563+
_debugPort->print(x0);
564+
_debugPort->print(F(","));
565+
_debugPort->print(y0);
566+
_debugPort->print(F(") ("));
567+
_debugPort->print(x1);
568+
_debugPort->print(F(","));
569+
_debugPort->print(y1);
570+
_debugPort->println(F(")"));
571+
}
572+
559573
uint8_t steep = abs(y1 - y0) > abs(x1 - x0);
560574
if (steep)
561575
{
562-
swapOLED(x0, y0);
563-
swapOLED(x1, y1);
564-
}
576+
swapOLED(&x0, &y0);
577+
swapOLED(&x1, &y1);
578+
if (_printDebug == true)
579+
_debugPort->println(F("line: line is steep"));
580+
}
565581

566582
if (x0 > x1)
567583
{
568-
swapOLED(x0, x1);
569-
swapOLED(y0, y1);
584+
swapOLED(&x0, &x1);
585+
swapOLED(&y0, &y1);
586+
if (_printDebug == true)
587+
_debugPort->println(F("line: x0 > x1"));
570588
}
571589

590+
// if (_printDebug == true)
591+
// {
592+
// _debugPort->print(F("line: line coords: ("));
593+
// _debugPort->print(x0);
594+
// _debugPort->print(F(","));
595+
// _debugPort->print(y0);
596+
// _debugPort->print(F(") ("));
597+
// _debugPort->print(x1);
598+
// _debugPort->print(F(","));
599+
// _debugPort->print(y1);
600+
// _debugPort->println(F(")"));
601+
// }
602+
572603
uint8_t dx, dy;
573604
dx = x1 - x0;
574605
dy = abs(y1 - y0);
575606

607+
if (_printDebug == true)
608+
{
609+
_debugPort->print(F("line: dx: "));
610+
_debugPort->print(dx);
611+
_debugPort->print(F(" dy: "));
612+
_debugPort->println(dy);
613+
}
614+
576615
int8_t err = dx / 2;
577616
int8_t ystep;
578617

618+
if (_printDebug == true)
619+
{
620+
_debugPort->print(F("line: err: "));
621+
_debugPort->println(err);
622+
}
623+
579624
if (y0 < y1)
580625
{
581626
ystep = 1;
@@ -585,15 +630,37 @@ void MicroOLED::line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t col
585630
ystep = -1;
586631
}
587632

588-
for (; x0 < x1; x0++)
633+
if (_printDebug == true)
634+
{
635+
_debugPort->print(F("line: ystep: "));
636+
_debugPort->println(ystep);
637+
}
638+
639+
for (; x0 <= x1; x0++)
589640
{
590641
if (steep)
591642
{
592643
pixel(y0, x0, color, mode);
644+
if (_printDebug == true)
645+
{
646+
_debugPort->print(F("line: steep pixel: ("));
647+
_debugPort->print(y0);
648+
_debugPort->print(F(","));
649+
_debugPort->print(x0);
650+
_debugPort->println(F(")"));
651+
}
593652
}
594653
else
595654
{
596655
pixel(x0, y0, color, mode);
656+
if (_printDebug == true)
657+
{
658+
_debugPort->print(F("line: pixel: ("));
659+
_debugPort->print(x0);
660+
_debugPort->print(F(","));
661+
_debugPort->print(y0);
662+
_debugPort->println(F(")"));
663+
}
597664
}
598665
err -= dy;
599666
if (err < 0)
@@ -1193,3 +1260,10 @@ uint8_t MicroOLED::getI2CTransactionSize(void)
11931260
{
11941261
return (i2cTransactionSize);
11951262
}
1263+
1264+
void MicroOLED::swapOLED(uint8_t *x, uint8_t *y)
1265+
{
1266+
uint8_t t = *x;
1267+
*x = *y;
1268+
*y = t;
1269+
}

src/SFE_MicroOLED.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ class MicroOLED : public Print
154154
void begin(void);
155155
virtual size_t write(uint8_t);
156156

157+
void enableDebugging(Stream &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used.
158+
157159
// RAW LCD functions
158160
void command(uint8_t c);
159161
void data(uint8_t c);
@@ -231,6 +233,11 @@ class MicroOLED : public Print
231233
uint8_t foreColor, drawMode, fontWidth, fontHeight, fontType, fontStartChar, fontTotalChar, cursorX, cursorY;
232234
uint16_t fontMapWidth;
233235
static const unsigned char *fontsPointer[];
236+
void swapOLED(uint8_t *x, uint8_t *y);
237+
238+
//Debug
239+
Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial.
240+
boolean _printDebug = false; //Flag to print debugging variables
234241

235242
// Communication
236243
void spiTransfer(byte data);

0 commit comments

Comments
 (0)