-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGLEDMatrix80x7.cpp
More file actions
150 lines (118 loc) · 2.96 KB
/
RGLEDMatrix80x7.cpp
File metadata and controls
150 lines (118 loc) · 2.96 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "RGLEDMatrix80x7.h"
byte lineArrayGREEN[10];
byte lineArrayRED[10];
RGLEDMatrix80x7Class::RGLEDMatrix80x7Class()
{
DDRB |= B00111111;
PORTB &= B11000000;
ClearBuffer();
}
void RGLEDMatrix80x7Class::RandomPattern()
{
for (byte i = 0; i < 7; i++)
{
for (byte j = 0; j < 10; j++)
{
LEDBufferRED[i][j] = random() % 255;
LEDBufferGREEN[i][j] = random() % 255;
}
}
}
void RGLEDMatrix80x7Class::Refresh()
{
byte setMask;
for (byte selectInput = 1; selectInput <= 7; selectInput++)
{
setMask = selectInput << 3;
memcpy(&lineArrayGREEN[0], LEDBufferGREEN[selectInput - 1], sizeof(byte) * 10);
memcpy(&lineArrayRED[0], LEDBufferRED[selectInput - 1], sizeof(byte) * 10);
FeedLine(lineArrayGREEN, lineArrayRED);
PORTB = (PORTB & B11000111) | setMask;
delayMicroseconds(100); //line on (PWM)
PORTB = (PORTB & B11000111);
//delayMicroseconds(200); //line off
}
}
void RGLEDMatrix80x7Class::FeedLine(byte lineBytesGREEN[10], byte lineBytesRED[10])
{
byte redByte, greenByte;
bool bit;
for (uint8_t i = 0; i < 10; i++)
{
greenByte = flipByte(lineBytesGREEN[i]);
redByte = flipByte(lineArrayRED[i]);
for (byte mask = B00000001; mask; mask <<= 1)
{
//bit = GreenByte % 2;
//GreenByte = GreenByte >> 1;
if (greenByte & mask)
PORTB = PORTB | (1 << Serial_GREEN);
else
PORTB = PORTB & ~(1 << Serial_GREEN);
/*bit = RedByte % 2;
RedByte = RedByte >> 1;*/
if (redByte & mask)
PORTB = PORTB | (1 << Serial_RED);
else
PORTB = PORTB & ~(1 << Serial_RED);
PORTB = PORTB | (1 << Serial_CLK); //set //serial clock tick
PORTB = PORTB & ~(1 << Serial_CLK); //clear
}
}
}
void RGLEDMatrix80x7Class::ClearBuffer()
{
memset(&LEDBufferGREEN, 0, sizeof(LEDBufferGREEN));
memset(&LEDBufferRED, 0, sizeof(LEDBufferRED));
}
void RGLEDMatrix80x7Class::printBits(byte myByte)
{
for (byte mask = 0x80; mask; mask >>= 1)
{
if (mask & myByte)
Serial.print('1');
else
Serial.print('0');
}
}
byte RGLEDMatrix80x7Class::flipByte(byte c) //TODO
{
c = ((c >> 1) & 0x55) | ((c << 1) & 0xAA);
c = ((c >> 2) & 0x33) | ((c << 2) & 0xCC);
c = (c >> 4) | (c << 4);
return c;
}
void RGLEDMatrix80x7Class::DisplaySpectrum(byte data[80])
{
ClearBuffer();
for (byte i = 0; i < 80; i++)
{
SetPixel(i + 1, data[i], RED);
}
}
void RGLEDMatrix80x7Class::SetPixel(byte x, byte y, color ledColor)
{
if (x < 1 || x > 80 || y < 1 || y > 7) return;
x--;
y--;
switch (ledColor)
{
case GREEN:
LEDBufferGREEN[6 - y][x / 8] |= flipByte((1 << (x % 8)));
break;
case RED:
LEDBufferRED[6 - y][x / 8] |= flipByte((1 << (x % 8)));
break;
case ORANGE:
LEDBufferRED[6 - y][x / 8] |= flipByte((1 << (x % 8)));
LEDBufferGREEN[6 - y][x / 8] |= flipByte((1 << (x % 8)));
break;
case BLANK:
LEDBufferRED[6 - y][x / 8] &= flipByte(~(1 << (x % 8)));
LEDBufferGREEN[6 - y][x / 8] &= flipByte(~(1 << (x % 8)));
break;
default:
break;
}
}
RGLEDMatrix80x7Class RGLEDMatrix80x7;