Yihui Xiong / LEDMatrix

Dependents:   led_matrix

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, PinName pinR1, PinName pinSTB, PinName pinCLK) :
00031         a(pinA), b(pinB), c(pinC), d(pinD), oe(pinOE), r1(pinR1), stb(pinSTB), clk(pinCLK)
00032 {
00033     this->clk = clk;
00034     this->r1 = r1;
00035     this->stb = stb;
00036     this->oe = oe;
00037     this->a = a;
00038     this->b = b;
00039     this->c = c;
00040     this->d = d;
00041 
00042     mask = 0xff;
00043     state = 0;
00044 }
00045 
00046 void LEDMatrix::begin(uint8_t *displaybuf, uint16_t width, uint16_t height)
00047 {
00048     ASSERT(0 == (width % 32));
00049     ASSERT(0 == (height % 16));
00050 
00051     this->displaybuf = displaybuf;
00052     this->width = width;
00053     this->height = height;
00054     
00055     state = 1;
00056 }
00057 
00058 void LEDMatrix::drawPoint(uint16_t x, uint16_t y, uint8_t pixel)
00059 {
00060     ASSERT(width > x);
00061     ASSERT(height > y);
00062 
00063     uint8_t *byte = displaybuf + x / 8 + y * width / 8;
00064     uint8_t  bit = x % 8;
00065 
00066     if (pixel) {
00067         *byte |= 0x80 >> bit;
00068     } else {
00069         *byte &= ~(0x80 >> bit);
00070     }
00071 }
00072 
00073 void LEDMatrix::drawRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t pixel)
00074 {
00075     for (uint16_t x = x1; x < x2; x++) {
00076         for (uint16_t y = y1; y < y2; y++) {
00077             drawPoint(x, y, pixel);
00078         }
00079     }
00080 }
00081 
00082 void LEDMatrix::drawImage(uint16_t xoffset, uint16_t yoffset, uint16_t width, uint16_t height, const uint8_t *image)
00083 {
00084     for (uint16_t y = 0; y < height; y++) {
00085         for (uint16_t x = 0; x < width; x++) {
00086             const uint8_t *byte = image + (x + y * width) / 8;
00087             uint8_t  bit = 7 - x % 8;
00088             uint8_t  pixel = (*byte >> bit) & 1;
00089 
00090             drawPoint(x + xoffset, y + yoffset, pixel);
00091         }
00092     }
00093 }
00094 
00095 void LEDMatrix::clear()
00096 {
00097     uint8_t *ptr = displaybuf;
00098     for (uint16_t i = 0; i < (width * height / 8); i++) {
00099         *ptr = 0x00;
00100         ptr++;
00101     }
00102 }
00103 
00104 void LEDMatrix::reverse()
00105 {
00106     mask = ~mask;
00107 }
00108 
00109 uint8_t LEDMatrix::isReversed()
00110 {
00111     return mask;
00112 }
00113 
00114 void LEDMatrix::scan()
00115 {
00116     static uint8_t row = 0;
00117 
00118     if (!state) {
00119         return;
00120     }
00121 
00122     uint8_t *head = displaybuf + row * (width / 8);
00123     for (uint8_t line = 0; line < (height / 16); line++) {
00124         uint8_t *ptr = head;
00125         head += width * 2;              // width * 16 / 8
00126 
00127         for (uint8_t byte = 0; byte < (width / 8); byte++) {
00128             uint8_t pixels = *ptr;
00129             ptr++;
00130             pixels = pixels ^ mask;   // reverse: mask = 0xff, normal: mask =0x00 
00131             for (uint8_t bit = 0; bit < 8; bit++) {
00132                 clk = 0;
00133                 r1 = pixels & (0x80 >> bit);
00134                 clk = 1;
00135             }
00136         }
00137     }
00138 
00139     oe = 1;              // disable display
00140 
00141     // select row
00142     a = (row & 0x01);
00143     b = (row & 0x02);
00144     c = (row & 0x04);
00145     d = (row & 0x08);
00146 
00147     // latch data
00148     stb = 0;
00149     stb = 1;
00150 
00151     oe = 0;              // enable display
00152 
00153     row = (row + 1) & 0x0F;
00154 }
00155 
00156 void LEDMatrix::on()
00157 {
00158     state = 1;
00159 }
00160 
00161 void LEDMatrix::off()
00162 {
00163     state = 0;
00164     oe = 1;
00165 }
00166