LAST PUBLISH MATRIX PROJECT

Dependencies:   mbed

Committer:
antoine_carpentier
Date:
Wed Jun 23 14:32:47 2021 +0000
Revision:
2:552c5891b8c5
Parent:
1:7a7366246b30
LAST COMMIT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iut_cachan01 0:c78ec5cab98d 1 /**
iut_cachan01 0:c78ec5cab98d 2 * LED Matrix library for http://www.seeedstudio.com/depot/ultrathin-16x32-red-led-matrix-panel-p-1582.html
iut_cachan01 0:c78ec5cab98d 3 * The LED Matrix panel has 32x16 pixels. Several panel can be combined together as a large screen.
iut_cachan01 0:c78ec5cab98d 4 *
iut_cachan01 0:c78ec5cab98d 5 * Coordinate & Connection (mbed -> panel 0 -> panel 1 -> ...)
iut_cachan01 0:c78ec5cab98d 6 * (0, 0) (0, 0)
iut_cachan01 0:c78ec5cab98d 7 * +--------+--------+--------+ +--------+--------+
iut_cachan01 0:c78ec5cab98d 8 * | 5 | 3 | 1 | | 1 | 0 |
iut_cachan01 0:c78ec5cab98d 9 * | | | | | | |<----- mbed
iut_cachan01 0:c78ec5cab98d 10 * +--------+--------+--------+ +--------+--------+
iut_cachan01 0:c78ec5cab98d 11 * | 4 | 2 | 0 | (64, 16)
iut_cachan01 0:c78ec5cab98d 12 * | | | |<----- mbed
iut_cachan01 0:c78ec5cab98d 13 * +--------+--------+--------+
iut_cachan01 0:c78ec5cab98d 14 * (96, 32)
iut_cachan01 0:c78ec5cab98d 15 * Copyright (c) 2013 Seeed Technology Inc.
iut_cachan01 0:c78ec5cab98d 16 * @auther Yihui Xiong
iut_cachan01 0:c78ec5cab98d 17 * @date Nov 8, 2013
iut_cachan01 0:c78ec5cab98d 18 * @license Apache
iut_cachan01 0:c78ec5cab98d 19 */
iut_cachan01 0:c78ec5cab98d 20
iut_cachan01 0:c78ec5cab98d 21 #include "LEDMatrix.h"
iut_cachan01 0:c78ec5cab98d 22 #include "mbed.h"
iut_cachan01 0:c78ec5cab98d 23
iut_cachan01 0:c78ec5cab98d 24 #if 0
iut_cachan01 0:c78ec5cab98d 25 #define ASSERT(e) if (!(e)) { Serial.println(#e); while (1); }
iut_cachan01 0:c78ec5cab98d 26 #else
iut_cachan01 0:c78ec5cab98d 27 #define ASSERT(e)
iut_cachan01 0:c78ec5cab98d 28 #endif
iut_cachan01 0:c78ec5cab98d 29
iut_cachan01 0:c78ec5cab98d 30 LEDMatrix::LEDMatrix(PinName pinA, PinName pinB, PinName pinC, PinName pinD, PinName pinOE,
iut_cachan01 0:c78ec5cab98d 31 PinName pinR1, PinName pinR2, PinName pinB1, PinName pinB2, PinName pinG1, PinName pinG2,
iut_cachan01 0:c78ec5cab98d 32 PinName pinSTB, PinName pinCLK) :
antoine_carpentier 1:7a7366246b30 33 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)
iut_cachan01 0:c78ec5cab98d 34 {
iut_cachan01 0:c78ec5cab98d 35 oe = 1;
iut_cachan01 0:c78ec5cab98d 36 clk = 1;
iut_cachan01 0:c78ec5cab98d 37 stb = 1;
iut_cachan01 0:c78ec5cab98d 38 mask = 0xff;
iut_cachan01 0:c78ec5cab98d 39 state = 0;
iut_cachan01 0:c78ec5cab98d 40 bufferIndex = 0;
iut_cachan01 0:c78ec5cab98d 41 flagSwap = 0;
iut_cachan01 0:c78ec5cab98d 42 }
iut_cachan01 0:c78ec5cab98d 43
iut_cachan01 0:c78ec5cab98d 44 void LEDMatrix::begin(uint8_t *displaybuf, uint16_t width, uint16_t height)
iut_cachan01 0:c78ec5cab98d 45 {
iut_cachan01 0:c78ec5cab98d 46 this->displaybuf = displaybuf;
iut_cachan01 0:c78ec5cab98d 47 memoBuf = displaybuf;
iut_cachan01 0:c78ec5cab98d 48 drawBuf = memoBuf + width * height;
iut_cachan01 0:c78ec5cab98d 49 this->width = width;
iut_cachan01 0:c78ec5cab98d 50 this->height = height;
iut_cachan01 0:c78ec5cab98d 51
iut_cachan01 0:c78ec5cab98d 52 state = 1;
iut_cachan01 0:c78ec5cab98d 53 }
iut_cachan01 0:c78ec5cab98d 54
iut_cachan01 0:c78ec5cab98d 55 void LEDMatrix::drawPoint(uint16_t x, uint16_t y, uint8_t pixel)
iut_cachan01 0:c78ec5cab98d 56 {
iut_cachan01 0:c78ec5cab98d 57 if (x>=width) return;
iut_cachan01 0:c78ec5cab98d 58 if (y>=height) return;
iut_cachan01 0:c78ec5cab98d 59 drawBuf[x+ width * y] = pixel & 0x07;
iut_cachan01 0:c78ec5cab98d 60 }
iut_cachan01 0:c78ec5cab98d 61
iut_cachan01 0:c78ec5cab98d 62 void LEDMatrix::drawRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t pixel)
iut_cachan01 0:c78ec5cab98d 63 {
iut_cachan01 0:c78ec5cab98d 64 for (uint16_t x = x1; x < x2; x++) {
iut_cachan01 0:c78ec5cab98d 65 for (uint16_t y = y1; y < y2; y++) {
iut_cachan01 0:c78ec5cab98d 66 drawPoint(x, y, pixel);
iut_cachan01 0:c78ec5cab98d 67 }
iut_cachan01 0:c78ec5cab98d 68 }
iut_cachan01 0:c78ec5cab98d 69 }
iut_cachan01 0:c78ec5cab98d 70
iut_cachan01 0:c78ec5cab98d 71 int LEDMatrix::drawChar(uint16_t x, uint16_t y, char c, uint8_t pixel, Font *font)
iut_cachan01 0:c78ec5cab98d 72 {
iut_cachan01 0:c78ec5cab98d 73 int i, j,k, col, max = 0;
iut_cachan01 0:c78ec5cab98d 74 for (i=0; i<font->hauteur(); i++) {
iut_cachan01 0:c78ec5cab98d 75 for (k=0; k<font->largeur(); k++) {
iut_cachan01 0:c78ec5cab98d 76 col = font->octet(c, i, k);
iut_cachan01 0:c78ec5cab98d 77 for (j=8; j>=0; j--) {
iut_cachan01 0:c78ec5cab98d 78 if (col & 0x01 != 0) {
iut_cachan01 0:c78ec5cab98d 79 int dx = j+8*k;
iut_cachan01 0:c78ec5cab98d 80 if (dx > max) max = dx;
iut_cachan01 0:c78ec5cab98d 81 drawPoint(x + dx, y+i, pixel);
iut_cachan01 0:c78ec5cab98d 82 }
iut_cachan01 0:c78ec5cab98d 83 col = col >> 1;
iut_cachan01 0:c78ec5cab98d 84 }
iut_cachan01 0:c78ec5cab98d 85 }
iut_cachan01 0:c78ec5cab98d 86 }
iut_cachan01 0:c78ec5cab98d 87 if (c==' ') {
iut_cachan01 0:c78ec5cab98d 88 return 5*font->largeur();
iut_cachan01 0:c78ec5cab98d 89 } else {
iut_cachan01 0:c78ec5cab98d 90 return max + font->largeur();
iut_cachan01 0:c78ec5cab98d 91 }
iut_cachan01 0:c78ec5cab98d 92 }
iut_cachan01 0:c78ec5cab98d 93
iut_cachan01 0:c78ec5cab98d 94
iut_cachan01 0:c78ec5cab98d 95 void LEDMatrix::drawCharString (uint16_t x, uint16_t y, char *c, uint8_t pixel,Font *font)
iut_cachan01 0:c78ec5cab98d 96 {
iut_cachan01 0:c78ec5cab98d 97 int i=0;
iut_cachan01 0:c78ec5cab98d 98 while(c[i] != '\0') {
iut_cachan01 0:c78ec5cab98d 99 int dx = drawChar(x,y,c[i],pixel,font);
iut_cachan01 0:c78ec5cab98d 100 i++;
iut_cachan01 0:c78ec5cab98d 101 x += dx;
iut_cachan01 0:c78ec5cab98d 102 }
iut_cachan01 0:c78ec5cab98d 103 }
iut_cachan01 0:c78ec5cab98d 104
iut_cachan01 0:c78ec5cab98d 105
iut_cachan01 0:c78ec5cab98d 106 void LEDMatrix::drawImage(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t *image)
iut_cachan01 0:c78ec5cab98d 107 {
iut_cachan01 0:c78ec5cab98d 108 ASSERT(0 == ((x2 - x1) % 8));
iut_cachan01 0:c78ec5cab98d 109
iut_cachan01 0:c78ec5cab98d 110 for (uint16_t x = x1; x < x2; x++) {
iut_cachan01 0:c78ec5cab98d 111 for (uint16_t y = y1; y < y2; y++) {
iut_cachan01 0:c78ec5cab98d 112 uint8_t *byte = image + x * 8 + y / 8;
iut_cachan01 0:c78ec5cab98d 113 uint8_t bit = 7 - (y % 8);
iut_cachan01 0:c78ec5cab98d 114 uint8_t pixel = (*byte >> bit) & 1;
iut_cachan01 0:c78ec5cab98d 115
iut_cachan01 0:c78ec5cab98d 116 drawPoint(x, y, pixel);
iut_cachan01 0:c78ec5cab98d 117 }
iut_cachan01 0:c78ec5cab98d 118 }
iut_cachan01 0:c78ec5cab98d 119 }
iut_cachan01 0:c78ec5cab98d 120
iut_cachan01 0:c78ec5cab98d 121 void LEDMatrix::clear()
iut_cachan01 0:c78ec5cab98d 122 {
iut_cachan01 0:c78ec5cab98d 123 uint8_t *ptr = drawBuf;
iut_cachan01 0:c78ec5cab98d 124 for (uint16_t i = 0; i < (width * height); i++) {
iut_cachan01 0:c78ec5cab98d 125 *ptr = 0x00;
iut_cachan01 0:c78ec5cab98d 126 ptr++;
iut_cachan01 0:c78ec5cab98d 127 }
iut_cachan01 0:c78ec5cab98d 128 }
iut_cachan01 0:c78ec5cab98d 129
iut_cachan01 0:c78ec5cab98d 130 void LEDMatrix::reverse()
iut_cachan01 0:c78ec5cab98d 131 {
iut_cachan01 0:c78ec5cab98d 132 mask = ~mask;
iut_cachan01 0:c78ec5cab98d 133 }
iut_cachan01 0:c78ec5cab98d 134
iut_cachan01 0:c78ec5cab98d 135 uint8_t LEDMatrix::isReversed()
iut_cachan01 0:c78ec5cab98d 136 {
iut_cachan01 0:c78ec5cab98d 137 return mask;
iut_cachan01 0:c78ec5cab98d 138 }
iut_cachan01 0:c78ec5cab98d 139
iut_cachan01 0:c78ec5cab98d 140 void LEDMatrix::scan()
iut_cachan01 0:c78ec5cab98d 141 {
iut_cachan01 0:c78ec5cab98d 142 static uint8_t row = 0;
iut_cachan01 0:c78ec5cab98d 143
iut_cachan01 0:c78ec5cab98d 144 if (!state) {
iut_cachan01 0:c78ec5cab98d 145 return;
iut_cachan01 0:c78ec5cab98d 146 }
iut_cachan01 0:c78ec5cab98d 147
antoine_carpentier 1:7a7366246b30 148 int debut1 = 0 + row * width ;
antoine_carpentier 1:7a7366246b30 149 int debut2 = 1024 + row * width ;
iut_cachan01 0:c78ec5cab98d 150
iut_cachan01 0:c78ec5cab98d 151 for (uint8_t i = 0; i <64 ; i++) {
iut_cachan01 0:c78ec5cab98d 152 clk = 0;
antoine_carpentier 1:7a7366246b30 153
iut_cachan01 0:c78ec5cab98d 154 r1 = displaybuf[debut1+i] & 0x01;
iut_cachan01 0:c78ec5cab98d 155 r2 = displaybuf[debut2+i] & 0x01;
iut_cachan01 0:c78ec5cab98d 156 g1 = (displaybuf[debut1+i] & 0x02) >> 1;
iut_cachan01 0:c78ec5cab98d 157 g2 = (displaybuf[debut2+i] & 0x02) >> 1;
iut_cachan01 0:c78ec5cab98d 158 b1 = (displaybuf[debut1+i] & 0x04) >> 2;
iut_cachan01 0:c78ec5cab98d 159 b2 = (displaybuf[debut2+i] & 0x04) >> 2;
antoine_carpentier 1:7a7366246b30 160
antoine_carpentier 1:7a7366246b30 161 /*r1=0;
antoine_carpentier 1:7a7366246b30 162 r2=0;
antoine_carpentier 1:7a7366246b30 163 g1=0;
antoine_carpentier 1:7a7366246b30 164 g2=0;
antoine_carpentier 1:7a7366246b30 165 b1=0;
antoine_carpentier 1:7a7366246b30 166 b2=0;*/
iut_cachan01 0:c78ec5cab98d 167 clk = 1;
iut_cachan01 0:c78ec5cab98d 168 }
antoine_carpentier 1:7a7366246b30 169 /*
antoine_carpentier 1:7a7366246b30 170 debut1 = 0 + row * width;
antoine_carpentier 1:7a7366246b30 171 debut2 = 1024 + row * width;
antoine_carpentier 1:7a7366246b30 172
iut_cachan01 0:c78ec5cab98d 173 for (uint8_t i = 0; i <64 ; i++) {
iut_cachan01 0:c78ec5cab98d 174 clk = 0;
antoine_carpentier 1:7a7366246b30 175
iut_cachan01 0:c78ec5cab98d 176 r1 = displaybuf[debut1+i] & 0x01;
iut_cachan01 0:c78ec5cab98d 177 r2 = displaybuf[debut2+i] & 0x01;
iut_cachan01 0:c78ec5cab98d 178 g1 = (displaybuf[debut1+i] & 0x02) >> 1;
iut_cachan01 0:c78ec5cab98d 179 g2 = (displaybuf[debut2+i] & 0x02) >> 1;
iut_cachan01 0:c78ec5cab98d 180 b1 = (displaybuf[debut1+i] & 0x04) >> 2;
iut_cachan01 0:c78ec5cab98d 181 b2 = (displaybuf[debut2+i] & 0x04) >> 2;
antoine_carpentier 1:7a7366246b30 182
antoine_carpentier 1:7a7366246b30 183 r1=1;
antoine_carpentier 1:7a7366246b30 184 r2=1;
antoine_carpentier 1:7a7366246b30 185 g1=0;
antoine_carpentier 1:7a7366246b30 186 g2=0;
antoine_carpentier 1:7a7366246b30 187 b1=0;
antoine_carpentier 1:7a7366246b30 188 b2=0;
antoine_carpentier 1:7a7366246b30 189
iut_cachan01 0:c78ec5cab98d 190 clk = 1;
iut_cachan01 0:c78ec5cab98d 191 }
antoine_carpentier 1:7a7366246b30 192 */
iut_cachan01 0:c78ec5cab98d 193 oe = 1; // disable display
iut_cachan01 0:c78ec5cab98d 194
iut_cachan01 0:c78ec5cab98d 195 // select row
iut_cachan01 0:c78ec5cab98d 196 a = (row & 0x01);
iut_cachan01 0:c78ec5cab98d 197 b = (row & 0x02);
iut_cachan01 0:c78ec5cab98d 198 c = (row & 0x04);
iut_cachan01 0:c78ec5cab98d 199 d = (row & 0x08);
iut_cachan01 0:c78ec5cab98d 200
iut_cachan01 0:c78ec5cab98d 201 // latch data
iut_cachan01 0:c78ec5cab98d 202 stb = 0;
iut_cachan01 0:c78ec5cab98d 203 stb = 1;
iut_cachan01 0:c78ec5cab98d 204
iut_cachan01 0:c78ec5cab98d 205 oe = 0; // enable display
iut_cachan01 0:c78ec5cab98d 206
iut_cachan01 0:c78ec5cab98d 207 row=(row + 1) & 0x0F;
iut_cachan01 0:c78ec5cab98d 208
iut_cachan01 0:c78ec5cab98d 209 if (row==0) {
iut_cachan01 0:c78ec5cab98d 210 if (flagSwap == 1) {
iut_cachan01 0:c78ec5cab98d 211 if (bufferIndex==0) {
iut_cachan01 0:c78ec5cab98d 212 bufferIndex = 1;
iut_cachan01 0:c78ec5cab98d 213 displaybuf = memoBuf + width*height;
iut_cachan01 0:c78ec5cab98d 214 drawBuf = memoBuf;
iut_cachan01 0:c78ec5cab98d 215 } else {
iut_cachan01 0:c78ec5cab98d 216 bufferIndex = 0;
iut_cachan01 0:c78ec5cab98d 217 displaybuf = memoBuf;
iut_cachan01 0:c78ec5cab98d 218 drawBuf = memoBuf + width*height;
iut_cachan01 0:c78ec5cab98d 219 }
iut_cachan01 0:c78ec5cab98d 220 flagSwap = 0;
iut_cachan01 0:c78ec5cab98d 221 }
iut_cachan01 0:c78ec5cab98d 222 }
iut_cachan01 0:c78ec5cab98d 223 }
iut_cachan01 0:c78ec5cab98d 224
iut_cachan01 0:c78ec5cab98d 225 void LEDMatrix::on()
iut_cachan01 0:c78ec5cab98d 226 {
iut_cachan01 0:c78ec5cab98d 227 state = 1;
iut_cachan01 0:c78ec5cab98d 228 }
iut_cachan01 0:c78ec5cab98d 229
iut_cachan01 0:c78ec5cab98d 230 void LEDMatrix::off()
iut_cachan01 0:c78ec5cab98d 231 {
iut_cachan01 0:c78ec5cab98d 232 state = 0;
iut_cachan01 0:c78ec5cab98d 233 oe = 1;
iut_cachan01 0:c78ec5cab98d 234 }
iut_cachan01 0:c78ec5cab98d 235
iut_cachan01 0:c78ec5cab98d 236 void LEDMatrix::swap()
iut_cachan01 0:c78ec5cab98d 237 {
iut_cachan01 0:c78ec5cab98d 238 flagSwap = 1;
iut_cachan01 0:c78ec5cab98d 239 }
iut_cachan01 0:c78ec5cab98d 240
iut_cachan01 0:c78ec5cab98d 241 int LEDMatrix::synchro()
iut_cachan01 0:c78ec5cab98d 242 {
iut_cachan01 0:c78ec5cab98d 243 return !flagSwap;
iut_cachan01 0:c78ec5cab98d 244 }