前回投稿したプログラムはコピーモードで描画されるので間延びしてpdfファイ
ルなどを印刷すると一枚に入り切れなかったのですが今度の版はネイティブモー
ドで描画するので少し小さいですが一枚の紙に入ります
データの送信も8ドット列印字から24ドット列印字にしました
これでしばらく使ってみようと思います
大城貴紀
---
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <dev/ppbus/ppi.h>
#include <dev/ppbus/ppbconf.h>
#include <unistd.h>

#define WAIT 1000000 /* adjust this with your cpu speed */

void ppout(int,unsigned char);

int main(int argc, char *argv[]){
        FILE *fp;
        char tmp1[256],tmp2[256],c;
        int x,y,i,n,m,j,k,l;
        int *data;
        int raster[3000][3000];
        int fd;
        unsigned char b;
        char *p;

        if((fp=fopen(*++argv,"r"))==NULL){
                printf("can't open %s\n",*argv);
                return 1;
        }
        fscanf(fp,"%s %s %d",tmp1,tmp2,&x);
        fscanf(fp,"%s %s %d",tmp1,tmp2,&y);
        while((c=getc(fp))!='\n')
                ;
        if(x%8){
                n=(x/8+1)*y;
        }else{
                n=(x/8)*y;
        }

        data = (int *)malloc(n * sizeof(int));

        for(i=0;i<n-1;i++){
                j=0;
                while((c=getc(fp)) !=',' ){
                        tmp1[j++]=c;
                }
                j++;
                tmp1[j]=c;
                j=0;
                while(tmp1[j++]!='x')
                        ;
                tmp2[0]='0';
                tmp2[1]='x';
                tmp2[2]=tmp1[j];
                tmp2[3]=tmp1[j+1];
                tmp2[4]='\0';
                sscanf(tmp2,"%x",&m);
                *data = m;
                data++;
        }
        j=0;
        while((c=getc(fp))!='}'){
                tmp1[j++]=c;
        }
        j++;
        tmp1[j]=c;
        j=0;
        while(tmp1[j++] != 'x')
                ;
        tmp2[0]='0';
        tmp2[1]='x';
        tmp2[2]=tmp1[j];
        tmp2[3]=tmp1[j+1];
        tmp2[4]='\0';
        sscanf(tmp2,"%x",&m);
        *data = m;

        data -= n-1;
        if(x%8){
                k=x/8+1;
        }else{
                k=x/8;
        }
        for(i=0;i<y;i++){
                for(j=0;j<k;j++){
                        n=*data & 001;
                        raster[j*8][i]=n;
                        n=*data & 002;
                        n = n>>1;
                        raster[j*8+1][i]=n;
                        n=*data & 004;
                        n = n>>2;
                        raster[j*8+2][i]=n;
                        n=*data & 010;
                        n= n>>3;
                        raster[j*8+3][i]=n;
                        n=*data & 020;
                        n= n>>4;
                        raster[j*8+4][i]=n;
                        n=*data & 040;
                        n=n>>5;
                        raster[j*8+5][i]=n;
                        n=*data & 0100;
                        n=n>>6;
                        raster[j*8+6][i]=n;
                        n=*data & 0200;
                        n=n>>7;
                        raster[j*8+7][i]=n;
                        data++;
                }
        }

        fd = open("/dev/ppi0",O_RDWR,0600);

        if(x<10){
                sprintf(tmp1,"000%d",x);
        }else if(x<100){
          sprintf(tmp1,"00%d",x);
        }else if(x<1000){
          sprintf(tmp1,"0%d",x);
        }else{
          sprintf(tmp1,"%d",x);
        }

        p = tmp1;

        b = 0x1b;
        ioctl(fd,PPISDATA,&b);
        ioctl(fd,PPIGCTRL,&b);
        b |= STROBE;
        ioctl(fd,PPISCTRL,&b);
        for(i=0;i<WAIT;i++)
                ;
        b &= ~STROBE;
        ioctl(fd,PPISCTRL,&b);
        for(i=0;i<WAIT;i++)
                ;
        b = 'T';
        ioctl(fd,PPISDATA,&b);
        ioctl(fd,PPIGCTRL,&b);
        b |= STROBE;
        ioctl(fd,PPISCTRL,&b);
        for(i=0;i<WAIT;i++)
                ;
        b &= ~STROBE;
        ioctl(fd,PPISCTRL,&b);
        for(i=0;i<WAIT;i++)
                ;
        b = '1';
        ioctl(fd,PPISDATA,&b);
        ioctl(fd,PPIGCTRL,&b);
        b |= STROBE;
        ioctl(fd,PPISCTRL,&b);
        for(i=0;i<WAIT;i++)
                ;
        b &= ~STROBE;
        ioctl(fd,PPISCTRL,&b);
        for(i=0;i<WAIT;i++)
                ;
        b = '8';
        ioctl(fd,PPISDATA,&b);
        ioctl(fd,PPIGCTRL,&b);
        b |= STROBE;
        ioctl(fd,PPISCTRL,&b);
        for(i=0;i<WAIT;i++)
                ;
        b &= ~STROBE;
        ioctl(fd,PPISCTRL,&b);
        for(i=0;i<WAIT;i++)
                ;

        if(y%24){
                k=y/24+1;
        }else{
                k=y/24;
        }

        for(i=0;i<k;i++){
          b = 0x1b;
          ioctl(fd,PPISDATA,&b);
          ioctl(fd,PPIGCTRL,&b);
          b |= STROBE;
          ioctl(fd,PPISCTRL,&b);
          for(j=0;j<WAIT;j++)
            ;
          b &= ~STROBE;
          ioctl(fd,PPISCTRL,&b);
          for(j=0;j<WAIT;j++)
            ;
          b = 'J';
          ioctl(fd,PPISDATA,&b);
          ioctl(fd,PPIGCTRL,&b);
          b |= STROBE;
          ioctl(fd,PPISCTRL,&b);
          for(j=0;j<WAIT;j++)
            ;
          b &= ~STROBE;
          ioctl(fd,PPISCTRL,&b);
          for(j=0;j<WAIT;j++)
            ;
          for(l=0;l<4;l++){
            b=*p++;
            ioctl(fd,PPISDATA,&b);
            ioctl(fd,PPIGCTRL,&b);
            b |= STROBE;
            ioctl(fd,PPISCTRL,&b);
            for(j=0;j<WAIT;j++)
              ;
            b &= ~STROBE;
            ioctl(fd,PPISCTRL,&b);
            for(j=0;j<WAIT;j++)
              ;
          }
          p-=4;
                for(j=0;j<x;j++){
                        n=0;
                        n += raster[j][i*24];
                        n += raster[j][i*24+1] * 2;
                        n += raster[j][i*24+2] *4;
                        n += raster[j][i*24+3] *8;
                        n += raster[j][i*24+4] *16;
                        n+=  raster[j][i*24+5] *32;
                        n += raster[j][i*24+6] *64;
                        n += raster[j][i*24+7] *128;
                        b = (unsigned char)n;
                        ppout(fd,b);
                        n=0;
                        n += raster[j][i*24+8];
                        n += raster[j][i*24+9] *2;
                        n += raster[j][i*24 +10]*4;
                        n += raster[j][i*24 +11]*8;
                        n += raster[j][i*24 +12]*16;
                        n += raster[j][i*24+13]*31;
                        n += raster[j][i*24 +14]*64;
                        n += raster[j][i*24 +15]*128;
                        b = (unsigned char)n;
                        ppout(fd,b);
                        n=0;
                        n += raster[j][i*24+16];
                        n += raster[j][i*24+17]*2;
                        n += raster[j][i*24+18]*4;
                        n += raster[j][i*24+19]*8;
                        n += raster[j][i*24+20]*16;
                        n += raster[j][i*24+21]*32;
                        n += raster[j][i*24+22]*64;
                        n += raster[j][i*24+23]*128;
                        b = (unsigned char)n;
                        ppout(fd,b);
                }
                b = 0x0d;
                ioctl(fd,PPISDATA,&b);
                ioctl(fd,PPIGCTRL,&b);
                b |= STROBE;
                ioctl(fd,PPISCTRL,&b);
                for(l=0;l<WAIT;l++)
                  ;
                b &= ~STROBE;
                ioctl(fd,PPISCTRL,&b);
                for(l=0;l<WAIT;l++)
                  ;
                b = 0x0a;
                ioctl(fd,PPISDATA,&b);
                ioctl(fd,PPIGCTRL,&b);
                b |= STROBE;
                ioctl(fd,PPISCTRL,&b);
                for(l=0;l<WAIT;l++)
                  ;
                b &= ~STROBE;
                ioctl(fd,PPISCTRL,&b);
                for(l=0;l<WAIT;l++)
                  ;
                for(l=0;l<100000000;l++)
                        ;
        }

        close(fd);
        free(data);
        fclose(fp);
        return 0;
}

void ppout(int fd, unsigned char b){
  unsigned char val;
  int i;

  do{
    ioctl(fd,PPIGSTATUS,&val);
    val &=nACK;
  }while(val != 0x40);
  
  val = b;
  ioctl(fd,PPISDATA,&val);
  ioctl(fd,PPIGCTRL,&val);
  val |= STROBE;
  ioctl(fd,PPISCTRL,&val);
  for(i=0;i<WAIT;i++)
    ;
  val &= ~STROBE;
  ioctl(fd,PPISCTRL,&val);
  for(i=0;i<WAIT;i++)
    ;
}