admin 管理员组

文章数量: 887021


2024年1月17日发(作者:openstack概念)

mod = 8 - mod; // note - lookup table results in a nearly 10% performance // improvement in fill* functions // uint8_t mask = ~(0xFF >> mod); static const uint8_t PROGMEM premask[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; uint8_t mask = pgm_read_byte(&premask[mod]); // adjust the mask if we're not going to reach the end of this byte if(h < mod) mask &= (0XFF >> (mod - h)); switch(color) { case SSD1306_WHITE: *pBuf |= mask; break; case SSD1306_BLACK: *pBuf &= ~mask; break; case SSD1306_INVERSE: *pBuf ^= mask; break; } pBuf += WIDTH; } if(h >= mod) { // More to go? h -= mod; // Write solid bytes while we can - effectively 8 rows at a time if(h >= 8) { if(color == SSD1306_INVERSE) { // separate copy of the code so we don't impact performance of // black/white write version with an extra comparison per loop do { *pBuf ^= 0xFF; // Invert byte pBuf += WIDTH; // Advance pointer 8 rows h -= 8; // Subtract 8 rows from height } while(h >= 8); } else { // store a local value to work with uint8_t val = (color != SSD1306_BLACK) ? 255 : 0; do { *pBuf = val; // Set byte pBuf += WIDTH; // Advance pointer 8 rows h -= 8; // Subtract 8 rows from height } while(h >= 8); } } if(h) { // Do the final partial byte, if necessary mod = h & 7; // this time we want to mask the low bits of the byte, // vs the high bits we did above // uint8_t mask = (1 << mod) - 1; // note - lookup table results in a nearly 10% performance // improvement in fill* functions static const uint8_t PROGMEM postmask[8] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F }; uint8_t mask = pgm_read_byte(&postmask[mod]); switch(color) { case SSD1306_WHITE: *pBuf |= mask; break; case SSD1306_BLACK: *pBuf &= ~mask; break; case SSD1306_INVERSE: *pBuf ^= mask; break; } } } } // endif positive height } // endif x in bounds}3.3.4 drawLine—— 绘制线函数说明:

if (y0 > y1) { _swap_int16_t(y0, y1); _swap_int16_t(x0, x1); } startWrite(); 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; writeFastHLine(a, y0, b-a+1, color); endWrite(); return; } int16_t dx01 = x1 - x0, dy01 = y1 - y0, dx02 = x2 - x0, dy02 = y2 - y0, dx12 = x2 - x1, dy12 = y2 - y1; int32_t 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 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_int16_t(a,b); writeFastHLine(a, y, b-a+1, color); } // For lower part of triangle, find scanline crossings for segments // 0-2 and 1-2. This loop is skipped if y1=y2. sa = (int32_t)dx12 * (y - y1); sb = (int32_t)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_int16_t(a,b); writeFastHLine(a, y, b-a+1, color); } endWrite();}

// NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS. // THIS IS ON PURPOSE AND BY DESIGN. The background color feature // has typically been used with the 'classic' font to overwrite old // screen contents with new data. This ONLY works because the // characters are a uniform size; it's not a sensible thing to do with // proportionally-spaced fonts with glyphs of varying sizes (and that // may overlap). To replace previously-drawn text when using a custom // font, use the getTextBounds() function to determine the smallest // rectangle encompassing a string, erase the area with fillRect(), // then draw new text. This WILL infortunately 'blink' the text, but // is unavoidable. Drawing 'background' pixels will NOT fix this, // only creates a new set of problems. Have an idea to work around // this (a canvas object type for MCUs that can afford the RAM and // displays supporting setAddrWindow() and pushColors()), but haven't // implemented this yet. startWrite(); for(yy=0; yy

delay(1); } delay(250); isplay(); for(i=0; i<(); i+=4) { ne(0, ()-1, i, 0, SSD1306_WHITE); y(); delay(1); } for(i=()-1; i>=0; i-=4) { ne(0, ()-1, ()-1, i, SSD1306_WHITE); y(); delay(1); } delay(250); isplay(); for(i=()-1; i>=0; i-=4) { ne(()-1, ()-1, i, 0, SSD1306_WHITE); y(); delay(1); } for(i=()-1; i>=0; i-=4) { ne(()-1, ()-1, 0, i, SSD1306_WHITE); y(); delay(1); } delay(250); isplay(); for(i=0; i<(); i+=4) { ne(()-1, 0, 0, i, SSD1306_WHITE); y(); delay(1); } for(i=0; i<(); i+=4) { ne(()-1, 0, i, ()-1, SSD1306_WHITE); y(); delay(1); } delay(2000); // Pause for 2 seconds}void testdrawrect(void) { isplay(); for(int16_t i=0; i<()/2; i+=2) { ct(i, i, ()-2*i, ()-2*i, SSD1306_WHITE); y(); // Update screen with each newly-drawn rectangle delay(1); } delay(2000);}void testfillrect(void) { isplay(); for(int16_t i=0; i<()/2; i+=3) { // The INVERSE color is used so rectangles alternate white/black ct(i, i, ()-i*2, ()-i*2, SSD1306_INVERSE);

ct(i, i, ()-i*2, ()-i*2, SSD1306_INVERSE); y(); // Update screen with each newly-drawn rectangle delay(1); } delay(2000);}void testdrawcircle(void) { isplay(); for(int16_t i=0; i0; i-=3) { // The INVERSE color is used so circles alternate white/black rcle(() / 2, () / 2, i, SSD1306_INVERSE); y(); // Update screen with each newly-drawn circle delay(1); } delay(2000);}void testdrawroundrect(void) { isplay(); for(int16_t i=0; i<()/2-2; i+=2) { undRect(i, i, ()-2*i, ()-2*i, ()/4, SSD1306_WHITE); y(); delay(1); } delay(2000);}void testfillroundrect(void) { isplay(); for(int16_t i=0; i<()/2-2; i+=2) { // The INVERSE color is used so round-rects alternate white/black undRect(i, i, ()-2*i, ()-2*i, ()/4, SSD1306_INVERSE); y(); delay(1); } delay(2000);}void testdrawtriangle(void) { isplay(); for(int16_t i=0; i

()/2 , ()/2-i, ()/2-i, ()/2+i, ()/2+i, ()/2+i, SSD1306_WHITE); y(); delay(1); } delay(2000);}void testfilltriangle(void) { isplay(); for(int16_t i=max((),())/2; i>0; i-=5) { // The INVERSE color is used so triangles alternate white/black iangle( ()/2 , ()/2-i, ()/2-i, ()/2+i, ()/2+i, ()/2+i, SSD1306_INVERSE); y(); delay(1); } delay(2000);}void testdrawchar(void) { isplay(); tSize(1); // Normal 1:1 pixel scale tColor(SSD1306_WHITE); // Draw white text sor(0, 0); // Start at top-left corner 437(true); // Use full 256 char 'Code Page 437' font // Not all the characters will fit on the display. This is normal. // Library will draw what it can and the rest will be clipped. for(int16_t i=0; i<256; i++) { if(i == 'n') (' '); else (i); } y(); delay(2000);}void testdrawstyles(void) { isplay(); tSize(1); // Normal 1:1 pixel scale tColor(SSD1306_WHITE); // Draw white text sor(0,0); // Start at top-left corner n(F("Hello, world!")); tColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text n(3.141592); tSize(2); // Draw 2X-scale text tColor(SSD1306_WHITE); (F("0x")); n(0xDEADBEEF, HEX); y(); delay(2000);}void testscrolltext(void) { isplay();

tSize(2); // Draw 2X-scale text tColor(SSD1306_WHITE); sor(10, 0); n(F("scroll")); y(); // Show initial text delay(100); // Scroll in various directions, pausing in-between: crollright(0x00, 0x0F); delay(2000); roll(); delay(1000); crollleft(0x00, 0x0F); delay(2000); roll(); delay(1000); crolldiagright(0x00, 0x07); delay(2000); crolldiagleft(0x00, 0x07); delay(2000); roll(); delay(1000);}void testdrawbitmap(void) { isplay(); tmap( (() - LOGO_WIDTH ) / 2, (() - LOGO_HEIGHT) / 2, logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1); y(); delay(1000);}#define XPOS 0 // Indexes into the 'icons' array in function below#define YPOS 1#define DELTAY 2void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) { int8_t f, icons[NUMFLAKES][3]; // Initialize 'snowflake' positions for(f=0; f< NUMFLAKES; f++) { icons[f][XPOS] = random(1 - LOGO_WIDTH, ()); icons[f][YPOS] = -LOGO_HEIGHT; icons[f][DELTAY] = random(1, 6); (F("x: ")); (icons[f][XPOS], DEC); (F(" y: ")); (icons[f][YPOS], DEC); (F(" dy: ")); n(icons[f][DELTAY], DEC); } for(;;) { // isplay(); // Clear the display buffer // Draw each snowflake: for(f=0; f< NUMFLAKES; f++) { tmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE); } y(); // Show the display buffer on the screen


本文标签: 说明 概念 绘制 作者 函数