As often happens, a new usage for something uncovers issues. That happened when I went to add the ability to print a banner to my retro-computer. Up to this point the printer interface had never had to print anything harder than a few Tiny Basic listings. Although I had tested the XON/XOFF flow control when I originally wrote the sketch, the implementation of banner in the computer ended up refreshing my knowledge of buffer management. Ultimately the problem was a deep buffer in the computer coupled with a delay seeing the XOFF flow-control from the printer that over-ran the printer's 2 kB buffer (1 kB in the arduino, 1 kB in the HP printer itself). But debugging the problem uncovered bugs in the printer code too.
The new, v1.1 code is included in the zip file for this project. It fixes the following bugs.
- Remove a buffer near-full check on incoming data so the printer can continue to receive data (through the Arduino's RX buffer) even after it has requested flow control.
- Refactored the code that drove parallel bytes to the printer to not block the main thread waiting for the printer to report ready for writing.
- Changed how serial data was read in the main loop to make flow-control generation more accurate.
- Decreased the high-water buffer mark to allow for more data to be sent after flow-control has been requested.
In addition, this code has a new feature where it will echo back the ETB (End of Transmission Block) control character when that character flows through the RX buffer to the printer. This allows external devices to know when buffered data in the arduino-side of the printer has been exhausted. The idea is that the external device will include the ETB character as a marker. When it sees the character echoed back it knows all the buffered data before the character has been loaded into the printer. I make use of this in the banner functionality to show (more-or-less) on the screen what character is being printed.