Adaptation of libraries from Swordfish for Firewing

Discuss the Firewing language

Adaptation of libraries from Swordfish for Firewing

Postby demanik » Sun Apr 06, 2014 9:33 am

Prompt as it is correct to redo read and write procedures a string from Swordfish EEPROM library

Code: Select all
sub ReadItem(byref pStr as string)
   dim StrPtr as POSTINC0
   FSR0 = @pStr
   do
      StrPtr = ReadByte(Address)
   loop until char(EEDATA) = null
end sub

sub WriteItem(pValue as string)
   dim StrPtr as POSTINC0
   FSR0 = @pValue
   do
      WriteByte(Address,StrPtr)
   loop until char(EEDATA) = null
end sub

thanks
Image
User avatar
demanik
 
Posts: 56
Joined: Sat Aug 17, 2013 5:53 pm
Location: Ukraine

Re: Adaptation of libraries from Swordfish for Firewing

Postby David John Barker » Sun Apr 06, 2014 10:49 am

You could use something like
Code: Select all
sub ReadItem(byref pStr as string)
   dim StrPtr as POSTINC0
   FSR0 = addressof(pStr)
   do
      StrPtr = ReadByte(Address)
   loop until cchar(EEDATA) = nothing
end sub

sub WriteItem(pValue as string)
   dim StrPtr as POSTINC0
   FSR0 = addressof(pValue)
   do
      WriteByte(Address,StrPtr)
   loop until cchar(EEDATA) = nothing
end sub

However, when using Firewing it's a good idea to get used to using "generic" pointer references. For example, you could write the code like this:
Code: Select all
Sub ReadItem(ByRef text As String)
   addr0 = AddressOf(text)
   Do
      *(addr0+) = ReadByte(Address)
   Loop Until CChar(EEDATA) = Nothing
End Sub

Sub WriteItem(text As String)
   addr0 = AddressOf(text)
   Do
      WriteByte(Address,*(addr0+))
   Loop Until CChar(EEDATA) = Nothing
End Sub

Note the use of "addr0". This is a portable pointer reference which will work with PIC18, PIC24 and PIC32 targets. All of the Firewing libraries use this technique so they can be built using 8, 16 or 32 bit compilers. For example, if you look at the "strings" module (click on the library in the explorer window) you will see something like this:
Code: Select all
Public Function Len(ByVal value As String) As Byte
   addr0 = AddressOf(value)
   Len = 0
   While *(addr0+) <> 0
      Len += 1
   End While
End Function

which determines the length of a given string. If the above function had used "FSR0" and "POSTINC0" it could only ever be built for PIC18 devices. There is more of a discussion here:

http://www.firewing.info/forum/viewtopic.php?f=7&t=39&p=190
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 5 guests

x