work fine

Dependencies:   mbed

Committer:
lixianyu
Date:
Thu Jun 02 01:07:50 2016 +0000
Revision:
2:487a727d6181
Parent:
1:14b7c3a3ec60
drawBMP work.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:5ca227682ee7 1 // 本作品采用知识共享 署名-非商业性使用-相同方式共享 3.0 未本地化版本 许可协议进行许可
lixianyu 0:5ca227682ee7 2 // 访问 http://creativecommons.org/licenses/by-nc-sa/3.0/ 查看该许可协议
lixianyu 0:5ca227682ee7 3 // ==============
lixianyu 0:5ca227682ee7 4
lixianyu 0:5ca227682ee7 5 // 版权所有:
lixianyu 0:5ca227682ee7 6 // @老潘orz wasdpkj@hotmail.com
lixianyu 0:5ca227682ee7 7 // ==============
lixianyu 0:5ca227682ee7 8
lixianyu 0:5ca227682ee7 9 // Microduino-IDE
lixianyu 0:5ca227682ee7 10 // ==============
lixianyu 0:5ca227682ee7 11 // Microduino Getting start:
lixianyu 0:5ca227682ee7 12 // http://www.microduino.cc/download/
lixianyu 0:5ca227682ee7 13
lixianyu 0:5ca227682ee7 14 // Microduino IDE Support:
lixianyu 0:5ca227682ee7 15 // https://github.com/wasdpkj/Microduino-IDE-Support/
lixianyu 0:5ca227682ee7 16
lixianyu 0:5ca227682ee7 17 // ==============
lixianyu 0:5ca227682ee7 18 // Microduino wiki:
lixianyu 0:5ca227682ee7 19 // http://wiki.microduino.cc
lixianyu 0:5ca227682ee7 20
lixianyu 0:5ca227682ee7 21 // ==============
lixianyu 0:5ca227682ee7 22 // E-mail:
lixianyu 0:5ca227682ee7 23 // Kejia Pan
lixianyu 0:5ca227682ee7 24 // pankejia@microduino.cc
lixianyu 0:5ca227682ee7 25
lixianyu 0:5ca227682ee7 26 // ==============
lixianyu 0:5ca227682ee7 27 // Weibo:
lixianyu 0:5ca227682ee7 28 // @老潘orz
lixianyu 0:5ca227682ee7 29
lixianyu 0:5ca227682ee7 30 #include "Microduino_Matrix.h"
lixianyu 0:5ca227682ee7 31 #include "Fonts.h"
lixianyu 0:5ca227682ee7 32
lixianyu 2:487a727d6181 33 extern Serial pc;
lixianyu 2:487a727d6181 34
lixianyu 0:5ca227682ee7 35 #define read16(Y,Z) (uint16_t)((uint8_t)pgm_read_byte((Y) + (Z++)) | ((uint8_t)pgm_read_byte((Y) + (Z++)) << 8))
lixianyu 0:5ca227682ee7 36 #define read32(Y,Z) (uint32_t)((uint8_t)pgm_read_byte((Y) + (Z++)) | ((uint8_t)pgm_read_byte((Y) + (Z++)) << 8) | ((uint8_t)pgm_read_byte((Y) + (Z++)) << 16) | ((uint8_t)pgm_read_byte((Y) + (Z++)) << 24))
lixianyu 0:5ca227682ee7 37 #define BUFFPIXEL (MatrixPix_X * 8 * 3)
lixianyu 0:5ca227682ee7 38
lixianyu 0:5ca227682ee7 39 Matrix::Matrix(uint8_t (*_addr)[8])
lixianyu 0:5ca227682ee7 40 {
lixianyu 0:5ca227682ee7 41 // uint8_t (*p)[10]=_addr;
lixianyu 0:5ca227682ee7 42 uint8_t _x = 0, _y = 0;
lixianyu 0:5ca227682ee7 43 for (uint8_t a = 0; a < 8; a++) { //判断第一层
lixianyu 0:5ca227682ee7 44 if (_addr[0][a] == 0) {
lixianyu 0:5ca227682ee7 45
lixianyu 0:5ca227682ee7 46 break; //NULL,结束当前层判断
lixianyu 0:5ca227682ee7 47 } else {
lixianyu 0:5ca227682ee7 48 _x = a + 1;
lixianyu 0:5ca227682ee7 49 for (uint8_t b = 0; b < 8; b++) { //判断第二层
lixianyu 0:5ca227682ee7 50 if (_addr[b][a] == 0) {
lixianyu 0:5ca227682ee7 51 break; //NULL,结束当前层判断
lixianyu 0:5ca227682ee7 52 } else {
lixianyu 0:5ca227682ee7 53 _y = b + 1;
lixianyu 0:5ca227682ee7 54 }
lixianyu 0:5ca227682ee7 55 }
lixianyu 0:5ca227682ee7 56 }
lixianyu 0:5ca227682ee7 57 }
lixianyu 0:5ca227682ee7 58
lixianyu 0:5ca227682ee7 59 this->_numX = _x;
lixianyu 0:5ca227682ee7 60 this->_numY = _y;
lixianyu 0:5ca227682ee7 61
lixianyu 0:5ca227682ee7 62 this->_matrixNum = this->_numX * this->_numY;
lixianyu 0:5ca227682ee7 63 led = new LedControl[this->_matrixNum];
lixianyu 0:5ca227682ee7 64
lixianyu 0:5ca227682ee7 65 this->cursor_y = 0;
lixianyu 0:5ca227682ee7 66 this->cursor_x = 0;
lixianyu 0:5ca227682ee7 67
lixianyu 0:5ca227682ee7 68 uint8_t _p[64];
lixianyu 0:5ca227682ee7 69 for (int a = 0; a < this->_numY; a++) {
lixianyu 0:5ca227682ee7 70 for (int b = 0; b < this->_numX ; b++) {
lixianyu 0:5ca227682ee7 71 uint8_t _s = b + a * this->_numX ;
lixianyu 0:5ca227682ee7 72 _p[_s] = _addr[a][b];
lixianyu 0:5ca227682ee7 73 }
lixianyu 0:5ca227682ee7 74 }
lixianyu 0:5ca227682ee7 75 setDeviceAddr(_p);
lixianyu 0:5ca227682ee7 76
lixianyu 0:5ca227682ee7 77 clearFastMode();
lixianyu 0:5ca227682ee7 78 clearColor();
lixianyu 0:5ca227682ee7 79 setFontMode(true);
lixianyu 0:5ca227682ee7 80 }
lixianyu 0:5ca227682ee7 81
lixianyu 0:5ca227682ee7 82 uint8_t Matrix::getDeviceAddr(uint8_t _a)
lixianyu 0:5ca227682ee7 83 {
lixianyu 0:5ca227682ee7 84 return led[_a].getDeviceAddr();
lixianyu 0:5ca227682ee7 85 }
lixianyu 0:5ca227682ee7 86
lixianyu 0:5ca227682ee7 87 void Matrix::setDeviceAddr(uint8_t* _addr)
lixianyu 0:5ca227682ee7 88 {
lixianyu 0:5ca227682ee7 89 for (int a = 0; a < getMatrixNum(); a++)
lixianyu 0:5ca227682ee7 90 led[a].setDeviceAddr(_addr[a]);
lixianyu 0:5ca227682ee7 91 }
lixianyu 0:5ca227682ee7 92
lixianyu 0:5ca227682ee7 93 void Matrix::clearDisplay()
lixianyu 0:5ca227682ee7 94 {
lixianyu 0:5ca227682ee7 95 for (int a = 0; a < getMatrixNum(); a++)
lixianyu 0:5ca227682ee7 96 led[a].clearDisplay();
lixianyu 0:5ca227682ee7 97 }
lixianyu 0:5ca227682ee7 98
lixianyu 0:5ca227682ee7 99 void Matrix::setLedColor(uint8_t _row, uint8_t _col, uint8_t _value_r, uint8_t _value_g, uint8_t _value_b)
lixianyu 0:5ca227682ee7 100 {
lixianyu 0:5ca227682ee7 101 if((_col > (getHeight() * 8 - 1)) || (_row > (getWidth() * 8 - 1)))
lixianyu 0:5ca227682ee7 102 return;
lixianyu 0:5ca227682ee7 103 int16_t _s = (_row / 8) + (_col / 8) * getWidth();
lixianyu 0:5ca227682ee7 104 led[_s].setLedColor(_row % 8, _col % 8, _value_r, _value_g, _value_b);
lixianyu 0:5ca227682ee7 105 }
lixianyu 0:5ca227682ee7 106
lixianyu 0:5ca227682ee7 107 void Matrix::setLedColorFast(uint8_t _row, uint8_t _col, uint8_t _value_r, uint8_t _value_g, uint8_t _value_b)
lixianyu 0:5ca227682ee7 108 {
lixianyu 0:5ca227682ee7 109 if((_col > (getHeight() * 8 - 1)) || (_row > (getWidth() * 8 - 1)))
lixianyu 0:5ca227682ee7 110 return;
lixianyu 0:5ca227682ee7 111 int16_t _s = (_row / 8) + (_col / 8) * getWidth();
lixianyu 0:5ca227682ee7 112 led[_s].setLedColorFast(_row % 8, _col % 8, _value_r, _value_g, _value_b);
lixianyu 0:5ca227682ee7 113 }
lixianyu 0:5ca227682ee7 114
lixianyu 0:5ca227682ee7 115 void Matrix::setLed(uint8_t _row, uint8_t _col, bool _state)
lixianyu 0:5ca227682ee7 116 {
lixianyu 0:5ca227682ee7 117 if((_col > (getHeight() * 8 - 1)) || (_row > (getWidth() * 8 - 1)))
lixianyu 0:5ca227682ee7 118 return;
lixianyu 0:5ca227682ee7 119 int16_t _s = (_row / 8) + (_col / 8) * getWidth();
lixianyu 0:5ca227682ee7 120 led[_s].setLed(_row % 8, _col % 8, _state);
lixianyu 0:5ca227682ee7 121 }
lixianyu 0:5ca227682ee7 122
lixianyu 0:5ca227682ee7 123 void Matrix::setCursor(int16_t _x, int16_t _y)
lixianyu 0:5ca227682ee7 124 {
lixianyu 0:5ca227682ee7 125 this->cursor_x = _x;
lixianyu 0:5ca227682ee7 126 this->cursor_y = _y;
lixianyu 0:5ca227682ee7 127
lixianyu 0:5ca227682ee7 128 for (int _y = 0; _y < getHeight(); _y++) {
lixianyu 0:5ca227682ee7 129 for (int _x = 0; _x < getWidth(); _x++) {
lixianyu 0:5ca227682ee7 130 uint8_t _s = _x + _y * getWidth();
lixianyu 0:5ca227682ee7 131 led[_s].setCursor(-(8 * _x) + this->cursor_x, -(8 * _y) + this->cursor_y);
lixianyu 0:5ca227682ee7 132 }
lixianyu 0:5ca227682ee7 133 }
lixianyu 0:5ca227682ee7 134 }
lixianyu 0:5ca227682ee7 135
lixianyu 0:5ca227682ee7 136 void Matrix::setFastMode()
lixianyu 0:5ca227682ee7 137 {
lixianyu 0:5ca227682ee7 138 // runFun(&setFastMode);
lixianyu 0:5ca227682ee7 139 // this->Fast_mode = true;
lixianyu 0:5ca227682ee7 140 for (int a = 0; a < getMatrixNum(); a++)
lixianyu 0:5ca227682ee7 141 led[a].setFastMode();
lixianyu 0:5ca227682ee7 142 }
lixianyu 0:5ca227682ee7 143
lixianyu 0:5ca227682ee7 144 void Matrix::clearFastMode()
lixianyu 0:5ca227682ee7 145 {
lixianyu 0:5ca227682ee7 146 // this->Fast_mode = false;
lixianyu 0:5ca227682ee7 147 for (int a = 0; a < getMatrixNum(); a++)
lixianyu 0:5ca227682ee7 148 led[a].clearFastMode();
lixianyu 0:5ca227682ee7 149 }
lixianyu 0:5ca227682ee7 150
lixianyu 0:5ca227682ee7 151 void Matrix::setFontMode(bool _Mode)
lixianyu 0:5ca227682ee7 152 {
lixianyu 0:5ca227682ee7 153 for (int a = 0; a < getMatrixNum(); a++)
lixianyu 0:5ca227682ee7 154 led[a].setFontMode(_Mode);
lixianyu 0:5ca227682ee7 155 }
lixianyu 0:5ca227682ee7 156
lixianyu 0:5ca227682ee7 157 void Matrix::setColor(uint8_t value_r, uint8_t value_g, uint8_t value_b)
lixianyu 0:5ca227682ee7 158 {
lixianyu 0:5ca227682ee7 159 for (int a = 0; a < getMatrixNum(); a++)
lixianyu 0:5ca227682ee7 160 led[a].setColor(value_r, value_g, value_b);
lixianyu 0:5ca227682ee7 161 }
lixianyu 0:5ca227682ee7 162
lixianyu 0:5ca227682ee7 163 void Matrix::clearColor()
lixianyu 0:5ca227682ee7 164 {
lixianyu 0:5ca227682ee7 165 for (int a = 0; a < getMatrixNum(); a++)
lixianyu 0:5ca227682ee7 166 led[a].clearColor();
lixianyu 0:5ca227682ee7 167 }
lixianyu 0:5ca227682ee7 168
lixianyu 0:5ca227682ee7 169 void Matrix::drawLine(int8_t x1, int8_t y1, int8_t x2, int8_t y2)
lixianyu 0:5ca227682ee7 170 {
lixianyu 0:5ca227682ee7 171 uint8_t tmp;
lixianyu 0:5ca227682ee7 172 uint8_t x, y;
lixianyu 0:5ca227682ee7 173 uint8_t dx, dy;
lixianyu 0:5ca227682ee7 174 int8_t err;
lixianyu 0:5ca227682ee7 175 int8_t ystep;
lixianyu 0:5ca227682ee7 176
lixianyu 0:5ca227682ee7 177 uint8_t swapxy = 0;
lixianyu 0:5ca227682ee7 178
lixianyu 0:5ca227682ee7 179 /* no BBX intersection check at the moment, should be added... */
lixianyu 0:5ca227682ee7 180
lixianyu 0:5ca227682ee7 181 if ( x1 > x2 ) dx = x1 - x2;
lixianyu 0:5ca227682ee7 182 else dx = x2 - x1;
lixianyu 0:5ca227682ee7 183 if ( y1 > y2 ) dy = y1 - y2;
lixianyu 0:5ca227682ee7 184 else dy = y2 - y1;
lixianyu 0:5ca227682ee7 185
lixianyu 0:5ca227682ee7 186 if ( dy > dx ) {
lixianyu 0:5ca227682ee7 187 swapxy = 1;
lixianyu 0:5ca227682ee7 188 tmp = dx;
lixianyu 0:5ca227682ee7 189 dx = dy;
lixianyu 0:5ca227682ee7 190 dy = tmp;
lixianyu 0:5ca227682ee7 191 tmp = x1;
lixianyu 0:5ca227682ee7 192 x1 = y1;
lixianyu 0:5ca227682ee7 193 y1 = tmp;
lixianyu 0:5ca227682ee7 194 tmp = x2;
lixianyu 0:5ca227682ee7 195 x2 = y2;
lixianyu 0:5ca227682ee7 196 y2 = tmp;
lixianyu 0:5ca227682ee7 197 }
lixianyu 0:5ca227682ee7 198 if ( x1 > x2 ) {
lixianyu 0:5ca227682ee7 199 tmp = x1;
lixianyu 0:5ca227682ee7 200 x1 = x2;
lixianyu 0:5ca227682ee7 201 x2 = tmp;
lixianyu 0:5ca227682ee7 202 tmp = y1;
lixianyu 0:5ca227682ee7 203 y1 = y2;
lixianyu 0:5ca227682ee7 204 y2 = tmp;
lixianyu 0:5ca227682ee7 205 }
lixianyu 0:5ca227682ee7 206 err = dx >> 1;
lixianyu 0:5ca227682ee7 207 if ( y2 > y1 ) ystep = 1;
lixianyu 0:5ca227682ee7 208 else ystep = -1;
lixianyu 0:5ca227682ee7 209 y = y1;
lixianyu 0:5ca227682ee7 210 for ( x = x1; x <= x2; x++ ) {
lixianyu 0:5ca227682ee7 211 if ( swapxy == 0 )
lixianyu 0:5ca227682ee7 212 setLed(x, y, 1);
lixianyu 0:5ca227682ee7 213 else
lixianyu 0:5ca227682ee7 214 setLed(y, x, 1);
lixianyu 0:5ca227682ee7 215 err -= (uint8_t)dy;
lixianyu 0:5ca227682ee7 216 if ( err < 0 ) {
lixianyu 0:5ca227682ee7 217 y += (uint8_t)ystep;
lixianyu 0:5ca227682ee7 218 err += (uint8_t)dx;
lixianyu 0:5ca227682ee7 219 }
lixianyu 0:5ca227682ee7 220 }
lixianyu 0:5ca227682ee7 221 }
lixianyu 0:5ca227682ee7 222
lixianyu 0:5ca227682ee7 223 void Matrix::drawCircle_section(int8_t x, int8_t y, int8_t x0, int8_t y0, uint8_t option)
lixianyu 0:5ca227682ee7 224 {
lixianyu 0:5ca227682ee7 225 /* upper right */
lixianyu 0:5ca227682ee7 226 if ( option & U8G_DRAW_UPPER_RIGHT ) {
lixianyu 0:5ca227682ee7 227 setLed(x0 + x, y0 - y, 1);
lixianyu 0:5ca227682ee7 228 setLed(x0 + y, y0 - x, 1);
lixianyu 0:5ca227682ee7 229 }
lixianyu 0:5ca227682ee7 230
lixianyu 0:5ca227682ee7 231 /* upper left */
lixianyu 0:5ca227682ee7 232 if ( option & U8G_DRAW_UPPER_LEFT ) {
lixianyu 0:5ca227682ee7 233 setLed(x0 - x, y0 - y, 1);
lixianyu 0:5ca227682ee7 234 setLed(x0 - y, y0 - x, 1);
lixianyu 0:5ca227682ee7 235 }
lixianyu 0:5ca227682ee7 236
lixianyu 0:5ca227682ee7 237 /* lower right */
lixianyu 0:5ca227682ee7 238 if ( option & U8G_DRAW_LOWER_RIGHT ) {
lixianyu 0:5ca227682ee7 239 setLed(x0 + x, y0 + y, 1);
lixianyu 0:5ca227682ee7 240 setLed(x0 + y, y0 + x, 1);
lixianyu 0:5ca227682ee7 241 }
lixianyu 0:5ca227682ee7 242
lixianyu 0:5ca227682ee7 243 /* lower left */
lixianyu 0:5ca227682ee7 244 if ( option & U8G_DRAW_LOWER_LEFT ) {
lixianyu 0:5ca227682ee7 245 setLed(x0 - x, y0 + y, 1);
lixianyu 0:5ca227682ee7 246 setLed(x0 - y, y0 + x, 1);
lixianyu 0:5ca227682ee7 247 }
lixianyu 0:5ca227682ee7 248 }
lixianyu 0:5ca227682ee7 249
lixianyu 0:5ca227682ee7 250 void Matrix::drawVLine(int8_t x, int8_t y, int8_t w)
lixianyu 0:5ca227682ee7 251 {
lixianyu 0:5ca227682ee7 252 if(w<=0)
lixianyu 0:5ca227682ee7 253 return;
lixianyu 0:5ca227682ee7 254
lixianyu 0:5ca227682ee7 255 while (w--) {
lixianyu 0:5ca227682ee7 256 setLed(x, y + w, 1);
lixianyu 0:5ca227682ee7 257 }
lixianyu 0:5ca227682ee7 258 }
lixianyu 0:5ca227682ee7 259
lixianyu 0:5ca227682ee7 260 void Matrix::drawHLine(int8_t x, int8_t y, int8_t h)
lixianyu 0:5ca227682ee7 261 {
lixianyu 0:5ca227682ee7 262 if(h<=0)
lixianyu 0:5ca227682ee7 263 return;
lixianyu 0:5ca227682ee7 264
lixianyu 0:5ca227682ee7 265 while (h--) {
lixianyu 0:5ca227682ee7 266 setLed(x + h, y, 1);
lixianyu 0:5ca227682ee7 267 }
lixianyu 0:5ca227682ee7 268 }
lixianyu 0:5ca227682ee7 269
lixianyu 0:5ca227682ee7 270 void Matrix::drawDisc_section(int8_t x, int8_t y, int8_t x0, int8_t y0, uint8_t option)
lixianyu 0:5ca227682ee7 271 {
lixianyu 0:5ca227682ee7 272 /* upper right */
lixianyu 0:5ca227682ee7 273 if ( option & U8G_DRAW_UPPER_RIGHT ) {
lixianyu 0:5ca227682ee7 274 drawVLine(x0 + x, y0 - y, y + 1);
lixianyu 0:5ca227682ee7 275 drawVLine(x0 + y, y0 - x, x + 1);
lixianyu 0:5ca227682ee7 276 }
lixianyu 0:5ca227682ee7 277
lixianyu 0:5ca227682ee7 278 /* upper left */
lixianyu 0:5ca227682ee7 279 if ( option & U8G_DRAW_UPPER_LEFT ) {
lixianyu 0:5ca227682ee7 280 drawVLine(x0 - x, y0 - y, y + 1);
lixianyu 0:5ca227682ee7 281 drawVLine(x0 - y, y0 - x, x + 1);
lixianyu 0:5ca227682ee7 282 }
lixianyu 0:5ca227682ee7 283
lixianyu 0:5ca227682ee7 284 /* lower right */
lixianyu 0:5ca227682ee7 285 if ( option & U8G_DRAW_LOWER_RIGHT ) {
lixianyu 0:5ca227682ee7 286 drawVLine(x0 + x, y0, y + 1);
lixianyu 0:5ca227682ee7 287 drawVLine(x0 + y, y0, x + 1);
lixianyu 0:5ca227682ee7 288 }
lixianyu 0:5ca227682ee7 289
lixianyu 0:5ca227682ee7 290 /* lower left */
lixianyu 0:5ca227682ee7 291 if ( option & U8G_DRAW_LOWER_LEFT ) {
lixianyu 0:5ca227682ee7 292 drawVLine(x0 - x, y0, y + 1);
lixianyu 0:5ca227682ee7 293 drawVLine(x0 - y, y0, x + 1);
lixianyu 0:5ca227682ee7 294 }
lixianyu 0:5ca227682ee7 295 }
lixianyu 0:5ca227682ee7 296
lixianyu 0:5ca227682ee7 297 void Matrix::drawCircle(int8_t x0, int8_t y0, int8_t rad, int8_t option)
lixianyu 0:5ca227682ee7 298 {
lixianyu 0:5ca227682ee7 299 if(rad<=0)
lixianyu 0:5ca227682ee7 300 return;
lixianyu 0:5ca227682ee7 301
lixianyu 0:5ca227682ee7 302 int8_t f;
lixianyu 0:5ca227682ee7 303 int8_t ddF_x;
lixianyu 0:5ca227682ee7 304 int8_t ddF_y;
lixianyu 0:5ca227682ee7 305 uint8_t x;
lixianyu 0:5ca227682ee7 306 uint8_t y;
lixianyu 0:5ca227682ee7 307
lixianyu 0:5ca227682ee7 308 f = 1;
lixianyu 0:5ca227682ee7 309 f -= rad;
lixianyu 0:5ca227682ee7 310 ddF_x = 1;
lixianyu 0:5ca227682ee7 311 ddF_y = 0;
lixianyu 0:5ca227682ee7 312 ddF_y -= rad;
lixianyu 0:5ca227682ee7 313 ddF_y *= 2;
lixianyu 0:5ca227682ee7 314 x = 0;
lixianyu 0:5ca227682ee7 315 y = rad;
lixianyu 0:5ca227682ee7 316
lixianyu 0:5ca227682ee7 317 drawCircle_section(x, y, x0, y0, option);
lixianyu 0:5ca227682ee7 318
lixianyu 0:5ca227682ee7 319 while ( x < y ) {
lixianyu 0:5ca227682ee7 320 if (f >= 0) {
lixianyu 0:5ca227682ee7 321 y--;
lixianyu 0:5ca227682ee7 322 ddF_y += 2;
lixianyu 0:5ca227682ee7 323 f += ddF_y;
lixianyu 0:5ca227682ee7 324 }
lixianyu 0:5ca227682ee7 325 x++;
lixianyu 0:5ca227682ee7 326 ddF_x += 2;
lixianyu 0:5ca227682ee7 327 f += ddF_x;
lixianyu 0:5ca227682ee7 328
lixianyu 0:5ca227682ee7 329 drawCircle_section(x, y, x0, y0, option);
lixianyu 0:5ca227682ee7 330 }
lixianyu 0:5ca227682ee7 331 }
lixianyu 0:5ca227682ee7 332
lixianyu 0:5ca227682ee7 333 void Matrix::drawDisc(int8_t x0, int8_t y0, int8_t rad, int8_t option)
lixianyu 0:5ca227682ee7 334 {
lixianyu 0:5ca227682ee7 335 if(rad<=0)
lixianyu 0:5ca227682ee7 336 return;
lixianyu 0:5ca227682ee7 337
lixianyu 0:5ca227682ee7 338 int8_t f;
lixianyu 0:5ca227682ee7 339 int8_t ddF_x;
lixianyu 0:5ca227682ee7 340 int8_t ddF_y;
lixianyu 0:5ca227682ee7 341 uint8_t x;
lixianyu 0:5ca227682ee7 342 uint8_t y;
lixianyu 0:5ca227682ee7 343
lixianyu 0:5ca227682ee7 344 f = 1;
lixianyu 0:5ca227682ee7 345 f -= rad;
lixianyu 0:5ca227682ee7 346 ddF_x = 1;
lixianyu 0:5ca227682ee7 347 ddF_y = 0;
lixianyu 0:5ca227682ee7 348 ddF_y -= rad;
lixianyu 0:5ca227682ee7 349 ddF_y *= 2;
lixianyu 0:5ca227682ee7 350 x = 0;
lixianyu 0:5ca227682ee7 351 y = rad;
lixianyu 0:5ca227682ee7 352
lixianyu 0:5ca227682ee7 353 drawDisc_section(x, y, x0, y0, option);
lixianyu 0:5ca227682ee7 354
lixianyu 0:5ca227682ee7 355 while ( x < y ) {
lixianyu 0:5ca227682ee7 356 if (f >= 0) {
lixianyu 0:5ca227682ee7 357 y--;
lixianyu 0:5ca227682ee7 358 ddF_y += 2;
lixianyu 0:5ca227682ee7 359 f += ddF_y;
lixianyu 0:5ca227682ee7 360 }
lixianyu 0:5ca227682ee7 361 x++;
lixianyu 0:5ca227682ee7 362 ddF_x += 2;
lixianyu 0:5ca227682ee7 363 f += ddF_x;
lixianyu 0:5ca227682ee7 364
lixianyu 0:5ca227682ee7 365 drawDisc_section(x, y, x0, y0, option);
lixianyu 0:5ca227682ee7 366 }
lixianyu 0:5ca227682ee7 367 }
lixianyu 0:5ca227682ee7 368
lixianyu 0:5ca227682ee7 369 void Matrix::drawFrame(int8_t x, int8_t y, int8_t w, int8_t h)
lixianyu 0:5ca227682ee7 370 {
lixianyu 0:5ca227682ee7 371 if(h<=0 || w<=0)
lixianyu 0:5ca227682ee7 372 return;
lixianyu 0:5ca227682ee7 373
lixianyu 0:5ca227682ee7 374 int8_t xtmp = x;
lixianyu 0:5ca227682ee7 375
lixianyu 0:5ca227682ee7 376 drawHLine(x, y, w);
lixianyu 0:5ca227682ee7 377 drawVLine(x, y, h);
lixianyu 0:5ca227682ee7 378 x+=w;
lixianyu 0:5ca227682ee7 379 x--;
lixianyu 0:5ca227682ee7 380 drawVLine(x, y, h);
lixianyu 0:5ca227682ee7 381 y+=h;
lixianyu 0:5ca227682ee7 382 y--;
lixianyu 0:5ca227682ee7 383 drawHLine(xtmp, y, w);
lixianyu 0:5ca227682ee7 384 }
lixianyu 0:5ca227682ee7 385
lixianyu 0:5ca227682ee7 386 void Matrix::drawRFrame(int8_t x, int8_t y, int8_t w, int8_t h, uint8_t r)
lixianyu 0:5ca227682ee7 387 {
lixianyu 0:5ca227682ee7 388 if(h<3 || w<3)
lixianyu 0:5ca227682ee7 389 return;
lixianyu 0:5ca227682ee7 390
lixianyu 0:5ca227682ee7 391 if(r>(w-2)/2 || r>(h-2)/2)
lixianyu 0:5ca227682ee7 392 r = min((h-2)/2,(w-2)/2);
lixianyu 0:5ca227682ee7 393
lixianyu 0:5ca227682ee7 394 int8_t xl, yu;
lixianyu 0:5ca227682ee7 395 xl = x;
lixianyu 0:5ca227682ee7 396 xl += r;
lixianyu 0:5ca227682ee7 397 yu = y;
lixianyu 0:5ca227682ee7 398 yu += r;
lixianyu 0:5ca227682ee7 399
lixianyu 0:5ca227682ee7 400 {
lixianyu 0:5ca227682ee7 401 int8_t yl, xr;
lixianyu 0:5ca227682ee7 402
lixianyu 0:5ca227682ee7 403 xr = x;
lixianyu 0:5ca227682ee7 404 xr += w;
lixianyu 0:5ca227682ee7 405 xr -= r;
lixianyu 0:5ca227682ee7 406 xr -= 1;
lixianyu 0:5ca227682ee7 407
lixianyu 0:5ca227682ee7 408 yl = y;
lixianyu 0:5ca227682ee7 409 yl += h;
lixianyu 0:5ca227682ee7 410 yl -= r;
lixianyu 0:5ca227682ee7 411 yl -= 1;
lixianyu 0:5ca227682ee7 412
lixianyu 0:5ca227682ee7 413 drawCircle(xl, yu, r, U8G_DRAW_UPPER_LEFT);
lixianyu 0:5ca227682ee7 414 drawCircle(xr, yu, r, U8G_DRAW_UPPER_RIGHT);
lixianyu 0:5ca227682ee7 415 drawCircle(xl, yl, r, U8G_DRAW_LOWER_LEFT);
lixianyu 0:5ca227682ee7 416 drawCircle(xr, yl, r, U8G_DRAW_LOWER_RIGHT);
lixianyu 0:5ca227682ee7 417 }
lixianyu 0:5ca227682ee7 418
lixianyu 0:5ca227682ee7 419 {
lixianyu 0:5ca227682ee7 420 int8_t ww, hh;
lixianyu 0:5ca227682ee7 421
lixianyu 0:5ca227682ee7 422 ww = w;
lixianyu 0:5ca227682ee7 423 ww -= r;
lixianyu 0:5ca227682ee7 424 ww -= r;
lixianyu 0:5ca227682ee7 425 ww -= 2;
lixianyu 0:5ca227682ee7 426 hh = h;
lixianyu 0:5ca227682ee7 427 hh -= r;
lixianyu 0:5ca227682ee7 428 hh -= r;
lixianyu 0:5ca227682ee7 429 hh -= 2;
lixianyu 0:5ca227682ee7 430
lixianyu 0:5ca227682ee7 431 xl++;
lixianyu 0:5ca227682ee7 432 yu++;
lixianyu 0:5ca227682ee7 433 h--;
lixianyu 0:5ca227682ee7 434 w--;
lixianyu 0:5ca227682ee7 435 drawHLine(xl, y, ww);
lixianyu 0:5ca227682ee7 436 drawHLine(xl, y+h, ww);
lixianyu 0:5ca227682ee7 437 drawVLine(x, yu, hh);
lixianyu 0:5ca227682ee7 438 drawVLine(x+w, yu, hh);
lixianyu 0:5ca227682ee7 439 }
lixianyu 0:5ca227682ee7 440 }
lixianyu 0:5ca227682ee7 441
lixianyu 0:5ca227682ee7 442 void Matrix::drawBox(int8_t x, int8_t y, int8_t w, int8_t h)
lixianyu 0:5ca227682ee7 443 {
lixianyu 0:5ca227682ee7 444 if(h<=0 || w<=0)
lixianyu 0:5ca227682ee7 445 return;
lixianyu 0:5ca227682ee7 446
lixianyu 0:5ca227682ee7 447 do {
lixianyu 0:5ca227682ee7 448 drawHLine(x, y, w);
lixianyu 0:5ca227682ee7 449 y++;
lixianyu 0:5ca227682ee7 450 h--;
lixianyu 0:5ca227682ee7 451 } while( h != 0 );
lixianyu 0:5ca227682ee7 452 }
lixianyu 0:5ca227682ee7 453
lixianyu 0:5ca227682ee7 454 void Matrix::drawRBox(int8_t x, int8_t y, int8_t w, int8_t h, uint8_t r)
lixianyu 0:5ca227682ee7 455 {
lixianyu 0:5ca227682ee7 456 if(h<3 || w<3)
lixianyu 0:5ca227682ee7 457 return;
lixianyu 0:5ca227682ee7 458
lixianyu 0:5ca227682ee7 459 if(r>(w-2)/2 || r>(h-2)/2)
lixianyu 0:5ca227682ee7 460 r=min((h-2)/2,(w-2)/2);
lixianyu 0:5ca227682ee7 461
lixianyu 0:5ca227682ee7 462 int8_t xl, yu;
lixianyu 0:5ca227682ee7 463 int8_t yl, xr;
lixianyu 0:5ca227682ee7 464
lixianyu 0:5ca227682ee7 465
lixianyu 0:5ca227682ee7 466 xl = x;
lixianyu 0:5ca227682ee7 467 xl += r;
lixianyu 0:5ca227682ee7 468 yu = y;
lixianyu 0:5ca227682ee7 469 yu += r;
lixianyu 0:5ca227682ee7 470
lixianyu 0:5ca227682ee7 471 xr = x;
lixianyu 0:5ca227682ee7 472 xr += w;
lixianyu 0:5ca227682ee7 473 xr -= r;
lixianyu 0:5ca227682ee7 474 xr -= 1;
lixianyu 0:5ca227682ee7 475
lixianyu 0:5ca227682ee7 476 yl = y;
lixianyu 0:5ca227682ee7 477 yl += h;
lixianyu 0:5ca227682ee7 478 yl -= r;
lixianyu 0:5ca227682ee7 479 yl -= 1;
lixianyu 0:5ca227682ee7 480
lixianyu 0:5ca227682ee7 481 drawDisc(xl, yu, r, U8G_DRAW_UPPER_LEFT);
lixianyu 0:5ca227682ee7 482 drawDisc(xr, yu, r, U8G_DRAW_UPPER_RIGHT);
lixianyu 0:5ca227682ee7 483 drawDisc(xl, yl, r, U8G_DRAW_LOWER_LEFT);
lixianyu 0:5ca227682ee7 484 drawDisc(xr, yl, r, U8G_DRAW_LOWER_RIGHT);
lixianyu 0:5ca227682ee7 485
lixianyu 0:5ca227682ee7 486 {
lixianyu 0:5ca227682ee7 487 int8_t ww, hh;
lixianyu 0:5ca227682ee7 488
lixianyu 0:5ca227682ee7 489 ww = w;
lixianyu 0:5ca227682ee7 490 ww -= r;
lixianyu 0:5ca227682ee7 491 ww -= r;
lixianyu 0:5ca227682ee7 492 ww -= 2;
lixianyu 0:5ca227682ee7 493 hh = h;
lixianyu 0:5ca227682ee7 494 hh -= r;
lixianyu 0:5ca227682ee7 495 hh -= r;
lixianyu 0:5ca227682ee7 496 hh -= 2;
lixianyu 0:5ca227682ee7 497
lixianyu 0:5ca227682ee7 498 xl++;
lixianyu 0:5ca227682ee7 499 yu++;
lixianyu 0:5ca227682ee7 500 h--;
lixianyu 0:5ca227682ee7 501 drawBox(xl, y, ww, r+1);
lixianyu 0:5ca227682ee7 502 drawBox(xl, yl, ww, r+1);
lixianyu 0:5ca227682ee7 503 // drawHLine(xl, y+h, ww);
lixianyu 0:5ca227682ee7 504 drawBox(x, yu, w, hh);
lixianyu 0:5ca227682ee7 505 // drawVLine(x+w, yu, hh);
lixianyu 0:5ca227682ee7 506 }
lixianyu 0:5ca227682ee7 507 }
lixianyu 0:5ca227682ee7 508
lixianyu 0:5ca227682ee7 509 void Matrix::drawBMP(int16_t x, int16_t y, int16_t w, int16_t h,const uint8_t *bitmap)
lixianyu 0:5ca227682ee7 510 {
lixianyu 0:5ca227682ee7 511 int16_t i, j, byteWidth = (w + 7) / 8;
lixianyu 0:5ca227682ee7 512
lixianyu 0:5ca227682ee7 513 for(j=0; j<h; j++) {
lixianyu 0:5ca227682ee7 514 for(i=0; i<w; i++ ) {
lixianyu 0:5ca227682ee7 515 #if 0
lixianyu 0:5ca227682ee7 516 if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> ((h-i-1) & 7))) {
lixianyu 0:5ca227682ee7 517 setLed(x+i, y+j, 1);
lixianyu 0:5ca227682ee7 518 }
lixianyu 0:5ca227682ee7 519 #else
lixianyu 0:5ca227682ee7 520 if (bitmap[j * byteWidth + i / 8] & (128 >> ((h-i-1) & 7))) {
lixianyu 0:5ca227682ee7 521 setLed(x+i, y+j, 1);
lixianyu 0:5ca227682ee7 522 }
lixianyu 0:5ca227682ee7 523 #endif
lixianyu 0:5ca227682ee7 524 }
lixianyu 0:5ca227682ee7 525 }
lixianyu 0:5ca227682ee7 526 }
lixianyu 0:5ca227682ee7 527
lixianyu 2:487a727d6181 528 #if 0
lixianyu 0:5ca227682ee7 529 bool Matrix::drawBMP(int16_t x, int16_t y, const uint8_t *bitmap)
lixianyu 0:5ca227682ee7 530 {
lixianyu 0:5ca227682ee7 531 uint32_t _dataNum = 0;
lixianyu 0:5ca227682ee7 532 uint8_t _dataBuffer[BUFFPIXEL]; //pixel buffer (R+G+B per pixel)
lixianyu 0:5ca227682ee7 533
lixianyu 0:5ca227682ee7 534 //Parse BMP header
lixianyu 0:5ca227682ee7 535 if (read16((uint8_t*)bitmap, _dataNum) == 0x4D42) { //BMP signature
lixianyu 0:5ca227682ee7 536 (void)read32((uint8_t*)bitmap, _dataNum); //File size
lixianyu 0:5ca227682ee7 537 (void)read32((uint8_t*)bitmap, _dataNum); //Read & ignore creator bytes
lixianyu 0:5ca227682ee7 538 uint32_t bmpImageoffset = read32((uint8_t*)bitmap, _dataNum); //Start of image data in file
lixianyu 0:5ca227682ee7 539 //Read DIB header
lixianyu 0:5ca227682ee7 540 (void)read32((uint8_t*)bitmap, _dataNum); //Header size
lixianyu 0:5ca227682ee7 541 int bmpWidth = read32((uint8_t*)bitmap, _dataNum),
lixianyu 0:5ca227682ee7 542 bmpHeight = read32((uint8_t*)bitmap, _dataNum);
lixianyu 0:5ca227682ee7 543
lixianyu 0:5ca227682ee7 544 bool flip = true; //BMP is stored bottom-to-top
lixianyu 0:5ca227682ee7 545 //If bmpHeight is negative, image is in top-down order.
lixianyu 0:5ca227682ee7 546 if (bmpHeight < 0) {
lixianyu 0:5ca227682ee7 547 bmpHeight = -bmpHeight;
lixianyu 0:5ca227682ee7 548 flip = false;
lixianyu 0:5ca227682ee7 549 }
lixianyu 0:5ca227682ee7 550
lixianyu 0:5ca227682ee7 551 if (read16((uint8_t*)bitmap, _dataNum) == 1) { //# planes -- must be '1'
lixianyu 0:5ca227682ee7 552 uint8_t bmpDepth = read16((uint8_t*)bitmap, _dataNum); //Bit depth (currently must be 24)
lixianyu 0:5ca227682ee7 553 if ((bmpDepth == 24) && (read32((uint8_t*)bitmap, _dataNum) == 0)) { //0 = uncompressed
lixianyu 0:5ca227682ee7 554 //BMP rows are padded (if needed) to 4-byte boundary
lixianyu 0:5ca227682ee7 555 uint32_t rowSize = (bmpWidth * 3 + 3) & ~3; //Not always = bmpWidth; may have padding
lixianyu 0:5ca227682ee7 556
lixianyu 0:5ca227682ee7 557 //Crop area to be loaded
lixianyu 0:5ca227682ee7 558 int w = bmpWidth,
lixianyu 0:5ca227682ee7 559 h = bmpHeight;
lixianyu 0:5ca227682ee7 560
lixianyu 0:5ca227682ee7 561 if ((x + w - 1) >= (getWidth() * 8)) w = (getWidth() * 8) - x;
lixianyu 0:5ca227682ee7 562 if ((y + h - 1) >= (getHeight() * 8)) h = (getHeight() * 8) - y;
lixianyu 0:5ca227682ee7 563
lixianyu 0:5ca227682ee7 564 for (int row = 0; row < h; row++) { //For each scanline...
lixianyu 0:5ca227682ee7 565 uint32_t pos = bmpImageoffset + (flip ? (bmpHeight - 1 - row) : row) * rowSize ;
lixianyu 0:5ca227682ee7 566 uint8_t buffidx = sizeof(_dataBuffer); //Current position in _dataBuffer
lixianyu 0:5ca227682ee7 567 for (int col = 0; col < w; col++) { //For each pixel...
lixianyu 0:5ca227682ee7 568 //Time to read more pixel data?
lixianyu 0:5ca227682ee7 569 if (buffidx >= sizeof(_dataBuffer)) { //Indeed
lixianyu 0:5ca227682ee7 570 buffidx = 0; //Set index to beginning
lixianyu 0:5ca227682ee7 571 for (int a = 0; a < BUFFPIXEL; a++) {
lixianyu 0:5ca227682ee7 572 _dataBuffer[a] = pgm_read_byte((uint8_t*)bitmap + (pos + a));
lixianyu 0:5ca227682ee7 573 }
lixianyu 0:5ca227682ee7 574 }
lixianyu 0:5ca227682ee7 575
lixianyu 0:5ca227682ee7 576 uint8_t _b = _dataBuffer[buffidx++],
lixianyu 0:5ca227682ee7 577 _g = _dataBuffer[buffidx++],
lixianyu 0:5ca227682ee7 578 _r = _dataBuffer[buffidx++];
lixianyu 0:5ca227682ee7 579 setLedColor(col + x, row + y, _r, _g, _b);
lixianyu 0:5ca227682ee7 580 } //end pixel
lixianyu 0:5ca227682ee7 581 } //end scanline
lixianyu 0:5ca227682ee7 582 } //end goodBmp
lixianyu 0:5ca227682ee7 583 else {
lixianyu 0:5ca227682ee7 584 return false;
lixianyu 0:5ca227682ee7 585 }
lixianyu 0:5ca227682ee7 586 }//end planes
lixianyu 0:5ca227682ee7 587 else {
lixianyu 0:5ca227682ee7 588 return false;
lixianyu 0:5ca227682ee7 589 }
lixianyu 0:5ca227682ee7 590 }//end sianatrue
lixianyu 0:5ca227682ee7 591 else {
lixianyu 0:5ca227682ee7 592 return false;
lixianyu 0:5ca227682ee7 593 }
lixianyu 0:5ca227682ee7 594 return true;
lixianyu 0:5ca227682ee7 595 }
lixianyu 2:487a727d6181 596 #else
lixianyu 2:487a727d6181 597 uint8_t _dataBuffer[BUFFPIXEL]; //pixel buffer (R+G+B per pixel)
lixianyu 2:487a727d6181 598 bool Matrix::drawBMP(int16_t x, int16_t y, const uint8_t *bitmap)
lixianyu 2:487a727d6181 599 {
lixianyu 2:487a727d6181 600 uint32_t _dataNum = 0;
lixianyu 2:487a727d6181 601 uint32_t tempData = 0;
lixianyu 2:487a727d6181 602
lixianyu 2:487a727d6181 603 pc.printf("Enter drawBMP()\r\n");
lixianyu 2:487a727d6181 604 //Parse BMP header
lixianyu 2:487a727d6181 605 //if (read16((uint8_t*)bitmap, _dataNum) == 0x4D42) { //BMP signature
lixianyu 2:487a727d6181 606 if ((bitmap[_dataNum++] == 0x42) && (bitmap[_dataNum++] == 0x4D)) { //BMP signature
lixianyu 2:487a727d6181 607 pc.printf("Enter if\r\n");
lixianyu 2:487a727d6181 608 //(void)read32((uint8_t*)bitmap, _dataNum); //File size
lixianyu 2:487a727d6181 609 _dataNum += 4; //File size
lixianyu 2:487a727d6181 610 //(void)read32((uint8_t*)bitmap, _dataNum); //Read & ignore creator bytes
lixianyu 2:487a727d6181 611 _dataNum += 4; //Read & ignore creator bytes
lixianyu 2:487a727d6181 612 //uint32_t bmpImageoffset = read32((uint8_t*)bitmap, _dataNum); //Start of image data in file
lixianyu 2:487a727d6181 613 uint32_t bmpImageoffset = bitmap[_dataNum] | bitmap[_dataNum+1]<<8 | bitmap[_dataNum+2]<< 16 | bitmap[_dataNum+3] << 24; //Start of image data in file
lixianyu 2:487a727d6181 614 _dataNum += 4;
lixianyu 2:487a727d6181 615 pc.printf("bmpImageoffset = %u\r\n", bmpImageoffset);
lixianyu 2:487a727d6181 616 //Read DIB header
lixianyu 2:487a727d6181 617 //(void)read32((uint8_t*)bitmap, _dataNum); //Header size
lixianyu 2:487a727d6181 618 _dataNum += 4; //Header size
lixianyu 2:487a727d6181 619 //int bmpWidth = read32((uint8_t*)bitmap, _dataNum);
lixianyu 2:487a727d6181 620 int bmpWidth = bitmap[_dataNum] | bitmap[_dataNum+1]<<8 | bitmap[_dataNum+2]<< 16 | bitmap[_dataNum+3] << 24;
lixianyu 2:487a727d6181 621 _dataNum += 4;
lixianyu 2:487a727d6181 622 //int bmpHeight = read32((uint8_t*)bitmap, _dataNum);
lixianyu 2:487a727d6181 623 int bmpHeight = bitmap[_dataNum] | bitmap[_dataNum+1]<<8 | bitmap[_dataNum+2]<< 16 | bitmap[_dataNum+3] << 24;
lixianyu 2:487a727d6181 624 _dataNum += 4;
lixianyu 2:487a727d6181 625
lixianyu 2:487a727d6181 626 bool flip = true; //BMP is stored bottom-to-top
lixianyu 2:487a727d6181 627 //If bmpHeight is negative, image is in top-down order.
lixianyu 2:487a727d6181 628 if (bmpHeight < 0) {
lixianyu 2:487a727d6181 629 bmpHeight = -bmpHeight;
lixianyu 2:487a727d6181 630 flip = false;
lixianyu 2:487a727d6181 631 }
lixianyu 2:487a727d6181 632
lixianyu 2:487a727d6181 633 //if (read16((uint8_t*)bitmap, _dataNum) == 1) { //# planes -- must be '1'
lixianyu 2:487a727d6181 634 if ((bitmap[_dataNum] | bitmap[_dataNum+1]<<8) == 1) { //# planes -- must be '1'
lixianyu 2:487a727d6181 635 _dataNum += 2;
lixianyu 2:487a727d6181 636 //uint8_t bmpDepth = read16((uint8_t*)bitmap, _dataNum); //Bit depth (currently must be 24)
lixianyu 2:487a727d6181 637 uint8_t bmpDepth = bitmap[_dataNum] | bitmap[_dataNum+1]<<8; //Bit depth (currently must be 24)
lixianyu 2:487a727d6181 638 _dataNum += 2;
lixianyu 2:487a727d6181 639 tempData = bitmap[_dataNum] | bitmap[_dataNum+1]<<8 | bitmap[_dataNum+2]<< 16 | bitmap[_dataNum+3] << 24;
lixianyu 2:487a727d6181 640 _dataNum += 4;
lixianyu 2:487a727d6181 641 //if ((bmpDepth == 24) && (read32((uint8_t*)bitmap, _dataNum) == 0)) { //0 = uncompressed
lixianyu 2:487a727d6181 642 if ((bmpDepth == 24) && (tempData == 0)) { //0 = uncompressed
lixianyu 2:487a727d6181 643 //BMP rows are padded (if needed) to 4-byte boundary
lixianyu 2:487a727d6181 644 uint32_t rowSize = (bmpWidth * 3 + 3) & ~3; //Not always = bmpWidth; may have padding
lixianyu 2:487a727d6181 645
lixianyu 2:487a727d6181 646 //Crop area to be loaded
lixianyu 2:487a727d6181 647 int w = bmpWidth,
lixianyu 2:487a727d6181 648 h = bmpHeight;
lixianyu 2:487a727d6181 649
lixianyu 2:487a727d6181 650 if ((x + w - 1) >= (getWidth() * 8)) w = (getWidth() * 8) - x;
lixianyu 2:487a727d6181 651 if ((y + h - 1) >= (getHeight() * 8)) h = (getHeight() * 8) - y;
lixianyu 2:487a727d6181 652
lixianyu 2:487a727d6181 653 for (int row = 0; row < h; row++) { //For each scanline...
lixianyu 2:487a727d6181 654 uint32_t pos = bmpImageoffset + (flip ? (bmpHeight - 1 - row) : row) * rowSize ;
lixianyu 2:487a727d6181 655 uint8_t buffidx = sizeof(_dataBuffer); //Current position in _dataBuffer
lixianyu 2:487a727d6181 656 for (int col = 0; col < w; col++) { //For each pixel...
lixianyu 2:487a727d6181 657 //Time to read more pixel data?
lixianyu 2:487a727d6181 658 if (buffidx >= sizeof(_dataBuffer)) { //Indeed
lixianyu 2:487a727d6181 659 buffidx = 0; //Set index to beginning
lixianyu 2:487a727d6181 660 for (int a = 0; a < BUFFPIXEL; a++) {
lixianyu 2:487a727d6181 661 _dataBuffer[a] = pgm_read_byte((uint8_t*)bitmap + (pos + a));
lixianyu 2:487a727d6181 662 }
lixianyu 2:487a727d6181 663 }
lixianyu 2:487a727d6181 664
lixianyu 2:487a727d6181 665 uint8_t _b = _dataBuffer[buffidx++],
lixianyu 2:487a727d6181 666 _g = _dataBuffer[buffidx++],
lixianyu 2:487a727d6181 667 _r = _dataBuffer[buffidx++];
lixianyu 2:487a727d6181 668 setLedColor(col + x, row + y, _r, _g, _b);
lixianyu 2:487a727d6181 669 } //end pixel
lixianyu 2:487a727d6181 670 } //end scanline
lixianyu 2:487a727d6181 671 } //end goodBmp
lixianyu 2:487a727d6181 672 else {
lixianyu 2:487a727d6181 673 return false;
lixianyu 2:487a727d6181 674 }
lixianyu 2:487a727d6181 675 }//end planes
lixianyu 2:487a727d6181 676 else {
lixianyu 2:487a727d6181 677 return false;
lixianyu 2:487a727d6181 678 }
lixianyu 2:487a727d6181 679 }//end sianatrue
lixianyu 2:487a727d6181 680 else {
lixianyu 2:487a727d6181 681 return false;
lixianyu 2:487a727d6181 682 }
lixianyu 2:487a727d6181 683 return true;
lixianyu 2:487a727d6181 684 }
lixianyu 2:487a727d6181 685 #endif
lixianyu 0:5ca227682ee7 686
lixianyu 0:5ca227682ee7 687 void Matrix::writeString(char* _c, bool _m, uint16_t _t, int16_t _xy)
lixianyu 0:5ca227682ee7 688 {
lixianyu 0:5ca227682ee7 689 setFontMode(_m);
lixianyu 0:5ca227682ee7 690 int c1 = (_m ? getWidth() : getHeight()) * 8;
lixianyu 0:5ca227682ee7 691 int c2 = -(_m ? getStringWidth(_c) : getStringHeight(_c)) - c1;
lixianyu 0:5ca227682ee7 692 for (int a = c1; a > c2; a--) {
lixianyu 0:5ca227682ee7 693 setCursor((_m ? a : _xy), (_m ? _xy : a));
lixianyu 0:5ca227682ee7 694 print(_c);
lixianyu 0:5ca227682ee7 695 wait_ms(_t);
lixianyu 0:5ca227682ee7 696 #ifdef WDT
lixianyu 0:5ca227682ee7 697 //wdt_reset();
lixianyu 0:5ca227682ee7 698 #endif
lixianyu 0:5ca227682ee7 699 }
lixianyu 0:5ca227682ee7 700 }
lixianyu 0:5ca227682ee7 701
lixianyu 0:5ca227682ee7 702 size_t Matrix::write(uint8_t c)
lixianyu 0:5ca227682ee7 703 {
lixianyu 0:5ca227682ee7 704 if (CharToInt(c) > 94 || CharToInt(c) < 0)
lixianyu 0:5ca227682ee7 705 return 0;
lixianyu 0:5ca227682ee7 706
lixianyu 0:5ca227682ee7 707 for (int a = 0; a < getMatrixNum(); a++)
lixianyu 0:5ca227682ee7 708 led[a].write(c);
lixianyu 0:5ca227682ee7 709
lixianyu 0:5ca227682ee7 710 return 1;
lixianyu 0:5ca227682ee7 711 /* void Matrix::print(char* _c) {
lixianyu 0:5ca227682ee7 712 for (int a = 0; a < this->_matrixNum; a++)
lixianyu 0:5ca227682ee7 713 led[a].print(_c);
lixianyu 0:5ca227682ee7 714 } */
lixianyu 0:5ca227682ee7 715 }
lixianyu 0:5ca227682ee7 716
lixianyu 0:5ca227682ee7 717 int16_t Matrix::getStringWidth(char * _String)
lixianyu 0:5ca227682ee7 718 {
lixianyu 0:5ca227682ee7 719 // return (uint32_t)(offset + millis() / 1000);
lixianyu 0:5ca227682ee7 720 int _leng = 0;
lixianyu 0:5ca227682ee7 721 int _Width = 0;
lixianyu 0:5ca227682ee7 722 while (_String[_leng] != NULL) {
lixianyu 0:5ca227682ee7 723 #if 0
lixianyu 1:14b7c3a3ec60 724 _Width += 1 + pgm_read_byte(alphabetBitmap[(_String[_leng] - 32)] + FONE_SIZE_X);
lixianyu 0:5ca227682ee7 725 #else
lixianyu 0:5ca227682ee7 726 //_Width += (1 + alphabetBitmap[((int)_String[_leng] - 32)] + FONE_SIZE_X);
lixianyu 0:5ca227682ee7 727 _Width += 1 + FONE_SIZE_X;
lixianyu 0:5ca227682ee7 728 #endif
lixianyu 0:5ca227682ee7 729 _leng++;
lixianyu 0:5ca227682ee7 730 }
lixianyu 0:5ca227682ee7 731 return _Width;
lixianyu 0:5ca227682ee7 732 }
lixianyu 0:5ca227682ee7 733
lixianyu 0:5ca227682ee7 734 int16_t Matrix::getStringHeight(char* _String)
lixianyu 0:5ca227682ee7 735 {
lixianyu 0:5ca227682ee7 736 // return (uint32_t)(offset + millis() / 1000);
lixianyu 0:5ca227682ee7 737 int _leng = 0;
lixianyu 0:5ca227682ee7 738 int _Height = 0;
lixianyu 0:5ca227682ee7 739 while (_String[_leng] != NULL) {
lixianyu 0:5ca227682ee7 740 _Height += 1 + FONE_SIZE_Y;
lixianyu 0:5ca227682ee7 741 _leng++;
lixianyu 0:5ca227682ee7 742 }
lixianyu 0:5ca227682ee7 743 return _Height;
lixianyu 0:5ca227682ee7 744 }
lixianyu 0:5ca227682ee7 745
lixianyu 0:5ca227682ee7 746 int16_t Matrix::getMatrixNum()
lixianyu 0:5ca227682ee7 747 {
lixianyu 0:5ca227682ee7 748 return this->_matrixNum;
lixianyu 0:5ca227682ee7 749 }
lixianyu 0:5ca227682ee7 750
lixianyu 0:5ca227682ee7 751 int16_t Matrix::getWidth()
lixianyu 0:5ca227682ee7 752 {
lixianyu 0:5ca227682ee7 753 return this->_numX;
lixianyu 0:5ca227682ee7 754 }
lixianyu 0:5ca227682ee7 755
lixianyu 0:5ca227682ee7 756 int16_t Matrix::getHeight()
lixianyu 0:5ca227682ee7 757 {
lixianyu 0:5ca227682ee7 758 return this->_numY;
lixianyu 0:5ca227682ee7 759 }