StrToFloat

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Gary Barnes
Posts: 4
Joined: Mon May 11, 2009 12:26 am
Location: Lesmurdie, Western Australia

StrToFloat

Post by Gary Barnes » Sun Dec 06, 2009 6:47 pm

I have been busy developing a 18F2620 project to control my relatively new Zenbot CNC mill and have to say the following things.

1. SF is by far the best pic compiler that I have ever used.
2. Refer to point 1

With reference to point 1, I have the PBP compiler, PDS, Basic18, Hi Tech C and a couple of others and Swordfish so far is the only one that is a proper modern modular basic.
The only other compiler that is as much a pleasure to use is Power Basic for windows, and that doesn't support pics.

PDS is fantastic for any pic that isn't an 18F and is my second choice now.

PicForth is really cool if you like an environment that allows native compilation on the target device at run time, which can have its uses, especially when debugging hardware real time. Beats the pants off simulators and ICD thingies.

I digress, there are only a couple of things that annoy me a little and most of these are easily fixed with function/sub wrappers and aliases.

Some curious choices in the lexicon, for example using the word 'length' to return the length of a string, standard basic syntax is len(astring).
Anyway that is pretty minor, it just means that I have to be a little careful testing code in PowerBasic before cutting and pasting it directly into SF.

It is the first time that I have been able to directly cut and paste from PB into a pic compiler and have it work 90% of the time without any changes.
I am truly impressed.

I could go on, but time doesn't permit.

To make sense of the subject of this post, why isn't there a SrtToFloat in the convert.bas module.
FloattoStr lives in there and it seems inconsistent not to have StrtoFloat. I have found a solution of course but it isn't very elegant.

I have to teach my Zenbot to plot vector fonts now, so I had better finish up.

Just another small digression, I had a lot of fun converting the bresenham line drawing algorithm to SF Basic and a couple of hours afterwards it occurred to me that SF and the Pic are so fast that it might have been a complete waste of time. I can simply use y=mx+b with float variables and an 80 line sub will shrink to about 10 lines.

For those that are interested.

Code: Select all

' Lineblock is defined as the following struct

Structure Quad
    X1    As Integer
    Y1    As Integer
    X2    As Integer
    Y2    As Integer
End Structure

dim lineblock as quad

Sub Drawline()
Dim Diagyinc,diagxinc,shortdistance,straightxinc,straightyinc,straightcount,diagcount As Integer
Dim ax,bx,Cx,dx,di,si As Integer


With LineBlock
    DiagYInc = 0                      'clear some variables
    DiagXInc = 0
    ShortDistance = 0
    StraightXinc = 0
    StraightYinc = 0
    StraightCount = 0
    DiagCount = 0
    Cx = 1                            'initial increments for x-axis
    DX = 1                            'initial increments for y-axis
    DI = Y2 - Y1                      'DI holds distance on y-axis
    
    If DI < 0 Then                    'go ahead if positive slope
       DX = -1                        'y-axis increment is -1
       DI = abs(DI)                   'make DI positive
    End If

    DiagYInc = DX                     'save Diagonal Y Increment

    SI = X2 - X1                      'SI holds distance on x-axis
        
    If SI < 0 Then                    'go ahead if positive slope
       Cx = -1                        'x-axis increment is -1
       SI = abs(SI)                   'make SI positive
    EndIf

    DiagXInc = Cx                     'save Diagonal X Increment

    'determine if straight segments of line are horizontal or vertical
    If SI > DI Then                   'is x-distance greater than y-distance
       DX = 0                         'if not the we have vertical segments
    Else   
       Cx = 0                         'no x-axis change during segments
       Swap(DI,SI)                    'swap SI and DI
    EndIf   

    ShortDistance = DI                'DI contains short distance
    StraightXinc = Cx                 'save Straight X Increment
    StraightYinc = DX                 'save Straight Y Increment
    AX = DI * 2                       'double short distance
    StraightCount = AX                'save for straight loop
    BX = AX - SI                      'save for loop counter
    AX = AX - 2 * SI                  'AX = 2 * DI - 2 * SI
    DiagCount = AX                    'save it for loop
    Cx = X1                           'starting x coordinate
    DX = Y1                           'starting y coordinate
    Inc(SI,1)                         'increase long axis by 1 for endpoint

While SI > 0
    Dec(SI,1)                         'decrement counter for long distance

    Destination.Xpos = Cx
    Destination.Ypos = DX

    DoMove                             ' or plot(x'y) or whatever takes your fancy
    If SI > 0 Then                    'are we done with long axis yet
       If BX < 0 Then 
          Inc(Cx,StraightXinc)           'add increments for straight segment
          Inc(DX,StraightYinc)
          Inc(BX,StraightCount)          'add to adjustment factor
       Else
          Inc(Cx,DiagXInc)               'add increments for diagonal segments
          Inc(DX,DiagYInc)
          Inc(BX,DiagCount)              'subtract from adjustment factor
       EndIf   
    EndIf       
Wend
End With
End Sub

Another tip for those that are interested.
If you are looking for ways to do stuff in basic with integer math (for speed etc) get hold of a copy of ethan winers PDQ and his various DOS basic products. They come with source code and it is dead easy to convert them to SF.
I have found them to be an invaluable reference for graphic stuff, menus and such for LCD use. Not to mention that it is all tightly coded and well commented.


:D
Nothing succeeds like a Budgie

Raistlin
Registered User
Registered User
Posts: 69
Joined: Tue Apr 01, 2008 1:13 pm

Post by Raistlin » Sun Dec 06, 2009 8:17 pm

Hello Gary
I noticed your from Lesmurdie and it rang a bell so I had a look on google maps and it's because when I spent a while in WA I was staying with a friend in cloverdale near middleton park , so not far off :) small world.

I wrote a breshenham algo for my home brew mill ages back in PDS you might be interested in, I will try to dig out the code.
It used integer math but I also wrote it in 3D with the Z axis as part of the algorithm so not only did it interpolate in X/Y but also Z which meant I could send the mill head to any point in its 3D space in one command should be real easy to convert to SF too.
If you can read this you are too close

Gary Barnes
Posts: 4
Joined: Mon May 11, 2009 12:26 am
Location: Lesmurdie, Western Australia

Post by Gary Barnes » Sun Dec 06, 2009 9:40 pm

Hi Raistlin,
Thanks for your reply :)
You are right, Lesmurdie isn't too far from Cloverdale.
I'll look forward to your 3D bresenham algorithm.
Cheers
Nothing succeeds like a Budgie

Post Reply