Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
Added printing functionality
Browse files Browse the repository at this point in the history
This commit adds the printlib2 from https://github.com/T0biasCZe/printlib2 and a function which allows to print
the currently displayed image with a white border. This has been tested using the mGBA emulator.
  • Loading branch information
Christian Reinbacher authored and HerrZatacke committed May 14, 2022
1 parent 7d7ca68 commit 5976b47
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ obj/bank_16.o: src/banks/bank_16.c
obj/main.o: src/main.c
$(CC) -c -o $@ $<

obj/printCmd.o: src/printCmd.c
$(CC) -c -o $@ $<

clean:
rm -rf obj && rm -f version.h && rm -f branch.h

Expand All @@ -73,5 +76,5 @@ version.h: version
branch.h:
git rev-parse --abbrev-ref HEAD | tr -d '\r\n' | sed 's/master/ /g' | xxd -u -p -c 1 | sed 's/\s+/_/g' | sed 's/^/ 0x/g; s/$$/,/g' | sed -z 's/^/unsigned char branch[] = {\n/g; s/$$/};\n/g' > $@

$(BIN): obj branch.h version.h obj/main.o obj/bank_00.o obj/bank_01.o obj/bank_02.o obj/bank_03.o obj/bank_04.o obj/bank_05.o obj/bank_06.o obj/bank_07.o obj/bank_08.o obj/bank_09.o obj/bank_10.o obj/bank_11.o obj/bank_12.o obj/bank_13.o obj/bank_14.o obj/bank_15.o obj/bank_16.o obj/pxlr.sav
$(CC) -Wl-yt0xFC -Wl-yo4 -Wl-ya16 -Wm-yn"PXLR CAMERA" -o obj/$@ obj/main.o obj/bank_00.o obj/bank_01.o obj/bank_02.o obj/bank_03.o obj/bank_04.o obj/bank_05.o obj/bank_06.o obj/bank_07.o obj/bank_08.o obj/bank_09.o obj/bank_10.o obj/bank_11.o obj/bank_12.o obj/bank_13.o obj/bank_14.o obj/bank_15.o obj/bank_16.o
$(BIN): obj branch.h version.h obj/main.o obj/bank_00.o obj/bank_01.o obj/bank_02.o obj/bank_03.o obj/bank_04.o obj/bank_05.o obj/bank_06.o obj/bank_07.o obj/bank_08.o obj/bank_09.o obj/bank_10.o obj/bank_11.o obj/bank_12.o obj/bank_13.o obj/bank_14.o obj/bank_15.o obj/bank_16.o obj/pxlr.sav obj/printCmd.o
$(CC) -Wl-yt0xFC -Wl-yo4 -Wl-ya16 -Wm-yn"PXLR CAMERA" -o obj/$@ obj/main.o obj/bank_00.o obj/bank_01.o obj/bank_02.o obj/bank_03.o obj/bank_04.o obj/bank_05.o obj/bank_06.o obj/bank_07.o obj/bank_08.o obj/bank_09.o obj/bank_10.o obj/bank_11.o obj/bank_12.o obj/bank_13.o obj/bank_14.o obj/bank_15.o obj/bank_16.o obj/printCmd.o
12 changes: 11 additions & 1 deletion src/gallery.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ void loadAndShowGalleryImage();
#include "menus/imageMenuItems.h"
#include "./bleep.h"
#include "./expose.h"
#include "./printCmd.h"

