git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/examples/cross-platform/gbprinter/src/gbprinter.h

      1 #ifndef __GBPRINTER_H_INCLUDE__
      2 #define __GBPRINTER_H_INCLUDE__
      3 
      4 #include <gbdk/platform.h>
      5 #include <stdint.h>
      6 
      7 /** Width of the printed image in tiles
      8 */
      9 #define PRN_TILE_WIDTH          20
     10 
     11 #define PRN_LOW(A) ((A) & 0xFF)
     12 #define PRN_HIGH(A) ((A) >> 8)
     13 
     14 /** 0x88,0x33 are mandatory first bytes to initialise a communication with printer
     15     Any command sequence begins by these
     16 */
     17 #define PRN_MAGIC               0x3388
     18 #define PRN_LE(A)               PRN_LOW(A),PRN_HIGH(A)
     19 
     20 /** magic number that is sent in the reply packet by the printer before the status byte
     21  */
     22 #define PRN_MAGIC_DETECT        0x81
     23 
     24 /** INIT command is mandatory to initialize communication protocol with the printer
     25     Two consecutive linked commands must never be more than 150 ms apart except the INIT command which is valid at least 10 seconds
     26 */
     27 #define PRN_CMD_INIT            0x01
     28 
     29 /** PRINT command
     30     Contains the palette, margins, number of prints and printing intensity
     31 */
     32 #define PRN_CMD_PRINT           0x02
     33 
     34 /** DATA command
     35     Can be any length between 0 and 640 bytes.
     36     DATA command with lenght 0 triggers PRN_STATUS_FULL and is mandatory before print command
     37 */
     38 #define PRN_CMD_DATA            0x04
     39 
     40 /** BREAK command
     41     Not very usefull but exists (see Game Boy Programming Manual)
     42 */
     43 #define PRN_CMD_BREAK           0x08
     44 
     45 /** STATUS command
     46     Used to check status bits
     47     Maybe be used alone before an INIT command to check physical connection with printer
     48     Resets PRN_STATUS_UNTRAN
     49 */
     50 #define PRN_CMD_STATUS          0x0F
     51 
     52 /** Palette format: the bits, grouped two by two, give the printing color of the encoded pixel value
     53     for the default palette 0xE4 = 0b11100100 = [3 2 1 0]
     54     Any value is valid, which means that 1 to 4 color images are possible
     55     0x00 acts the same as 0xE4 for the printer
     56 */
     57 #define PRN_PALETTE_NORMAL      0b11100100u
     58 #define PRN_PALETTE_INV         0b00011011u
     59 
     60 /** Don't use margins
     61 */
     62 #define PRN_NO_MARGINS          0x00
     63 
     64 /** Exposure: 0x40 is default value, values from 0x80 to 0xFF act as 0x40
     65     Determines the time used by the printer head to heat the thermal paper
     66 */
     67 #define PRN_EXPOSURE_LIGHT      0x00
     68 #define PRN_EXPOSURE_DEFAULT    0x40
     69 #define PRN_EXPOSURE_DARK       0x7F
     70 
     71 /** Battery too low
     72 */
     73 #define PRN_STATUS_LOWBAT       0x80
     74 
     75 /** Error not specified according to the Game Boy Programming manual
     76 */
     77 #define PRN_STATUS_ER2          0x40
     78 
     79 /** Paper jam  (abnormal motor operation)
     80 */
     81 #define PRN_STATUS_ER1          0x20
     82 
     83 /** Packet error (but not checksum error)
     84 */
     85 #define PRN_STATUS_ER0          0x10
     86 
     87 /** Unprocessed data present in printer memory
     88     Allows to verify that printer got some data in memory with correct checksum
     89     is resetted by STATUS command
     90 */
     91 #define PRN_STATUS_UNTRAN       0x08
     92 
     93 /** status data ready, mandatory to allow printing
     94     is triggered by DATA command with lenght 0
     95 */
     96 #define PRN_STATUS_FULL         0x04
     97 
     98 /** Message sent by the printer while physically printing
     99 */
    100 #define PRN_STATUS_BUSY         0x02
    101 
    102 /** The received packet has a ckecksum error
    103 */
    104 #define PRN_STATUS_SUM          0x01
    105 
    106 /** Everything is fine, printer ready for further transmission
    107 */
    108 #define PRN_STATUS_OK           0x00
    109 
    110 #define PRN_STATUS_MASK_ERRORS  0xF0
    111 #define PRN_STATUS_MASK_ANY     0xFF
    112 
    113 #define PRN_SECONDS(A)          ((A)*60)
    114 
    115 #define PRN_MAX_PROGRESS        8
    116 
    117 #define PRN_STATUS_CANCELLED    PRN_STATUS_ER2
    118 
    119 #define PRINTER_DETECT_TIMEOUT  10
    120 
    121 typedef struct start_print_pkt_t {
    122     uint16_t magic;
    123     uint16_t command;
    124     uint16_t length;
    125     uint8_t print;
    126     uint8_t margins;
    127     uint8_t palette;
    128     uint8_t exposure;
    129     uint16_t crc;
    130     uint16_t trail;
    131 } start_print_pkt_t;
    132 
    133 extern uint8_t printer_completion;
    134 
    135 extern start_print_pkt_t PRN_PKT_START;
    136 
    137 inline void gbprinter_set_print_params(uint8_t margins, uint8_t palette, uint8_t exposure) {
    138     PRN_PKT_START.crc = ((PRN_CMD_PRINT + 0x04u + 0x01u) + (PRN_PKT_START.margins = margins) + (PRN_PKT_START.palette = palette) + (PRN_PKT_START.exposure = exposure));
    139 }
    140 
    141 uint8_t gbprinter_detect(uint8_t delay);
    142 uint8_t gbprinter_print_image(const uint8_t * image_map, const uint8_t * image, int8_t pos_x, uint8_t width, uint8_t height);
    143 
    144 #endif

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.