main_imu, MPU6050 , racolta_dati sono per il funzionamento dell' accelerometro. my_img_sd è una libreria per gestire i dati su un sd sulla quale vengono forniti solamente le funzioni di lettura e scrittura a blocchi i file trasmetti sono la definizione e implementazione delle funzioni del protoccolo per la gestione dell' invio dei dati con il relativo formato
Dependencies: mbed
my_img_sd.h@0:a9753886e1e0, 2017-11-05 (annotated)
- Committer:
- rattokiller
- Date:
- Sun Nov 05 14:20:26 2017 +0000
- Revision:
- 0:a9753886e1e0
librerie utili
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rattokiller | 0:a9753886e1e0 | 1 | #ifndef img_sd_h |
rattokiller | 0:a9753886e1e0 | 2 | #define img_sd_h |
rattokiller | 0:a9753886e1e0 | 3 | |
rattokiller | 0:a9753886e1e0 | 4 | |
rattokiller | 0:a9753886e1e0 | 5 | |
rattokiller | 0:a9753886e1e0 | 6 | #define BLOCK_START_ADDR 0 /* Block start address */ |
rattokiller | 0:a9753886e1e0 | 7 | |
rattokiller | 0:a9753886e1e0 | 8 | #define BLOCKSIZE 512 /* Block Size in Bytes */ |
rattokiller | 0:a9753886e1e0 | 9 | #define NUM_OF_BLOCKS 2 /* Total number of blocks */ |
rattokiller | 0:a9753886e1e0 | 10 | #define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */ |
rattokiller | 0:a9753886e1e0 | 11 | |
rattokiller | 0:a9753886e1e0 | 12 | #include <mbed.h> |
rattokiller | 0:a9753886e1e0 | 13 | |
rattokiller | 0:a9753886e1e0 | 14 | Serial pc(USBTX, USBRX); // tx, rx |
rattokiller | 0:a9753886e1e0 | 15 | |
rattokiller | 0:a9753886e1e0 | 16 | void stampa_B(uint32_t b[],int n,int x,int y,int x0,int y0,int x_0,int y_0); |
rattokiller | 0:a9753886e1e0 | 17 | void riemp_B(uint32_t b[],int n,int x0,int y0,int x,int y); |
rattokiller | 0:a9753886e1e0 | 18 | |
rattokiller | 0:a9753886e1e0 | 19 | |
rattokiller | 0:a9753886e1e0 | 20 | void serializza_img(uint32_t** img ,int x,int y,uint64_t addr){ |
rattokiller | 0:a9753886e1e0 | 21 | |
rattokiller | 0:a9753886e1e0 | 22 | int n_b=4*x*y/BLOCKSIZE +1; |
rattokiller | 0:a9753886e1e0 | 23 | uint32_t Buffer[BLOCKSIZE]; |
rattokiller | 0:a9753886e1e0 | 24 | Buffer[0]=n_b; |
rattokiller | 0:a9753886e1e0 | 25 | Buffer[1]=0; |
rattokiller | 0:a9753886e1e0 | 26 | Buffer[2]=x; |
rattokiller | 0:a9753886e1e0 | 27 | Buffer[3]=y; |
rattokiller | 0:a9753886e1e0 | 28 | // pc.printf("n_b= %d\r\n",n_b); |
rattokiller | 0:a9753886e1e0 | 29 | riemp_B(&Buffer[4],124,0,0,x,y); |
rattokiller | 0:a9753886e1e0 | 30 | if(sd.WriteBlocks(Buffer,addr, BLOCKSIZE, 1) == SD_OK) |
rattokiller | 0:a9753886e1e0 | 31 | pc.printf("SD OK"); |
rattokiller | 0:a9753886e1e0 | 32 | int incx,incy; |
rattokiller | 0:a9753886e1e0 | 33 | incx=128/y; |
rattokiller | 0:a9753886e1e0 | 34 | incy=128%y; |
rattokiller | 0:a9753886e1e0 | 35 | int x0,y0; |
rattokiller | 0:a9753886e1e0 | 36 | x0=124/y; |
rattokiller | 0:a9753886e1e0 | 37 | y0=124%y; |
rattokiller | 0:a9753886e1e0 | 38 | for(int i=1;i<n_b;i++){ |
rattokiller | 0:a9753886e1e0 | 39 | // pc.printf("sto caricando il buffer.. "); |
rattokiller | 0:a9753886e1e0 | 40 | riemp_B(Buffer,128,x0,y0,x,y); |
rattokiller | 0:a9753886e1e0 | 41 | if(sd.WriteBlocks(Buffer,addr+BLOCKSIZE*i, BLOCKSIZE,1) != SD_OK) |
rattokiller | 0:a9753886e1e0 | 42 | pc.printf("Errore i= %d",i); |
rattokiller | 0:a9753886e1e0 | 43 | x0+=incx; |
rattokiller | 0:a9753886e1e0 | 44 | y0+=incy; |
rattokiller | 0:a9753886e1e0 | 45 | if(y0>=y){ |
rattokiller | 0:a9753886e1e0 | 46 | x0++; |
rattokiller | 0:a9753886e1e0 | 47 | y0-=y; |
rattokiller | 0:a9753886e1e0 | 48 | } |
rattokiller | 0:a9753886e1e0 | 49 | |
rattokiller | 0:a9753886e1e0 | 50 | } |
rattokiller | 0:a9753886e1e0 | 51 | |
rattokiller | 0:a9753886e1e0 | 52 | //pc.printf("ok, serializato!\r\n"); |
rattokiller | 0:a9753886e1e0 | 53 | |
rattokiller | 0:a9753886e1e0 | 54 | |
rattokiller | 0:a9753886e1e0 | 55 | |
rattokiller | 0:a9753886e1e0 | 56 | } |
rattokiller | 0:a9753886e1e0 | 57 | unsigned int N_bk = 120; |
rattokiller | 0:a9753886e1e0 | 58 | |
rattokiller | 0:a9753886e1e0 | 59 | void leggi_img(int x_0,int y_0,uint64_t addr){ |
rattokiller | 0:a9753886e1e0 | 60 | int x,y; |
rattokiller | 0:a9753886e1e0 | 61 | uint32_t Buffer[N_bk*BLOCKSIZE+1]; |
rattokiller | 0:a9753886e1e0 | 62 | int n_b=0; |
rattokiller | 0:a9753886e1e0 | 63 | if(sd.ReadBlocks(Buffer, addr, BLOCKSIZE,1)== SD_OK){ |
rattokiller | 0:a9753886e1e0 | 64 | // pc.printf("SD READ : OK.\r\n"); |
rattokiller | 0:a9753886e1e0 | 65 | |
rattokiller | 0:a9753886e1e0 | 66 | n_b=Buffer[0]; |
rattokiller | 0:a9753886e1e0 | 67 | x=Buffer[2]; |
rattokiller | 0:a9753886e1e0 | 68 | y=Buffer[3]; |
rattokiller | 0:a9753886e1e0 | 69 | // pc.printf("nb= %d\r\n",n_b); |
rattokiller | 0:a9753886e1e0 | 70 | // pc.printf("x = %d - y= %d\r\n",x,y); |
rattokiller | 0:a9753886e1e0 | 71 | } |
rattokiller | 0:a9753886e1e0 | 72 | if(n_b<N_bk)N_bk=n_b; |
rattokiller | 0:a9753886e1e0 | 73 | stampa_B(&Buffer[4],124,x,y,0,0,x_0,y_0); |
rattokiller | 0:a9753886e1e0 | 74 | int incx,incy; |
rattokiller | 0:a9753886e1e0 | 75 | incx=N_bk*128/y; |
rattokiller | 0:a9753886e1e0 | 76 | incy=(N_bk*128)%y; |
rattokiller | 0:a9753886e1e0 | 77 | int x0,y0; |
rattokiller | 0:a9753886e1e0 | 78 | x0=124/y; |
rattokiller | 0:a9753886e1e0 | 79 | y0=124%y; |
rattokiller | 0:a9753886e1e0 | 80 | |
rattokiller | 0:a9753886e1e0 | 81 | |
rattokiller | 0:a9753886e1e0 | 82 | // pc.printf("x= %d, y= %d, x0= %d, yo= %d, y_00= %d\r\n",x,y,x0,y0,y_0); |
rattokiller | 0:a9753886e1e0 | 83 | for(int i=1;i<n_b;i+=N_bk) |
rattokiller | 0:a9753886e1e0 | 84 | |
rattokiller | 0:a9753886e1e0 | 85 | if(sd.ReadBlocks(Buffer, addr+BLOCKSIZE*i, BLOCKSIZE,N_bk)== SD_OK){ |
rattokiller | 0:a9753886e1e0 | 86 | // pc.printf("SD READ %d: OK.\r\n",i); |
rattokiller | 0:a9753886e1e0 | 87 | stampa_B(Buffer,128*N_bk,x,y,x0,y0,x_0,y_0); |
rattokiller | 0:a9753886e1e0 | 88 | x0+=incx; |
rattokiller | 0:a9753886e1e0 | 89 | y0+=incy; |
rattokiller | 0:a9753886e1e0 | 90 | if(y0>=y){ |
rattokiller | 0:a9753886e1e0 | 91 | x0++; |
rattokiller | 0:a9753886e1e0 | 92 | y0-=y; |
rattokiller | 0:a9753886e1e0 | 93 | } |
rattokiller | 0:a9753886e1e0 | 94 | |
rattokiller | 0:a9753886e1e0 | 95 | } |
rattokiller | 0:a9753886e1e0 | 96 | |
rattokiller | 0:a9753886e1e0 | 97 | |
rattokiller | 0:a9753886e1e0 | 98 | // pc.printf("finito !"); |
rattokiller | 0:a9753886e1e0 | 99 | |
rattokiller | 0:a9753886e1e0 | 100 | } |
rattokiller | 0:a9753886e1e0 | 101 | |
rattokiller | 0:a9753886e1e0 | 102 | |
rattokiller | 0:a9753886e1e0 | 103 | void riemp_B(uint32_t b[],int n,int x0,int y0,int x,int y){ |
rattokiller | 0:a9753886e1e0 | 104 | |
rattokiller | 0:a9753886e1e0 | 105 | // pc.printf("ok\r\n"); |
rattokiller | 0:a9753886e1e0 | 106 | } |
rattokiller | 0:a9753886e1e0 | 107 | |
rattokiller | 0:a9753886e1e0 | 108 | void stampa_B(uint32_t b[],int n,int x,int y,int x0,int y0,int x_0,int y_0){ |
rattokiller | 0:a9753886e1e0 | 109 | int cont=0; |
rattokiller | 0:a9753886e1e0 | 110 | int y1=y0; |
rattokiller | 0:a9753886e1e0 | 111 | int j; |
rattokiller | 0:a9753886e1e0 | 112 | |
rattokiller | 0:a9753886e1e0 | 113 | for(int i=x0;i<x;i++) |
rattokiller | 0:a9753886e1e0 | 114 | for(j=y1;j<y;j++){ |
rattokiller | 0:a9753886e1e0 | 115 | y1=0; |
rattokiller | 0:a9753886e1e0 | 116 | lcd.DrawPixel(j+x_0,x-i+y_0,b[(i-x0)*y+j-y0]); |
rattokiller | 0:a9753886e1e0 | 117 | //cont++; |
rattokiller | 0:a9753886e1e0 | 118 | if(++cont==n)return; |
rattokiller | 0:a9753886e1e0 | 119 | } |
rattokiller | 0:a9753886e1e0 | 120 | } |
rattokiller | 0:a9753886e1e0 | 121 | |
rattokiller | 0:a9753886e1e0 | 122 | void scrivi_dato(uint32_t Buffer[],uint64_t addr,int n=1){ |
rattokiller | 0:a9753886e1e0 | 123 | //pc.baud(921600); |
rattokiller | 0:a9753886e1e0 | 124 | if(addr>=start_addr_dato){ |
rattokiller | 0:a9753886e1e0 | 125 | if(sd.WriteBlocks(Buffer,addr, BLOCKSIZE*n,1) != SD_OK){ |
rattokiller | 0:a9753886e1e0 | 126 | pc.printf("\t\t\t\t -> Errore scrittura : "); |
rattokiller | 0:a9753886e1e0 | 127 | pc.printf("addres = %x \r\n",addr); |
rattokiller | 0:a9753886e1e0 | 128 | } |
rattokiller | 0:a9753886e1e0 | 129 | else |
rattokiller | 0:a9753886e1e0 | 130 | { |
rattokiller | 0:a9753886e1e0 | 131 | // pc.printf("non salvato:\t%s\r\n",(char*)Buffer); |
rattokiller | 0:a9753886e1e0 | 132 | // pc.printf("addres = %x ",addr); |
rattokiller | 0:a9753886e1e0 | 133 | |
rattokiller | 0:a9753886e1e0 | 134 | //slave.address(0xA0); |
rattokiller | 0:a9753886e1e0 | 135 | |
rattokiller | 0:a9753886e1e0 | 136 | } |
rattokiller | 0:a9753886e1e0 | 137 | } |
rattokiller | 0:a9753886e1e0 | 138 | else pc.printf("errore indirizzo non valido, addr= %d -- addr_Base = %d\r\n",addr,start_addr_dato); |
rattokiller | 0:a9753886e1e0 | 139 | } |
rattokiller | 0:a9753886e1e0 | 140 | |
rattokiller | 0:a9753886e1e0 | 141 | void printa_dato(uint32_t Buffer[],uint64_t addr){ |
rattokiller | 0:a9753886e1e0 | 142 | if(addr>=start_addr_dato) |
rattokiller | 0:a9753886e1e0 | 143 | if(sd.ReadBlocks(Buffer,addr, BLOCKSIZE,1) != SD_OK) |
rattokiller | 0:a9753886e1e0 | 144 | pc.printf("errore lettura"); |
rattokiller | 0:a9753886e1e0 | 145 | else pc.printf("Dato letto: %s",(char*)Buffer); |
rattokiller | 0:a9753886e1e0 | 146 | } |
rattokiller | 0:a9753886e1e0 | 147 | |
rattokiller | 0:a9753886e1e0 | 148 | void scrivi_dato_progresione(uint32_t Buffer[]){ |
rattokiller | 0:a9753886e1e0 | 149 | /* |
rattokiller | 0:a9753886e1e0 | 150 | uint32_t B[512]; |
rattokiller | 0:a9753886e1e0 | 151 | sd.ReadBlocks((uint32_t*)B,0, BLOCKSIZE,1); |
rattokiller | 0:a9753886e1e0 | 152 | B[10]++; |
rattokiller | 0:a9753886e1e0 | 153 | sd.WriteBlocks((uint32_t*)B,0, BLOCKSIZE,1); |
rattokiller | 0:a9753886e1e0 | 154 | |
rattokiller | 0:a9753886e1e0 | 155 | scrivi_dato(Buffer,512*B[10]); |
rattokiller | 0:a9753886e1e0 | 156 | */ |
rattokiller | 0:a9753886e1e0 | 157 | static int u=0; |
rattokiller | 0:a9753886e1e0 | 158 | |
rattokiller | 0:a9753886e1e0 | 159 | static int n=-1; |
rattokiller | 0:a9753886e1e0 | 160 | const int k=32; |
rattokiller | 0:a9753886e1e0 | 161 | static uint32_t Dato_B[BLOCKSIZE*k]; |
rattokiller | 0:a9753886e1e0 | 162 | static int B[BLOCKSIZE]; |
rattokiller | 0:a9753886e1e0 | 163 | uint64_t addr; |
rattokiller | 0:a9753886e1e0 | 164 | //pc.printf("n=%d",n); |
rattokiller | 0:a9753886e1e0 | 165 | if(n<0){pc.printf("inizio scritura\r\n");sd.ReadBlocks((uint32_t*)B,0, BLOCKSIZE,1);n=0; |
rattokiller | 0:a9753886e1e0 | 166 | if(B[10]<start_addr_dato/512)pc.printf("problemone, l' indirizzo salvato non è valido!\r\n"); |
rattokiller | 0:a9753886e1e0 | 167 | } |
rattokiller | 0:a9753886e1e0 | 168 | //pc.printf("inizio scritura"); |
rattokiller | 0:a9753886e1e0 | 169 | |
rattokiller | 0:a9753886e1e0 | 170 | if(n==k-1){ |
rattokiller | 0:a9753886e1e0 | 171 | |
rattokiller | 0:a9753886e1e0 | 172 | //sd.ReadBlocks((uint32_t*)B,0, BLOCKSIZE,1); |
rattokiller | 0:a9753886e1e0 | 173 | sd.ReadBlocks((uint32_t*)B,0, BLOCKSIZE,1); |
rattokiller | 0:a9753886e1e0 | 174 | strcpy((char*)Dato_B[BLOCKSIZE*n],(char*)Buffer); |
rattokiller | 0:a9753886e1e0 | 175 | addr=B[10]; |
rattokiller | 0:a9753886e1e0 | 176 | addr=BLOCKSIZE*addr; |
rattokiller | 0:a9753886e1e0 | 177 | |
rattokiller | 0:a9753886e1e0 | 178 | scrivi_dato(Dato_B,addr,k); |
rattokiller | 0:a9753886e1e0 | 179 | |
rattokiller | 0:a9753886e1e0 | 180 | u++; |
rattokiller | 0:a9753886e1e0 | 181 | //if(u==640||u==800||u==2240||u==2976||u==2720)B[10]=B[10]+2*k;else |
rattokiller | 0:a9753886e1e0 | 182 | B[10]=B[10]+k; |
rattokiller | 0:a9753886e1e0 | 183 | |
rattokiller | 0:a9753886e1e0 | 184 | sd.WriteBlocks((uint32_t*)B,0, BLOCKSIZE,1); |
rattokiller | 0:a9753886e1e0 | 185 | |
rattokiller | 0:a9753886e1e0 | 186 | n=0; |
rattokiller | 0:a9753886e1e0 | 187 | } |
rattokiller | 0:a9753886e1e0 | 188 | else {strcpy((char*)Dato_B[BLOCKSIZE*n],(char*)Buffer);n++;} |
rattokiller | 0:a9753886e1e0 | 189 | |
rattokiller | 0:a9753886e1e0 | 190 | |
rattokiller | 0:a9753886e1e0 | 191 | |
rattokiller | 0:a9753886e1e0 | 192 | } |
rattokiller | 0:a9753886e1e0 | 193 | |
rattokiller | 0:a9753886e1e0 | 194 | void printa_dato_progressione(){ |
rattokiller | 0:a9753886e1e0 | 195 | |
rattokiller | 0:a9753886e1e0 | 196 | pc.printf("\r\n Sto per legere in progresione ... "); |
rattokiller | 0:a9753886e1e0 | 197 | uint32_t B[BLOCKSIZE]; |
rattokiller | 0:a9753886e1e0 | 198 | sd.ReadBlocks((uint32_t*)B,0, BLOCKSIZE,1); |
rattokiller | 0:a9753886e1e0 | 199 | int n=B[10]; |
rattokiller | 0:a9753886e1e0 | 200 | pc.printf("n=%d\r\n",n-start_addr_dato/512); |
rattokiller | 0:a9753886e1e0 | 201 | for(int i=start_addr_dato;i<n*BLOCKSIZE;i+=BLOCKSIZE) |
rattokiller | 0:a9753886e1e0 | 202 | printa_dato((uint32_t*)B,i); |
rattokiller | 0:a9753886e1e0 | 203 | |
rattokiller | 0:a9753886e1e0 | 204 | pc.printf("\tfine\r\n"); |
rattokiller | 0:a9753886e1e0 | 205 | } |
rattokiller | 0:a9753886e1e0 | 206 | |
rattokiller | 0:a9753886e1e0 | 207 | void cancella_dati(){ |
rattokiller | 0:a9753886e1e0 | 208 | |
rattokiller | 0:a9753886e1e0 | 209 | int B[512]; |
rattokiller | 0:a9753886e1e0 | 210 | pc.printf("\n dati cancellati \n"); |
rattokiller | 0:a9753886e1e0 | 211 | B[10]=start_addr_dato/512; |
rattokiller | 0:a9753886e1e0 | 212 | sd.WriteBlocks((uint32_t*)B,0, BLOCKSIZE,1); |
rattokiller | 0:a9753886e1e0 | 213 | } |
rattokiller | 0:a9753886e1e0 | 214 | |
rattokiller | 0:a9753886e1e0 | 215 | #endif |