inline void appearImageMenu() {
HIDE_SPRITES;
Expand Down Expand Up @@ -120,7 +121,16 @@ inline void imageMenuAction(unsigned char value) {
loadAndShowGalleryImage();
clonk();
} else if (value == IMAGE_MENU_PRINT) {
boop();
PrinterInit();
if (GetPrinterStatus()) {
clonk();
} else {
disappearImageMenu();
unsigned char imageSlot = getImageSlot(imageIndex);
PrintImage(images[imageSlot]->tilesLower, images[imageSlot]->tilesUpper);
boop();
appearImageMenu();
}
} else if (value == IMAGE_MENU_BLEEP) {
disappearImageMenu();
bleep();
Expand Down
180 changes: 180 additions & 0 deletions src/printCmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Original library by DragonEagles from http://shen.mansell.tripod.com/games/gameboy/gameboy.html
// Ported to GBDK 2020 by T0biasCZe
// Print Screen function added by Christianr
// Print Screen (360 tile mode) added by T0biasCZe

#include <gb/gb.h>
uint8_t PrinterStatus[3];

const uint8_t PRINTER_INIT[]={
10,0x88,0x33,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00
};
const uint8_t PRINTER_STATUS[]={
10,0x88,0x33,0x0F,0x00,0x00,0x00,0x0F,0x00,0x00,0x00
};
const uint8_t PRINTER_EOF[]={
10,0x88,0x33,0x04,0x00,0x00,0x00,0x04,0x00,0x00,0x00
};
const uint8_t PRINTER_START[]={
14,0x88,0x33,0x02,0x00,0x04,0x00,0x01,0x03,0xE4,0x7F,0x6D,0x01,0x00,0x00
};
const uint8_t PRINT_TILE[]={
6,0x88,0x33,0x04,0x00,0x80,0x02
};
const uint8_t PRINTER_LINE[]={
14,0x88,0x33,0x02,0x00,0x04,0x00,0x01,0x00,0xE4,0x7F,0x6A,0x01,0x00,0x00
};

uint8_t tile_num, packet_num;

uint16_t CRC;

uint8_t SendPrinterByte(uint8_t byte){
uint8_t result;
disable_interrupts();
SB_REG = byte; //data to send
SC_REG = 0x81; //1000 0001 - start, internal clock
while (SC_REG & 0x80){} //wait until b1 reset
result = SB_REG; //return response stored in SB_REG
enable_interrupts();
return result;
}

void SendByte(uint8_t byte){
uint8_t result;
result = SendPrinterByte(byte);
PrinterStatus[0]=PrinterStatus[1];
PrinterStatus[1]=PrinterStatus[2];
PrinterStatus[2]=result;
}

void SendPrinterCommand(uint8_t *Command){
uint8_t length,index;
index=0;
length=*Command;
while(index < length){
index++;
SendByte(*(Command+index));
}
}

void PrinterInit (void)
{
tile_num = 0;
CRC = 0;
packet_num = 0;

SendPrinterCommand(PRINTER_INIT);
}

int CheckLinkCable(){
if(PrinterStatus[0] != 0){
return 2;
}
if((PrinterStatus[1] & 0xF0) != 0x80){
return 2;
}
return 0;
}

int GetPrinterStatus(){
SendPrinterCommand(PRINTER_STATUS);
return CheckLinkCable();
}

int CheckForErrors(){
if(PrinterStatus[2] & 128){
return 1;
}
if(PrinterStatus[2] & 64){
return 4;
}
if(PrinterStatus[2] & 32){
return 3;
}
return 0;
}

uint8_t CheckBusy() {
SendPrinterCommand(PRINTER_STATUS);
return (PrinterStatus[2] & 0x2);
}

uint8_t GetHigh(uint16_t w) {
return (w & 0xFF00u) >> 8;
}

uint8_t GetLow(uint16_t w) {
return (w & 0xFFu);
}

void PrintTileData(uint8_t *TileData, uint8_t lf, uint8_t num_packets){
uint8_t TileIndex;

if (tile_num == 0)
{
SendPrinterCommand(PRINT_TILE);
CRC = 0x04 + 0x80 + 0x02;
}

tile_num ++;

for(TileIndex = 0; TileIndex < 16; TileIndex++)
{
CRC += TileData[TileIndex];
SendByte(TileData[TileIndex]);
}

if (tile_num == 40)
{
SendByte(GetLow(CRC));
SendByte(GetHigh(CRC));
SendByte(0x00);
SendByte(0x00);
tile_num = 0;
CRC = 0;
packet_num ++;

if (packet_num == num_packets) // all done the page
{
SendPrinterCommand(PRINTER_EOF); // data end packet
if (lf)
SendPrinterCommand(PRINTER_START);
else
SendPrinterCommand(PRINTER_LINE);
packet_num = 0;
SendPrinterCommand(PRINTER_STATUS);
}
}
}

void PrintScreen(uint8_t linefeed) {
uint8_t x, y;
uint8_t p_data[16];
for (y=0; y<18; y++) {
for (x=0; x<20; x++) {
get_bkg_data(get_vram_byte(get_bkg_xy_addr(x, y)), 1, p_data);
PrintTileData(p_data, linefeed, 9);
}
}
}

void PrintImage(uint8_t* lower, uint8_t* upper) {
// We need to print a border of 16x16 pixels (2x2 tiles)
uint8_t x, y;
const uint8_t border[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
uint8_t* image = upper;
for (y=0; y<18; y++) {
for (x=0; x<20; x++) {
if (x==0 && y==9) {
image = lower;
}
if (x<2 || y<2 || x>=18 || y>=16) {
PrintTileData(border, TRUE, 9);
} else {
PrintTileData(image, TRUE, 9);
image += 16;
}
}
}
}
19 changes: 19 additions & 0 deletions src/printCmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Original library by DragonEagles from http://shen.mansell.tripod.com/games/gameboy/gameboy.html
// Ported to GBDK 2020 by T0biasCZe
// Print Screen function added by Christianr
// Print Screen (360 tile mode) added by T0biasCZe

#include <gb/gb.h>

extern unsigned char PrinterStatus[3];

extern void PrinterInit (void);
extern void SendByte(unsigned char byte);
extern void SendPrinterCommand(unsigned char *Command);
extern int CheckLinkCable();
extern int GetPrinterStatus();
extern int CheckForErrors();
extern void PrintTileData(unsigned char *TileData, uint8_t lf, uint8_t num_packets);
extern uint8_t CheckBusy();
extern void PrintScreen(uint8_t linefeed);
extern void PrintImage(uint8_t* lower, uint8_t* upper);

0 comments on commit 5976b47

Please sign in to comment.