gameboy printer

Dependencies:   PokittoLib

Fork of HelloWorld by Pokitto Community Team

Revision:
8:82f64c4b9904
Parent:
5:c4a0391b43ac
Child:
9:f5dbcf0d30ba
--- a/main.cpp	Sun Oct 22 19:13:22 2017 +0000
+++ b/main.cpp	Fri Dec 22 20:42:41 2017 +0000
@@ -1,13 +1,232 @@
+// this might be better...
+// https://github.com/davedarko/GBPrinter
+// https://github.com/davedarko/GBPrinter/blob/master/examples/GBPrinter/GBPrinter.ino
+
 #include "Pokitto.h"
+Pokitto::Core mygame;
+
+uint8_t hadl[640] = {
+0xEF, 0xFF, 0xF7, 0xF7, 0xE7, 0xB7, 0xC3, 0xC3, 0xF1, 0xE1, 0xFA, 0xFA, 0xFC, 0xFC, 0xFC, 0xFC, 0xFE, 0xFF, 0xFD, 0xFD, 0xFC, 0xFC, 0xF8, 0xF8, 0xF1, 0xB0, 0x0B, 0x0B, 0x07, 0x07, 0x07, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0xBF, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x7F, 0x7F, 0x4E, 0x16, 0x25, 0x65, 0x67, 0x66, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1C, 0x1E, 0xC9, 0xCC, 0x09, 0x89, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF3, 0xF3, 0xF3, 0xF3, 0x33, 0x73,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igitalInOut datIn(P0_17);
+DigitalInOut datOut(P0_13);
+
+unsigned short checksum = 0;
+int whichChar = 0;
+
+void sendByte(unsigned char byteToSend) {
 
-Pokitto::Core mygame;
+    checksum += byteToSend;              //Always build this, even if not used
+    for (int x = 0 ; x < 8 ; x++) {
+        if (byteToSend & B10000000) {       
+            datOut = 1; // data high
+        } else {
+            datOut = 0; // data low
+        }
+        datClk = 0; // clock low
+        wait_us(55);
+        datClk = 1; // clock high
+        byteToSend <<= 1;
+        wait_us(55);
+    }
+    datOut = 0; // test...
+    wait_us(200);
+}
+
+
+void getResponse() {
+    respond[0] = 0;
+    respond[1] = 0;
+    
+    for (int xx = 0 ; xx < 2 ; xx++) {
+        for (int x = 0 ; x < 8 ; x++) {
+            respond[xx] <<= 1;              //Shift to the left making room for next bit. DO this first so we don't lose the last one
+            datClk = 0;
+            wait_us(25);
+            if(datIn){
+                respond[xx] |= 1;           //Set it       
+            }
+            wait_us(25);
+            datClk = 1;
+            wait_us(55);
+        }
+        wait_us(200);
+    }
+}
+
+void sendChecksum() {
+    unsigned short lineChecksum = checksum; //Grab the state of checksum so we don't change it while sending
+    sendByte(lineChecksum & 0xFF);          //Send checksum low byte
+    sendByte(lineChecksum >> 8);            //Send checksum high byte
+    getResponse();
+}
+
 
 int main () {
     mygame.begin();
+
+    datClk.output(); // clock to output
+    datOut.output(); // data out to output
+    datIn.input(); // data In to input
+    datIn.mode(PullUp);
+    
+    datClk = 1; // start clock line high
+    datOut = 0; // start data low
+
+
+    //wait(5); // hang on a little
+
+    //printShape(1); // test!
+
+    mygame.display.persistence = 1;
+    mygame.display.clear();
+    mygame.display.print("Printer Test\n");
+
     while (mygame.isRunning()) {
         if (mygame.update()) {            
-            mygame.display.print("Hello World!");
-            } 
-        }    
+            if (mygame.buttons.aBtn()){
+                mygame.display.print("Sending init code\n");
+                mygame.display.update();
+
+                sendByte(0x88);    // magic
+                sendByte(0x33);    // magic
+                checksum = 0;
+                sendByte(0x01);    // 1 = init printer
+                sendByte(0x00);    // compression, 0 = none, 1 = RLE
+                sendByte(0x00);    // data length
+                sendByte(0x00);    // data length 2nd byte
+                int temp = checksum;
+                sendByte(temp & 0xFF);    // checksum 2nd byte
+                sendByte(temp >> 8);    // checksum
+                getResponse();
+
+                mygame.display.print("Done\n");
+                
+                char tempText[20];
+                sprintf(tempText,"Response = %#04x, %#04x\n", respond[0], respond[1]);
+                mygame.display.print(tempText);
+                
+                if(respond[0]==0x81 && respond[1]==0x00){
+                    mygame.display.print("OK, Proceed\n");
+                    mygame.display.update();
+
+                    // send test data
+                    sendByte(0x88);    // magic
+                    sendByte(0x33);    // magic
+                    checksum = 0;
+                    sendByte(0x04);    // 4 = Send some print data
+                    sendByte(0x00);    // compression, 0 = none, 1 = RLE
+                    // send 0x280 which is 640, the maximum data allowed at a time
+                    sendByte(0x80);    // data length
+                    sendByte(0x02);    // data length 2nd byte
+                    for(int t=0; t<640; t++){
+                        sendByte(hadl[t]);    // test data
+                    }
+                    temp = checksum;
+                    sendByte(temp & 0xFF);    // checksum 2nd byte
+                    sendByte(temp >> 8);    // checksum
+                    getResponse();
+
+                    sprintf(tempText,"Response = %#04x, %#04x\n", respond[0], respond[1]);
+                    mygame.display.print(tempText);
+                    mygame.display.update();
+
+                    // end data statement
+                    sendByte(0x88);    // magic
+                    sendByte(0x33);    // magic
+                    checksum = 0;
+                    sendByte(0x04);    // 
+                    sendByte(0x00);    // 
+                    sendByte(0x00);    // 
+                    sendByte(0x00);    // 
+                    temp = checksum;
+                    sendByte(temp & 0xFF);    // checksum 2nd byte
+                    sendByte(temp >> 8);    // checksum
+                    getResponse();
+                    
+                    sprintf(tempText,"Response = %#04x, %#04x\n", respond[0], respond[1]);
+                    mygame.display.print(tempText);
+                    mygame.display.update();
+
+                    sendByte(0x88); // Magic bytes
+                    sendByte(0x33);
+                    checksum = 0;
+                    sendByte(0x02); // Commande 2 (Print)
+                    sendByte(0x00);
+                    sendByte(0x04); // 4 bytes to send
+                    sendByte(0x00);
+                    sendByte(0x01); // 1 = print, 0 = line feed
+                    sendByte(0x13); // Marges (nibbles avant 1, après 3)
+                    sendByte(0xE4); // Palette (11100100: Noir, Foncé, Clair, Blanc)
+                    sendByte(0x40); // Expo (7 bits)
+
+                    temp = checksum;
+                    sendByte(temp & 0xFF);    // checksum 2nd byte
+                    sendByte(temp >> 8);    // checksum
+                    getResponse();
+                    
+                    sprintf(tempText,"Response = %#04x, %#04x\n", respond[0], respond[1]);
+                    mygame.display.print(tempText);
+                    mygame.display.update();
+
+                bool waiting=1;
+                while(waiting){
+                    sendByte(0x88);    // magic
+                    sendByte(0x33);    // magic
+                    checksum = 0;
+                    sendByte(0x0F);    // 
+                    sendByte(0x00);    // 
+                    sendByte(0x00);    // 
+                    sendByte(0x00);    // 
+                    temp = checksum;
+                    sendByte(temp & 0xFF);    // checksum 2nd byte
+                    sendByte(temp >> 8);    // checksum
+                    getResponse();
+                    
+                    sprintf(tempText,"Response = %#04x, %#04x\n", respond[0], respond[1]);
+                    mygame.display.print(tempText);
+                    mygame.display.update();
+                    if(respond[1] & 2)waiting=0;
+                }
+
+                waiting=1;
+                while(waiting){
+                    sendByte(0x88);    // magic
+                    sendByte(0x33);    // magic
+                    checksum = 0;
+                    sendByte(0x0F);    // 
+                    sendByte(0x00);    // 
+                    sendByte(0x00);    // 
+                    sendByte(0x00);    // 
+                    temp = checksum;
+                    sendByte(temp & 0xFF);    // checksum 2nd byte
+                    sendByte(temp >> 8);    // checksum
+                    getResponse();
+                    
+                    sprintf(tempText,"Response = %#04x, %#04x\n", respond[0], respond[1]);
+                    mygame.display.print(tempText);
+                    mygame.display.update();
+                    if(!(respond[1] & 2))waiting=0;
+                }
+
+
+                }else{
+                    // show error log here
+                    if(respond[0] != 0x81){
+                        mygame.display.print("Not Printer\n");
+                    }else{
+                        mygame.display.print("Printer Error\n");
+                    }
+                }
+                mygame.display.update();
+                
+                wait(1);
+            }
+        } 
+    }    
     
 }
\ No newline at end of file