GY-63_MS5611/libraries/ParallelPrinter/ParallelPrinter.cpp

165 lines
3.0 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: ParallelPrinter.cpp
// AUTHOR: Rob Tillaart
2022-11-19 09:28:46 -05:00
// VERSION: 0.2.5
2021-01-29 06:31:58 -05:00
// PURPOSE: parallel printer class that implements the Print interface
// DATE: 2013-09-30
// URL: https://github.com/RobTillaart/ParallelPrinter
2022-11-19 09:28:46 -05:00
2021-11-11 07:38:49 -05:00
2021-01-29 06:31:58 -05:00
#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 )
{
2022-11-19 09:28:46 -05:00
// CONTROL LINES
2021-01-29 06:31:58 -05:00
_strobePin = STROBE;
_busyPin = BUSY;
_oopPin = OOP;
pinMode(_oopPin, INPUT);
pinMode(_busyPin, INPUT);
pinMode(_strobePin, OUTPUT);
2021-12-23 04:08:01 -05:00
2022-11-19 09:28:46 -05:00
// DATA LINES
2021-01-29 06:31:58 -05:00
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;
}
2022-11-19 09:28:46 -05:00
// write() implements the virtual write of the Print class
2021-01-29 06:31:58 -05:00
size_t ParallelPrinter::write(uint8_t c)
{
2022-11-19 09:28:46 -05:00
if (c == '\t') // TAB
2021-01-29 06:31:58 -05:00
{
uint8_t spaces = _tabSize - _pos % _tabSize;
for (uint8_t i = 0; i < spaces; i++) processSingleChar(' ');
return spaces;
}
2022-11-19 09:28:46 -05:00
if (c == '\n') // LINEFEED
2021-01-29 06:31:58 -05:00
{
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)
{
2022-11-19 09:28:46 -05:00
// not nice - implicit recursion...
2021-01-29 06:31:58 -05:00
print(_lineNr);
print('\t');
}
if (c == FORMFEED)
{
sendByte(c);
sendByte('\n');
_pos = 0;
_lineNr = 0;
_pageNr++;
}
else
{
sendByte(c);
}
2022-11-19 09:28:46 -05:00
// HANDLE FULL LINE
2021-01-29 06:31:58 -05:00
if (_pos > _lineLength)
{
_pos = 0;
for (uint8_t i = 0; i < _lineFeed; i++)
{
sendByte('\n');
_lineNr++;
}
}
2022-11-19 09:28:46 -05:00
// HANDLE FULL PAGE
2021-01-29 06:31:58 -05:00
if (_lineNr > _pageLength)
{
sendByte(FORMFEED);
sendByte('\n');
_pos = 0;
_lineNr = 0;
_lineNr = 0;
_pageNr++;
}
}
2022-11-19 09:28:46 -05:00
// lowest level communication
2021-01-29 06:31:58 -05:00
void ParallelPrinter::sendByte(uint8_t c)
{
2022-11-19 09:28:46 -05:00
// BLOCK WHEN OUT OF PAPER TODO
// while (digitalRead(_oopPin) == LOW) yield();
// indication in hardware?
2021-12-23 04:08:01 -05:00
2022-11-19 09:28:46 -05:00
Serial.write(c); // debugging
2021-01-29 06:31:58 -05:00
return;
2022-11-19 09:28:46 -05:00
// wait until printer is ready.
2021-01-29 06:31:58 -05:00
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);
2022-11-19 09:28:46 -05:00
// time consuming part
2021-01-29 06:31:58 -05:00
if (_strobeDelay) delayMicroseconds(_strobeDelay);
digitalWrite(_strobePin, HIGH);
2022-11-19 09:28:46 -05:00
// wait till data is read by printer.
2021-01-29 06:31:58 -05:00
while (digitalRead(_busyPin) == LOW) yield();
}
// -- END OF FILE --
2021-12-23 04:08:01 -05:00