added double buffering to prevent tearning
[Arduino] / Hub08_LedMatrix / LEDMatrix.h
1 /**
2  * LED Matrix library for http://www.seeedstudio.com/depot/ultrathin-16x32-red-led-matrix-panel-p-1582.html
3  * The LED Matrix panel has 32x16 pixels. Several panel can be combined together as a large screen.
4  *
5  * Coordinate & Connection (Arduino -> panel 0 -> panel 1 -> ...)
6  *   (0, 0)                                     (0, 0)
7  *     +--------+--------+--------+               +--------+--------+
8  *     |   5    |    4   |    3   |               |    1   |    0   |
9  *     |        |        |        |               |        |        |<----- Arduino
10  *     +--------+--------+--------+               +--------+--------+
11  *     |   2    |    1   |    0   |                              (64, 16)
12  *     |        |        |        |<----- Arduino
13  *     +--------+--------+--------+
14  *                             (96, 32)
15  *  Copyright (c) 2013 Seeed Technology Inc.
16  *  @auther     Yihui Xiong
17  *  @date       Nov 7, 2013
18  *  @license    MIT
19  */
20
21
22 #ifndef __LED_MATRIX_H__
23 #define __LED_MATRIX_H__
24
25  #include <stdint.h>
26
27 // use hardware SPI
28 #define USE_SPI 0
29
30 class LEDMatrix {
31 public:
32     LEDMatrix(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t oe, uint8_t r1, uint8_t stb, uint8_t clk);
33
34     /**
35      * set the display's display buffer and number, the buffer's size must be not less than 512 * number / 8 bytes
36      * @param displaybuf    display buffer
37      * @param number        panels' number
38      */
39     void begin(uint8_t *displaybuf, uint16_t width, uint16_t height);
40
41     /**
42      * draw a point
43      * @param x     x
44      * @param y     y
45      * @param pixel 0: led off, >0: led on
46      */
47     void drawPoint(uint16_t x, uint16_t y, uint8_t pixel);
48
49     /**
50      * draw a rect
51      * @param (x1, y1)   top-left position
52      * @param (x2, y2)   bottom-right position, not included in the rect
53      * @param pixel      0: rect off, >0: rect on
54      */
55     void drawRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t pixel);
56
57     /**
58      * draw a image
59      * @param (xoffset, yoffset)   top-left offset of image
60      * @param (width, height)      image's width and height
61      * @param pixels     contents, 1 bit to 1 led
62      */
63     void drawImage(uint16_t xoffset, uint16_t yoffset, uint16_t width, uint16_t height, const uint8_t *image);
64
65     /**
66      * Set screen buffer to zero
67      */
68     void clear();
69
70     /**
71      * turn off 1/16 leds and turn on another 1/16 leds
72      */
73     void scan();
74
75     void swap();
76
77     uint8_t *offscreen_buffer();
78
79     void reverse();
80
81     uint8_t isReversed();
82
83     void on();
84
85     void off();
86
87 private:
88     uint8_t clk, r1, stb, oe, a, b, c, d;
89     uint8_t *displaybuf;
90     uint16_t width;
91     uint16_t height;
92     uint8_t  mask;
93     uint8_t  state;
94     uint8_t  buffer;
95 };
96
97 #endif