Author Topic: ACPCPE - Amstrad CPC Printer Emulator (DIY)  (Read 384 times)

0 Members and 1 Guest are viewing this topic.

Offline elanstra

  • CPC464
  • **
  • Posts: 20
  • Country: de
  • Liked: 12
  • Likes Given: 4
ACPCPE - Amstrad CPC Printer Emulator (DIY)
« on: 14:10, 30 August 19 »
Hi everybody, I would like to present to you my Epson-compatible dot matrix printer emulator for the Amstrad CPC.

It connects the Amstrad CPC's printer port to a PC acting as a virtual printer and generates files (currently txt, html and markdown) instead of printing paper.

It uses an Arduino-compatible  board (I used Teensy 2.0) to translate the parallel data coming out of the CPC's printer port into serial hexadecimal data, which is sent to a PC running Pyhton which processes the data and generates the files. If the data received contains ESC/P codes, it generates HTML or Markdown files. If the data is plain text, it generates TXT files.


It's great for printing BASIC programs and copy&paste to emulators using LIST#8 command.

You can find the details here: https://github.com/asmCcoder/ACPCPE


I hope it's of some use to somebody.

Online Gryzor

  • Administrator
  • 6128 Plus
  • *****
  • Posts: 15.040
  • Country: gr
  • CPC-Wiki maintainer
    • CPCWiki
  • Liked: 2982
  • Likes Given: 5180
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #1 on: 14:29, 30 August 19 »
Heh, fun and geeky :D

Offline Patrick

  • CPC464
  • **
  • Posts: 13
  • Country: fr
  • Liked: 8
  • Likes Given: 7
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #2 on: 16:08, 30 August 19 »
Nice project.
But why using a NE555 just to debounce the push button. It could be done in software or with just a RC circuit.

Offline norecess

  • CPC664
  • ***
  • Posts: 70
  • Country: ca
  • Liked: 142
  • Likes Given: 61
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #3 on: 16:17, 30 August 19 »
Quote
This README was written on a real Amstrad CPC6128 using Protext and then printed as Markdown file on a Linux machine.
Wow, respect !! Good project.

Offline elanstra

  • CPC464
  • **
  • Posts: 20
  • Country: de
  • Liked: 12
  • Likes Given: 4
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #4 on: 11:23, 31 August 19 »
Nice project.
But why using a NE555 just to debounce the push button. It could be done in software or with just a RC circuit.
They why was simply because I'm more familiar with the NE555 than with RC circuits  :-[ But you're absolutely right, I think it could be done by software on the Arduino.

I got a bit obsessed with debouncing the button after my first tests. The Arduino wasn't fast enough for the Amstrad CPC Printer port. Crazy, isn't it? The Printer port of the CPC is lacking the /Ack signal, so you have to read the parallel data at the speed of the /Strobe. I had to use ISR (Interrupt Service Routine) to be able read the pins (/Strobe) fast enough and then I did the same for the online/offline button. But after your comment, I thought a bit about it and actually this button doesn't need to be that fast because it's (should be) used when the CPC is not sending data.
Lets see if I have some free time soon and I can make a version 2 of the project. Thanks for the advice!

Offline pelrun

  • VK4CPC
  • Supporter
  • 6128 Plus
  • *
  • Posts: 573
  • Country: au
    • index.php?action=treasury
  • Liked: 296
  • Likes Given: 161
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #5 on: 16:17, 31 August 19 »
The Arduino wasn't fast enough for the Amstrad CPC Printer port. Crazy, isn't it?

There's a couple of reasons for it. While the CPC may not be sending characters particularly fast, there's only a very small window after Strobe is pulsed that the data is still valid. And entering an interrupt has a minimum delay, which is roughly 1ms. If the CPC changes the value too quickly then you can't capture it in a naive manner and would need some extra hardware support such as an external 8-way latch.

Also, GPIO's are arranged into groups of 8, and each is read as a whole byte. But since you're manually reading each bit individually you're really reading each pin many times but discarding the results, which makes the whole process take much longer in the critical period.

You currently have the data lines split across ports B and D, so instead you can just read each port once and then do the bit manipulation to extract the actual data byte afterwards, which will make it much easier to keep up with the CPC (partly because you eliminate 6 unnecessary port reads entirely, and partly because you move all the calculations out of the critical section.)

I rewrote the arduino side quickly, code is below. It adds software debouncing in the main loop (rather than using interrupts) and changed the serial writes to send the raw bytes instead of unnecessarily converting to hex ascii, although that also requires updating the python code. Also the select/deselect codes are 17 and 19 decimal, not hex! :D

