forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelPrinter.cpp
More file actions
167 lines (142 loc) · 3.17 KB
/
Copy pathParallelPrinter.cpp
File metadata and controls
167 lines (142 loc) · 3.17 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// FILE: ParallelPrinter.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.2
// PURPOSE: parallel printer class that implements the Print interface
// DATE: 2013-09-30
// URL: https://github.com/RobTillaart/ParallelPrinter
//
// HISTORY
// 0.1.0 2013-09-30 initial release
// 0.2.0 2020-05-26 refactor, examples
// 0.2.1 2021-01-04 arduino-CI + unit test
// 0.2.2 2021-01-14 update readme, add linefeed(), add keywords.txt
#include "ParallelPrinter.h"
ParallelPrinter::ParallelPrinter()
{
uint8_t dataPins[] = {3, 4, 5, 6, 7, 8, 9, 10};
ParallelPrinter(13, 2, 12, dataPins );
}
ParallelPrinter::ParallelPrinter(uint8_t STROBE, uint8_t BUSY, uint8_t OOP, uint8_t * p )
{
// CONTROL LINES
_strobePin = STROBE;
_busyPin = BUSY;
_oopPin = OOP;
pinMode(_oopPin, INPUT);
pinMode(_busyPin, INPUT);
pinMode(_strobePin, OUTPUT);
// DATA LINES
for (uint8_t i = 0; i < 8; i++)
{
_pin[i] = p[i];
pinMode(_pin[i], OUTPUT);
}
setLineLength(80);
setPageLength(60);
reset();
}
void ParallelPrinter::begin(uint8_t lineLength, uint8_t pageLength)
{
setLineLength(lineLength);
setPageLength(pageLength);
reset();
}
void ParallelPrinter::reset()
{
_pos = 0;
_lineNr = 0;
_pageNr = 0;
_tabSize = 2;
_lineFeed = 1;
_strobeDelay = 2000;
_printLineNumber = false;
}
// write() implements the virtual write of the Print class
size_t ParallelPrinter::write(uint8_t c)
{
if (c == '\t') // TAB
{
uint8_t spaces = _tabSize - _pos % _tabSize;
for (uint8_t i = 0; i < spaces; i++) processSingleChar(' ');
return spaces;
}
if (c == '\n') // LINEFEED
{
for (uint8_t i = 0; i < _lineFeed; i++)
{
processSingleChar(c);
_pos = 0;
_lineNr++;
}
return _lineFeed;
}
processSingleChar(c);
return 1;
}
void ParallelPrinter::processSingleChar(uint8_t c)
{
_pos++;
if ((_pos == 1) && _printLineNumber)
{
// not nice - implicit recursion...
print(_lineNr);
print('\t');
}
if (c == FORMFEED)
{
sendByte(c);
sendByte('\n');
_pos = 0;
_lineNr = 0;
_pageNr++;
}
else
{
sendByte(c);
}
// HANDLE FULL LINE
if (_pos > _lineLength)
{
_pos = 0;
for (uint8_t i = 0; i < _lineFeed; i++)
{
sendByte('\n');
_lineNr++;
}
}
// HANDLE FULL PAGE
if (_lineNr > _pageLength)
{
sendByte(FORMFEED);
sendByte('\n');
_pos = 0;
_lineNr = 0;
_lineNr = 0;
_pageNr++;
}
}
// lowest level communication
void ParallelPrinter::sendByte(uint8_t c)
{
// BLOCK WHEN OUT OF PAPER TODO
// while (digitalRead(_oopPin) == LOW) yield();
// indication in hardware?
Serial.write(c); // debugging
return;
// wait until printer is ready.
while (digitalRead(_busyPin) == HIGH) yield();
uint8_t mask = 0x80;
for (uint8_t i = 0; i < 8; i++)
{
digitalWrite(_pin[i], c & mask );
mask >>= 1;
}
digitalWrite(_strobePin, LOW);
// time consuming part
if (_strobeDelay) delayMicroseconds(_strobeDelay);
digitalWrite(_strobePin, HIGH);
// wait till data is read by printer.
while (digitalRead(_busyPin) == LOW) yield();
}
// -- END OF FILE --