p1

PGSdisplay.cpp

Committer:
bosko001
Date:
2020-06-18
Revision:
7:6a700c98b9b5
Parent:
6:19e7658575fe

File content as of revision 7:6a700c98b9b5:


//#include "mbed.h"
//#include "platform/mbed_thread.h"

#include "mbed.h"

#include "PGSdisplay.h"

#define DISP_ADR   40

DigitalOut tast(PTB19,0);
DigitalOut proba(PTB18,0);
////RawSerial rs485(PTC17,PTC16);
//Serial rs485(PTD3,PTD2);
UARTSerial rs485(PTD3,PTD2);
DigitalOut led(LED2,1);


void printT(char *s, char *tel, int duzina);

char urxbuf[100];
int urb=0;


void rec( void )
{
   int i = 0, j = 0;
    while(true)
    {
proba = 1;
            int k;
     //     if( rs485.readable() ) 
          {
              urb = rs485.read((uint8_t*)&urxbuf[0],100/*,NULL,NULL*/);
 
//             printf("k=%x\n\r",k);
                printT("prijem", urxbuf, urb);
            }
proba = 0;
        thread_sleep_for(1);
    }
}


int dispGetResponce( void )
{
   urb = 0;
   thread_sleep_for(100);
   if(urb && (urxbuf[0] == 0xe5))      return 1;
   return 0;
}

unsigned char buf_tel[100] = {0x71,0x00,0x0D,0x71,0x43,DISP_ADR,0x1B,0x4A,0x01,0x01,  /*0x1b,0x45,0x1,*/  0x1B,0x43,1,0x33,0xD5,0x16};

void init_disp( void)
{
    rs485.set_format(8,SerialBase::Even,1);
//   rs485.attach( tast_off, Serial::TxIrq );  
//   rs485.attach( rc, Serial::RxIrq );  

    Thread *Ttast = new Thread;
    extern void tast_fun(void);
    Ttast->start( (tast_fun ));


    Thread *Trec = new Thread;
    Trec->start( rec );

    
    while( true )
    {      
     extern void putOnDispNo( int i, char boja);
     static int i=1;
       buf_tel[5] = i;
        putOnDispNo(i, 1);

        char *r;
        if( dispGetResponce( ) ) { printf( "adresa je %d\n\r", i); break;}
        i++;
        if(i==0) i++;
        else if(i == 254) {i = 1; break;}
    }

}



#define NO_BEFORE_DATA 13
#define NO_WITH_DATA (NO_BEFORE_DATA + strlen(s))

void putOnDisp( char *s, char boja)
{
    int ubb = NO_BEFORE_DATA+strlen(s)+1+1-4-2; // ubb = ukupan broj bajtova
    buf_tel[NO_BEFORE_DATA-1]=boja;
    memcpy(buf_tel+NO_BEFORE_DATA, s, strlen(s)+2);

    char suma=0;
    for(int i = 4; i < NO_WITH_DATA; i++) {
        suma +=buf_tel[i];
    }

    buf_tel[NO_WITH_DATA] = suma;
    buf_tel[NO_WITH_DATA+1] = 22;
    buf_tel[1]=ubb>>8;
    buf_tel[2]=ubb;

    
//      Thread *t = new Thread;
//    extern void tast_fun(Thread* t);
//    t->start( callback(tast_fun, t ));
//    thread_sleep_for(1);  

    tast = 1;
    rs485.write((const void*)buf_tel, (size_t)(NO_WITH_DATA+2+2));
//     for(int i=0; i<  NO_WITH_DATA+2+2; i++ ) rs485.putc(buf_tel[i]);
}

void putOnDispNo(int broj, char boja)
{
    char s[10];
    sprintf( s, "%d", broj);
    putOnDisp( s, boja);
}


void slanjefun()
{

    int j=0;


    while (true) {
//       printf("disp thred %d\n\r",j);
        if(j >10 && j<15 ) putOnDisp((char*)"des",1);
        else putOnDispNo(j,1);
        j++;
        thread_sleep_for(1000);
    }
}



int rs485tastovanje( int tast )
{ 
static int start = 0;
int ret = -1;
      if( tast == 0 ) { start = 0;  }
      else
      { 
        if(  start == 0)  { start = 1; ret = 0;  thread_sleep_for(1);} 
        else 
        {
          volatile char c = UART2->S1;
           if ( c & UART_S1_TC_MASK)  {ret = 1; start = 0; }
        }        
      }
  return ret;
}


void tast_fun( /*Thread *t*/void )
{
    while( true )
   {
     extern int rs485tastovanje( int );
     int ret;
      if( (ret = rs485tastovanje( tast )) == 1 ) { tast = 0; }
    }   
//   kos = t;
//  printf("___istastovanje %d  tast = %d   thr=%d \n\r", brb, (int)tast, t);
//  fflush(stdout);
}


char * extract_string( char delimiter, char *ulazni_str, int n_str )
{
    int br_str=0;
    char *ret_str=ulazni_str;
    char* ptr = ulazni_str;

    while(*ptr) {
        if(*ptr == delimiter) {
            *ptr = 0;
            if( br_str == n_str )   return ret_str;
            else {
                br_str++;
                ret_str = ptr + 1;
            }
        }
        ptr++;
    }
    if( br_str == n_str )   return ret_str;
    return NULL;
}


void printT(char *s, char *tel, int duzina){
    int i=0;
    printf("%s duzina = %d: ",s,duzina);
    while(i<duzina){   
        printf("%02X ", tel[i]);
        i++;
    }
    printf("\n\r");
    fflush(stdout);
}