#author("2021-12-05T16:37:12+09:00","default:Real2Virtual202111","Real2Virtual202111")
#author("2024-07-01T11:44:08+09:00","default:TESLA202407","TESLA202407")
[[Real2Virtual202111]]

#code(c){{
#include "OutBuffer.h"
#include <string.h>
//#include "rtos.h"

void OutBuffer::setPortConf(unsigned char ledn, void (*portf)(unsigned char x), unsigned char (*gportf)(void), void (*modef)(unsigned char x)){
    setPort[ledn]=portf;
    getPort[ledn]=gportf;
    setPortMode[ledn]=modef;
   (setPortMode[ledn])( (unsigned char)1); /* output */
   (setPort[ledn])( (unsigned char)0);
}
void OutBuffer::setPortXConf(void portf(unsigned char x), void modef(unsigned char y)){
    setPortX=portf;
    setPortXMode=modef;
    setPortXMode((unsigned char)1); /* output */
    (*setPortX)( (unsigned char)1);
    setAllOrOne((unsigned char)0);
    setLedNumber((unsigned char)0);
    setPortXOn();
}
void OutBuffer::clear(void){
    setPortXOn();
    pb=0;
    output=0;
    sampleIntervalCounter=0;
    status=4;
    strp=0;
    strl=0;
}

void OutBuffer::setLedNumber( unsigned char n){
    ledNo=n;
}

//unsigned char OutBuffer::getLedNumber(void){
//   return this->ledNo;
//}

void OutBuffer::setLedOn(unsigned char n){
   (setPort[n])((unsigned char)1);
//   printf("port-%d-on ",n);
}

void OutBuffer::setLedOff( unsigned char n){
   (setPort[n])((unsigned char)0);
//   printf("port-%d-of ",n);
}

//unsigned char OutBuffer::getLedStatus( unsigned char n){
//   return (*getPort[n])();
//}

void OutBuffer::setPortXOn(void){
   setPortX((unsigned char)1);
//   printf("f%d-portx-on ",(*this).channelID);
}

void OutBuffer::setPortXOff(void){
    setPortX( (unsigned char)0);
//   printf("f%d-portx-off ",(*this).channelID);
}

void OutBuffer::setAllOrOne(unsigned char b){
     allOrOne=b;
//  printf("setAllOrOne ...%d\n",(int)b);
}

//void intrProcessForOutBuffer(struct outBuffer *this){
void OutBuffer::intrProcess(void){
   volatile unsigned char c;
   volatile int i;
//   printf("%d ",(*this).status);
   if(status>=3) return;
//   printf("%d ",(*this).status);
   if(status==0){ /* no data */
//       printf(" dirMax=%d\n",dirMax);
       if(output!=0){
          status=1;
          pb=0;
          sampleIntervalCounter=0;
          if((allOrOne)==0){
              for(i=0;i<DirMax;i++)
                  setLedOff(i);
//               ((*this).setLedOff)(this,(*this).ledNo);
               setPortXOff();
          }
          else{
              setPortXOn();
              for(i=0;i<DirMax;i++)
                  setLedOn(i);
          }
       }
       else{
          status=2;
       }
       return;
   }
   else
   if(status==1){ /* out putting */
       if(sampleIntervalCounter==0){
              if(allOrOne==0){
                if(bits[pb]==1){
                    setLedOn(ledNo);
                }
                else{
                    setLedOff(ledNo);
                }
              }
              else{
                if(bits[pb]==1){
                    setPortXOff();
                }
                else{
                    setPortXOn();
                }
              }
          pb++;
          if(pb>=BitsParChar){
             status=2;
             pb=0;
             output=0;
          }
       }
       sampleIntervalCounter++;
//       if(sampleIntervalCounter>=sampleInterval){
       if(sampleIntervalCounter>=SampleInterval){
                  sampleIntervalCounter=0;
       }
       return;
   }
   else
   if(status<=2){ /* output is done */
       if(strl==0){
          output=0;
          status=4;
          setPortXOn();
          return;
       }
       if(strp<strl){
//         l=(*this).strp;
//         m=(*this).strl;
           c=str[strp];
           putChar( c);
           strp++;
       }
       else{
          strl=0;
          strp=0;
          output=0;
                 status=4;
       }
       return;
   }
}

void OutBuffer::waitUntilOutputDone(void){
    int i;
//    printf("w start.\n");
//    xputs("w start....");
    while(status<4){
        for(i=0;i<1000;i++){}
    }
//    printf("w end.\n");
//    xputs("w end.\n\r");
}

unsigned char OutBuffer::putChar(unsigned char c){
   unsigned char cmask, cm;
   unsigned char i;
//   if((*this).status==0) return 0x00;
//   if((*this).status==1) return 0x00;
//   MK0H.6=1;
//   printf(" %c ",c);
//   xputc(c);
   output=c;
   bits[0]=1;
   cmask=0x80;
   for(i=1;i<9;i++){
     cm=cmask & c;
     if(cm!=0){
        bits[i]=1;
     }
     else{
        bits[i]=0;
     }
     cmask=cmask>>1;
   }
   for(i=9;i<BitsParChar;i++){
      bits[i]=0;
   }
   pb=0;
   status=0;

//   MK0H.6=0;
//   for(i=0;i<127;i++){}
   return 0x01;
}

