bosko lekovic
/
EthTCPclient_05_02
p1
PGSdisplay.cpp@7:6a700c98b9b5, 2020-06-18 (annotated)
- Committer:
- bosko001
- Date:
- Thu Jun 18 15:23:05 2020 +0000
- Revision:
- 7:6a700c98b9b5
- 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:6a700c98b9b5 | 12 | DigitalOut proba(PTB18,0); |
bosko001 | 5:ecaf089d1c8a | 13 | ////RawSerial rs485(PTC17,PTC16); |
bosko001 | 7:6a700c98b9b5 | 14 | //Serial rs485(PTD3,PTD2); |
bosko001 | 7:6a700c98b9b5 | 15 | UARTSerial rs485(PTD3,PTD2); |
bosko001 | 7:6a700c98b9b5 | 16 | DigitalOut led(LED2,1); |
bosko001 | 7:6a700c98b9b5 | 17 | |
bosko001 | 7:6a700c98b9b5 | 18 | |
bosko001 | 7:6a700c98b9b5 | 19 | void printT(char *s, char *tel, int duzina); |
bosko001 | 7:6a700c98b9b5 | 20 | |
bosko001 | 7:6a700c98b9b5 | 21 | char urxbuf[100]; |
bosko001 | 7:6a700c98b9b5 | 22 | int urb=0; |
bosko001 | 4:0aec01df33d2 | 23 | |
bosko001 | 4:0aec01df33d2 | 24 | |
bosko001 | 7:6a700c98b9b5 | 25 | void rec( void ) |
bosko001 | 4:0aec01df33d2 | 26 | { |
bosko001 | 7:6a700c98b9b5 | 27 | int i = 0, j = 0; |
bosko001 | 7:6a700c98b9b5 | 28 | while(true) |
bosko001 | 7:6a700c98b9b5 | 29 | { |
bosko001 | 7:6a700c98b9b5 | 30 | proba = 1; |
bosko001 | 7:6a700c98b9b5 | 31 | int k; |
bosko001 | 7:6a700c98b9b5 | 32 | // if( rs485.readable() ) |
bosko001 | 7:6a700c98b9b5 | 33 | { |
bosko001 | 7:6a700c98b9b5 | 34 | urb = rs485.read((uint8_t*)&urxbuf[0],100/*,NULL,NULL*/); |
bosko001 | 7:6a700c98b9b5 | 35 | |
bosko001 | 7:6a700c98b9b5 | 36 | // printf("k=%x\n\r",k); |
bosko001 | 7:6a700c98b9b5 | 37 | printT("prijem", urxbuf, urb); |
bosko001 | 7:6a700c98b9b5 | 38 | } |
bosko001 | 7:6a700c98b9b5 | 39 | proba = 0; |
bosko001 | 7:6a700c98b9b5 | 40 | thread_sleep_for(1); |
bosko001 | 7:6a700c98b9b5 | 41 | } |
bosko001 | 4:0aec01df33d2 | 42 | } |
bosko001 | 4:0aec01df33d2 | 43 | |
bosko001 | 7:6a700c98b9b5 | 44 | |
bosko001 | 7:6a700c98b9b5 | 45 | int dispGetResponce( void ) |
bosko001 | 5:ecaf089d1c8a | 46 | { |
bosko001 | 7:6a700c98b9b5 | 47 | urb = 0; |
bosko001 | 7:6a700c98b9b5 | 48 | thread_sleep_for(100); |
bosko001 | 7:6a700c98b9b5 | 49 | if(urb && (urxbuf[0] == 0xe5)) return 1; |
bosko001 | 7:6a700c98b9b5 | 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:6a700c98b9b5 | 54 | |
bosko001 | 7:6a700c98b9b5 | 55 | void init_disp( void) |
bosko001 | 7:6a700c98b9b5 | 56 | { |
bosko001 | 7:6a700c98b9b5 | 57 | rs485.set_format(8,SerialBase::Even,1); |
bosko001 | 7:6a700c98b9b5 | 58 | // rs485.attach( tast_off, Serial::TxIrq ); |
bosko001 | 7:6a700c98b9b5 | 59 | // rs485.attach( rc, Serial::RxIrq ); |
bosko001 | 7:6a700c98b9b5 | 60 | |
bosko001 | 7:6a700c98b9b5 | 61 | Thread *Ttast = new Thread; |
bosko001 | 7:6a700c98b9b5 | 62 | extern void tast_fun(void); |
bosko001 | 7:6a700c98b9b5 | 63 | Ttast->start( (tast_fun )); |
bosko001 | 7:6a700c98b9b5 | 64 | |
bosko001 | 7:6a700c98b9b5 | 65 | |
bosko001 | 7:6a700c98b9b5 | 66 | Thread *Trec = new Thread; |
bosko001 | 7:6a700c98b9b5 | 67 | Trec->start( rec ); |
bosko001 | 7:6a700c98b9b5 | 68 | |
bosko001 | 7:6a700c98b9b5 | 69 | |
bosko001 | 7:6a700c98b9b5 | 70 | while( true ) |
bosko001 | 7:6a700c98b9b5 | 71 | { |
bosko001 | 7:6a700c98b9b5 | 72 | extern void putOnDispNo( int i, char boja); |
bosko001 | 7:6a700c98b9b5 | 73 | static int i=1; |
bosko001 | 7:6a700c98b9b5 | 74 | buf_tel[5] = i; |
bosko001 | 7:6a700c98b9b5 | 75 | putOnDispNo(i, 1); |
bosko001 | 7:6a700c98b9b5 | 76 | |
bosko001 | 7:6a700c98b9b5 | 77 | char *r; |
bosko001 | 7:6a700c98b9b5 | 78 | if( dispGetResponce( ) ) { printf( "adresa je %d\n\r", i); break;} |
bosko001 | 7:6a700c98b9b5 | 79 | i++; |
bosko001 | 7:6a700c98b9b5 | 80 | if(i==0) i++; |
bosko001 | 7:6a700c98b9b5 | 81 | else if(i == 254) {i = 1; break;} |
bosko001 | 7:6a700c98b9b5 | 82 | } |
bosko001 | 7:6a700c98b9b5 | 83 | |
bosko001 | 7:6a700c98b9b5 | 84 | } |
bosko001 | 7:6a700c98b9b5 | 85 | |
bosko001 | 7:6a700c98b9b5 | 86 | |
bosko001 | 7:6a700c98b9b5 | 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:6a700c98b9b5 | 107 | |
bosko001 | 7:6a700c98b9b5 | 108 | // Thread *t = new Thread; |
bosko001 | 7:6a700c98b9b5 | 109 | // extern void tast_fun(Thread* t); |
bosko001 | 7:6a700c98b9b5 | 110 | // t->start( callback(tast_fun, t )); |
bosko001 | 7:6a700c98b9b5 | 111 | // thread_sleep_for(1); |
bosko001 | 4:0aec01df33d2 | 112 | |
bosko001 | 7:6a700c98b9b5 | 113 | tast = 1; |
bosko001 | 7:6a700c98b9b5 | 114 | rs485.write((const void*)buf_tel, (size_t)(NO_WITH_DATA+2+2)); |
bosko001 | 7:6a700c98b9b5 | 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:6a700c98b9b5 | 142 | |
bosko001 | 7:6a700c98b9b5 | 143 | int rs485tastovanje( int tast ) |
bosko001 | 7:6a700c98b9b5 | 144 | { |
bosko001 | 7:6a700c98b9b5 | 145 | static int start = 0; |
bosko001 | 7:6a700c98b9b5 | 146 | int ret = -1; |
bosko001 | 7:6a700c98b9b5 | 147 | if( tast == 0 ) { start = 0; } |
bosko001 | 7:6a700c98b9b5 | 148 | else |
bosko001 | 7:6a700c98b9b5 | 149 | { |
bosko001 | 7:6a700c98b9b5 | 150 | if( start == 0) { start = 1; ret = 0; thread_sleep_for(1);} |
bosko001 | 7:6a700c98b9b5 | 151 | else |
bosko001 | 7:6a700c98b9b5 | 152 | { |
bosko001 | 7:6a700c98b9b5 | 153 | volatile char c = UART2->S1; |
bosko001 | 7:6a700c98b9b5 | 154 | if ( c & UART_S1_TC_MASK) {ret = 1; start = 0; } |
bosko001 | 7:6a700c98b9b5 | 155 | } |
bosko001 | 7:6a700c98b9b5 | 156 | } |
bosko001 | 7:6a700c98b9b5 | 157 | return ret; |
bosko001 | 7:6a700c98b9b5 | 158 | } |
bosko001 | 7:6a700c98b9b5 | 159 | |
bosko001 | 7:6a700c98b9b5 | 160 | |
bosko001 | 7:6a700c98b9b5 | 161 | void tast_fun( /*Thread *t*/void ) |
bosko001 | 7:6a700c98b9b5 | 162 | { |
bosko001 | 7:6a700c98b9b5 | 163 | while( true ) |
bosko001 | 7:6a700c98b9b5 | 164 | { |
bosko001 | 7:6a700c98b9b5 | 165 | extern int rs485tastovanje( int ); |
bosko001 | 7:6a700c98b9b5 | 166 | int ret; |
bosko001 | 7:6a700c98b9b5 | 167 | if( (ret = rs485tastovanje( tast )) == 1 ) { tast = 0; } |
bosko001 | 7:6a700c98b9b5 | 168 | } |
bosko001 | 7:6a700c98b9b5 | 169 | // kos = t; |
bosko001 | 7:6a700c98b9b5 | 170 | // printf("___istastovanje %d tast = %d thr=%d \n\r", brb, (int)tast, t); |
bosko001 | 7:6a700c98b9b5 | 171 | // fflush(stdout); |
bosko001 | 7:6a700c98b9b5 | 172 | } |
bosko001 | 7:6a700c98b9b5 | 173 | |
bosko001 | 7:6a700c98b9b5 | 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:6a700c98b9b5 | 196 | |
bosko001 | 7:6a700c98b9b5 | 197 | void printT(char *s, char *tel, int duzina){ |
bosko001 | 7:6a700c98b9b5 | 198 | int i=0; |
bosko001 | 7:6a700c98b9b5 | 199 | printf("%s duzina = %d: ",s,duzina); |
bosko001 | 7:6a700c98b9b5 | 200 | while(i<duzina){ |
bosko001 | 7:6a700c98b9b5 | 201 | printf("%02X ", tel[i]); |
bosko001 | 7:6a700c98b9b5 | 202 | i++; |
bosko001 | 7:6a700c98b9b5 | 203 | } |
bosko001 | 7:6a700c98b9b5 | 204 | printf("\n\r"); |
bosko001 | 7:6a700c98b9b5 | 205 | fflush(stdout); |
bosko001 | 7:6a700c98b9b5 | 206 | } |