Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ssd1331 by
ssd1331.cpp
00001 00002 #include "mbed.h" 00003 #include "ssd1331.h" 00004 00005 #define countof(x) ( sizeof(x) / sizeof(x[0]) ) 00006 00007 static const char font6x8[0x60][6] = { 00008 { 0x00,0x00,0x00,0x00,0x00,0x00 } , /*SPC */ 00009 { 0x00,0x00,0x5F,0x00,0x00,0x00 } , /* ! */ 00010 { 0x04,0x03,0x04,0x03,0x00,0x00 } , /* " */ 00011 { 0x28,0x7E,0x14,0x3F,0x0A,0x00 } , /* # */ 00012 { 0x24,0x2A,0x7F,0x2A,0x12,0x00 } , /* $ */ 00013 { 0x23,0x13,0x08,0x64,0x62,0x00 } , /* % */ 00014 { 0x30,0x4E,0x59,0x26,0x50,0x00 } , /* & */ 00015 { 0x00,0x00,0x02,0x01,0x00,0x00 } , /* ' */ 00016 { 0x00,0x00,0x1C,0x22,0x41,0x00 } , /* ( */ 00017 { 0x41,0x22,0x1C,0x00,0x00,0x00 } , /* ) */ 00018 { 0x22,0x14,0x08,0x14,0x22,0x00 } , /* * */ 00019 { 0x08,0x08,0x3E,0x08,0x08,0x00 } , /* + */ 00020 { 0x50,0x30,0x00,0x00,0x00,0x00 } , /* , */ 00021 { 0x08,0x08,0x08,0x08,0x08,0x00 } , /* - */ 00022 { 0x60,0x60,0x00,0x00,0x00,0x00 } , /* . */ 00023 { 0x20,0x10,0x08,0x04,0x02,0x00 } , /* / */ 00024 { 0x3E,0x51,0x49,0x45,0x3E,0x00 } , /* 0 */ 00025 { 0x00,0x42,0x7F,0x40,0x00,0x00 } , /* 1 */ 00026 { 0x62,0x51,0x49,0x49,0x46,0x00 } , /* 2 */ 00027 { 0x22,0x41,0x49,0x49,0x36,0x00 } , /* 3 */ 00028 { 0x18,0x14,0x12,0x7F,0x10,0x00 } , /* 4 */ 00029 { 0x2F,0x45,0x45,0x45,0x39,0x00 } , /* 5 */ 00030 { 0x3E,0x49,0x49,0x49,0x32,0x00 } , /* 6 */ 00031 { 0x01,0x61,0x19,0x05,0x03,0x00 } , /* 7 */ 00032 { 0x36,0x49,0x49,0x49,0x36,0x00 } , /* 8 */ 00033 { 0x26,0x49,0x49,0x49,0x3E,0x00 } , /* 9 */ 00034 { 0x00,0x36,0x36,0x00,0x00,0x00 } , /* : */ 00035 { 0x00,0x56,0x36,0x00,0x00,0x00 } , /* ; */ 00036 { 0x00,0x08,0x14,0x22,0x41,0x00 } , /* < */ 00037 { 0x14,0x14,0x14,0x14,0x14,0x00 } , /* = */ 00038 { 0x41,0x22,0x14,0x08,0x00,0x00 } , /* > */ 00039 { 0x02,0x01,0x59,0x09,0x06,0x00 } , /* ? */ 00040 { 0x3E,0x41,0x5D,0x55,0x2E,0x00 } , /* @ */ 00041 { 0x60,0x1C,0x13,0x1C,0x60,0x00 } , /* A */ 00042 { 0x7F,0x49,0x49,0x49,0x36,0x00 } , /* B */ 00043 { 0x3E,0x41,0x41,0x41,0x22,0x00 } , /* C */ 00044 { 0x7F,0x41,0x41,0x22,0x1C,0x00 } , /* D */ 00045 { 0x7F,0x49,0x49,0x49,0x41,0x00 } , /* E */ 00046 { 0x7F,0x09,0x09,0x09,0x01,0x00 } , /* F */ 00047 { 0x1C,0x22,0x41,0x49,0x3A,0x00 } , /* G */ 00048 { 0x7F,0x08,0x08,0x08,0x7F,0x00 } , /* H */ 00049 { 0x00,0x41,0x7F,0x41,0x00,0x00 } , /* I */ 00050 { 0x20,0x40,0x40,0x40,0x3F,0x00 } , /* J */ 00051 { 0x7F,0x08,0x14,0x22,0x41,0x00 } , /* K */ 00052 { 0x7F,0x40,0x40,0x40,0x00,0x00 } , /* L */ 00053 { 0x7F,0x04,0x18,0x04,0x7F,0x00 } , /* M */ 00054 { 0x7F,0x04,0x08,0x10,0x7F,0x00 } , /* N */ 00055 { 0x3E,0x41,0x41,0x41,0x3E,0x00 } , /* O */ 00056 { 0x7F,0x09,0x09,0x09,0x06,0x00 } , /* P */ 00057 { 0x3E,0x41,0x51,0x21,0x5E,0x00 } , /* Q */ 00058 { 0x7F,0x09,0x19,0x29,0x46,0x00 } , /* R */ 00059 { 0x26,0x49,0x49,0x49,0x32,0x00 } , /* S */ 00060 { 0x01,0x01,0x7F,0x01,0x01,0x00 } , /* T */ 00061 { 0x3F,0x40,0x40,0x40,0x3F,0x00 } , /* U */ 00062 { 0x03,0x1C,0x60,0x1C,0x03,0x00 } , /* V */ 00063 { 0x0F,0x70,0x0F,0x70,0x0F,0x00 } , /* W */ 00064 { 0x41,0x36,0x08,0x36,0x41,0x00 } , /* X */ 00065 { 0x01,0x06,0x78,0x02,0x01,0x00 } , /* Y */ 00066 { 0x61,0x51,0x49,0x45,0x43,0x00 } , /* Z */ 00067 { 0x00,0x00,0x7F,0x41,0x41,0x00 } , /* [ */ 00068 { 0x15,0x16,0x7C,0x16,0x11,0x00 } , /* \ */ 00069 { 0x41,0x41,0x7F,0x00,0x00,0x00 } , /* ] */ 00070 { 0x00,0x02,0x01,0x02,0x00,0x00 } , /* ^ */ 00071 { 0x40,0x40,0x40,0x40,0x40,0x00 } , /* _ */ 00072 { 0x00,0x01,0x02,0x00,0x00,0x00 } , /* ` */ 00073 { 0x00,0x20,0x54,0x54,0x78,0x00 } , /* a */ 00074 { 0x00,0x7F,0x44,0x44,0x38,0x00 } , /* b */ 00075 { 0x00,0x38,0x44,0x44,0x28,0x00 } , /* c */ 00076 { 0x00,0x38,0x44,0x44,0x7F,0x00 } , /* d */ 00077 { 0x00,0x38,0x54,0x54,0x18,0x00 } , /* e */ 00078 { 0x00,0x04,0x3E,0x05,0x01,0x00 } , /* f */ 00079 { 0x00,0x08,0x54,0x54,0x3C,0x00 } , /* g */ 00080 { 0x00,0x7F,0x04,0x04,0x78,0x00 } , /* h */ 00081 { 0x00,0x00,0x7D,0x00,0x00,0x00 } , /* i */ 00082 { 0x00,0x40,0x40,0x3D,0x00,0x00 } , /* j */ 00083 { 0x00,0x7F,0x10,0x28,0x44,0x00 } , /* k */ 00084 { 0x00,0x01,0x7F,0x00,0x00,0x00 } , /* l */ 00085 { 0x7C,0x04,0x7C,0x04,0x78,0x00 } , /* m */ 00086 { 0x00,0x7C,0x04,0x04,0x78,0x00 } , /* n */ 00087 { 0x00,0x38,0x44,0x44,0x38,0x00 } , /* o */ 00088 { 0x00,0x7C,0x14,0x14,0x08,0x00 } , /* p */ 00089 { 0x00,0x08,0x14,0x14,0x7C,0x00 } , /* q */ 00090 { 0x00,0x7C,0x08,0x04,0x04,0x00 } , /* r */ 00091 { 0x00,0x48,0x54,0x54,0x24,0x00 } , /* s */ 00092 { 0x00,0x04,0x3E,0x44,0x40,0x00 } , /* t */ 00093 { 0x00,0x3C,0x40,0x40,0x7C,0x00 } , /* u */ 00094 { 0x00,0x7C,0x20,0x10,0x0C,0x00 } , /* v */ 00095 { 0x1C,0x60,0x1C,0x60,0x1C,0x00 } , /* w */ 00096 { 0x00,0x6C,0x10,0x10,0x6C,0x00 } , /* x */ 00097 { 0x00,0x4C,0x50,0x30,0x1C,0x00 } , /* y */ 00098 { 0x00,0x44,0x64,0x54,0x4C,0x00 } , /* z */ 00099 { 0x00,0x08,0x36,0x41,0x41,0x00 } , /* { */ 00100 { 0x00,0x00,0x7F,0x00,0x00,0x00 } , /* | */ 00101 { 0x41,0x41,0x36,0x08,0x00,0x00 } , /* } */ 00102 { 0x08,0x04,0x08,0x10,0x08,0x00 } , /* ~ */ 00103 { 0x00,0x00,0x00,0x00,0x00,0x00 } /*null*/ 00104 }; 00105 00106 ssd1331::ssd1331(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin) 00107 : CS(cs_pin), RES(rst_pin), DC(a0_pin), spi(mosi_pin, miso_pin, sclk_pin) 00108 { 00109 Init(); 00110 } 00111 00112 void ssd1331::Init(void) 00113 { 00114 spi.format(8,3); 00115 spi.frequency(24000000); // 12Mhz max for KL25z 00116 00117 // reset 00118 wait_ms(200); 00119 RES = 0; //Reset active 00120 wait_ms(200); 00121 RES = 1; 00122 00123 // initialize sequence 00124 RegWrite(0xAE); //OLED display OFF 00125 RegWrite(0x75); /* Set Row Address */ 00126 RegWrite(0x00); /* Start = 0 */ 00127 RegWrite(0x3F); /* End = 63 */ 00128 RegWrite(0x15); /* Set Column Address */ 00129 RegWrite(0x00); /* Start = 0 */ 00130 RegWrite(0x5F); /* End = 95 */ 00131 RegWrite(0xA0); //Set remap & data format 0111 0000 00132 RegWrite(0x72); // RGB colour 00133 RegWrite(0xA1); //set display start row RAM 00134 RegWrite(0x00); 00135 RegWrite(0xA2); //set dispaly offset 00136 RegWrite(0x00); 00137 RegWrite(0xA4); //Set Display Mode 00138 RegWrite(0xA8); //Set Multiplex Ratio 00139 RegWrite(0x3F); 00140 RegWrite(0xAD); //Set Master Configuration 00141 RegWrite(0x8F); //(External VCC Supply Selected) 00142 RegWrite(0xB0); //Set Power Saving Mode 00143 RegWrite(0x1A); 00144 RegWrite(0xB1); //Set Phase 1 & 2 Period Adjustment 00145 RegWrite(0x74); 00146 RegWrite(0xB3); //Set Display Clock Divide Ratio / Oscillator Frequency 00147 RegWrite(0xD0); 00148 RegWrite(0x8A); //Set Second Pre-charge Speed of Color A 00149 RegWrite(0x81); 00150 RegWrite(0x8B); //Set Second Pre-charge Speed of Color B 00151 RegWrite(0x82); 00152 RegWrite(0x8C); //Set Second Pre-charge Speed of Color C 00153 RegWrite(0x83); 00154 RegWrite(0xBB); //Set Pre-charge Level 00155 RegWrite(0x3E); 00156 RegWrite(0xBE); //Set VCOMH 00157 RegWrite(0x3E); 00158 RegWrite(0x87); //Set Master Current Control 00159 RegWrite(0x0F); 00160 RegWrite(0x81); //Set Contrast Control for Color gAh 00161 RegWrite(0x80); 00162 RegWrite(0x82); //Set Contrast Control for Color gBh 00163 RegWrite(0x80); 00164 RegWrite(0x83); //Set Contrast Control for Color gCh 00165 RegWrite(0x80); 00166 RegWrite(0xAF); //display ON 00167 00168 chr_size = NORMAL; 00169 cls(); 00170 } 00171 00172 void ssd1331::on() 00173 { 00174 RegWrite(display_on); 00175 } 00176 00177 void ssd1331::off() 00178 { 00179 RegWrite(display_off); 00180 } 00181 00182 void ssd1331::cls() 00183 { 00184 unsigned char cmd[6]= {GAC_CLEAR_WINDOW,0,0,width,height}; 00185 RegWriteM(cmd,5); 00186 wait_us(500); 00187 Maxwindow(); 00188 background(0); 00189 } 00190 00191 void ssd1331::set_font(unsigned char* f) 00192 { 00193 font = f; 00194 if (font==NULL) { 00195 externalfont=0; // set display.font 00196 } else { 00197 externalfont=1; 00198 } 00199 } 00200 00201 void ssd1331::PutChar(uint8_t column,uint8_t row, int value) 00202 { 00203 if(externalfont) { // external font 00204 uint8_t hor,vert,offset,bpl,j,i,b; 00205 const unsigned char* sign; 00206 unsigned char z,w; 00207 // read font parameter from start of array 00208 offset = font[0]; // bytes / char 00209 hor = font[1]; // get hor size of font 00210 vert = font[2]; // get vert size of font 00211 bpl = font[3]; // bytes per line 00212 if(value == '\n') { 00213 char_x = 0; 00214 char_y = char_y + vert; 00215 } 00216 if ((value < 31) || (value > 127)) return; // test char range 00217 if (char_x + hor > 95) { 00218 char_x = 0; 00219 char_y = char_y + vert; 00220 if (char_y >= 63 - font[2]) { 00221 char_y = 0; 00222 } 00223 } 00224 window(char_x, char_y,hor,vert); 00225 sign = &font[((value -32) * offset) + 4]; 00226 w = sign[0]; 00227 for (j=0; j<vert; j++) { 00228 for (i=0; i<hor; i++) { 00229 z = sign[bpl * i + ((j & 0xF8) >> 3)+1]; 00230 b = 1 << (j & 0x07); 00231 if (( z & b ) == 0x00) { 00232 putp(BGround_Color); 00233 } else { 00234 putp(Char_Color); 00235 } 00236 } 00237 } 00238 if ((w + 2) < hor) { // x offset to next char 00239 char_x += w + 2; 00240 } else char_x += hor; 00241 00242 } else { 00243 // internal font 00244 if(value == '\n') { 00245 char_x = 0; 00246 char_y = char_y + Y_height; 00247 } 00248 if ((value < 31) || (value > 127)) return; // test char range 00249 if (char_x + X_width > width) { 00250 char_x = 0; 00251 char_y = char_y + Y_height; 00252 if (char_y >= height - Y_height) { 00253 char_y = 0; 00254 } 00255 } 00256 int i,j,w,lpx,lpy,k,l,xw; 00257 unsigned char Temp=0; 00258 j = 0; i = 0; 00259 w = X_width; 00260 FontSizeConvert(&lpx, &lpy); 00261 xw = X_width; 00262 00263 for(i=0; i<xw; i++) { 00264 for ( l=0; l<lpx; l++) { 00265 Temp = font6x8[value-32][i]; 00266 for(j=Y_height-1; j>=0; j--) { 00267 for (k=0; k<lpy; k++) { 00268 pixel(char_x+(i*lpx)+l, char_y+(((j+1)*lpy)-1)-k, ((Temp & 0x80)==0x80) ? Char_Color : BGround_Color); 00269 } 00270 Temp = Temp << 1; 00271 } 00272 } 00273 } 00274 FontSizeConvert(&lpx, &lpy); 00275 char_x += (w*lpx); 00276 } 00277 } 00278 00279 void ssd1331::dim() 00280 { 00281 unsigned char cmd[5]= {GAC_DIM_WINDOW,0,0,width,height}; 00282 RegWriteM(cmd,5); 00283 } 00284 00285 void ssd1331::contrast(char value) 00286 { 00287 int v = value*20; 00288 if(v>180) { 00289 v=180; 00290 } 00291 unsigned char cmd[7]; 00292 cmd[0] = contrastA; 00293 cmd[1] = v; 00294 cmd[2] = contrastB; 00295 cmd[3] = v; 00296 cmd[4] = contrastC; 00297 cmd[5] = v; 00298 RegWriteM(cmd, 6); 00299 } 00300 00301 uint16_t ssd1331::toRGB(uint16_t R,uint16_t G,uint16_t B) 00302 { 00303 uint16_t c; 00304 c = R >> 3; 00305 c <<= 6; 00306 c |= G >> 2; 00307 c <<= 5; 00308 c |= B >> 3; 00309 return c; 00310 } 00311 00312 void ssd1331::rect(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t colorline) 00313 { 00314 if ( x1 > width ) x1 = width; 00315 if ( y1 > height ) y1 = height; 00316 if ( x2 > width ) x2 = width; 00317 if ( y2 > height ) y2 = height; 00318 00319 unsigned char cmd[11]= { 0 }; 00320 cmd[0] = GAC_FILL_ENABLE_DISABLE; 00321 cmd[1] = 0; // fill 1, empty 0 00322 RegWriteM(cmd, 2); 00323 cmd[0] = GAC_DRAW_RECTANGLE; 00324 cmd[1] = (unsigned char)x1; 00325 cmd[2] = (unsigned char)y1; 00326 cmd[3] = (unsigned char)x2; 00327 cmd[4] = (unsigned char)y2; 00328 cmd[5] = (unsigned char)((colorline>> 11) << 1); // Outline Blue 00329 cmd[6] = (unsigned char)((colorline>> 5 ) & 0x3F); // Outline Green 00330 cmd[7] = (unsigned char)((colorline<< 1 ) & 0x3F); // Outline Red 00331 cmd[8] = (0); 00332 cmd[9] = (0); 00333 cmd[10]= (0); 00334 RegWriteM(cmd, 11); 00335 wait_us(500); 00336 } 00337 00338 void ssd1331::fillrect(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t colorline,uint16_t colorfill) 00339 { 00340 if ( x1 > width ) x1 = width; 00341 if ( y1 > height ) y1 = height; 00342 if ( x2 > width ) x2 = width; 00343 if ( y2 > height ) y2 = height; 00344 00345 unsigned char cmd[11]= { 0 }; 00346 cmd[0] = GAC_FILL_ENABLE_DISABLE; 00347 cmd[1] = 1; // fill 1, empty 0 00348 RegWriteM(cmd, 2); 00349 cmd[0] = GAC_DRAW_RECTANGLE; 00350 cmd[1] = (unsigned char)x1; 00351 cmd[2] = (unsigned char)y1; 00352 cmd[3] = (unsigned char)x2; 00353 cmd[4] = (unsigned char)y2; 00354 cmd[5] = (unsigned char)((colorline>> 11) << 1); // Outline Blue 00355 cmd[6] = (unsigned char)((colorline>> 5 ) & 0x3F); // Outline Green 00356 cmd[7] = (unsigned char)((colorline<< 1 ) & 0x3F); // Outline Red 00357 cmd[8] = (unsigned char)((colorfill>> 11) << 1); // fill Blue 00358 cmd[9] = (unsigned char)((colorfill>> 5 ) & 0x3F); // fill Green 00359 cmd[10]= (unsigned char)((colorfill<< 1 ) & 0x3F); // fill Red 00360 RegWriteM(cmd, 11); 00361 wait_us(500); 00362 } 00363 00364 void ssd1331::line(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint16_t color) 00365 { 00366 if ( x1 > width ) x1 = width; 00367 if ( y1 > height ) y1 = height; 00368 if ( x2 > width ) x2 = width; 00369 if ( y2 > height ) y2 = height; 00370 00371 unsigned char cmd[11]= { 0 }; 00372 cmd[0] = GAC_FILL_ENABLE_DISABLE; 00373 cmd[1] = 0; // fill 0, empty 0 00374 RegWriteM(cmd, 2); 00375 cmd[0] = GAC_DRAW_LINE; 00376 cmd[1] = (unsigned char)x1; 00377 cmd[2] = (unsigned char)y1; 00378 cmd[3] = (unsigned char)x2; 00379 cmd[4] = (unsigned char)y2; 00380 cmd[5] = (unsigned char)(((color>>11)&0x1F)<<1); // Blue 00381 cmd[6] = (unsigned char)((color>>5)&0x3F); // Green 00382 cmd[7] = (unsigned char)((color&0x1F)<<1); // Red 00383 RegWriteM(cmd, 8); 00384 wait_us(500); 00385 } 00386 00387 void ssd1331::circle (uint8_t radius, uint8_t x, uint8_t y , uint16_t col, uint16_t fill) 00388 { 00389 if ( x > width ) x = width; 00390 if ( y > height ) y = height; 00391 00392 int16_t cx, cy, d; 00393 d = 3 - 2 * radius; 00394 cy = radius; 00395 pixel(x, radius+y, col); 00396 pixel(x, -radius+y, col); 00397 pixel(radius+x, y, col); 00398 pixel(-radius+x, y, col); 00399 if(fill) { 00400 line(x,radius+y,x,-radius+y,col); 00401 line( radius+x,y,-radius+x,y,col); 00402 } 00403 00404 for (cx = 0; cx <= cy; cx++) { 00405 if(d>=0) { 00406 d+=10+4*cx-4*cy; 00407 cy--; 00408 } else { 00409 d+=6+4*cx; 00410 } 00411 pixel(cy+x, cx+y, col); 00412 pixel(cx+x, cy+y, col); 00413 pixel(-cx+x, cy+y, col); 00414 pixel(-cy+x, cx+y, col); 00415 pixel(-cy+x, -cx+y, col); 00416 pixel(-cx+x, -cy+y, col); 00417 pixel(cx+x, -cy+y, col); 00418 pixel(cy+x, -cx+y, col); 00419 if(fill) { 00420 line(cy+x, cx+y, cy+x, -cx+y, col); 00421 line(cx+x, cy+y, cx+x, -cy + y, col); 00422 line(-cx+x, cy+y, -cx+x, cy+y, col); 00423 line(-cy+x, cx+y, -cy+x, cx+y, col); 00424 line(-cy+x, -cx+y, -cy+x, cx+y, col); 00425 line(-cx+x, -cy+y, -cx+x, cy+y, col); 00426 line(cx+x, -cy+y, cx+x, cy+y, col); 00427 line(cy+x, -cx+y, cy+x, cx+y, col); 00428 } 00429 } 00430 } 00431 00432 void ssd1331::ScrollSet(int8_t horizontal, int8_t startline, int8_t linecount, int8_t vertical , int8_t frame_interval) 00433 { 00434 unsigned char cmd[7]; 00435 if((startline>height+1)||((startline+linecount)>height+1)) return ; 00436 if ( frame_interval > 3 ) frame_interval = 3; 00437 cmd[0] = SCROLL_SETUP; // 00438 cmd[1] = horizontal; 00439 cmd[2] = startline; 00440 cmd[3] = linecount; 00441 cmd[4] = vertical; 00442 cmd[5] = frame_interval; 00443 RegWriteM(cmd,6); 00444 } 00445 00446 void ssd1331::Bitmap(const uint8_t *bitmap, uint8_t x, uint8_t y, uint8_t w, uint8_t h, unsigned char color) { 00447 00448 window(x, y, w, h); 00449 int16_t i, j, byteWidth = (w + 7) / 8; 00450 for(j=0; j<h; j++) { 00451 for(i=0; i<w; i++ ) { 00452 if(bitmap[ j * byteWidth + i / 8] & (128 >> (i & 7))) { 00453 pixel(x+i, y+j, color); 00454 } 00455 } 00456 } 00457 Maxwindow(); 00458 } 00459 00460 void ssd1331::Bitmap16(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *bitmap) 00461 { 00462 int8_t i,j; 00463 int8_t padd; 00464 unsigned short *bitmap_ptr = (unsigned short *)bitmap; 00465 00466 window(x, y, w, h); 00467 // the lines are padded to multiple of 4 bytes in a bitmap 00468 padd = -1; 00469 do { 00470 padd ++; 00471 } while (2*(w + padd)%4 != 0); 00472 00473 window(x, y, w, h); 00474 00475 bitmap_ptr += ((h - 1)* (w + padd)); 00476 00477 for (j = 0; j < h; j++) { //Lines 00478 for (i = 0; i < w; i++) { // one line 00479 DataWrite_to(*bitmap_ptr); 00480 bitmap_ptr++; 00481 } 00482 bitmap_ptr -= 2*w; 00483 bitmap_ptr -= padd; 00484 } 00485 Maxwindow(); 00486 } 00487 00488 int ssd1331::Bitmap16RAM(uint8_t x, uint8_t y, unsigned char *Name_BMP) 00489 { 00490 #define OffsetPixelWidth 18 00491 #define OffsetPixelHeigh 22 00492 #define OffsetFileSize 34 00493 #define OffsetPixData 10 00494 #define OffsetBPP 28 00495 char filename[50]; 00496 unsigned char BMP_Header[54]; 00497 unsigned short BPP_t; 00498 unsigned int PixelWidth,PixelHeigh,start_data; 00499 char * buffer; 00500 size_t result; 00501 int fileSize,padd,i,j; 00502 00503 i=0; 00504 while (*Name_BMP!='\0') { 00505 filename[i++]=*Name_BMP++; 00506 } 00507 00508 filename[i] = 0; 00509 FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file 00510 if (!Image) { 00511 return(0); // error file not found ! 00512 } 00513 00514 fread(&BMP_Header[0],1,54,Image); // get the BMP Header 00515 00516 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte 00517 fclose(Image); 00518 return(-1); // error not BMP file 00519 } 00520 00521 BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); 00522 if (BPP_t != 0x0010) { 00523 fclose(Image); 00524 return(-2); // error not 16 bit BMP 00525 } 00526 PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); 00527 PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); 00528 if (PixelHeigh > height+1 + y || PixelWidth > width+1 + x) { 00529 fclose(Image); 00530 return(-3); // to big 00531 } 00532 00533 start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); 00534 00535 // obtain file size: 00536 fseek (Image , 0 , SEEK_END); 00537 fileSize = ftell (Image)-start_data; 00538 //rewind (Image); 00539 00540 // allocate memory to contain the whole file: 00541 buffer = (char*) malloc (sizeof(char)*fileSize/2); 00542 if (buffer == NULL) {return (fileSize/2);} 00543 00544 // copy the file into the buffer: 00545 fseek (Image, start_data , SEEK_SET ); // set SD file data start position 00546 result = fread (buffer,1,fileSize,Image); 00547 fclose (Image); 00548 if (result != fileSize) {return (-5);} 00549 00550 unsigned short *bitmap_ptr = (unsigned short *)buffer; 00551 00552 // the lines are padded to multiple of 4 bytes in a bitmap 00553 padd = -1; 00554 do { 00555 padd ++; 00556 } while (2*(PixelWidth + padd)%4 != 0); 00557 00558 bitmap_ptr += ((PixelHeigh - 1)* (PixelWidth + padd)); 00559 00560 window(x, y, PixelWidth, PixelHeigh); 00561 00562 for (j = 0; j < PixelHeigh; j++) { //Lines 00563 for (i = 0; i < PixelWidth; i++) { // one line 00564 DataWrite_to(*bitmap_ptr); 00565 bitmap_ptr++; 00566 } 00567 bitmap_ptr -= 2*PixelWidth ; 00568 bitmap_ptr -= padd; 00569 } 00570 // terminate 00571 free (buffer); 00572 Maxwindow(); 00573 return (fileSize); 00574 } 00575 00576 int ssd1331::Bitmap16FS(uint8_t x, uint8_t y, unsigned char *Name_BMP) { 00577 #define OffsetPixelWidth 18 00578 #define OffsetPixelHeigh 22 00579 #define OffsetFileSize 34 00580 #define OffsetPixData 10 00581 #define OffsetBPP 28 00582 char filename[50]; 00583 unsigned char BMP_Header[54]; 00584 unsigned short BPP_t; 00585 unsigned int PixelWidth,PixelHeigh,start_data; 00586 unsigned int i,off; 00587 int padd,j; 00588 unsigned short *line; 00589 00590 i=0; 00591 while (*Name_BMP!='\0') { 00592 filename[i++]=*Name_BMP++; 00593 } 00594 00595 filename[i] = 0; 00596 FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file 00597 if (!Image) { 00598 return(0); // error file not found ! 00599 } 00600 00601 fread(&BMP_Header[0],1,54,Image); // get the BMP Header 00602 00603 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte 00604 fclose(Image); 00605 return(-1); // error no BMP file 00606 } 00607 BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); 00608 if (BPP_t != 0x0010) { 00609 fclose(Image); 00610 return(-2); // error no 16 bit BMP 00611 } 00612 PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); 00613 PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); 00614 if (PixelHeigh > height+1 + y || PixelWidth > width+1 + x) { 00615 fclose(Image); 00616 return(-3); // to big 00617 } 00618 start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); 00619 00620 line = (unsigned short *) malloc (PixelWidth); // we need a buffer for a line 00621 if (line == NULL) { 00622 return(-4); // error no memory 00623 } 00624 // the lines are padded to multiple of 4 bytes 00625 padd = -1; 00626 do { 00627 padd ++; 00628 } while ((PixelWidth * 2 + padd)%4 != 0); 00629 00630 window(x, y,PixelWidth,PixelHeigh); 00631 00632 for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up 00633 off = j * (PixelWidth * 2 + padd) + start_data; // start of line 00634 fseek(Image, off ,SEEK_SET); 00635 fread(line,1,PixelWidth * 2,Image); // read a line - slow ! 00636 for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT 00637 DataWrite_to(line[i]); 00638 } 00639 } 00640 free (line); 00641 fclose(Image); 00642 Maxwindow(); 00643 return(PixelWidth); 00644 } 00645 00646 void ssd1331::Copy(uint8_t src_x1,uint8_t src_y1,uint8_t src_x2,uint8_t src_y2,uint8_t dst_x,uint8_t dst_y) 00647 { 00648 unsigned char cmd[8]= { 0 }; 00649 if ((src_x1>width)||(src_y1>height)||(src_x2>width)||(src_y2>height)) return; 00650 if ((dst_x>width)||(dst_y>height))return; 00651 cmd[0] = GAC_COPY_AREA; // 00652 cmd[1] = (unsigned char)src_x1; 00653 cmd[2] = (unsigned char)src_y1; 00654 cmd[3] = (unsigned char)src_x2; 00655 cmd[4] = (unsigned char)src_y2; 00656 cmd[5] = (unsigned char)dst_x; 00657 cmd[6] = (unsigned char)dst_y; 00658 RegWriteM(cmd,7); 00659 } 00660 00661 void ssd1331::Fill_Screen(uint16_t color) 00662 { 00663 BGround_Color = color; 00664 fillrect(0,0,width,height,color,color); 00665 } 00666 00667 void ssd1331::locate(uint8_t column, uint8_t row) 00668 { 00669 char_x = column; 00670 char_y = row; 00671 } 00672 00673 void ssd1331::foreground(uint16_t color) 00674 { 00675 Char_Color = color; 00676 } 00677 void ssd1331::background(uint16_t color) 00678 { 00679 BGround_Color = color; 00680 } 00681 00682 void ssd1331::SetFontSize(uint8_t Csize) 00683 { 00684 chr_size = Csize; 00685 } 00686 00687 void ssd1331::putp(int colour) 00688 { 00689 pixel(_x, _y, colour); 00690 _x++; 00691 if(_x > _x2) { 00692 _x = _x1; 00693 _y++; 00694 if(_y > _y2) { 00695 _y = _y1; 00696 } 00697 } 00698 } 00699 00700 int ssd1331::_putc( int c ) 00701 { 00702 PutChar( char_x , char_y ,c); 00703 return c; 00704 } 00705 00706 void ssd1331::pixel(uint8_t x,uint8_t y,uint16_t Color) 00707 { 00708 unsigned char cmd[7]= {Set_Column_Address,0x00,0x00,Set_Row_Address,0x00,0x00}; 00709 if ((x>width)||(y>height)) return ; 00710 cmd[1] = (unsigned char)x; 00711 cmd[2] = (unsigned char)x; 00712 cmd[4] = (unsigned char)y; 00713 cmd[5] = (unsigned char)y; 00714 RegWriteM(cmd, 6); 00715 DataWrite_to(Color); 00716 } 00717 00718 void ssd1331::Maxwindow() 00719 { 00720 unsigned char cmd[7]= {Set_Column_Address,0x00,0x5F,Set_Row_Address,0x00,0x3F}; 00721 RegWriteM(cmd, 6); 00722 } 00723 00724 void ssd1331::window(uint8_t x, uint8_t y, uint8_t w, uint8_t h) 00725 { 00726 _x = x; 00727 _y = y; 00728 // window settings 00729 _x1 = x; // start y 00730 _x2 = x + w - 1; // end x 00731 _y1 = y; // start y 00732 _y2 = y + h - 1; // end y 00733 unsigned char cmd[7]= {Set_Column_Address,0x00,0x00,Set_Row_Address,0x00,0x00}; 00734 RegWriteM(cmd, 6); 00735 unsigned char cmd2[7]= {Set_Column_Address,_x1,_x2,Set_Row_Address,_y1,_y2}; 00736 RegWriteM(cmd2, 6); 00737 } 00738 00739 void ssd1331::Scrollstart() 00740 { 00741 RegWrite(SCROLL_START); 00742 } 00743 00744 void ssd1331::Scrollstop() 00745 { 00746 RegWrite(SCROLL_STOP); 00747 } 00748 00749 int ssd1331::_getc() 00750 { 00751 return -1; 00752 } 00753 00754 uint8_t ssd1331::row() 00755 { 00756 return char_y; 00757 } 00758 uint8_t ssd1331::column() 00759 { 00760 return char_x; 00761 } 00762 00763 void ssd1331::FontSizeConvert(int *lpx,int *lpy) 00764 { 00765 switch( chr_size ) { 00766 case WIDE: 00767 *lpx=2; 00768 *lpy=1; 00769 break; 00770 case HIGH: 00771 *lpx=1; 00772 *lpy=2; 00773 break; 00774 case WH : 00775 *lpx=2; 00776 *lpy=2; 00777 break; 00778 case WHx36 : 00779 *lpx=6; 00780 *lpy=6; 00781 break; 00782 case NORMAL: 00783 default: 00784 *lpx=1; 00785 *lpy=1; 00786 break; 00787 } 00788 } 00789 00790 void ssd1331::RegWrite(unsigned char Command) 00791 { 00792 DC = 0; // Command 00793 CS = 0; // CS enable 00794 spi.write(Command); 00795 CS = 1; // CS dissable 00796 } 00797 00798 void ssd1331::RegWriteM(unsigned char *Command, uint8_t count) 00799 { 00800 int i; 00801 DC = 0; // Command 00802 CS = 0; // CS enable 00803 for( i=0; i<count; i++) { 00804 spi.write(*Command++); 00805 } 00806 CS = 1; // CS dissable 00807 } 00808 00809 void ssd1331::DataWrite(unsigned char c) 00810 { 00811 DC = 1; // DATA 00812 CS = 0; // CS enable 00813 spi.write(c); 00814 CS = 1; // CS dissable 00815 } 00816 00817 void ssd1331::DataWrite_to(uint16_t Dat) 00818 { 00819 DC = 1; // DATA 00820 CS = 0; // CS enable 00821 spi.write((unsigned char)((Dat >> 8))); 00822 spi.write((unsigned char)(Dat)); 00823 CS = 1; // CS dissable 00824 } 00825 00826 00827 00828 00829
Generated on Sat Jul 16 2022 23:15:18 by
1.7.2
