CARPENTIER ANTOINE / Mbed 2 deprecated panneau

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LEDMatrix.cpp Source File

LEDMatrix.cpp

00001 /**
00002  * LED Matrix library for http://www.seeedstudio.com/depot/ultrathin-16x32-red-led-matrix-panel-p-1582.html
00003  * The LED Matrix panel has 32x16 pixels. Several panel can be combined together as a large screen.
00004  *
00005  * Coordinate & Connection (mbed -> panel 0 -> panel 1 -> ...)
00006  *   (0, 0)                                     (0, 0)
00007  *     +--------+--------+--------+               +--------+--------+
00008  *     |   5    |    3   |    1   |               |    1   |    0   |
00009  *     |        |        |        |               |        |        |<----- mbed
00010  *     +--------+--------+--------+               +--------+--------+
00011  *     |   4    |    2   |    0   |                              (64, 16)
00012  *     |        |        |        |<----- mbed
00013  *     +--------+--------+--------+
00014  *                             (96, 32)
00015  *  Copyright (c) 2013 Seeed Technology Inc.
00016  *  @auther     Yihui Xiong 
00017  *  @date       Nov 8, 2013
00018  *  @license    Apache
00019  */
00020 
00021 #include "LEDMatrix.h"
00022 #include "mbed.h"
00023 
00024 #if 0
00025 #define ASSERT(e)   if (!(e)) { Serial.println(#e); while (1); }
00026 #else
00027 #define ASSERT(e)
00028 #endif
00029 
00030 LEDMatrix::LEDMatrix(PinName pinA, PinName pinB, PinName pinC, PinName pinD, PinName pinOE,
00031                      PinName pinR1, PinName pinR2, PinName pinB1, PinName pinB2, PinName pinG1, PinName pinG2,
00032                      PinName pinSTB, PinName pinCLK) :
00033     a(pinA), b(pinB), c(pinC), d(pinD), oe(pinOE), r1(pinR1), r2(pinR2), b1(pinB1), b2(pinB2), g1(pinG1), g2(pinG2), stb(pinSTB), clk(pinCLK)
00034 {
00035     oe = 1;
00036     clk = 1;
00037     stb = 1;
00038     mask = 0xff;
00039     state = 0;
00040     bufferIndex = 0;
00041     flagSwap = 0;
00042 }
00043 
00044 void LEDMatrix::begin(uint8_t *displaybuf, uint16_t width, uint16_t height)
00045 {
00046     this->displaybuf = displaybuf;
00047     memoBuf = displaybuf;
00048     drawBuf = memoBuf + width * height;
00049     this->width = width;
00050     this->height = height;
00051 
00052     state = 1;
00053 }
00054 
00055 void LEDMatrix::drawPoint(uint16_t x, uint16_t y, uint8_t pixel)
00056 {
00057     if (x>=width) return;
00058     if (y>=height) return;
00059     drawBuf[x+ width * y] = pixel & 0x07;
00060 }
00061 
00062 void LEDMatrix::drawRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t pixel)
00063 {
00064     for (uint16_t x = x1; x < x2; x++) {
00065         for (uint16_t y = y1; y < y2; y++) {
00066             drawPoint(x, y, pixel);
00067         }
00068     }
00069 }
00070 
00071 int LEDMatrix::drawChar(uint16_t x, uint16_t y, char c, uint8_t pixel, Font *font)
00072 {
00073     int i, j,k, col, max = 0;
00074     for (i=0; i<font->hauteur(); i++) {
00075         for (k=0; k<font->largeur(); k++) {
00076             col = font->octet(c, i, k);
00077             for (j=8; j>=0; j--) {
00078                 if (col & 0x01 != 0) {
00079                     int dx = j+8*k;
00080                     if (dx > max) max = dx;
00081                     drawPoint(x + dx, y+i, pixel);
00082                 }
00083                 col = col >> 1;
00084             }
00085         }
00086     }
00087     if (c==' ') {
00088         return 5*font->largeur();
00089     } else {
00090         return max + font->largeur();
00091     }
00092 }
00093 
00094 
00095 void LEDMatrix::drawCharString (uint16_t x, uint16_t y, char *c, uint8_t pixel,Font *font)
00096 {
00097     int i=0;
00098     while(c[i] != '\0') {
00099         int dx = drawChar(x,y,c[i],pixel,font);
00100         i++;
00101         x += dx;
00102     }
00103 }
00104 
00105 
00106 void LEDMatrix::drawImage(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t *image)
00107 {
00108     ASSERT(0 == ((x2 - x1) % 8));
00109 
00110     for (uint16_t x = x1; x < x2; x++) {
00111         for (uint16_t y = y1; y < y2; y++) {
00112             uint8_t *byte = image + x * 8 + y / 8;
00113             uint8_t  bit = 7 - (y % 8);
00114             uint8_t  pixel = (*byte >> bit) & 1;
00115 
00116             drawPoint(x, y, pixel);
00117         }
00118     }
00119 }
00120 
00121 void LEDMatrix::clear()
00122 {
00123     uint8_t *ptr = drawBuf;
00124     for (uint16_t i = 0; i < (width * height); i++) {
00125         *ptr = 0x00;
00126         ptr++;
00127     }
00128 }
00129 
00130 void LEDMatrix::reverse()
00131 {
00132     mask = ~mask;
00133 }
00134 
00135 uint8_t LEDMatrix::isReversed()
00136 {
00137     return mask;
00138 }
00139 
00140 void LEDMatrix::scan()
00141 {
00142     static uint8_t row = 0;
00143 
00144     if (!state) {
00145         return;
00146     }
00147 
00148     int debut1 = 0 + row * width ;
00149     int debut2 = 1024 + row * width ;
00150 
00151     for (uint8_t i = 0; i <64 ; i++) {
00152         clk = 0;
00153         
00154         r1 = displaybuf[debut1+i] & 0x01;
00155         r2 = displaybuf[debut2+i] & 0x01;
00156         g1 = (displaybuf[debut1+i] & 0x02) >> 1;
00157         g2 = (displaybuf[debut2+i] & 0x02) >> 1;
00158         b1 = (displaybuf[debut1+i] & 0x04) >> 2;
00159         b2 = (displaybuf[debut2+i] & 0x04) >> 2;
00160         
00161         /*r1=0;
00162         r2=0;
00163         g1=0;
00164         g2=0;
00165         b1=0;
00166         b2=0;*/
00167         clk = 1;
00168     }
00169 /*
00170     debut1 = 0 + row * width;
00171     debut2 = 1024 + row * width;
00172     
00173     for (uint8_t i = 0; i <64 ; i++) {
00174         clk = 0;
00175         
00176         r1 = displaybuf[debut1+i] & 0x01;
00177         r2 = displaybuf[debut2+i] & 0x01;
00178         g1 = (displaybuf[debut1+i] & 0x02) >> 1;
00179         g2 = (displaybuf[debut2+i] & 0x02) >> 1;
00180         b1 = (displaybuf[debut1+i] & 0x04) >> 2;
00181         b2 = (displaybuf[debut2+i] & 0x04) >> 2;
00182         
00183         r1=1;
00184         r2=1;
00185         g1=0;
00186         g2=0;
00187         b1=0;
00188         b2=0;
00189         
00190         clk = 1;
00191     }
00192 */
00193     oe = 1;              // disable display
00194 
00195     // select row
00196     a = (row & 0x01);
00197     b = (row & 0x02);
00198     c = (row & 0x04);
00199     d = (row & 0x08);
00200 
00201     // latch data
00202     stb = 0;
00203     stb = 1;
00204 
00205     oe = 0;              // enable display
00206 
00207     row=(row + 1) & 0x0F;
00208 
00209     if (row==0) {
00210         if (flagSwap == 1) {
00211             if (bufferIndex==0) {
00212                 bufferIndex = 1;
00213                 displaybuf = memoBuf + width*height;
00214                 drawBuf = memoBuf;
00215             } else {
00216                 bufferIndex = 0;
00217                 displaybuf = memoBuf;
00218                 drawBuf = memoBuf + width*height;
00219             }
00220             flagSwap = 0;
00221         }
00222     }
00223 }
00224 
00225 void LEDMatrix::on()
00226 {
00227     state = 1;
00228 }
00229 
00230 void LEDMatrix::off()
00231 {
00232     state = 0;
00233     oe = 1;
00234 }
00235 
00236 void LEDMatrix::swap()
00237 {
00238     flagSwap = 1;
00239 }
00240 
00241 int LEDMatrix::synchro()
00242 {
00243     return !flagSwap;
00244 }