This repository was archived by the owner on Mar 15, 2021. It is now read-only.
forked from lutzer/OlimexLEDMatrix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathled_matrix.h
121 lines (96 loc) · 2.52 KB
/
led_matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
OliLedMatrix is a Library to drive one Olimex 8x8 rgb matrix
Created by Lutz Reiter, Dec 2014.
Use it however you want to.
The standard SPI pins are: CLOCK: 13, DATA: 10 on Arduinon Uno and Duemilanove,
see http://arduino.cc/en/Reference/SPI for other models
*/
#ifndef OliLedMatrixRGB_h
#define OliLedMatrixRGB_h
#define ROWS 8
#include "Arduino.h"
#include "SPI.h"
typedef unsigned char uchar;
enum LedColor {
Blue = 1, //001
Green = 2, //010
Cyan = 3, //011
Red = 4, //100
Purple = 5, //101
Yellow = 6, //110
White = 7 //111
};
class OliLedMatrixRGB {
public:
OliLedMatrixRGB(int latchPin) {
this->latchPin = latchPin;
pinMode (latchPin, OUTPUT);
//SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV64);
SPI.begin();
this->color = White;
this->clear();
}
void clear() {
for (int i=0;i<ROWS*3;i++)
this->matrix[i] = 0;
}
void setColor(LedColor color) {
this->color = color;
}
void drawPixel(int x, int y) {
if (y < ROWS && y >= 0) {
// reset color
matrix[3*y] &= ~(1 << (8-x-1));
matrix[3*y+1] &= ~(1 << (8-x-1));
matrix[3*y+2] &= ~(1 << (8-x-1));
// draw color
if ( (this->color & (1 << 2)) > 0)
matrix[3*y] |= (1 << (8-x-1) );
if ( (this->color & (1 << 1)) > 0)
matrix[3*y+1] |= (1 << (8-x-1) );
if ( (this->color & 1) > 0)
matrix[3*y+2] |= (1 << (8-x-1) );
}
}
void drawRectangle(int x, int y, int w, int h, boolean filled) {
if (filled) {
for(int i=y; i<y+h; i++)
for(int j=x; j<x+w; j++)
this->drawPixel(i,j);
} else {
for(int i=y; i<y+h; i++) {
this->drawPixel(x,i);
this->drawPixel(x+w-1,i);
}
for(int i=x; i<x+w; i++) {
this->drawPixel(i,y);
this->drawPixel(i,y+h-1);
}
}
}
void drawArray(int x, int y, const uchar *array, int rows) {
for (int i=0; i<rows; i++) {
if ( (this->color & (1 << 2)) > 0)
matrix[3*(y+i)] |= (array[i] >> x);
if ( (this->color & (1 << 1)) > 0)
matrix[3*(y+i)+1] |= (array[i] >> x);
if ( (this->color & 1) > 0)
matrix[3*(y+i)+2] |= (array[i] >> x);
}
}
void display() {
digitalWrite(this->latchPin, LOW);
for (byte i=0; i<ROWS*3; i++) {
SPI.transfer(matrix[i]);
delayMicroseconds(10);
}
digitalWrite(this->latchPin, HIGH);
}
private:
uchar matrix[ROWS*3];
int latchPin;
LedColor color;
uchar brightness;
};
#endif