unsigned char OutBuffer::isOutPutting(void){
   if(status==0) return 0x01;
   if(status==1) return 0x01;
     if(status==2) return 0x01;
   return 0x00;
}

void OutBuffer::putChar1(unsigned char c){
   char a[3];
   a[0]=c;
   a[1]=0;
//   ((*this).putString)(this,a);
   putStringX(a);
}
/*
void strcatx(char *x, char *y, char *a,unsigned len){
   char p,p1;
   p=0;p1=0;
   while(p<len){
     if(x[p1]==0) break;
     a[p]=x[p1];
     p++;
     p1++;
   }
   p1=0;
   while(p<len){
     if(y[p1]==0) break;
     a[p]=y[p1];
     p++;
     p1++;
   }
   a[p]=0;
}

void i2sx(char i, unsigned char *s){
   char tmp[4];
   char cx;
   char p;
   char j;
   s[0]='0';
   s[1]=0;
   p=0;
   if(i<0) i=-i;
   while(i!=0){
      cx=i%10;
      i=i/10;
      tmp[p]='0'+cx;
      p++;
   }
   for(j=0;j<p;j++){
      s[j]=tmp[p-j-1];
      s[j+1]=0;
   }
}
*/
void hex2str(unsigned char i, unsigned char *s){
  unsigned char c;
  c=i >>(0x04);
  c=c & 0x0f;
  if(c>0x09){
     s[0]='a'+(c-0x0a);
  }
  else{
     s[0]='0'+c;
  }
  c=i & 0x0f;
  if(c>0x09){
    s[1]='a'+(c-0x0a);
  }
  else{
    s[1]='0'+c;
  }
  s[2]=0;
}
/*
void trg_putHexArray(unsigned char *x, unsigned char n){
   unsigned char s[3];
   unsigned char i;
   for(i=0;i<n;i++){
      hex2str(x[i],s);
//      trg_puts(s);
//      printf("%s",s);
      xputs(s);
//      sputs((char *)s);
   }
}
*/

void OutBuffer::putStringStart( char *s){
//    unsigned short i;
//  unsigned int l;
//  sputs(s);
//  printf("\n putString %s ",s);
//  printf(" status=%d",(*this).status);
    strl=0;
    sendingLength=strlen(s);
    sentLength=0;
    stringOutputStatus=1;
}

int OutBuffer::putStringEnd(char *s){
    char *p;
    p=sentLength+s;
    if(*p==0) return 1;
    if(sendingLength<=sentLength) return 1;
    if(sentLength>MaxInterFaceStrLen) return 1;
    return 0;
}

void OutBuffer::putStringWaitPreviousOutput(void){
    int i;
//  while(stringOutputStatus==1){
      while(status<=3){
          for(i=0;i<100;i++){}
      }
//  }
    status=2;
    stringOutputStatus=0;
}

int OutBuffer::waitForAck(){
//  int i;
    /*
    for(i=0;i<MaxInterFaceStrLen*BitsParChar;i++){
        if(stringOutputStatus==2){
                   stringOutputStatus=0;
                   return 1;
            }
            wait((float)0.001) ;
    }
    return 0;
    */
    return 1;
}

unsigned char OutBuffer::getStringOutputStatus(){
    return stringOutputStatus;
}

int OutBuffer::putString( char *s){
//    unsigned char cx;
    unsigned short i;
//  unsigned int l;
//  sputs(s);
//  printf("\n putString %s ",s);
//  printf(" status=%d",(*this).status);
//    printf(".2.");

    putStringWaitPreviousOutput();
    str[0]='p';
    int ll;
    if((sendingLength-sentLength)<MaxInterFaceStrLen-4){
        ll=sendingLength-sentLength;
        str[1]=i2h1((ll >> 4) & 0x0f) ;
          str[2]=i2h1(ll & 0x0f );
    }
    else{
        ll=MaxInterFaceStrLen-4;
        str[1]=i2h1((ll >> 4) & 0x0f) ;
          str[2]=i2h1(ll & 0x0f );
    }
    for(i=0;i<ll;i++){
        str[3+i]=s[sentLength+i];
    }
        strl=ll+3;
        sentLength+=ll;
    str[3+ll]='\0';
    strp=0;
    setStatus(2); 
//      setStatus(0);
    while((isOutPutting())==1){
            wait(0.01f);
            //wait(0.01f);
            //ThisThread::sleep_for(100);
            wait_us(One_Pulse); //20230622
        }
    (setPortXOn)();
        return waitForAck();
        
}

unsigned char OutBuffer::getStatus(void){
    return status;
}

void OutBuffer::setStatus(unsigned char x){
    status=x;
}
void OutBuffer::setStringOutputStatus(unsigned char x){
      stringOutputStatus=x;
}

OutBuffer::OutBuffer(unsigned char id){
    channelID=id;
    output=0x00;
    pb=0;
    strl=0;
    strp=0;
    status=4;
    ackFlag=0;
    stringOutputStatus=0; // 0:
}

void OutBuffer::putStringX( char *x){
   putStringStart(x);
   while(1){
       if( putStringEnd(x)){
             stringOutputStatus=0;
             setStatus(4);
           return;
       }
       if(!putString(x)){
             setStatus(4);
             strl=0;
             strp=0;
             pb=0;
             ackFlag=0;
             stringOutputStatus=0;
             return;
         }
   }
}

void OutBuffer::setLedNo(unsigned char x){
   ledNo=x;
}

}}
----
#counter

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS