From Swordfish Wiki

SwordfishUser: SDFileSystemSampleCode1

Sample Code 1 - Demo of most commands in module

Device = 18F4620
Clock =  40
Config OSC = HSPLL

// Uses USART, SDFileSystem, Convert and String libraries...
#option SD_SPI = MSSP
#option SD_SPI_SPEED = spiOscDiv4
#option SD_SUPPORT_SUB_DIRECTORIES = True
#option USART_BRGH = True
#option USART_BRG16 = True
Include "USART.bas"
Include "SDFileSystem.bas"
Include "String.bas"
Include "Convert.bas"

Const ClockDiv = _clock * 1000000

Dim MenuOption As String
Dim Timer As TMR1L.AsWord                         // Alias to Timer1
Dim TimerOn As T1CON.Booleans(0)                  // Start and stop
Dim TimerInterruptsEnabled As PIE1.Booleans(0)    // Enable interrupts
Dim Counter As LongWord
Dim CounterUpper As Counter.Word1

// Timer interrupt handler - every time Timer1 overflows, we
// need to increment the upper word of our big 32 bit counter...
Interrupt OnTimer()
   Inc(CounterUpper)
   PIR1.0 = 0
End Interrupt

Sub TimerStart()
   Timer = 0                              // Clear timer one
   CounterUpper = 0                       // Clear upper count value
   TimerOn = True                         // Start timer
End Sub

Sub TimerStop()
   TimerOn = False                        // Timer off
   Counter.Word0 = Timer                  // Save whats left in timer one
End Sub

// New Line
Sub NewLine()
   USART.Write(13,10)
End Sub

// Display line
Sub DisplayLine(pString As String)
Dim Spaces As Byte
   USART.Write("*  ", pString)
   Spaces = 70 - Length(pString)
   Repeat
      USART.Write(" ")
      Dec(Spaces)
   Until Spaces = 0
   USART.Write("*")
   NewLine()  
End Sub

// Separator line
Sub SeparatorLine()
Dim Spaces As Byte
   Spaces = 74
   Repeat
      USART.Write("*")
      Dec(Spaces)
   Until Spaces = 0
   NewLine  
End Sub

// Display menu
Sub Menu()
   DisplayLine("SDFileSystem Test Program Menu - Select Option:")
   SeparatorLine()
   DisplayLine("A: Init          I: OpenFile...   Q: DiskMounted   Y: Format")
   DisplayLine("B: QuickFormat   J: DeleteFile... R: DiskSizeKB    Z: SerialNumber")
   DisplayLine("C: Dir           K: FileExists... S: FreeSpaceKB   1: MemoryType")
   DisplayLine("D: ChDir...      L: DirExists...  T: IsOpen        2: FATType")
   DisplayLine("E: ChDir(Root)   M: MkDir...      U: IsRootDir     3: DestroyFormat")
   DisplayLine("F: ChDir(Up)     N: RmDir...      V: RWError       4: FileCount")
   DisplayLine("G: NewFile...    O: Rename...     W: NewFile(1MB)  5: DirCount")
   DisplayLine("H: AppendFile... P: DiskFull      X: SaveFile...")
   SeparatorLine()
End Sub

// Display response for Byte type responses
Sub DisplayResponse(pResponse As Byte)
   Select pResponse
      Case errOK
         DisplayLine(">: Response: errOK")
      Case errDiskFull                                              
         DisplayLine(">: Response: errDiskFull")
      Case errNotFound
         DisplayLine(">: Response: errNotFound")
      Case errRootDirFull
         DisplayLine(">: Response: errRootDirFull")
      Case errDirNotEmpty
         DisplayLine(">: Response: errDirNotEmpty")
      Case errExists
         DisplayLine(">: Response: errExists")
      Case errInUse
         DisplayLine(">: Response: errInUse")
      Case errRWError
         DisplayLine(">: Response: errRWError")
      Case errFileNotOpen
         DisplayLine(">: Response: errFileNotOpen")
      Case errBeyondEOF
         DisplayLine(">: Response: errBeyondEOF")
      Case errNoResponse
         DisplayLine(">: Response: errNoResponse")
      Case errInvalidFormat
         DisplayLine(">: Response: errInvalidFormat")
      Case errInvalidPath
         DisplayLine(">: Response: errInvalidPath")
   EndSelect
   SeparatorLine()
End Sub

// Display response for Boolean type responses
Sub DisplayResponse(pResponseB As Boolean)
   Select pResponseB
      Case True
         DisplayLine(">: Response: True")
      Case False                                              
         DisplayLine(">: Response: False")
   EndSelect
   SeparatorLine()
End Sub

// Display time
Sub DisplayTime()
Dim Time As Float
   Time = 4 * Counter / ClockDiv
   DisplayLine(">: Time Taken: " + FloatToStr(Time,4,3) + " sec")
   SeparatorLine()                     
End Sub

Function GetName(pPrompt As String) As String * 24
   DisplayLine("?: " + pPrompt + ":")
   SeparatorLine()
   USART.Read(GetName)
End Function

Sub SDCode(pMenuOption As String)   
Dim Index As LongWord
Dim Name As String
Dim NameNew As String
Dim FileName As String(13)
Dim Response As Byte
   If pMenuOption <> " " Then
      SeparatorLine()
      DisplayLine(">: Option: " + pMenuOption)
   EndIf  
   SeparatorLine()
   Select pMenuOption
      Case " "   // Menu
         Menu()
      Case "A"   // Init
         TimerStart()
         DisplayResponse(SD.Init())
         TimerStop()
         DisplayTime()         
      Case "B"   // QuickFormat
         TimerStart()
         DisplayResponse(SD.QuickFormat)
         TimerStop()
         DisplayTime()
      Case "C"   // Dir
         DisplayLine(">: Directory of SD/MMC:")
         TimerStart()
         FileName = SD.Dir(dirFirst, sdFile)
         While FileName <> Null
            DisplayLine(">: " + FileName)
            FileName = SD.Dir(dirNext, sdFile)
         Wend
         FileName = SD.Dir(dirFirst, sdDirectory)         
         While FileName <> Null
            DisplayLine(">: " + FileName + "      <DIR>")
            FileName = SD.Dir(dirNext, sdDirectory)
         Wend
         TimerStop()
         DisplayLine(">: " + DecToStr(SD.FileCount, 2) + " File(s)")
         DisplayLine(">: " + DecToStr(SD.DirCount, 2) + " Directory(s)")        
         SeparatorLine()
         DisplayTime()
      Case "D"   // ChDir
         Name = GetName("Enter Directory Name")
         TimerStart()
         DisplayResponse(SD.ChDir(Name))
         TimerStop()
         DisplayTime()         
      Case "E"   // ChDir(Root)
         TimerStart()
         DisplayResponse(SD.ChDir(cdRoot))
         TimerStop()
         DisplayTime()         
      Case "F"   // ChDir(Up)
         TimerStart()
         DisplayResponse(SD.ChDir(cdUp))
         TimerStop()
         DisplayTime()         
      Case "G"   // NewFile
         Name = GetName("NewFile - Enter File Name")
         Response = SD.NewFile(Name)
         DisplayResponse(Response)
         If Response = errOK Then
            Index = 1
            TimerStart()
            Repeat
               SD.Write("SDFileSystemTest - Section 1 - Line ",DecToStr(Index,4),13,10)
               Inc(Index)
            Until Index > 300
            SD.CloseFile
            TimerStop()
            DisplayLine(">: File Closed")
            SeparatorLine()
            DisplayTime()         
         EndIf
      Case "H"   // AppendFile
         Name = GetName("AppendFile - Enter File Name")
         Response = SD.AppendFile(Name)
         DisplayResponse(Response)
         If Response = errOK Then
            Index = 1
            TimerStart()
            Repeat
               SD.Write("SDFileSystemTest - Section 2 - Line ",DecToStr(Index,4),13,10)
               Inc(Index)
            Until Index > 300
            SD.CloseFile()
            TimerStop()
            DisplayLine(">: File Closed")
            SeparatorLine()
            DisplayTime()
         EndIf      
      Case "I"   // OpenFile
         Name = GetName("OpenFile - Enter File Name")
         Response = SD.OpenFile(Name)
         DisplayResponse(Response)
         If Response = errOK Then
            NewLine()
            TimerStart()
            Repeat
               USART.Write(SD.ReadByte())
            Until SD.EOF
            SD.CloseFile
            TimerStop()
            NewLine()
            SeparatorLine()
            DisplayLine(">: File Closed")
            SeparatorLine()
            DisplayTime()
         EndIf     
      Case "J"   // DeleteFile
         Name = GetName("DeleteFile - Enter File Name")
         TimerStart()
         DisplayResponse(SD.DeleteFile(Name))
         TimerStop()
         DisplayTime()         
      Case "K"   // FileExists
         Name = GetName("FileExists - Enter File Name")
         TimerStart()
         DisplayResponse(SD.FileExists(Name))
         TimerStop()
         DisplayTime()         
      Case "L"   // DirExists
         Name = GetName("DirExists - Enter Directory Name")
         TimerStart()
         DisplayResponse(SD.DirExists(Name))
         TimerStop()
         DisplayTime()         
      Case "M"   // MkDir
         Name = GetName("MkDir - Enter Directory Name")
         TimerStart()
         DisplayResponse(SD.MkDir(Name))
         TimerStop()
         DisplayTime()         
      Case "N"   // RmDir
         Name = GetName("RmDir - Enter Directory Name")
         TimerStart()
         DisplayResponse(SD.RmDir(Name))        
         TimerStop()
         DisplayTime()         
      Case "O"   // Rename
         Name = GetName("Rename - Enter Old File Name")
         NameNew = GetName("Rename - Enter New File Name")
         TimerStart()
         DisplayResponse(SD.Rename(Name, NameNew, sdFile))     
         TimerStop()
         DisplayTime()         
      Case "P"   // DiskFull
         TimerStart()
         DisplayResponse(SD.DiskFull)
         TimerStop()
         DisplayTime()         
      Case "Q"   // DiskMounted
         TimerStart()
         DisplayResponse(SD.DiskMounted)
         TimerStop()
         DisplayTime()         
      Case "R"   // DiskSizeKB
         TimerStart()
         DisplayLine(">: Disk Size: " + DecToStr(SD.DiskSizeKB()) + " KB")
         TimerStop()
         SeparatorLine()
         DisplayTime()         
      Case "S"   // FreeSpaceKB
         TimerStart()
         DisplayLine(">: Free Space: " + DecToStr(SD.FreeSpaceKB()) + " KB")
         TimerStop()
         SeparatorLine()
         DisplayTime()
      Case "T"   // IsOpen
         TimerStart()
         DisplayResponse(SD.IsOpen)
         TimerStop()
         DisplayTime()         
      Case "U"   // IsRootDir
         TimerStart()
         DisplayResponse(SD.IsRootDir)
         TimerStop()
         DisplayTime()         
      Case "V"   // RWError
         TimerStart()
         DisplayResponse(SD.RWError)        
         TimerStop()
         DisplayTime()         
      Case "W"   // NewFile(1MB)
         Name = GetName("NewFile(1MB) - Enter File Name")
         Response = SD.NewFile(Name)
         DisplayResponse(Response)
         If Response = errOK Then
            Index = 1
            TimerStart()
            Repeat
               SD.Write(".")
               Inc(Index)
            Until Index > 1048576
            SD.CloseFile
            TimerStop()
            DisplayLine(">: File Closed")
            SeparatorLine()
            DisplayTime()         
         EndIf
      Case "X"   // SaveFile
         Name = GetName("SaveFile - Enter File Name")
         Response = SD.NewFile(Name)
         DisplayResponse(Response)
         If Response = errOK Then
            TimerStart()
            Index = 1
            Repeat
               SD.Write("SDFileSystemTest - Section 1 - Line ",DecToStr(Index,4),13,10)
               Inc(Index)
            Until Index > 100
            SD.SaveFile
            Index = 1
            Repeat
               SD.Write("SDFileSystemTest - Section 2 - Line ",DecToStr(Index,4),13,10)
               Inc(Index)
            Until Index > 100
            SD.SaveFile
            Index = 1
            Repeat
               SD.Write("SDFileSystemTest - Section 3 - Line ",DecToStr(Index,4),13,10)
               Inc(Index)
            Until Index > 100
            SD.SaveFile
            Index = 1
            Repeat
               SD.Write("SDFileSystemTest - Section 4 - Line ",DecToStr(Index,4),13,10)
               Inc(Index)
            Until Index > 100
            SD.CloseFile
            TimerStop()
            DisplayLine(">: File Closed")
            SeparatorLine()
            DisplayTime()         
         EndIf
      Case "Y"   // Format
         TimerStart()
         DisplayResponse(SD.Format(1))
         TimerStop()
         DisplayTime()
      Case "Z"   // SerialNumber
         TimerStart()
         DisplayLine(">: SerialNumber: " + HexToStr(SD.SerialNumber()))
         TimerStop()
         SeparatorLine()
         DisplayTime()         
      Case "1"   // MemoryType
         TimerStart()
         Select SD.MemoryType()
            Case sdMMC  Name = "MMC"
            Case sdSD   Name = "SD"
            Case sdSDHC Name = "SDHC"
         EndSelect
         DisplayLine(">: MemoryType: " + Name)
         TimerStop()
         SeparatorLine()
         DisplayTime()         
      Case "2"   // FATType
         TimerStart()
         Select SD.FATType()
            Case 1 Name = "FAT16"
            Case 2 Name = "FAT32"
         EndSelect
         DisplayLine(">: FATType: " + Name)
         TimerStop()
         SeparatorLine()
         DisplayTime()         
      Case "3"   // DestroyFormat
         TimerStart()
         DisplayResponse(SD.DestroyFormat)
         TimerStop()
         DisplayTime()
      Case "4"   // FileCount
         TimerStart()
         DisplayLine(">: FileCount: " + DecToStr(SD.FileCount()))
         TimerStop()
         SeparatorLine()
         DisplayTime()         
      Case "5"   // DirCount
         TimerStart()
         DisplayLine(">: DirCount: " + DecToStr(SD.DirCount()))
         TimerStop()
         SeparatorLine()
         DisplayTime()         
   Else
         DisplayLine(">: Option Not Recognised")
         SeparatorLine()
   EndSelect
End Sub

// Main program...

SetBaudrate(br115200)
USART.ReadTerminator = #13
DelayMS(1000)
TimerInterruptsEnabled = True                     // Enable timer interrupts
Enable(OnTimer)                                   // Assign the interrupt handler

SDCode(" ")                                       // Display menu at start, then only with space
NewLine()

While True
   USART.Read(MenuOption)
   SDCode(Str.Uppercase(MenuOption))
   NewLine()
Wend
Retrieved from https://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.SDFileSystemSampleCode1
Page last modified on March 21, 2008, at 10:52 AM