Go Down

Topic: Arduino and gameboy printer (Read 1 time) previous topic - next topic

bokac

Well i have bought  a gameboy printer and now i am trying to print from it. Here is the code i have. It is not working. I have hardcoded a picture. Am I maby doing it wrong? When i send the questin mark, and gameboy printer is connected it replys 1, else it replys 0, so connetion between arduino and printer is working. Please help.
Code: [Select]

#include <avr/pgmspace.h>

#define delayMs 20

#define GBClock  7
#define GBIn  8
#define GBOut  9

void setupPrinter(int in, int out, int clock)
{
  pinMode(out, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(in, INPUT);
}

void setup() {
  setupPrinter(GBIn, GBOut, GBClock);
  Serial.begin(57600);
}

uint8_t buffer[64];
uint8_t cmd;
uint16_t CRC;

void loop() {
  cmd = Serial.read();
  uint8_t repl;
 
  if (cmd == '?') {
    GBSerialOut(0x88);
    GBSerialOut(0x33);
    GBSerialOut(0x01);
    GBSerialOut(0x00);
    GBSerialOut(0x00);
    GBSerialOut(0x00);
    GBSerialOut(0x01);
    GBSerialOut(0x00);
    repl = GBSerialOut(0);
    GBSerialOut(0);
    if (repl == 129) {
      Serial.print('1');
    } else {
      Serial.print('0');
    }
  }

  if (cmd == '!') {
    GBSerialOut(0x88);
    GBSerialOut(0x33);
    GBSerialOut(0x01);
    GBSerialOut(0x00);
    GBSerialOut(0x00);
    GBSerialOut(0x00);
    GBSerialOut(0x01);
    GBSerialOut(0x00);
    repl = GBSerialOut(0);
    repl = GBSerialOut(0);
   
    GBSerialOut(0x88);
    GBSerialOut(0x33);
    GBSerialOut(0x04);
    GBSerialOut(0x00);
    GBSerialOut(0x80);
    GBSerialOut(0x02);
    CRC = 0x86;

    for (int b=0;b<10;b++) {
      Serial.print('a');

      for (int c=0;c<64;c++) {
        if (c%2==0) {
          buffer[c] = 0x00;
        } else {
          buffer[c] = 0xff;
        }
      }

      for (int c=0;c<64;c++) {
        cmd = buffer[c];
        GBSerialOut(cmd);
        CRC += cmd;
      }
    }

    GBSerialOut((uint8_t)CRC & 0xFF); // CRC Low
    GBSerialOut((CRC & 0xFF00)>>8); // CRC High
    repl = GBSerialOut(0);
    repl = GBSerialOut(0);

    delay(10); // 131: répond 4 au lieu de 8, 265 timeout (0)

    GBSerialOut(0x88); // Data vide
    GBSerialOut(0x33);
    GBSerialOut(0x04);
    GBSerialOut(0x00);
    GBSerialOut(0x00);
    GBSerialOut(0x00);
    GBSerialOut(0x04);
    GBSerialOut(0x00);
    repl = GBSerialOut(0);
    repl = GBSerialOut(0);


    GBSerialOut(0x88); // Print
    GBSerialOut(0x33);
    GBSerialOut(0x02);
    GBSerialOut(0x00);
    GBSerialOut(0x04);
    GBSerialOut(0x00);

    CRC = 6;
    //Serial.print('1');
     
    GBSerialOut(0x01);
    CRC += 0x01;
    GBSerialOut(0x01);
    CRC += 0x01;   
    GBSerialOut(0x01);
    CRC += 0x01;
    GBSerialOut(0x01);
    CRC += 0x01;
   

    GBSerialOut((uint8_t)CRC & 0xFF); // CRC Low
    GBSerialOut((CRC & 0xFF00)>>8); // CRC High

    repl = GBSerialOut(0);
    repl = GBSerialOut(0);

    while(1) {
      GBSerialOut(0x88); // Status
      GBSerialOut(0x33);
      GBSerialOut(0x0F);
      GBSerialOut(0x00);
      GBSerialOut(0x00);
      GBSerialOut(0x00);
      GBSerialOut(0x0F);
      GBSerialOut(0x00);
      GBSerialOut(0);
      if (GBSerialOut(0) & 2) break;
      delay(100);
    }
    Serial.print('P');

    while(1) {
      GBSerialOut(0x88); // Status
      GBSerialOut(0x33);
      GBSerialOut(0x0F);
      GBSerialOut(0x00);
      GBSerialOut(0x00);
      GBSerialOut(0x00);
      GBSerialOut(0x0F);
      GBSerialOut(0x00);
      GBSerialOut(0);
      if (!(GBSerialOut(0) & 2)) break;
      delay(100);
    }
    Serial.print('O');

  }
 
}

uint8_t GBSerialOut(uint8_t command) {
  uint8_t gbReplyy=0;
  for (uint8_t clock=0;  clock<8;  ++clock) {
    gbReplyy <<= 1; // Shift Printer gbReplyy
    digitalWrite(GBClock, 0);           // Send clock signal

    if((command << clock) & 0x80) {
      digitalWrite(GBOut, 1); // Write out to printer
    }
    else{
      digitalWrite(GBOut, 0);
    }

    delayMicroseconds(delayMs); // Wait 20-60us
    digitalWrite(GBClock, 1);            // End clock
    if(digitalRead(GBIn))
    {
      gbReplyy |= 1;    // Fetch printer gbReplyy
    }
    delayMicroseconds(delayMs); // Wait 20-60us
  }
  delayMicroseconds(delayMs); // Wait 20-60us
 
  return gbReplyy;
}

Selda81


Looking for anotherthing, I found this:

http://milesburton.com/Gameboy_Printer_with_Arduino

maybe this will helpfull for u

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy