00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __WPROGRAM_H__
00025 #include <WaspClasses.h>
00026 #endif
00027
00028
00029
00030
00031
00032 WaspSD::WaspSD()
00033 {
00034
00035 }
00036
00037
00038
00039
00040
00041
00042
00043
00044 void WaspSD::ON(void)
00045 {
00046 begin();
00047 setMode(SD_ON);
00048 init();
00049 }
00050
00051
00052
00053
00054
00055
00056
00057 void WaspSD::OFF(void)
00058 {
00059 close();
00060 setMode(SD_OFF);
00061 }
00062
00063
00064
00065
00066
00067 void WaspSD::begin()
00068 {
00069
00070 pinMode(MEM_PW, OUTPUT);
00071 pinMode(SD_PRESENT, INPUT);
00072 }
00073
00074
00075
00076
00077
00078
00079
00080 void WaspSD::setMode(uint8_t mode)
00081 {
00082 _pwrMode=mode;
00083 switch(_pwrMode)
00084 {
00085 case SD_ON:
00086 digitalWrite(MEM_PW, HIGH);
00087 delay(10);
00088 break;
00089 case SD_OFF:
00090 digitalWrite(MEM_PW,LOW);
00091 break;
00092 }
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 char* WaspSD::init()
00113 {
00114
00115
00116 if (!isSD())
00117 {
00118 flag = CARD_NOT_PRESENT;
00119 return CARD_NOT_PRESENT_em;
00120 }
00121
00122
00123
00124 if(!sd_raw_init())
00125 {
00126 flag = INIT_FAILED;
00127 return INIT_FAILED_em;
00128 }
00129
00130
00131
00132 partition = partition_open(sd_raw_read,
00133 sd_raw_read_interval,
00134 sd_raw_write,
00135 sd_raw_write_interval,
00136 0
00137 );
00138
00139
00140 if(!partition)
00141 {
00142
00143
00144 partition = partition_open(sd_raw_read,
00145 sd_raw_read_interval,
00146 sd_raw_write,
00147 sd_raw_write_interval,
00148 -1
00149 );
00150
00151
00152 if(!partition)
00153 {
00154 flag = PARTITION_FAILED;
00155 return PARTITION_FAILED_em;
00156 }
00157 }
00158
00159
00160
00161 fs = fat_open(partition);
00162 if(!fs)
00163 {
00164 flag = FILESYSTEM_FAILED;
00165 return FILESYSTEM_FAILED_em;
00166 }
00167
00168
00169
00170 struct fat_dir_entry_struct directory;
00171 fat_get_dir_entry_of_path(fs, "/", &directory);
00172
00173
00174 dd = fat_open_dir(fs, &directory);
00175 if(!dd)
00176 {
00177 flag = ROOT_DIR_FAILED;
00178 return ROOT_DIR_FAILED_em;
00179 }
00180
00181
00182 flag = NOTHING_FAILED;
00183 return NOTHING_FAILED_em;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 void WaspSD::close()
00195 {
00196
00197 fat_close_dir(dd);
00198
00199
00200 fat_close(fs);
00201
00202
00203 partition_close(partition);
00204
00205 pinMode(SD_SS,INPUT);
00206 pinMode(SD_SCK,INPUT);
00207 pinMode(SD_MISO,INPUT);
00208 pinMode(SD_MOSI,INPUT);
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218 uint8_t WaspSD::isSD()
00219 {
00220 if (digitalRead(SD_PRESENT)) return 1;
00221
00222
00223
00224 close();
00225 return 0;
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235 offset_t WaspSD::getDiskSize()
00236 {
00237 struct fat_fs_struct* _fs;
00238 _fs=fs;
00239
00240
00241 if (!isSD())
00242 {
00243 flag = CARD_NOT_PRESENT;
00244 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00245 return 0;
00246 }
00247
00248
00249 diskSize = fat_get_fs_size(_fs);
00250 return diskSize;
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260 offset_t WaspSD::getDiskFree()
00261 {
00262 struct fat_fs_struct* _fs;
00263 _fs = fs;
00264
00265
00266 if (!isSD())
00267 {
00268 flag = CARD_NOT_PRESENT;
00269 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00270 return 0;
00271 }
00272
00273
00274 diskFree = fat_get_fs_free(_fs);
00275 return diskFree;
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 char* WaspSD::print_disk_info()
00308 {
00309 struct fat_fs_struct* _fs;
00310 _fs=fs;
00311
00312
00313 if (!isSD())
00314 {
00315 flag = CARD_NOT_PRESENT;
00316 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00317 return buffer;
00318 }
00319
00320 if(!fs)
00321 return 0;
00322
00323 struct sd_raw_info disk_info;
00324 if(!sd_raw_get_info(&disk_info))
00325 return 0;
00326
00327
00328 diskFree = fat_get_fs_free(_fs);
00329 diskSize = fat_get_fs_size(_fs);
00330
00331 sprintf(buffer, "" \
00332 "manuf: 0x%x\n" \
00333 "oem: %s\n" \
00334 "prod: %s\n" \
00335 "rev: %x\n" \
00336 "serial: 0x%lx\n" \
00337 "date: %u/%u\n" \
00338 "size: %u MB\n" \
00339 "free: %lu/%lu\n" \
00340 "copy: %u\n" \
00341 "wr.pr.: %u/%u\n" \
00342 "format: %u\n",
00343 disk_info.manufacturer,
00344 (char*) disk_info.oem,
00345 (char*) disk_info.product,
00346 disk_info.revision,
00347 disk_info.serial,
00348 disk_info.manufacturing_month,
00349 disk_info.manufacturing_year,
00350 disk_info.capacity / 1024 / 1024,
00351 diskFree>>16,
00352 diskSize>>16,
00353 disk_info.flag_copy,
00354 disk_info.flag_write_protect_temp,
00355 disk_info.flag_write_protect,
00356 disk_info.format);
00357
00358 return buffer;
00359 }
00360
00361 #if FAT_DATETIME_SUPPORT
00362 void WaspSD::get_datetime(uint16_t* year, uint8_t* month, uint8_t* day, uint8_t* hour, uint8_t* min, uint8_t* sec)
00363 {
00364 *year = 2007;
00365 *month = 1;
00366 *day = 1;
00367 *hour = 0;
00368 *min = 0;
00369 *sec = 0;
00370 }
00371 #endif
00372
00373
00374
00375
00376 uint8_t WaspSD::cd(struct fat_dir_entry_struct subdir_entry)
00377 {
00378 struct fat_fs_struct* _fs;
00379 _fs=fs;
00380
00381 struct fat_dir_struct* dd_new = fat_open_dir(_fs, &subdir_entry);
00382 if(dd_new)
00383 {
00384 fat_close_dir(dd);
00385 dd = dd_new;
00386 return 1;
00387 }
00388 return 0;
00389 }
00390
00391
00392
00393
00394 uint8_t WaspSD::cd(const char* command)
00395 {
00396 struct fat_fs_struct* _fs;
00397 _fs=fs;
00398
00399 uint8_t exit=2;
00400 while(exit>0)
00401 {
00402
00403 if (!isSD())
00404 {
00405 flag = CARD_NOT_PRESENT;
00406 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00407 return 0;
00408 }
00409 struct fat_dir_entry_struct subdir_entry;
00410 if(find_file_in_dir(command, &subdir_entry))
00411 {
00412 struct fat_dir_struct* dd_new = fat_open_dir(_fs, &subdir_entry);
00413 if(dd_new)
00414 {
00415 fat_close_dir(dd);
00416 dd = dd_new;
00417 return 1;
00418 }
00419 }
00420 exit--;
00421 if(!exit) return 0;
00422 }
00423 }
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 char* WaspSD::ls(int offset, int scope, uint8_t info = NAMES)
00458 {
00459 struct fat_fs_struct* _fs;
00460 struct fat_dir_struct* _dd;
00461 _fs=fs;
00462 _dd=dd;
00463
00464
00465 if (!isSD())
00466 {
00467 flag = CARD_NOT_PRESENT;
00468 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00469 return buffer;
00470 }
00471
00472
00473
00474 if (scope <= 0) scope = 1000;
00475
00476 flag &= ~(TRUNCATED_DATA);
00477 struct fat_dir_entry_struct dir_entry;
00478
00479
00480 if (offset-- > 0) while(fat_read_dir(_dd, &dir_entry) && offset > 0)
00481 {
00482 offset--;
00483 }
00484
00485
00486 int buff_count = 0, j = 0, buff_size = 30, BUFF_SIZE = 30, dos_buff_left = DOS_BUFFER_SIZE;
00487
00488
00489
00490
00491 while(fat_read_dir(_dd, &dir_entry) && scope > 0)
00492 {
00493 buff_size = BUFF_SIZE;
00494
00495
00496 char line[BUFF_SIZE];
00497
00498
00499 switch (info)
00500 {
00501 case NAMES:
00502 sprintf(line, "%s\n", dir_entry.long_name);
00503 break;
00504 case SIZES:
00505 sprintf(line, "%s%c\t%lu\n", dir_entry.long_name, dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' ', dir_entry.file_size);
00506 break;
00507 case ATTRIBUTES:
00508 sprintf(line, "%c%c %s\t%lu\n", dir_entry.attributes & FAT_ATTRIB_DIR ? 'd' : '-', dir_entry.attributes & FAT_ATTRIB_READONLY ? 'r' : 'w', dir_entry.long_name, dir_entry.file_size);
00509 break;
00510 default:
00511
00512 sprintf(line, "%s\n", dir_entry.long_name);
00513 break;
00514 }
00515
00516
00517 for (j=0; j < BUFF_SIZE; j++)
00518 {
00519 if (j + buff_count < DOS_BUFFER_SIZE - 4)
00520 {
00521 if (line[j] != '\0') buffer[j + buff_count] = line[j];
00522
00523
00524 if (buffer[j + buff_count] == '\n' || line[j] == '\0')
00525 {
00526 buff_size = j + buff_count + 1;
00527
00528 j = BUFF_SIZE;
00529 }
00530 }
00531 else
00532 {
00533
00534
00535 buffer[DOS_BUFFER_SIZE - 4] = '>';
00536 buffer[DOS_BUFFER_SIZE - 3] = '>';
00537 buffer[DOS_BUFFER_SIZE - 2] = '\n';
00538 buffer[DOS_BUFFER_SIZE - 1] = '\0';
00539 flag |= TRUNCATED_DATA;
00540
00541
00542 while(fat_read_dir(_dd, &dir_entry));
00543 return buffer;
00544 }
00545 }
00546 buff_count = buff_size;
00547 scope--;
00548 }
00549
00550
00551 buffer[buff_count] = '\0';
00552
00553
00554 while(fat_read_dir(_dd, &dir_entry));
00555
00556
00557
00558
00559 if (buff_count > 0) return buffer;
00560 return 0;
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597 char* WaspSD::ls(int offset)
00598 {
00599 return ls(offset, 1, SIZES);
00600 }
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629 char* WaspSD::ls(void)
00630 {
00631 return ls(0,0, SIZES);
00632 }
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643 uint8_t WaspSD::find_file_in_dir(const char* name, struct fat_dir_entry_struct* dir_entry)
00644 {
00645 struct fat_dir_struct* _dd;
00646 _dd=dd;
00647
00648 if (!isSD())
00649 {
00650 flag = CARD_NOT_PRESENT;
00651 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00652 return 0;
00653 }
00654
00655 while(fat_read_dir(_dd, dir_entry))
00656 {
00657 if(strcmp(dir_entry->long_name, name) == 0)
00658 {
00659 fat_reset_dir(_dd);
00660 return 1;
00661 }
00662 }
00663
00664 return 0;
00665 }
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677 int8_t WaspSD::isFile(const char* filename)
00678 {
00679 struct fat_dir_entry_struct file_entry;
00680 if(find_file_in_dir(filename,&file_entry))
00681 {
00682 return file_entry.attributes & FAT_ATTRIB_DIR ? 0 : 1;
00683 }
00684 return -1;
00685 }
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696 struct fat_file_struct* WaspSD::openFile(const char* filename)
00697 {
00698
00699 struct fat_fs_struct* _fs;
00700 struct fat_dir_struct* _dd;
00701 _fs=fs;
00702 _dd=dd;
00703 struct fat_file_struct* _fd;
00704 _fd=fd;
00705
00706
00707 if (!isSD())
00708 {
00709 flag = CARD_NOT_PRESENT;
00710 flag |= FILE_OPEN_ERROR;
00711 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00712 return 0;
00713 }
00714
00715 flag &= ~(FILE_OPEN_ERROR);
00716 struct fat_dir_entry_struct file_entry;
00717 if(!find_file_in_dir(filename,&file_entry))
00718 {
00719 flag |= FILE_OPEN_ERROR;
00720 return 0;
00721 }
00722
00723 _fd=fat_open_file(_fs,&file_entry);
00724 if(!_fd) return fat_open_file(_fs,&file_entry);
00725 return _fd;
00726 }
00727
00728
00729
00730
00731
00732
00733
00734 void WaspSD::closeFile (struct fat_file_struct* _fd)
00735 {
00736 fat_close_file(_fd);
00737 }
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747 int32_t WaspSD::getFileSize(const char* name)
00748 {
00749
00750 if (!isSD())
00751 {
00752 flag = CARD_NOT_PRESENT;
00753 flag |= FILE_OPEN_ERROR;
00754 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00755 return -1;
00756 }
00757
00758 flag &= ~(FILE_OPEN_ERROR);
00759 struct fat_dir_entry_struct file_entry;
00760 if(!find_file_in_dir(name,&file_entry))
00761 {
00762 flag |= FILE_OPEN_ERROR;
00763 return -1;
00764 }
00765
00766 return file_entry.file_size;
00767 }
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781 char* WaspSD::getAttributes(const char* name)
00782 {
00783
00784 if (!isSD())
00785 {
00786 flag = CARD_NOT_PRESENT;
00787 flag |= FILE_OPEN_ERROR;
00788 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00789 return buffer;
00790 }
00791
00792 flag &= ~(FILE_OPEN_ERROR);
00793 struct fat_dir_entry_struct file_entry;
00794 if(!find_file_in_dir(name,&file_entry))
00795 {
00796 flag |= FILE_OPEN_ERROR;
00797 return "--";
00798 }
00799
00800 char attrib[2];
00801 sprintf(attrib, "%c%c", file_entry.attributes & FAT_ATTRIB_DIR ? 'd' : '-', file_entry.attributes & FAT_ATTRIB_READONLY ? 'r' : 'w');
00802
00803 return attrib;
00804 }
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832 char* WaspSD::cat (const char* filename, int32_t offset, uint16_t scope)
00833 {
00834 struct fat_file_struct* _fd;
00835 _fd=fd;
00836
00837 if (!isSD())
00838 {
00839 flag = CARD_NOT_PRESENT;
00840 flag |= FILE_OPEN_ERROR;
00841 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00842 return buffer;
00843 }
00844
00845
00846
00847 if (scope <= 0) scope = 1000;
00848
00849 flag &= ~(TRUNCATED_DATA | FILE_OPEN_ERROR);
00850
00851
00852
00853
00854 _fd = openFile(filename);
00855 if(!_fd)
00856 {
00857 sprintf(buffer, "error opening %s", filename);
00858 flag |= FILE_OPEN_ERROR;
00859 return buffer;
00860 }
00861
00862
00863 byte maxBuffer = 10;
00864 uint8_t bufferSD[maxBuffer];
00865 uint32_t cont = 0;
00866
00867
00868 if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
00869 {
00870 sprintf(buffer, "error seeking on: %s\n", filename);
00871 fat_close_file(_fd);
00872 return buffer;
00873 }
00874
00875 uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
00876
00877
00878
00879 while(readRet > 0 && scope > 0 && cont < DOS_BUFFER_SIZE)
00880 {
00881 for(uint8_t i = 0; i < readRet; ++i)
00882 {
00883 buffer[cont++] = bufferSD[i];
00884 scope--;
00885 if( scope <=0 ) break;
00886 if( cont>=DOS_BUFFER_SIZE ) break;
00887 }
00888 if( readRet<maxBuffer ) break;
00889 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
00890 }
00891 if (cont < DOS_BUFFER_SIZE - 1) {
00892 buffer[cont++] = '\0';
00893 }
00894 else
00895 {
00896
00897
00898 buffer[DOS_BUFFER_SIZE - 4] = '>';
00899 buffer[DOS_BUFFER_SIZE - 3] = '>';
00900 buffer[DOS_BUFFER_SIZE - 2] = '\n';
00901 buffer[DOS_BUFFER_SIZE - 1] = '\0';
00902 flag |= TRUNCATED_DATA;
00903 }
00904
00905 fat_close_file(_fd);
00906
00907 return buffer;
00908
00909 }
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937 uint8_t* WaspSD::catBin (const char* filename, int32_t offset, uint16_t scope)
00938 {
00939 struct fat_file_struct* _fd;
00940 _fd=fd;
00941
00942 if (!isSD())
00943 {
00944 flag = CARD_NOT_PRESENT;
00945 flag |= FILE_OPEN_ERROR;
00946 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00947 return bufferBin;
00948 }
00949
00950
00951
00952 if (scope <= 0) scope = 1000;
00953
00954 flag &= ~(TRUNCATED_DATA | FILE_OPEN_ERROR);
00955
00956
00957
00958
00959 _fd = openFile(filename);
00960 if(!_fd)
00961 {
00962 sprintf(buffer, "error opening %s", filename);
00963 flag |= FILE_OPEN_ERROR;
00964 return bufferBin;
00965 }
00966
00967
00968 byte maxBuffer = 1;
00969 uint8_t bufferSD[maxBuffer];
00970 uint32_t cont = 0;
00971
00972
00973 if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
00974 {
00975 sprintf(buffer, "error seeking on: %s\n", filename);
00976 fat_close_file(_fd);
00977 return bufferBin;
00978 }
00979
00980 uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
00981
00982
00983
00984 while(readRet > 0 && scope > 0 && cont < BIN_BUFFER_SIZE)
00985 {
00986 for(uint8_t i = 0; i < maxBuffer; ++i)
00987 {
00988 bufferBin[cont++] = bufferSD[i];
00989 scope--;
00990 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
00991 }
00992 }
00993
00994 fat_close_file(_fd);
00995
00996 return bufferBin;
00997
00998 }
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027 char* WaspSD::catln (const char* filename, uint32_t offset, uint16_t scope)
01028 {
01029 struct fat_file_struct* _fd;
01030 _fd=fd;
01031
01032 if (!isSD())
01033 {
01034 flag = CARD_NOT_PRESENT;
01035 flag |= FILE_OPEN_ERROR;
01036 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01037 return buffer;
01038 }
01039
01040
01041
01042 if (scope <= 0) scope = 1000;
01043
01044 flag &= ~(TRUNCATED_DATA | FILE_OPEN_ERROR);
01045
01046
01047
01048
01049 _fd = openFile(filename);
01050 if(!_fd)
01051 {
01052 sprintf(buffer, "error opening %s", filename);
01053 flag |= FILE_OPEN_ERROR;
01054 return buffer;
01055 }
01056
01057 byte maxBuffer = 1;
01058 uint8_t bufferSD[maxBuffer];
01059 uint32_t cont = 0;
01060
01061
01062 uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01063 if (offset > 0)
01064 {
01065 while( readRet > 0 && offset > 0)
01066 {
01067 for(uint8_t i = 0; i < maxBuffer; ++i)
01068 {
01069 if (bufferSD[i] == '\n')
01070 offset--;
01071 }
01072 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01073 }
01074
01075
01076 for(uint8_t i = 0; i < maxBuffer; ++i)
01077 {
01078 buffer[cont++] = bufferSD[i];
01079 if (bufferSD[i] == '\n')
01080 scope--;
01081 }
01082
01083 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01084 }
01085
01086
01087 while(readRet > 0 && scope > 0 && cont < DOS_BUFFER_SIZE)
01088 {
01089 for(uint8_t i = 0; i < maxBuffer; ++i)
01090 {
01091 buffer[cont++] = bufferSD[i];
01092 if (bufferSD[i] == '\n')
01093 scope--;
01094 }
01095 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01096 }
01097
01098
01099 if (cont < DOS_BUFFER_SIZE - 1) {
01100 buffer[cont++] = '\0';
01101 }
01102 else
01103 {
01104
01105
01106 buffer[DOS_BUFFER_SIZE - 4] = '>';
01107 buffer[DOS_BUFFER_SIZE - 3] = '>';
01108 buffer[DOS_BUFFER_SIZE - 2] = '\n';
01109 buffer[DOS_BUFFER_SIZE - 1] = '\0';
01110 flag |= TRUNCATED_DATA;
01111 }
01112
01113 fat_close_file(_fd);
01114
01115 return buffer;
01116
01117 }
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145 int32_t WaspSD::indexOf (const char* filename, const char* pattern, uint32_t offset)
01146 {
01147 struct fat_file_struct* _fd;
01148 _fd=fd;
01149
01150 if (!isSD())
01151 {
01152 flag = CARD_NOT_PRESENT;
01153 flag |= FILE_OPEN_ERROR;
01154 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01155 return -1;
01156 }
01157
01158 flag &= ~(FILE_OPEN_ERROR);
01159
01160
01161
01162
01163 _fd = openFile(filename);
01164 if(!_fd)
01165 {
01166 sprintf(buffer, "error opening %s", filename);
01167 flag |= FILE_OPEN_ERROR;
01168 return -1;
01169 }
01170
01171 uint32_t limitSize = getFileSize(filename);
01172
01173 uint8_t limitPattern = Utils.sizeOf(pattern);
01174 uint8_t bufferSD[1];
01175 uint8_t cmpPattern[limitPattern];
01176 uint8_t exit = 0, contPattern = 0;
01177 uint32_t cont = 0;
01178
01179
01180 uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01181 if (offset > 0) while(readRet > 0 && offset > 0)
01182 {
01183 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01184 offset--;
01185
01186 }
01187
01188
01189 for(int j=0; j < limitPattern - 1; j++)
01190 if (readRet > 0)
01191 {
01192 cmpPattern[j] = bufferSD[0];
01193 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01194 }
01195 else
01196 exit = 1;
01197
01198
01199
01200
01201
01202 while(readRet > 0 && cont < limitSize && !exit)
01203 {
01204
01205 cmpPattern[limitPattern-1] = bufferSD[0];
01206
01207
01208
01209 if(Utils.strCmp((const char*) pattern, (const char*) cmpPattern, limitPattern) == 0)
01210 exit = 1;
01211
01212
01213 if (!exit)
01214 cont++;
01215
01216
01217 for(int j = 0; j < limitPattern - 1; j++) cmpPattern[j] = cmpPattern[j+1];
01218
01219
01220 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01221 }
01222
01223 fat_close_file(_fd);
01224
01225
01226 if (cont >= limitSize - 1)
01227 return -1;
01228
01229
01230 return cont;
01231
01232 }
01233
01234
01235
01236
01237
01238
01239
01240 uint8_t WaspSD::isDir(struct fat_dir_entry_struct dir_entry)
01241 {
01242 return dir_entry.attributes & FAT_ATTRIB_DIR ? 1 : 0;
01243 }
01244
01245
01246
01247
01248
01249
01250
01251
01252 int8_t WaspSD::isDir(const char* dirname)
01253 {
01254 struct fat_dir_entry_struct dir_entry;
01255 if(find_file_in_dir(dirname,&dir_entry))
01256 {
01257 return dir_entry.attributes & FAT_ATTRIB_DIR ? 1 : 0;
01258 }
01259 return -1;
01260 }
01261
01262
01263
01264
01265
01266
01267
01268 uint8_t WaspSD::delFile(struct fat_dir_entry_struct file_entry)
01269 {
01270 struct fat_fs_struct* _fs;
01271 _fs=fs;
01272 if(fat_delete_file(_fs,&file_entry)) return 1;
01273 return 0;
01274 }
01275
01276
01277
01278
01279
01280
01281
01282
01283 uint8_t WaspSD::delDir(uint8_t depth)
01284 {
01285 uint8_t exit = 1;
01286 struct fat_dir_entry_struct dir_entry;
01287 struct fat_dir_struct* _dd;
01288 struct fat_fs_struct* _fs;
01289 _fs=fs;
01290 _dd = dd;
01291 while(fat_read_dir(_dd, &dir_entry))
01292 {
01293 if (isDir(dir_entry) && !(Utils.strCmp(dir_entry.long_name,".\0",2) == 0 || Utils.strCmp(dir_entry.long_name,"..\0",3) == 0)) exit = 0;
01294 }
01295 if (exit) while(fat_read_dir(_dd, &dir_entry))
01296 {
01297 if (!(Utils.strCmp(dir_entry.long_name,".\0",2) == 0 || Utils.strCmp(dir_entry.long_name,"..\0",3) == 0))
01298 {
01299 exit &= fat_delete_file(_fs, &dir_entry);
01300 }
01301 }
01302 return exit;
01303 }
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321 uint8_t WaspSD::del(const char* name)
01322 {
01323
01324
01325 if (!isSD())
01326 {
01327 flag = CARD_NOT_PRESENT;
01328 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01329 return 0;
01330 }
01331
01332
01333 if (Utils.strCmp(name,".\0",2) == 0)
01334 {
01335 return delDir(0);
01336 }
01337
01338
01339 struct fat_dir_entry_struct file_entry;
01340 if(find_file_in_dir(name,&file_entry))
01341 {
01342 if (isDir(file_entry))
01343 {
01344 if(!(cd(file_entry))) return 0;
01345 uint8_t aux = delDir(0);
01346 if(!(cd(".."))) return 0;
01347 if (aux)
01348 {
01349 aux &= delFile(file_entry);
01350 }
01351 return aux;
01352 }
01353 else
01354 return delFile(file_entry);
01355 }
01356
01357
01358 return 0;
01359 }
01360
01361
01362
01363
01364
01365
01366
01367 uint8_t WaspSD::create(const char* filename)
01368 {
01369 struct fat_dir_struct* _dd;
01370 _dd=dd;
01371
01372 if (!isSD())
01373 {
01374 flag = CARD_NOT_PRESENT;
01375 flag |= FILE_CREATION_ERROR;
01376 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01377 return 0;
01378 }
01379
01380 flag &= ~(FILE_CREATION_ERROR);
01381 struct fat_dir_entry_struct file_entry;
01382 if(!fat_create_file(_dd, filename, &file_entry))
01383 {
01384 flag |= FILE_CREATION_ERROR;
01385 return 0;
01386 }
01387 return 1;
01388 }
01389
01390
01391
01392
01393
01394
01395
01396 uint8_t WaspSD::mkdir(const char* dirname)
01397 {
01398 struct fat_dir_struct* _dd;
01399 _dd=dd;
01400
01401 if (!isSD())
01402 {
01403 flag = CARD_NOT_PRESENT;
01404 flag |= DIR_CREATION_ERROR;
01405 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01406 return 0;
01407 }
01408
01409 flag &= ~(DIR_CREATION_ERROR);
01410 struct fat_dir_entry_struct dir_entry;
01411 if( (find_file_in_dir(dirname,&dir_entry)) || (!fat_create_dir(_dd, dirname, &dir_entry)) )
01412 {
01413 flag |= DIR_CREATION_ERROR;
01414 return 0;
01415 }
01416 return 1;
01417 }
01418
01419
01420
01421
01422
01423
01424
01425 int8_t WaspSD::numFiles()
01426 {
01427
01428 if (!isSD())
01429 {
01430 flag = CARD_NOT_PRESENT;
01431 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01432 return -1;
01433 }
01434
01435 uint8_t cont = -2;
01436 const char* command=".";
01437 struct fat_dir_entry_struct dir_entry;
01438 if(!(cd(command))) cont=0;
01439 while(fat_read_dir(dd, &dir_entry))
01440 {
01441 cont++;
01442 }
01443
01444 return cont;
01445 }
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455 uint8_t WaspSD::append(const char* filename, const char* str)
01456 {
01457 return writeSD(filename, str, getFileSize(filename));
01458 }
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468 uint8_t WaspSD::append(const char* filename, const char* str, uint16_t length)
01469 {
01470 return writeSD(filename, str, getFileSize(filename), length);
01471 }
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482 uint8_t WaspSD::append(const char* filename, uint8_t* str)
01483 {
01484 return writeSD(filename, str, getFileSize(filename));
01485 }
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496 uint8_t WaspSD::appendln(const char* filename, const char* str)
01497 {
01498 uint8_t exit = 0;
01499 exit = append(filename, str);
01500 #ifndef FILESYSTEM_LINUX
01501 if (exit) exit &= append(filename, "\r");
01502 #endif
01503 if (exit) exit &= append(filename, "\n");
01504 return exit;
01505 }
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516 uint8_t WaspSD::appendln(const char* filename, uint8_t* str)
01517 {
01518 uint8_t exit = 0;
01519 exit = append(filename, str);
01520 #ifndef FILESYSTEM_LINUX
01521 if (exit) exit &= append(filename, "\r");
01522 #endif
01523 if (exit) exit &= append(filename, "\n");
01524 return exit;
01525 }
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535 uint8_t WaspSD::writeSD(const char* filename, const char* str, int32_t offset)
01536 {
01537 struct fat_file_struct* _fd;
01538 _fd=fd;
01539
01540 if (!isSD())
01541 {
01542 flag = CARD_NOT_PRESENT;
01543 flag |= FILE_WRITING_ERROR;
01544 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01545 return 0;
01546 }
01547
01548 flag &= ~(FILE_WRITING_ERROR);
01549 uint8_t exit = 0;
01550
01551 _fd = openFile(filename);
01552 if(!_fd)
01553 {
01554 sprintf(buffer, "error opening: %s\n", filename);
01555 exit = 1;
01556 }
01557
01558 if(!exit) if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
01559 {
01560 sprintf(buffer, "error seeking on: %s\n", filename);
01561 fat_close_file(_fd);
01562 exit = 1;
01563 }
01564
01565 uint16_t data_len = Utils.sizeOf(str);
01566
01567
01568 if(!exit) if(fat_write_file(_fd, (uint8_t*) str, data_len) != data_len)
01569 {
01570 sprintf(buffer, "error writing to: %s\n", filename);
01571 fat_close_file(_fd);
01572 exit = 1;
01573 }
01574
01575 if(!exit) fat_close_file(_fd);
01576 else flag |= FILE_WRITING_ERROR;
01577 return !exit;
01578 }
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588 uint8_t WaspSD::writeSD(const char* filename, uint8_t* str, int32_t offset)
01589 {
01590 struct fat_file_struct* _fd;
01591 _fd=fd;
01592
01593 if (!isSD())
01594 {
01595 flag = CARD_NOT_PRESENT;
01596 flag |= FILE_WRITING_ERROR;
01597 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01598 return 0;
01599 }
01600
01601 flag &= ~(FILE_WRITING_ERROR);
01602 uint8_t exit = 0;
01603
01604 _fd = openFile(filename);
01605 if(!_fd)
01606 {
01607 sprintf(buffer, "error opening: %s\n", filename);
01608 exit = 1;
01609 }
01610
01611 if(!exit) if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
01612 {
01613 sprintf(buffer, "error seeking on: %s\n", filename);
01614 fat_close_file(_fd);
01615 exit = 1;
01616 }
01617
01618 uint16_t data_len = 0;
01619 while( (str[data_len]!=0xAA) || (str[data_len+1]!=0xAA) ) data_len++;
01620
01621
01622 if(!exit) if(fat_write_file(_fd, str, data_len) != data_len)
01623 {
01624 sprintf(buffer, "error writing to: %s\n", filename);
01625 fat_close_file(_fd);
01626 exit = 1;
01627 }
01628
01629 if(!exit) fat_close_file(_fd);
01630 else flag |= FILE_WRITING_ERROR;
01631 return !exit;
01632 }
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643 uint8_t WaspSD::writeSD(const char* filename, const char* str, int32_t offset, int16_t length)
01644 {
01645 struct fat_file_struct* _fd;
01646 _fd=fd;
01647
01648 if (!isSD())
01649 {
01650 flag = CARD_NOT_PRESENT;
01651 flag |= FILE_WRITING_ERROR;
01652 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01653 return 0;
01654 }
01655
01656 flag &= ~(FILE_WRITING_ERROR);
01657 uint8_t exit = 0;
01658
01659 _fd = openFile(filename);
01660 if(!_fd)
01661 {
01662 sprintf(buffer, "error opening: %s\n", filename);
01663 exit = 1;
01664 }
01665
01666 if(!exit) if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
01667 {
01668 sprintf(buffer, "error seeking on: %s\n", filename);
01669 fat_close_file(_fd);
01670 exit = 1;
01671 }
01672
01673
01674 if(!exit) if(fat_write_file(_fd, (uint8_t*) str, length) != length)
01675 {
01676 sprintf(buffer, "error writing to: %s\n", filename);
01677 fat_close_file(_fd);
01678 exit = 1;
01679 }
01680
01681 if(!exit) fat_close_file(_fd);
01682 else flag |= FILE_WRITING_ERROR;
01683 return !exit;
01684 }
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696 int32_t WaspSD::numln(const char* filename)
01697 {
01698 struct fat_file_struct* _fd;
01699 _fd=fd;
01700
01701 if (!isSD())
01702 {
01703 flag = CARD_NOT_PRESENT;
01704 flag |= FILE_OPEN_ERROR;
01705 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01706 return -1;
01707 }
01708
01709 flag &= ~(FILE_OPEN_ERROR);
01710
01711
01712
01713
01714 _fd = openFile(filename);
01715 if(!_fd)
01716 {
01717 sprintf(buffer, "error opening %s", filename);
01718 flag |= FILE_OPEN_ERROR;
01719 return -1;
01720 }
01721
01722 byte maxBuffer = 1;
01723 uint8_t bufferSD[maxBuffer];
01724 uint32_t cont = 0;
01725
01726
01727 uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01728 while( readRet > 0)
01729 {
01730 for(uint8_t i = 0; i < maxBuffer; ++i)
01731 {
01732 if (bufferSD[i] == '\n')
01733 cont++;
01734 }
01735 readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01736 }
01737
01738 fat_close_file(_fd);
01739
01740 return cont;
01741
01742 }
01743
01744
01745
01746
01747
01748
01749 uint16_t WaspSD::cleanFlags(void)
01750 {
01751 flag = 0;
01752 return flag;
01753 }
01754
01755
01756
01757
01758
01759
01760 WaspSD SD = WaspSD();
01761
01762