ssd1306 more functions (#310)

SSD1306 2D drawing/text functions, new example, builtin fonts support
This commit is contained in:
Zaltora 2016-12-13 14:42:23 +01:00 committed by Ruslan V. Uss
parent 61c3d509e5
commit 7432c019f7
39 changed files with 145365 additions and 5 deletions

View file

@ -3,10 +3,11 @@
*
* Copyright (c) 2016 urx (https://github.com/urx),
* Ruslan V. Uss (https://github.com/UncleRus)
* Zaltora (https://github.com/Zaltora)
*
* MIT Licensed as described in the file LICENSE
*
* @todo Scrolling, fonts
* @todo HW scrolling, sprites
*/
#include "ssd1306.h"
#include <stdio.h>
@ -62,12 +63,22 @@
#define SSD1306_CHARGE_PUMP_EN (0x14)
#define SSD1306_CHARGE_PUMP_DIS (0x10)
#define SSD1306_SCROLL_HOR_LEFT (0x27)
#define SSD1306_SCROLL_HOR_RIGHT (0x26)
#define SSD1306_SCROLL_HOR_VER_LEFT (0x2A)
#define SSD1306_SCROLL_HOR_VER_RIGHT (0x29)
#define SSD1306_SCROLL_ENABLE (0x2F)
#define SSD1306_SCROLL_DISABLE (0x2E)
#ifdef SSD1306_DEBUG
#define debug(fmt, ...) printf("%s: " fmt "\n", "SSD1306", ## __VA_ARGS__)
#else
#define debug(fmt, ...)
#endif
#define abs(x) ((x)<0 ? -(x) : (x))
#define swap(x, y) do { typeof(x) temp##x##y = x; x = y; y = temp##x##y; } while (0)
/* Issue a command to SSD1306 device
* I2C proto format:
* |S|Slave Address|W|ACK|0x00|Command|Ack|P|
@ -403,4 +414,603 @@ int ssd1306_load_xbm(const ssd1306_t *dev, uint8_t *xbm, uint8_t *fb)
return ssd1306_load_frame_buffer(dev, fb);
}
int ssd1306_draw_pixel(const ssd1306_t *dev, uint8_t *fb, int8_t x, int8_t y, ssd1306_color_t color)
{
uint16_t index;
if ((x >= dev->width) || (x < 0) || (y >= dev->height) || (y < 0))
return -EINVAL;
index = x + (y / 8) * dev->width;
switch (color)
{
case OLED_COLOR_WHITE:
fb[index] |= (1 << (y & 7));
break;
case OLED_COLOR_BLACK:
fb[index] &= ~(1 << (y & 7));
break;
case OLED_COLOR_INVERT:
fb[index] ^= (1 << (y & 7));
break;
default:
break;
}
return 0;
}
int ssd1306_draw_hline(const ssd1306_t *dev, uint8_t *fb, int8_t x, int8_t y, uint8_t w, ssd1306_color_t color)
{
uint16_t index;
uint8_t mask, t;
// boundary check
if ((x >= dev->width) || (x < 0) || (y >= dev->height) || (y < 0))
return -EINVAL;
if (w == 0)
return -EINVAL;
if (x + w > dev->width)
w = dev->width - x;
t = w;
index = x + (y / 8) * dev->width;
mask = 1 << (y & 7);
switch (color)
{
case OLED_COLOR_WHITE:
while (t--)
{
fb[index] |= mask;
++index;
}
break;
case OLED_COLOR_BLACK:
mask = ~mask;
while (t--)
{
fb[index] &= mask;
++index;
}
break;
case OLED_COLOR_INVERT:
while (t--)
{
fb[index] ^= mask;
++index;
}
break;
default:
break;
}
return 0;
}
int ssd1306_draw_vline(const ssd1306_t *dev, uint8_t *fb, int8_t x, int8_t y, uint8_t h, ssd1306_color_t color)
{
uint16_t index;
uint8_t mask, mod, t;
// boundary check
if ((x >= dev->width) || (x < 0) || (y >= dev->height) || (y < 0))
return -EINVAL;
if (h == 0)
return -EINVAL;
if (y + h > dev->height)
h = dev->height - y;
t = h;
index = x + (y / 8) * dev->width;
mod = y & 7;
if (mod) // partial line that does not fit into byte at top
{
// Magic from Adafruit
mod = 8 - mod;
static const uint8_t premask[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
mask = premask[mod];
if (t < mod)
mask &= (0xFF >> (mod - t));
switch (color)
{
case OLED_COLOR_WHITE:
fb[index] |= mask;
break;
case OLED_COLOR_BLACK:
fb[index] &= ~mask;
break;
case OLED_COLOR_INVERT:
fb[index] ^= mask;
break;
default:
break;
}
if (t < mod)
return 0;
t -= mod;
index += dev->width;
}
if (t >= 8) // byte aligned line at middle
{
switch (color)
{
case OLED_COLOR_WHITE:
do
{
fb[index] = 0xff;
index += dev->width;
t -= 8;
} while (t >= 8);
break;
case OLED_COLOR_BLACK:
do
{
fb[index] = 0x00;
index += dev->width;
t -= 8;
} while (t >= 8);
break;
case OLED_COLOR_INVERT:
do
{
fb[index] = ~fb[index];
index += dev->width;
t -= 8;
} while (t >= 8);
break;
default:
break;
}
}
if (t) // partial line at bottom
{
mod = t & 7;
static const uint8_t postmask[8] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F };
mask = postmask[mod];
switch (color)
{
case OLED_COLOR_WHITE:
fb[index] |= mask;
break;
case OLED_COLOR_BLACK:
fb[index] &= ~mask;
break;
case OLED_COLOR_INVERT:
fb[index] ^= mask;
break;
default:
break;
}
}
return 0;
}
int ssd1306_draw_rectangle(const ssd1306_t *dev, uint8_t *fb, int8_t x, int8_t y, uint8_t w, uint8_t h, ssd1306_color_t color)
{
int err = 0;
if ((err = ssd1306_draw_hline(dev, fb, x, y, w, color)))
return err;
if ((err = ssd1306_draw_hline(dev, fb, x, y + h - 1, w, color)))
return err;
if ((err = ssd1306_draw_vline(dev, fb, x, y, h, color)))
return err;
return ssd1306_draw_vline(dev, fb, x + w - 1, y, h, color);
}
int ssd1306_fill_rectangle(const ssd1306_t *dev, uint8_t *fb, int8_t x, int8_t y, uint8_t w, uint8_t h, ssd1306_color_t color)
{
// Can be optimized?
uint8_t i;
int err = 0;
for (i = x; i < x + w; ++i)
if ((err = ssd1306_draw_vline(dev, fb, i, y, h, color)))
return err;
return 0;
}
int ssd1306_draw_circle(const ssd1306_t *dev, uint8_t *fb, int8_t x0, int8_t y0, uint8_t r, ssd1306_color_t color)
{
// Refer to http://en.wikipedia.org/wiki/Midpoint_circle_algorithm for the algorithm
int8_t x = r;
int8_t y = 1;
int16_t radius_err = 1 - x;
int err = 0;
if (r == 0)
return -EINVAL;
if ((err = ssd1306_draw_pixel(dev, fb, x0 - r, y0, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0 + r, y0, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0, y0 - r, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0, y0 + r, color)))
return err;
while (x >= y)
{
if ((err = ssd1306_draw_pixel(dev, fb, x0 + x, y0 + y, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0 - x, y0 + y, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0 + x, y0 - y, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0 - x, y0 - y, color)))
return err;
if (x != y)
{
/* Otherwise the 4 drawings below are the same as above, causing
* problem when color is INVERT
*/
if ((err = ssd1306_draw_pixel(dev, fb, x0 + y, y0 + x, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0 - y, y0 + x, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0 + y, y0 - x, color)))
return err;
if ((err = ssd1306_draw_pixel(dev, fb, x0 - y, y0 - x, color)))
return err;
}
++y;
if (radius_err < 0) {
radius_err += 2 * y + 1;
}
else {
--x;
radius_err += 2 * (y - x + 1);
}
}
return 0;
}
int ssd1306_fill_circle(const ssd1306_t *dev, uint8_t *fb, int8_t x0, int8_t y0, uint8_t r, ssd1306_color_t color)
{
int8_t x = 1;
int8_t y = r;
int16_t radius_err = 1 - y;
int8_t x1;
int err = 0;
if (r == 0)
return -EINVAL;
if ((err = ssd1306_draw_vline(dev, fb, x0, y0 - r, 2 * r + 1, color))) // Center vertical line
return err;
while (y >= x)
{
if ((err = ssd1306_draw_vline(dev, fb, x0 - x, y0 - y, 2 * y + 1, color)))
return err;
if ((err = ssd1306_draw_vline(dev, fb, x0 + x, y0 - y, 2 * y + 1, color)))
return err;
if (color != OLED_COLOR_INVERT)
{
if ((err = ssd1306_draw_vline(dev, fb, x0 - y, y0 - x, 2 * x + 1, color)))
return err;
if ((err = ssd1306_draw_vline(dev, fb, x0 + y, y0 - x, 2 * x + 1, color)))
return err;
}
++x;
if (radius_err < 0) {
radius_err += 2 * x + 1;
}
else {
--y;
radius_err += 2 * (x - y + 1);
}
}
if (color == OLED_COLOR_INVERT)
{
x1 = x; // Save where we stopped
y = 1;
x = r;
radius_err = 1 - x;
if ((err = ssd1306_draw_hline(dev, fb, x0 + x1, y0, r - x1 + 1, color)))
return err;
if ((err = ssd1306_draw_hline(dev, fb, x0 - r, y0, r - x1 + 1, color)))
return err;
while (x >= y)
{
if ((err = ssd1306_draw_hline(dev, fb, x0 + x1, y0 - y, x - x1 + 1, color)))
return err;
if ((err = ssd1306_draw_hline(dev, fb, x0 + x1, y0 + y, x - x1 + 1, color)))
return err;
if ((err = ssd1306_draw_hline(dev, fb, x0 - x, y0 - y, x - x1 + 1, color)))
return err;
if ((err = ssd1306_draw_hline(dev, fb, x0 - x, y0 + y, x - x1 + 1, color)))
return err;
++y;
if (radius_err < 0) {
radius_err += 2 * y + 1;
}
else {
--x;
radius_err += 2 * (y - x + 1);
}
}
}
return 0;
}
int ssd1306_draw_line(const ssd1306_t *dev, uint8_t *fb, int16_t x0, int16_t y0,
int16_t x1, int16_t y1, ssd1306_color_t color)
{
if ((x0 >= dev->width) || (x0 < 0) || (y0 >= dev->height) || (y0 < 0))
return -EINVAL;
if ((x1 >= dev->width) || (x1 < 0) || (y1 >= dev->height) || (y1 < 0))
return -EINVAL;
int err;
bool steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t errx = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0 <= x1; x0++) {
if (steep) {
if ((err = ssd1306_draw_pixel(dev, fb, y0, x0, color)))
return err;
} else {
if ((err = ssd1306_draw_pixel(dev, fb, x0, y0, color)))
return err;
}
errx -= dy;
if (errx < 0) {
y0 += ystep;
errx += dx;
}
}
return 0;
}
int ssd1306_draw_triangle(const ssd1306_t *dev, uint8_t *fb, int16_t x0,
int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2,
ssd1306_color_t color)
{
int err;
if ((err = ssd1306_draw_line(dev, fb, x0, y0, x1, y1, color)))
return err;
if ((err = ssd1306_draw_line(dev, fb, x1, y1, x2, y2, color)))
return err;
return ssd1306_draw_line(dev, fb, x2, y2, x0, y0, color);
}
int ssd1306_fill_triangle(const ssd1306_t *dev, uint8_t *fb, int16_t x0,
int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2,
ssd1306_color_t color)
{
int16_t a, b, y, last;
int err;
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1) {
swap(y0, y1); swap(x0, x1);
}
if (y1 > y2) {
swap(y2, y1); swap(x2, x1);
}
if (y0 > y1) {
swap(y0, y1); swap(x0, x1);
}
if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if (x1 < a) a = x1;
else if (x1 > b) b = x1;
if (x2 < a) a = x2;
else if (x2 > b) b = x2;
if ((err = ssd1306_draw_hline(dev, fb, a, y0, b - a + 1, color)))
return err;
return 0;
}
int16_t
dx01 = x1 - x0,
dy01 = y1 - y0,
dx02 = x2 - x0,
dy02 = y2 - y0,
dx12 = x2 - x1,
dy12 = y2 - y1,
sa = 0,
sb = 0;
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if (y1 == y2) last = y1; // Include y1 scanline
else last = y1 - 1; // Skip it
for (y = y0; y <= last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if (a > b) swap(a, b);
if ((err = ssd1306_draw_hline(dev, fb, a, y, b - a + 1, color)))
return err;
}
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = dx12 * (y - y1);
sb = dx02 * (y - y0);
for (; y <= y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if (a > b) swap(a, b);
if ((err = ssd1306_draw_hline(dev, fb, a, y, b - a + 1, color)))
return err;
}
return 0;
}
int ssd1306_draw_char(const ssd1306_t *dev, uint8_t *fb,
const font_info_t *font, uint8_t x, uint8_t y, char c,
ssd1306_color_t foreground, ssd1306_color_t background)
{
uint8_t i, j;
const uint8_t *bitmap;
uint8_t line = 0;
int err;
if (font == NULL)
return 0;
const font_char_desc_t *d = font_get_char_desc(font, c);
if (d == NULL)
return 0;
bitmap = font->bitmap + d->offset;
for (j = 0; j < font->height; ++j) {
for (i = 0; i < d->width; ++i) {
if (i % 8 == 0) {
line = bitmap[(d->width + 7) / 8 * j + i / 8]; // line data
}
if (line & 0x80) {
err = ssd1306_draw_pixel(dev, fb, x + i, y + j, foreground);
}
else {
switch (background)
{
case OLED_COLOR_TRANSPARENT:
// Not drawing for transparent background
break;
case OLED_COLOR_WHITE:
case OLED_COLOR_BLACK:
err = ssd1306_draw_pixel(dev, fb, x + i, y + j, background);
break;
case OLED_COLOR_INVERT:
// I don't know why I need invert background
break;
}
}
if (err) return -ERANGE ;
line = line << 1;
}
}
return d->width;
}
int ssd1306_draw_string(const ssd1306_t *dev, uint8_t *fb,
const font_info_t *font, uint8_t x, uint8_t y, char *str,
ssd1306_color_t foreground, ssd1306_color_t background)
{
uint8_t t = x;
int err;
if (font == NULL || str == NULL)
return 0;
while (*str)
{
if ((err = ssd1306_draw_char(dev, fb, font, x, y, *str, foreground, background)) < 0 )
return err;
x += err;
++str;
if (*str)
x += font->c;
}
return x - t;
}
int ssd1306_stop_scroll(const ssd1306_t *dev)
{
return ssd1306_command(dev, SSD1306_SCROLL_DISABLE);
}
int ssd1306_start_scroll_hori(const ssd1306_t *dev, bool way, uint8_t start, uint8_t stop, ssd1306_scroll_t frame)
{
int err;
if (way)
{
if ((err = ssd1306_command(dev, SSD1306_SCROLL_HOR_LEFT)))
return err;
}
else
{
if ((err = ssd1306_command(dev, SSD1306_SCROLL_HOR_RIGHT)))
return err;
}
if (!ssd1306_command(dev, 0x00) && //dummy
!ssd1306_command(dev, (start&0x07)) &&
!ssd1306_command(dev, frame) &&
!ssd1306_command(dev, (stop&0x07)) &&
!ssd1306_command(dev, 0x00) && //dummy
!ssd1306_command(dev, 0xFF) && //dummy
!ssd1306_command(dev, SSD1306_SCROLL_ENABLE)) {
return 0;
}
return -EIO;
}
int ssd1306_start_scroll_hori_vert(const ssd1306_t *dev, bool way, uint8_t start, uint8_t stop, uint8_t dy, ssd1306_scroll_t frame)
{
//this function dont work well if no vertical setting.
if ((!dy) || (dy > 63))
return -EINVAL;
int err;
//vertical scrolling selection (all screen)
if ((err = ssd1306_command(dev, 0xA3)))
return err;
if ((err = ssd1306_command(dev, 0)))
return err;
if ((err = ssd1306_command(dev, dev->height)))
return err;
if (way)
{
if ((err = ssd1306_command(dev, SSD1306_SCROLL_HOR_VER_LEFT)))
return err;
}
else
{
if ((err = ssd1306_command(dev, SSD1306_SCROLL_HOR_VER_RIGHT)))
return err;
}
if (!ssd1306_command(dev, 0x00) && //dummy
!ssd1306_command(dev, (start&0x07)) &&
!ssd1306_command(dev, frame) &&
!ssd1306_command(dev, (stop&0x07)) &&
!ssd1306_command(dev, dy) &&
!ssd1306_command(dev, SSD1306_SCROLL_ENABLE)) {
return 0;
}
return -EIO;
}