Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
djuseeq authored Apr 4, 2019
1 parent eb16934 commit 9cac701
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
33 changes: 21 additions & 12 deletions Ch376msc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* Created on: Feb 25, 2019
* Author: György Kovács
*
*
*/

#include "Ch376msc.h"
Expand All @@ -18,6 +20,7 @@ Ch376msc::Ch376msc(HardwareSerial &usb,uint32_t speed) { // @suppress("Class mem
_tmpReturn = 0;
_mediaStatus = true;// true because of init
_answer = 0;
_sectorCounter = 0;
_controllerReady = false;
_fileWrite = false;
fileProcesSTM = REQUEST;
Expand All @@ -31,6 +34,7 @@ Ch376msc::Ch376msc(Stream &sUsb) { // @suppress("Class members should be properl
_tmpReturn = 0;
_mediaStatus = false;// false because of init
_answer = 0;
_sectorCounter = 0;
_controllerReady = false;
_fileWrite = false;
fileProcesSTM = REQUEST;
Expand Down Expand Up @@ -191,7 +195,7 @@ void Ch376msc::writeFatData(){// see fat info table under next filename
sendCommand(CMD_WR_OFS_DATA);
_comPort->write((uint8_t)0x00);
_comPort->write(32);
for(byte d = 0;d < 32; d++){
for(uint8_t d = 0;d < 32; d++){
_comPort->write(fatInfBuffer[d]);
//address++;
}
Expand All @@ -209,6 +213,7 @@ uint8_t Ch376msc::fileClose(){ // 0x00 - frissites nelkul, 0x01 adatmeret frissi
_filename[0] = '\0'; // put NULL char at the first place in a name string
//_filesize.value = 0;
_fileWrite = false;
_sectorCounter = 0;
return adatUsbTol();
}
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -293,10 +298,11 @@ uint8_t Ch376msc::listDir(){
}
//////////////////////////// Read cycle//////////////////////////
uint8_t Ch376msc::readFile(char* buffer, uint8_t b_num){ //buffer for reading, buffer size
uint8_t byteForRequest ;
bool bufferFull = false;
_fileWrite = false;
b_num--;// last byte is reserved for NULL terminating character
_tmpReturn = 0; // more data
bool bufferFull = false;
_byteCounter = 0;
if(_answer == ANSW_ERR_FILE_CLOSE || _answer == ANSW_ERR_MISS_FILE){
bufferFull = true;
Expand All @@ -306,8 +312,16 @@ uint8_t Ch376msc::readFile(char* buffer, uint8_t b_num){ //buffer for reading, b

switch (fileProcesSTM) {
case REQUEST:
byteForRequest = b_num - _byteCounter;
if(_sectorCounter == 512){ //if one sector has read out
_sectorCounter = 0;
fileProcesSTM = NEXT;
break;
} else if((_sectorCounter + byteForRequest) > 512){
byteForRequest = 512 - _sectorCounter;
}
////////////////
_answer = reqByteRead(b_num - _byteCounter);
_answer = reqByteRead(byteForRequest);
if(_answer == ANSW_USB_INT_DISK_READ){
fileProcesSTM = READWRITE;
_tmpReturn = 1; //we have not reached the EOF
Expand All @@ -317,21 +331,16 @@ uint8_t Ch376msc::readFile(char* buffer, uint8_t b_num){ //buffer for reading, b
}
break;
case READWRITE:
readDataToBuff(buffer); //fillup the buffer
_sectorCounter += readDataToBuff(buffer); //fillup the buffer
if(_byteCounter != b_num) {
fileProcesSTM = NEXT;
fileProcesSTM = REQUEST;
} else {
fileProcesSTM = DONE;
}
break;
case NEXT:
_answer = byteRdGo();
if(_answer == ANSW_USB_INT_SUCCESS){
fileProcesSTM = DONE;
} else if(_byteCounter != (b_num) ){
fileProcesSTM = READWRITE;
//fileReadSTM = REQUESTRD;
}
fileProcesSTM = REQUEST;
break;
case DONE:
fileProcesSTM = REQUEST;
Expand Down Expand Up @@ -405,7 +414,7 @@ void Ch376msc::rdUsbData(){
uint8_t fatInfBuffer[32]; //temporary buffer for raw file FAT info
sendCommand(CMD_RD_USB_DATA0);
uint8_t adatHossz = adatUsbTol();/// ALWAYS 32 byte, otherwise kaboom
for(byte s =0;s < adatHossz;s++){
for(uint8_t s =0;s < adatHossz;s++){
fatInfBuffer[s] = adatUsbTol();// fillup temp buffer
}
memcpy ( &_fileData, &fatInfBuffer, sizeof(fatInfBuffer) ); //copy raw data to structured variable
Expand Down
3 changes: 2 additions & 1 deletion Ch376msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class Ch376msc {

uint8_t _byteCounter; //vital variable for proper reading,writing
uint8_t _answer; //a CH jelenlegi statusza INTERRUPT
uint8_t _tmpReturn;
uint8_t _tmpReturn;// variable to hold temporary data
uint16_t _sectorCounter;// variable for proper reading
uint32_t _ul_oldMillis; // e.g. timeout

HardwareSerial* _comPortHW; // Serial interface
Expand Down
7 changes: 5 additions & 2 deletions examples/basicUsageHwSerial/basicUsageHwSerial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ char adat[]="Vivamus nec nisl molestie, blandit diam vel, varius mi. Fusce luctu
char adat2[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis efficitur ac est eu pharetra. \n";
//..............................................................................................................................
byte tmpCommand; //used to store data coming from serial port
boolean readMore;


void setup() {
Expand Down Expand Up @@ -68,9 +69,11 @@ void loop() {
printInfo("COMMAND3: Read File: TEST1.TXT"); // Read the contents of this file on the USB disk, and display contents in the Serial Monitor
flashDrive.setFileName("TEST1.TXT"); //set the file name
flashDrive.fileOpen(); //open the file
readMore = true;
//read data from flash drive until we reach EOF
while(flashDrive.readFile(adatBuffer, sizeof(adatBuffer))){ // our temporary buffer where we read data from flash drive and the size of that buffer
Serial.print(adatBuffer); //print the contents of the temporary buffer
while(readMore){ // our temporary buffer where we read data from flash drive and the size of that buffer
readMore = flashDrive.readFile(adatBuffer, sizeof(adatBuffer));
Serial.print(adatBuffer); //print the contents of the temporary buffer
}
flashDrive.fileClose(); //at the end, close the file
printInfo("Done!");
Expand Down
7 changes: 5 additions & 2 deletions examples/basicUsageSoftSerial/basicUsageSoftSerial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ char adat[]="Vivamus nec nisl molestie, blandit diam vel, varius mi. Fusce luctu
char adat2[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis efficitur ac est eu pharetra. \n";
//..............................................................................................................................
byte tmpCommand; //used to store data coming from serial port
boolean readMore;


void setup() {
Expand Down Expand Up @@ -71,9 +72,11 @@ void loop() {
printInfo("COMMAND3: Read File: TEST1.TXT"); // Read the contents of this file on the USB disk, and display contents in the Serial Monitor
flashDrive.setFileName("TEST1.TXT"); //set the file name
flashDrive.fileOpen(); //open the file
readMore = true;
//read data from flash drive until we reach EOF
while(flashDrive.readFile(adatBuffer, sizeof(adatBuffer))){ // our temporary buffer where we read data from flash drive and the size of that buffer
Serial.print(adatBuffer); //print the contents of the temporary buffer
while(readMore){ // our temporary buffer where we read data from flash drive and the size of that buffer
readMore = flashDrive.readFile(adatBuffer, sizeof(adatBuffer));
Serial.print(adatBuffer); //print the contents of the temporary buffer
}
flashDrive.fileClose(); //at the end, close the file
printInfo("Done!");
Expand Down

0 comments on commit 9cac701

Please sign in to comment.