bosko lekovic
/
EthTCPclient_05_03
p1
PGSdisplay.cpp@7:be56cf51685d, 2020-06-18 (annotated)
- Committer:
- bosko001
- Date:
- Thu Jun 18 15:23:39 2020 +0000
- Revision:
- 7:be56cf51685d
- Parent:
- 6:19e7658575fe
p1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 4:0aec01df33d2 | 1 | |
bosko001 | 4:0aec01df33d2 | 2 | //#include "mbed.h" |
bosko001 | 4:0aec01df33d2 | 3 | //#include "platform/mbed_thread.h" |
bosko001 | 4:0aec01df33d2 | 4 | |
bosko001 | 5:ecaf089d1c8a | 5 | #include "mbed.h" |
bosko001 | 4:0aec01df33d2 | 6 | |
bosko001 | 4:0aec01df33d2 | 7 | #include "PGSdisplay.h" |
bosko001 | 5:ecaf089d1c8a | 8 | |
bosko001 | 5:ecaf089d1c8a | 9 | #define DISP_ADR 40 |
bosko001 | 4:0aec01df33d2 | 10 | |
bosko001 | 5:ecaf089d1c8a | 11 | DigitalOut tast(PTB19,0); |
bosko001 | 7:be56cf51685d | 12 | DigitalOut proba(PTB18,0); |
bosko001 | 5:ecaf089d1c8a | 13 | ////RawSerial rs485(PTC17,PTC16); |
bosko001 | 7:be56cf51685d | 14 | //Serial rs485(PTD3,PTD2); |
bosko001 | 7:be56cf51685d | 15 | UARTSerial rs485(PTD3,PTD2); |
bosko001 | 7:be56cf51685d | 16 | DigitalOut led(LED2,1); |
bosko001 | 7:be56cf51685d | 17 | |
bosko001 | 7:be56cf51685d | 18 | |
bosko001 | 7:be56cf51685d | 19 | void printT(char *s, char *tel, int duzina); |
bosko001 | 7:be56cf51685d | 20 | |
bosko001 | 7:be56cf51685d | 21 | char urxbuf[100]; |
bosko001 | 7:be56cf51685d | 22 | int urb=0; |
bosko001 | 4:0aec01df33d2 | 23 | |
bosko001 | 4:0aec01df33d2 | 24 | |
bosko001 | 7:be56cf51685d | 25 | void rec( void ) |
bosko001 | 4:0aec01df33d2 | 26 | { |
bosko001 | 7:be56cf51685d | 27 | int i = 0, j = 0; |
bosko001 | 7:be56cf51685d | 28 | while(true) |
bosko001 | 7:be56cf51685d | 29 | { |
bosko001 | 7:be56cf51685d | 30 | proba = 1; |
bosko001 | 7:be56cf51685d | 31 | int k; |
bosko001 | 7:be56cf51685d | 32 | // if( rs485.readable() ) |
bosko001 | 7:be56cf51685d | 33 | { |
bosko001 | 7:be56cf51685d | 34 | urb = rs485.read((uint8_t*)&urxbuf[0],100/*,NULL,NULL*/); |
bosko001 | 7:be56cf51685d | 35 | |
bosko001 | 7:be56cf51685d | 36 | // printf("k=%x\n\r",k); |
bosko001 | 7:be56cf51685d | 37 | printT("prijem", urxbuf, urb); |
bosko001 | 7:be56cf51685d | 38 | } |
bosko001 | 7:be56cf51685d | 39 | proba = 0; |
bosko001 | 7:be56cf51685d | 40 | thread_sleep_for(1); |
bosko001 | 7:be56cf51685d | 41 | } |
bosko001 | 4:0aec01df33d2 | 42 | } |
bosko001 | 4:0aec01df33d2 | 43 | |
bosko001 | 7:be56cf51685d | 44 | |
bosko001 | 7:be56cf51685d | 45 | int dispGetResponce( void ) |
bosko001 | 5:ecaf089d1c8a | 46 | { |
bosko001 | 7:be56cf51685d | 47 | urb = 0; |
bosko001 | 7:be56cf51685d | 48 | thread_sleep_for(100); |
bosko001 | 7:be56cf51685d | 49 | if(urb && (urxbuf[0] == 0xe5)) return 1; |
bosko001 | 7:be56cf51685d | 50 | return 0; |
bosko001 | 5:ecaf089d1c8a | 51 | } |
bosko001 | 5:ecaf089d1c8a | 52 | |
bosko001 | 5:ecaf089d1c8a | 53 | 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}; |
bosko001 | 7:be56cf51685d | 54 | |
bosko001 | 7:be56cf51685d | 55 | void init_disp( void) |
bosko001 | 7:be56cf51685d | 56 | { |
bosko001 | 7:be56cf51685d | 57 | rs485.set_format(8,SerialBase::Even,1); |
bosko001 | 7:be56cf51685d | 58 | // rs485.attach( tast_off, Serial::TxIrq ); |
bosko001 | 7:be56cf51685d | 59 | // rs485.attach( rc, Serial::RxIrq ); |
bosko001 | 7:be56cf51685d | 60 | |
bosko001 | 7:be56cf51685d | 61 | Thread *Ttast = new Thread; |
bosko001 | 7:be56cf51685d | 62 | extern void tast_fun(void); |
bosko001 | 7:be56cf51685d | 63 | Ttast->start( (tast_fun )); |
bosko001 | 7:be56cf51685d | 64 | |
bosko001 | 7:be56cf51685d | 65 | |
bosko001 | 7:be56cf51685d | 66 | Thread *Trec = new Thread; |
bosko001 | 7:be56cf51685d | 67 | Trec->start( rec ); |
bosko001 | 7:be56cf51685d | 68 | |
bosko001 | 7:be56cf51685d | 69 | |
bosko001 | 7:be56cf51685d | 70 | while( true ) |
bosko001 | 7:be56cf51685d | 71 | { |
bosko001 | 7:be56cf51685d | 72 | extern void putOnDispNo( int i, char boja); |
bosko001 | 7:be56cf51685d | 73 | static int i=1; |
bosko001 | 7:be56cf51685d | 74 | buf_tel[5] = i; |
bosko001 | 7:be56cf51685d | 75 | putOnDispNo(i, 1); |
bosko001 | 7:be56cf51685d | 76 | |
bosko001 | 7:be56cf51685d | 77 | char *r; |
bosko001 | 7:be56cf51685d | 78 | if( dispGetResponce( ) ) { printf( "adresa je %d\n\r", i); break;} |
bosko001 | 7:be56cf51685d | 79 | i++; |
bosko001 | 7:be56cf51685d | 80 | if(i==0) i++; |
bosko001 | 7:be56cf51685d | 81 | else if(i == 254) {i = 1; break;} |
bosko001 | 7:be56cf51685d | 82 | } |
bosko001 | 7:be56cf51685d | 83 | |
bosko001 | 7:be56cf51685d | 84 | } |
bosko001 | 7:be56cf51685d | 85 | |
bosko001 | 7:be56cf51685d | 86 | |
bosko001 | 7:be56cf51685d | 87 | |
bosko001 | 4:0aec01df33d2 | 88 | #define NO_BEFORE_DATA 13 |
bosko001 | 4:0aec01df33d2 | 89 | #define NO_WITH_DATA (NO_BEFORE_DATA + strlen(s)) |
bosko001 | 4:0aec01df33d2 | 90 | |
bosko001 | 4:0aec01df33d2 | 91 | void putOnDisp( char *s, char boja) |
bosko001 | 4:0aec01df33d2 | 92 | { |
bosko001 | 4:0aec01df33d2 | 93 | int ubb = NO_BEFORE_DATA+strlen(s)+1+1-4-2; // ubb = ukupan broj bajtova |
bosko001 | 4:0aec01df33d2 | 94 | buf_tel[NO_BEFORE_DATA-1]=boja; |
bosko001 | 4:0aec01df33d2 | 95 | memcpy(buf_tel+NO_BEFORE_DATA, s, strlen(s)+2); |
bosko001 | 4:0aec01df33d2 | 96 | |
bosko001 | 4:0aec01df33d2 | 97 | char suma=0; |
bosko001 | 4:0aec01df33d2 | 98 | for(int i = 4; i < NO_WITH_DATA; i++) { |
bosko001 | 4:0aec01df33d2 | 99 | suma +=buf_tel[i]; |
bosko001 | 4:0aec01df33d2 | 100 | } |
bosko001 | 4:0aec01df33d2 | 101 | |
bosko001 | 4:0aec01df33d2 | 102 | buf_tel[NO_WITH_DATA] = suma; |
bosko001 | 4:0aec01df33d2 | 103 | buf_tel[NO_WITH_DATA+1] = 22; |
bosko001 | 4:0aec01df33d2 | 104 | buf_tel[1]=ubb>>8; |
bosko001 | 4:0aec01df33d2 | 105 | buf_tel[2]=ubb; |
bosko001 | 4:0aec01df33d2 | 106 | |
bosko001 | 7:be56cf51685d | 107 | |
bosko001 | 7:be56cf51685d | 108 | // Thread *t = new Thread; |
bosko001 | 7:be56cf51685d | 109 | // extern void tast_fun(Thread* t); |
bosko001 | 7:be56cf51685d | 110 | // t->start( callback(tast_fun, t )); |
bosko001 | 7:be56cf51685d | 111 | // thread_sleep_for(1); |
bosko001 | 4:0aec01df33d2 | 112 | |
bosko001 | 7:be56cf51685d | 113 | tast = 1; |
bosko001 | 7:be56cf51685d | 114 | rs485.write((const void*)buf_tel, (size_t)(NO_WITH_DATA+2+2)); |
bosko001 | 7:be56cf51685d | 115 | // for(int i=0; i< NO_WITH_DATA+2+2; i++ ) rs485.putc(buf_tel[i]); |
bosko001 | 4:0aec01df33d2 | 116 | } |
bosko001 | 4:0aec01df33d2 | 117 | |
bosko001 | 4:0aec01df33d2 | 118 | void putOnDispNo(int broj, char boja) |
bosko001 | 4:0aec01df33d2 | 119 | { |
bosko001 | 4:0aec01df33d2 | 120 | char s[10]; |
bosko001 | 4:0aec01df33d2 | 121 | sprintf( s, "%d", broj); |
bosko001 | 4:0aec01df33d2 | 122 | putOnDisp( s, boja); |
bosko001 | 4:0aec01df33d2 | 123 | } |
bosko001 | 4:0aec01df33d2 | 124 | |
bosko001 | 4:0aec01df33d2 | 125 | |
bosko001 | 4:0aec01df33d2 | 126 | void slanjefun() |
bosko001 | 4:0aec01df33d2 | 127 | { |
bosko001 | 4:0aec01df33d2 | 128 | |
bosko001 | 4:0aec01df33d2 | 129 | int j=0; |
bosko001 | 4:0aec01df33d2 | 130 | |
bosko001 | 4:0aec01df33d2 | 131 | |
bosko001 | 4:0aec01df33d2 | 132 | while (true) { |
bosko001 | 4:0aec01df33d2 | 133 | // printf("disp thred %d\n\r",j); |
bosko001 | 5:ecaf089d1c8a | 134 | if(j >10 && j<15 ) putOnDisp((char*)"des",1); |
bosko001 | 4:0aec01df33d2 | 135 | else putOnDispNo(j,1); |
bosko001 | 4:0aec01df33d2 | 136 | j++; |
bosko001 | 4:0aec01df33d2 | 137 | thread_sleep_for(1000); |
bosko001 | 4:0aec01df33d2 | 138 | } |
bosko001 | 4:0aec01df33d2 | 139 | } |
bosko001 | 4:0aec01df33d2 | 140 | |
bosko001 | 4:0aec01df33d2 | 141 | |
bosko001 | 7:be56cf51685d | 142 | |
bosko001 | 7:be56cf51685d | 143 | int rs485tastovanje( int tast ) |
bosko001 | 7:be56cf51685d | 144 | { |
bosko001 | 7:be56cf51685d | 145 | static int start = 0; |
bosko001 | 7:be56cf51685d | 146 | int ret = -1; |
bosko001 | 7:be56cf51685d | 147 | if( tast == 0 ) { start = 0; } |
bosko001 | 7:be56cf51685d | 148 | else |
bosko001 | 7:be56cf51685d | 149 | { |
bosko001 | 7:be56cf51685d | 150 | if( start == 0) { start = 1; ret = 0; thread_sleep_for(1);} |
bosko001 | 7:be56cf51685d | 151 | else |
bosko001 | 7:be56cf51685d | 152 | { |
bosko001 | 7:be56cf51685d | 153 | volatile char c = UART2->S1; |
bosko001 | 7:be56cf51685d | 154 | if ( c & UART_S1_TC_MASK) {ret = 1; start = 0; } |
bosko001 | 7:be56cf51685d | 155 | } |
bosko001 | 7:be56cf51685d | 156 | } |
bosko001 | 7:be56cf51685d | 157 | return ret; |
bosko001 | 7:be56cf51685d | 158 | } |
bosko001 | 7:be56cf51685d | 159 | |
bosko001 | 7:be56cf51685d | 160 | |
bosko001 | 7:be56cf51685d | 161 | void tast_fun( /*Thread *t*/void ) |
bosko001 | 7:be56cf51685d | 162 | { |
bosko001 | 7:be56cf51685d | 163 | while( true ) |
bosko001 | 7:be56cf51685d | 164 | { |
bosko001 | 7:be56cf51685d | 165 | extern int rs485tastovanje( int ); |
bosko001 | 7:be56cf51685d | 166 | int ret; |
bosko001 | 7:be56cf51685d | 167 | if( (ret = rs485tastovanje( tast )) == 1 ) { tast = 0; } |
bosko001 | 7:be56cf51685d | 168 | } |
bosko001 | 7:be56cf51685d | 169 | // kos = t; |
bosko001 | 7:be56cf51685d | 170 | // printf("___istastovanje %d tast = %d thr=%d \n\r", brb, (int)tast, t); |
bosko001 | 7:be56cf51685d | 171 | // fflush(stdout); |
bosko001 | 7:be56cf51685d | 172 | } |
bosko001 | 7:be56cf51685d | 173 | |
bosko001 | 7:be56cf51685d | 174 | |
bosko001 | 4:0aec01df33d2 | 175 | char * extract_string( char delimiter, char *ulazni_str, int n_str ) |
bosko001 | 4:0aec01df33d2 | 176 | { |
bosko001 | 4:0aec01df33d2 | 177 | int br_str=0; |
bosko001 | 4:0aec01df33d2 | 178 | char *ret_str=ulazni_str; |
bosko001 | 4:0aec01df33d2 | 179 | char* ptr = ulazni_str; |
bosko001 | 4:0aec01df33d2 | 180 | |
bosko001 | 4:0aec01df33d2 | 181 | while(*ptr) { |
bosko001 | 4:0aec01df33d2 | 182 | if(*ptr == delimiter) { |
bosko001 | 4:0aec01df33d2 | 183 | *ptr = 0; |
bosko001 | 4:0aec01df33d2 | 184 | if( br_str == n_str ) return ret_str; |
bosko001 | 4:0aec01df33d2 | 185 | else { |
bosko001 | 4:0aec01df33d2 | 186 | br_str++; |
bosko001 | 4:0aec01df33d2 | 187 | ret_str = ptr + 1; |
bosko001 | 4:0aec01df33d2 | 188 | } |
bosko001 | 4:0aec01df33d2 | 189 | } |
bosko001 | 4:0aec01df33d2 | 190 | ptr++; |
bosko001 | 4:0aec01df33d2 | 191 | } |
bosko001 | 4:0aec01df33d2 | 192 | if( br_str == n_str ) return ret_str; |
bosko001 | 4:0aec01df33d2 | 193 | return NULL; |
bosko001 | 4:0aec01df33d2 | 194 | } |
bosko001 | 4:0aec01df33d2 | 195 | |
bosko001 | 7:be56cf51685d | 196 | |
bosko001 | 7:be56cf51685d | 197 | void printT(char *s, char *tel, int duzina){ |
bosko001 | 7:be56cf51685d | 198 | int i=0; |
bosko001 | 7:be56cf51685d | 199 | printf("%s duzina = %d: ",s,duzina); |
bosko001 | 7:be56cf51685d | 200 | while(i<duzina){ |
bosko001 | 7:be56cf51685d | 201 | printf("%02X ", tel[i]); |
bosko001 | 7:be56cf51685d | 202 | i++; |
bosko001 | 7:be56cf51685d | 203 | } |
bosko001 | 7:be56cf51685d | 204 | printf("\n\r"); |
bosko001 | 7:be56cf51685d | 205 | fflush(stdout); |
bosko001 | 7:be56cf51685d | 206 | } |