(edit: fixed minor bug in debounce, it's not like I tested this or anything...)

Code: [Select]
#define PRN_STRB  5   // /Strobe
#define ONOFF_BTN 6   // Online/Offline signal (through push-buttons + 555 Timer IC)

#define PRN_D0    0   // Data 0
#define PRN_D1    1   // Data 1
#define PRN_D2    2   // Data 2
#define PRN_D3    3   // Data 3
#define PRN_D4    4   // Data 4
#define PRN_D5    7   // Data 5
#define PRN_D6    8   // Data 6

#define PRN_BUSY  9   // Busy = HIGH / Ready = LOW
#define ONOFF_LED 10  // Indicator of "printer" Online/Offline

#define COM_SPEED 115200 // Speed of the COM port between Arduino and PC
#define DEBOUNCE_LIMIT 10 // button input must be stable for this many reads to register

/////////////////////////////////////////////////////////////////////////
byte data = 0;      // Variable for converting parallel data from D0..D6 as a single byte
bool wasOnline = false; // Variable for storing Online/Offline push-button state
byte debounce_count = 0;

/////////////////////////////////////////////////////////////////////////
void setup() {
  pinMode(PRN_BUSY, OUTPUT);
  digitalWrite(PRN_BUSY, 1); // Tell CPC that we're busy setting up everything
 
  pinMode(ONOFF_LED, OUTPUT);
  digitalWrite(ONOFF_LED, LOW);
 
  pinMode(PRN_D0, INPUT);
  pinMode(PRN_D1, INPUT);
  pinMode(PRN_D2, INPUT);
  pinMode(PRN_D3, INPUT);
  pinMode(PRN_D4, INPUT);
  pinMode(PRN_D5, INPUT);
  pinMode(PRN_D6, INPUT);

  pinMode(PRN_STRB, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(PRN_STRB), readCPCbyte, LOW);

  digitalWrite(11, HIGH);   // turn the Teensy's internal LED on

  Serial.begin(COM_SPEED);
  Serial.write(19); // 19 = Deselect printer
}

void readCPCbyte(){
  // This function is called when /Strobe is Low (detected by Arduino interrupt)

  // read the ports as fast as possible
  // PINx returns the current input of port x
  byte port_b = PINB;
  byte port_d = PIND;

  // below just needs to happen before the next Strobe

  // reassemble data byte
  // (0) D3 D2 B7 B3 B2 B1 B0
  data = ((port_d & 0b00001100) << 3) | ((port_b & 0b1000000) >> 3) | (port_b & 0b00001111);

  // print byte to the Serial port
  Serial.write(buf);
}

void loop()
{
  // debounce the online button and toggle busy status when pressed
  if (!digitalRead(ONOFF_BTN))
  {
    if (debounce_count < DEBOUNCE_LIMIT)
    {
      debounce_count++;
    }
    else
    {
      if (wasOnline)
      {
        wasOnline = false;
        Serial.write(19); // 19 = Deselect printer
        digitalWrite(PRN_BUSY, HIGH); // Printer is Offline
      }
      else
      {
        wasOnline = true;
        Serial.write(17); // 17 = Select printer
        digitalWrite(PRN_BUSY, LOW); // Printer is Online, tell the CPC that we're ready for data
      }
      digitalWrite(ONOFF_LED, wasOnline);
    }
  }
  else
  {
    debounce_count = 0;
  }

}
« Last Edit: 16:26, 31 August 19 by pelrun »

Offline elanstra

  • CPC464
  • **
  • Posts: 20
  • Country: de
  • Liked: 12
  • Likes Given: 4
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #6 on: 19:46, 31 August 19 »
Nice project.
But why using a NE555 just to debounce the push button. It could be done in software or with just a RC circuit.
You asked for it (or maybe not  :laugh: ) and you have it: ACPCPE v2.0 with debounce by software and no more NE555. I even took the time to make a nicer connector for future improvements.
https://github.com/asmCcoder/ACPCPE


I rewrote the arduino side quickly, code is below. It adds software debouncing in the main loop (rather than using interrupts) and changed the serial writes to send the raw bytes instead of unnecessarily converting to hex ascii, although that also requires updating the python code. Also the select/deselect codes are 17 and 19 decimal, not hex! :D
Man, you were quick!  ;D Unfortunately I did already changes to debounce by software and remove the NE555 before I saw your post. I'm going to have a look at your code. Maybe for a newer version, but will have to be on another weekend.

Offline Neurox66

  • CPC464
  • **
  • Posts: 3
  • Country: it
    • My WebSite
  • Liked: 0
  • Likes Given: 3
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #7 on: 10:03, 01 September 19 »

Congratulations, interesting project.
It would be interesting to add the possibility to save data on SD without connecting a PC  :)
A project similar to RetroPrinter by Rwap Software but cheap  :D




Offline elanstra

  • CPC464
  • **
  • Posts: 20
  • Country: de
  • Liked: 12
  • Likes Given: 4
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #8 on: 15:56, 01 September 19 »
Congratulations, interesting project.
It would be interesting to add the possibility to save data on SD without connecting a PC  :)
A project similar to RetroPrinter by Rwap Software but cheap  :D
AFAIK, RetroPrinter uses a Raspberry Pi to generate the files, so technically speaking it uses a PC too  :D

Offline genesis8

  • CPC6128
  • ****
  • Posts: 179
  • Country: fr
    • Genesis8 Amstrad Page
  • Liked: 50
  • Likes Given: 20
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #9 on: 19:55, 01 September 19 »
Can I use the photo on the github page on my amstrad news site ?
____________
Amstrad news site at Genesis8 Amstrad Page

Offline elanstra

  • CPC464
  • **
  • Posts: 20
  • Country: de
  • Liked: 12
  • Likes Given: 4
Re: ACPCPE - Amstrad CPC Printer Emulator (DIY)
« Reply #10 on: 23:08, 01 September 19 »
Can I use the photo on the github page on my amstrad news site ?
Sure thing! I always release under MIT License so feel free to use whatever you need/want.