Shop OBEX P1 Docs P2 Docs Learn Events
FemtoBasic version for QuickStart board — Parallax Forums

FemtoBasic version for QuickStart board

Mike GreenMike Green Posts: 23,101
edited 2013-01-15 07:25 in Propeller 1
Just for fun ... I got my QuickStart board running today and thought I'd put together a version of FemtoBasic (actually DongleBasic) for it. The only differences from the DongleBasic in the Object Exchange is that the I/O pins for an optional SD card were changed to 12-15 and a BUTTONS statement was added to allow use of the touch buttons. This just calls the Touch Buttons object from the demo program for the QuickStart board. Look at the description in the included QSBasicDocs file.

Updated QSBasic archive to include I2C statement and its documentation.
Updated QSBasic archive. See comments at beginning of main file.
«1

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-20 15:22
    Here's a nice sample program:
    100 for i = 0 to 255
    110 outa[23..16] = i
    120 pause 100
    130 next i
    140 goto 100
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-20 20:52
    Another example:
    100 a = 0
    105 rem display 8 bit binary number
    110 outa[23..16] = a
    120 a = a + 1 : rem on to next number
    125 rem check for button 7
    130 if (buttons & %10000000) <> 0 then gosub 200
    140 pause 100
    150 if a < 256 then goto 110 : rem reset at 256
    160 goto 100
    190 rem wait for all buttons to be released
    200 if buttons <> 0 then goto 200
    205 rem wait for button 7 to be pressed again
    210 if (buttons & %10000000) = 0 then goto 210
    215 rem wait for all buttons to be released
    220 if buttons <> 0 then goto 220
    225 rem back to main loop
    230 return
    
  • cavelambcavelamb Posts: 720
    edited 2012-03-20 21:51
    Bless you, Mike!

    I've not played with any of the alternate languages yet.
    This will be the first.
    Looks like Tiny BASIC on the Propeller.

    It will be a week or more before I get the chance.
    I'm closing on the new house tomorrow and then moving again.
    Out of this apartment and into a real house! Yea!
    So as soon as I find my toys and get set up again I'll be on it.

    If you do requests, I'd love to see a real While-wend.
    And labels instead of line numbers.

    But if not, I'm ok with primitive BASIC.

    What's the performance like?

    How do you get it into the Prop?

    Is the source code available???

    Will you respect me in the morning???
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-21 06:49
    Performance is slow. Maybe 20 to 50 statements a second. If you need speed, you can add Spin or assembly code to the interpreter as a new statement. I've used it for testing new hardware. There's a version for a Propeller BoeBot with statements to control servos, a PING, some IR distance sensors, etc. That performance is more than adequate to experiment with a simple autonomous robot that avoids obstacles.

    You load it into the Prop like any other program. There's a pre-compiled binary in the .ZIP file that you can use with the Propeller Tool or BST.

    Source code? Look in the .ZIP file. It's intended for people to modify. Look in the Object Exchange for other versions for specific hardware with extensions for that hardware.
  • cavelambcavelamb Posts: 720
    edited 2012-03-21 06:55
    Thank you, Mike.
    When I get back up I'll give it a Spin! :)
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2012-03-21 08:23
    @cavelamb,

    Mike's Femtobasic is the bomb! Not only is the project very cool, but his source code is an excellent way to learn Spin if you tend to learn by example. Lots of good stuff there.

    OBC
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-23 21:43
    Here's another coding example. It does the KickStart demonstration for the L3G4200D Gyroscope. Follow the description of the hookup of the Gyroscope except connect the SCL pin to I/O pin 26 and the SDA pin to I/O pin 27. This is different from what's shown in the KickStart demonstration. You can use this with the PropBOE as well as long as you change to other I/O pins. For example, use I/O pin 0 for SCL and I/O pin 1 for SDA, then change all the 26s in this code to 0s.
    100 I2C [26,$d2,$22,1]=$08
    110 I2C [26,$d2,$23,1]=$80
    120 I2C [26,$d2,$20,1]=$1F
    130 IF (I2C [26,$d2,$27,1] &$08)=0 THEN GOTO 130
    140 x = I2C [26,$d2,$A8,2]
    150 y = I2C [26,$d2,$AA,2]
    160 z = I2C [26,$d2,$AC,2]
    170 IF x & $8000 THEN x = x | $FFFF0000
    180 IF y & $8000 THEN y = y | $FFFF0000
    190 IF z & $8000 THEN z = z | $FFFF0000
    200 PRINT "X=";x/114;" Y=";y/114;" Z=";z/114
    210 PAUSE 1000
    220 GOTO 100
    
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2012-03-23 22:10
    Hi Oldbitcollector;

    FemtoBasic is also useful for developing complicated software code that will eventually be converted to spin code.

    Here is an example of what I mean:

    I was developing a new command to add to FemtoBasic that:
    1. prints numbers in any number base from 2 to 36.
    2 I wanted to specify how many characters this number will occupy
    3. I wanted to specify where number separators were placed.

    I first made it work by writing it in Femto. OK, a bit slow but it worked nicely.
    Just cut and paste it into Femto and see how it works. Femto is much easier to test and edit than Spin.

    I then translated the Femto code into spin. Ok use repeat instead of for/next. But it's really easy to do. The code snipit is then added to my Dongle basic as a new command called "PRINTA".
                202: ' PRINTA number, base, number of characters, separator flag ' Number Type as Arbitrary Number Base
    	         ' NOTE! this routine only works with positive numbers at this time. $0000_0000 -> $7fff_ffff
    	       a := expr  ' long number
    	       skipspaces
    	       i := expr  ' number base
    	       skipspaces
    	       b := expr  ' number of printed character places
    	       skipspaces
    	       c := expr  ' serarator places
                   repeat d from 0 to b - 1
    	          e := a // i + 48
    		  if e > 57
    		     e := e + 7
                      byte[d + @f1] := e
    		  a := a / i
                   e := 0
    	       repeat d from b - 1 to 0
    	          if (byte[d + @f1] == 48) & (e == 0)
    		     ser.tx( space )  ' " "
    	          if (byte[d + @f1] <> 48) & (e == 0)
    		     e := 1
    		  if e <> 0
    		     ser.tx( byte[d + @f1] )
    		  if (c <> 0) & (d // c == 0) & (d <> 0) & (e <> 0)
    		     ser.tx( 95 )  ' "_"
    		  if (c <> 0) & (d // c == 0) & (d <> 0) & (e == 0)
    		     ser.tx( space )  ' " "
    {{
    NEW
    10 REM A=Number, I=Base, B=Number of Characters, C=Separator Flag
    20 REM D=For   , E=Scratch
    30   INPUT "#,Base,Characters,Flag A,I,B,C ? ";A,I,B,C
    210  FOR D = 0 TO B-1
    230    E = A // I + 48
    240    IF E > 57 THEN E = E + 7
    250    BYTE[D + $4D40] = E
    260    A=A / I
    280  NEXT D
    310  E=0
    320  FOR D = B-1 TO 0 STEP -1
    330    IF (BYTE[D+$4D40] =48) & (E=0) THEN DISPLAY 32:' Display Leading Spaces
    340    IF (BYTE[D+$4D40]<>48) & (E=0) THEN E=1:' Found the First Non "0"
    350    IF  E<>0 THEN DISPLAY BYTE[D+$4D40]:' Display Characters
    360    IF (C<>0) & (D//C = 0) & (D<>0) & (E<>0) THEN DISPLAY 95:' Display Separators
    370    IF (C<>0) & (D//C = 0) & (D<>0) & (E =0) THEN DISPLAY 32:' But only after First Non "0"
    380  NEXT D
    390  PRINT""
    900 GOTO 10
    RUN
    }}
    

    Duane J
  • HumanoidoHumanoido Posts: 5,770
    edited 2012-03-24 07:39
    Thanks Mike, this is an excellent and very useful version of FemtoBASIC.
    Programming examples are always helpful.
    Thanks for including the data and resources.
    The language is included in the following updated Propeller list.
    http://humanoidolabs.blogspot.com/2012/03/ultimate-list-of-big-brain-languages.html
    ULTIMATE LIST BIG BRAIN PROGRAMMING LANGUAGES
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-24 08:41
    Humanoido,
    Please do not add this or the PropBOE version of FemtoBasic to your list. It's very misleading. They are not languages. They are trivially reconfigured versions of the same language and same language interpreter. If the Propeller Tool supported compile-time conditionals like BST does, these would all be produced from the same source file. I wanted to keep them supported by the Propeller Tool, so they're not.
  • bsnutbsnut Posts: 521
    edited 2012-03-24 10:44
    Mike,

    You make me feel old by showing the first programming language that I learned back in the 80's and it's cool to see it again. I will add FemtoBasic to the programs that I currently have and continue to show examples, because it will help others learn something different.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-24 18:19
    Updated QSBasic. The I2C statement changed to use 100KHz bus speed because of occasional errors with non-memory devices.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-24 19:38
    Here's the KickStart example for the HMC5883L Compass translated into Basic for either the QuickStart board or the Propeller Board of Education. Remember that SCL is expected to be connected to I/O pin 0 and SDA is expected to be connected to I/O pin 1:
    100 I2C[0,$3C,$02,1] = 0 : REM Set compass to continuous output mode
    110 x = I2C[0,$3C,$03,2] : REM Read x value MSB first
    120 y = I2C[0,$3C,$05,2] : REM Read y value MSB first
    130 z = I2C[0,$3C,$07,2] : REM Read z value MSB first
    140 x = (x shr 8) | ((x & $FF) shl 8) : REM swap bytes
    150 y = (y shr 8) | ((y & $FF) shl 8)
    160 z = (z shr 8) | ((z & $FF) shl 8) : REM now sign extend
    170 if (x & $8000) = $8000 then x = x | $FFFF0000
    180 if (y & $8000) = $8000 then y = y | $FFFF0000
    190 if (z & $8000) = $8000 then z = z | $FFFF0000
    200 PRINT "X= ";x;" Y= ";y;" Z= ";z
    210 pause 1000
    220 goto 100
    
  • HumanoidoHumanoido Posts: 5,770
    edited 2012-03-25 05:28
    Mike Green wrote: »
    Humanoido,
    Please do not add this or the PropBOE version of FemtoBasic to your list. It's very misleading. They are not languages. They are trivially reconfigured versions of the same language and same language interpreter. If the Propeller Tool supported compile-time conditionals like BST does, these would all be produced from the same source file. I wanted to keep them supported by the Propeller Tool, so they're not.

    Mike, thanks for your note. I have added a notes section to the list and a note at the top and near the bottom of the blog indicating the list includes languages and their versions. I hope this clears up any confusion the list may have caused in the past.

    The old list at the Parallax site was suspended (Do to a problem posting to a post of extended length and/or the inability to find time to update the redundancy of two competing lists). The new updated and reformatted list is here.

    It's useful being able to reference a list to find a single language and a specific version of that language which runs on board A or board B or board C... Your updates to the same language, with references to the same source file, are valuable and useful contributions towards making the same language work with different flavored boards.
  • skylightskylight Posts: 1,915
    edited 2012-12-05 05:50
    Hi Mike, I have been having real fun with FemtoBasic using the Quickstart Board and Oldbitcollectors adapted version (2.004) that runs with PropTerminal as I havn't got the monitor side of things working just yet.
    I just tried the example you provided in post #3 but hit a problem with a syntax error with line 130, this I realise now is because the adapted version that Jeff created isn't the latest version of FemtoBasic which has the BUTTONS command implemented.

    I wouldn't have a clue as to how to adapt the latest version to run with PropTerminal and was wondering if you could kindly help with how it could be done as I'd really like to start playing with the quickstart buttons using FemtoBasic with PropTerminal

    Thanks
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-05 08:25
    I don't have a link to OBC's version. If you'll post one, I'll see if I can add the BUTTONS statement from this version. It's more work to go the other way.
  • skylightskylight Posts: 1,915
    edited 2012-12-07 00:14
    Hi Mike, Sorry for the delay in replying to you, the link on Oldbitcollecters site to the version of FemtoBasic he adapted is here:

    http://insonix.ch/propeller/objects/PropTerminal_0.4.zip

    This zip file contains the PropTerminal program and the version of FemtoBasic that Jeff adapted is located here:

    http://dl.dropbox.com/u/7557533/shared/FemtoBasic_Propterminal.zip

    Many thanks for your help in this
  • softconsoftcon Posts: 217
    edited 2012-12-07 06:52
    Ahh, thank you for this. I'd seen this basic, and thought I could get it to work on the qs board, but hadn't tried. Now because I procrastenated, it's already done. *yay*
    My kids might actually use this one.
    Now all I need to do is buy an sd card for the reader I have.
    Thanks again for this, it should be fun to play with it. Both of my quickstart boards are both configured as fm radios, and it's going to take some doing to convince either my son or myself to use one of them for other things (or I could just buy another one) :)
    I'd grab one at the rs nearby that sells them, but they want 43 bucks for one, even with shipping, and wait time from parallax, I don't think the mark-up on that one is worth it. <sigh>
    It'd be ok for the bs2 hw board though, they sell it for the same price, and that can't get here from parallax w/shipping charges for less than that, so when I need one of those, I do get them there.
    Nice to have the ability. :)
    Anyway, this will be a useful toy for a while.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2012-12-07 07:12
    Yeah, I really should revise that version of Propterminal Femtobasic with Mike's revisions. It's been a long time since I looked at that.
    I'll put this on my todo list. I really love Propterminal, (here's an article I wrote on it. as it's all the power of the PMC project without having to have more than a Quickstart. BTW, I've still got a few of the Quickstart/Servo&More combo kits left here.

    Jeff
  • skylightskylight Posts: 1,915
    edited 2012-12-08 11:28
    Thanks Jeff, you guys are amazing ,looking through the code and how extensive it is I am in awe as to where you begun on such a project and how much work it must have taken to get so far.
    Keep up the good work guys you are an inspiration to beginners like myself that maybe one day may aspire to something like this.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-08 12:53
    Modifying something like FemtoBasic isn't quite as daunting as you might think. Take a look at something like the BUTTONS statement. It only takes a couple of lines of code to add. The existing code takes care of evaluating expressions for you. If your new "whatever" produces a single value, you can write it like a function and let the existing code take care of storing the result somewhere.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2012-12-08 12:56
    @Skylight,

    I learned a LOT of my initial spin coding from following Mike's examples in Femtobasic. Mike is right on the money, it's only appears intimidating on the surface. I'd challenge you to dig into the code and as just add simple command that puts something back on the screen once you issue it. You'll get really hooked in a hurry. I did. :)

    Jeff
  • skylightskylight Posts: 1,915
    edited 2012-12-09 15:12
    Mike Green wrote: »
    Modifying something like FemtoBasic isn't quite as daunting as you might think. Take a look at something like the BUTTONS statement. It only takes a couple of lines of code to add. The existing code takes care of evaluating expressions for you. If your new "whatever" produces a single value, you can write it like a function and let the existing code take care of storing the result somewhere.
    Ha Ha that's a bit like Leonardo da Vinci asking his cook to have a go at painting the Mona Lisa but i'll give it a go even though i'm a complete beginner at this spin stuff.
    First thing I believe I need is to add the touch buttons object from the obex and then look at how jeff added those extra basic commands,
    I notice there are "tok byte" before each new command the last one jeff added was 65 so I need something like:

    tok 66 byte "buttons",0

    and add a tok 66 in the toks section like:

    @tok66

    Can it be as simple as that? or is there more I need to put elsewhere?
  • skylightskylight Posts: 1,915
    edited 2012-12-10 16:11
    I've not been having any luck trying to add a buttons command to FemtoBasic so after reading post #1 again I downloaded DongleBasic to see if I could work out how Mike had added the buttons command and now i'm really confused as after scouring through it over and over I cannot locate it in DongleBasic Is it the latest version in the Obex?
    or perhaps Mike hasn't got around to adding the buttons command just yet?
  • AribaAriba Posts: 2,690
    edited 2012-12-10 17:51
    Here is a PropTerminal FemtoBasic with the Buttons command. I just replaced the KEYCODE command, which was commented out, with the BUTTONS command.
    All I had to do was including the BB_TouchButtons object in the OBJ section, added the button.start methode in Main, renamed the "KEYCODE" token to "BUTTONS", and then copied the Button-token-code from the QSBasic version to the right token in the "factor" methode.
    You need the BB_TouchButtons.spin from the ZIP in the first post in addition to the attached Spin file.

    Have fun.

    Andy
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-10 21:41
    @skylight: You pretty much have it. You add a zero-byte terminated string to the toknn table like 'tok66 byte "buttons",0'. The order of these doesn't really matter since everything goes through the 'toks' address table. The '@tok66' entry has to come at the end and these have to be in numeric order, not so much that the name 'tok66' matters, but the keywords are numbered internally by the position of their address in this table. Items 0..66 in this table become tokens 128+0..128+66 in the interpreter code.

    Once you've added the string and the address to the tables, the keyword will be recognized by the interpreter. There's a big CASE statement for the Basic statements and you'll need to add an entry for the new keyword number if the keyword is used to begin a statement. Use some simple statement as a model. In the case of BUTTONS, this keyword is used as a function and there's another big CASE statement in the 'factor' method for simple functions. The code there just RETURNs the value of the function.

    @Ariba: Thanks Andy
  • skylightskylight Posts: 1,915
    edited 2012-12-11 07:19
    Thank you both It works!!
    I notice Andy mentioned that he replaced KEYCODE with BUTTONS where I was trying to add another line

    The bits I added were:

    added the touchbuttons obj

    tok66 byte "BUTTONS",0

    in the tok section added @tok66

    and in the pri factor | tok, t, i section

    added 178: 'BUTTONS
    return buttons.state

    this produces a "syntax error in line 10" when running femtobasic program:
    10 if (buttons & %10000000) then print "Hello World" 
    20 pause 200
    30 goto 10
    
    running Andys version it works perfectly so perhaps by adding a command rather than replacing the KEYCODE command( or another) I had to add some more code elsewhere?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-11 07:25
    The case that you add to pri factor has to have the right number. If you're adding token #66, the case number has to be 128+66 = 194, not 178. Case number 178 is token #50 (128+50 = 178) which is LET.
  • AribaAriba Posts: 2,690
    edited 2012-12-11 08:18
    And don't forget to start the Button object with button.start(clkfreq/100) somewhere at begin of the main methode, otherwise the button.state never will return the buttons.

    Andy
  • skylightskylight Posts: 1,915
    edited 2012-12-11 12:54
    Thanks Mike and Andy,
    With your last couple of posts I'm starting to get the hang of it somewhat, It shows how far behind I am with spin but I'll get there eventually.
    I understand now how Andy used tok 49 and got 177(128+49) and put the button.start there from Mike's post #27, I'm not understanding from reading the code quite where the 128 bit comes from (something to do with the keyword position in the table?) that's a bit over my head at the moment as is most of the femtobasic code, but i'm sort of understanding bits of it here and there, the next thing is to try what you said and add a simple command to do something although I'm not sure what at the moment.

    I think a nice addition would be a BEEP or SOUND command , although I'm not sure if that is too ambitious just yet, any ideas for something simple I could attempt that's not too daunting for a Spin beginner?
Sign In or Register to comment.