- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScreenBuffer.cpp
More file actions
Latest commit
149 lines (120 loc) · 2.75 KB
/
Copy pathScreenBuffer.cpp
File metadata and controls
149 lines (120 loc) · 2.75 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"ScreenBuffer.h"
#include"arduino.h"
ScreenBuffer* ScreenBuffer::_instance = 0;
ScreenBuffer::ScreenBuffer(int width, int height) : _width(width), _height(height), _size(width*height), _currentRow(0)
{
_instance = this;
_buffer = newunsignedlong[height];
ClearBuffer();
}
ScreenBuffer::~ScreenBuffer(void)
{
_instance = 0;
delete _buffer;
}
ScreenBuffer* ScreenBuffer::GetInstance()
{
return _instance;
}
voidScreenBuffer::SetupRowPins(int dataPin, int latchPin, int clockPin)
{
_rowDataPin = dataPin;
_rowLatchPin = latchPin;
_rowClockPin = clockPin;
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
voidScreenBuffer::SetupColumnPins(int dataPin, int latchPin, int clockPin)
{
_colDataPin = dataPin;
_colLatchPin = latchPin;
_colClockPin = clockPin;
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
intScreenBuffer::GetWidth()
{
return _width;
}
intScreenBuffer::GetHeight()
{
return _height;
}
voidScreenBuffer::Write(int x, int y, int value)
{
// Clip
if (x < 0 || x > _width-1 || y < 0 || y > _height-1)
return;
if (value > 0)
{
_buffer[y] |= 1 << x;
return;
}
_buffer[y] ^= 1 << x;
}
voidScreenBuffer::ClearBuffer()
{
for (int i=0; i<_height; ++i)
{
_buffer[i] = 0;
}
}
voidScreenBuffer::SyncNext()
{
_currentRow++;
if (_currentRow == _height)
_currentRow = 0;
SyncRow(_currentRow);
}
voidScreenBuffer::SyncRow(int row)
{
// Clear column data in register before moving to next row
ShiftZeros(_colDataPin, _colLatchPin, _colClockPin, _width);
// Shift current row out (1 of all rows will be illuminated)
ShiftSingle(_rowDataPin, _rowLatchPin, _rowClockPin, (row==0)?1:0);
// Shift column data out
digitalWrite(_colLatchPin, LOW);
for (int col=0; col<_width; ++col)
{
digitalWrite(_colClockPin, LOW);
int val = (_buffer[row] >> col) & 1;
digitalWrite(_colDataPin, val);
digitalWrite(_colClockPin, HIGH);
}
digitalWrite(_colLatchPin, HIGH);
}
voidScreenBuffer::Sync()
{
// Row by row shifting, top to bottom
for (int row=0; row<_height; ++row)
{
SyncRow(row);
// Let it flow
delayMicroseconds(500);
}
// Shift last bit out of the row
ShiftSingle(_rowDataPin, _rowLatchPin, _rowClockPin, 0);
}
voidScreenBuffer::ShiftZeros(int data, int latch, int clock, int numshifts)
{
digitalWrite(latch, LOW);
digitalWrite(data, LOW);
digitalWrite(clock, LOW);
for (int i=0; i<numshifts; i++)
{
digitalWrite(clock, LOW);
digitalWrite(data, LOW);
digitalWrite(clock, HIGH);
}
digitalWrite(latch, HIGH);
}
voidScreenBuffer::ShiftSingle(int data, int latch, int clock, int value)
{
digitalWrite(latch, LOW);
digitalWrite(clock, LOW);
digitalWrite(data, value);
digitalWrite(clock, HIGH);
digitalWrite(latch, HIGH);
}