File tree Expand file tree Collapse file tree 3 files changed +51
-0
lines changed
Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ The SSD1306Demo is a very comprehensive example demonstrating the most important
1515## Features
1616
1717* Draw pixels at given coordinates
18+ * Draw lines from given coordinates to given coordinates
1819* Draw or fill a rectangle with given dimensions
1920* Draw Text at given coordinates:
2021 * Define Alignment: Left, Right and Center
@@ -89,6 +90,9 @@ void setColor(SSD1306_COLOR color);
8990// Draw a pixel at given position
9091void setPixel(int16_t x, int16_t y);
9192
93+ // Draw a line from position 0 to position 1
94+ void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
95+
9296// Draw the border of a rectangle at the given location
9397void drawRect(int16_t x, int16_t y, int16_t width, int16_t height);
9498
Original file line number Diff line number Diff line change @@ -96,6 +96,46 @@ void SSD1306::setPixel(int16_t x, int16_t y) {
9696 }
9797}
9898
99+ // Bresenham's algorithm - thx wikipedia and Adafruit_GFX
100+ void SSD1306::drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1) {
101+ int16_t steep = abs (y1 - y0) > abs (x1 - x0);
102+ if (steep) {
103+ _swap_int16_t (x0, y0);
104+ _swap_int16_t (x1, y1);
105+ }
106+
107+ if (x0 > x1) {
108+ _swap_int16_t (x0, x1);
109+ _swap_int16_t (y0, y1);
110+ }
111+
112+ int16_t dx, dy;
113+ dx = x1 - x0;
114+ dy = abs (y1 - y0);
115+
116+ int16_t err = dx / 2 ;
117+ int16_t ystep;
118+
119+ if (y0 < y1) {
120+ ystep = 1 ;
121+ } else {
122+ ystep = -1 ;
123+ }
124+
125+ for (; x0<=x1; x0++) {
126+ if (steep) {
127+ setPixel (y0, x0);
128+ } else {
129+ setPixel (x0, y0);
130+ }
131+ err -= dy;
132+ if (err < 0 ) {
133+ y0 += ystep;
134+ err += dx;
135+ }
136+ }
137+ }
138+
99139void SSD1306::drawRect (int16_t x, int16_t y, int16_t width, int16_t height) {
100140 drawHorizontalLine (x, y, width);
101141 drawVerticalLine (x, y, height);
Original file line number Diff line number Diff line change 9191#define SETVCOMDETECT 0xDB
9292#define SWITCHCAPVCC 0x2
9393
94+ #ifndef _swap_int16_t
95+ #define _swap_int16_t (a, b ) { int16_t t = a; a = b; b = t; }
96+ #endif
97+
9498enum SSD1306_COLOR {
9599 BLACK = 0 ,
96100 WHITE = 1 ,
@@ -159,6 +163,9 @@ class SSD1306 {
159163
160164 // Draw a pixel at given position
161165 void setPixel (int16_t x, int16_t y);
166+
167+ // Draw a line from position 0 to position 1
168+ void drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1);
162169
163170 // Draw the border of a rectangle at the given location
164171 void drawRect (int16_t x, int16_t y, int16_t width, int16_t height);
You can’t perform that action at this time.
0 commit comments