Shop OBEX P1 Docs P2 Docs Learn Events
fastspin Basic RTC — Parallax Forums

fastspin Basic RTC

I thought I would try to do some User I/O, I am using an RTC(DS1302) for this program.

First off I get the error shown below with the program as listed. Next thing I noticed is a lack of a facility for doing "CRLF". I was trying to use "13" and "10", but I did not get very far. I also noticed when using input$(), there is no way to have it work with CR as the end of the input request. The way it is now, if you have a varying size on the input string, it forces you to create multiple 'if … end if' lines of code with the input$() having to match your string input. More to come after I figure out how fix the below lines of code.

For the program below I want to be able to manually(user I/O) set the date and time, if it needs to be updated.

So far when I use any Optimization setting, for this program, it seems to be working as expected.

Ray

|-DS1302_full.spin
|-FullDuplexSerial.spin
G:/fastspin/programs/test5/test5.bas(31) error: syntax error, unexpected end of line
child process exited abnormally
rem test5.bas
'
' April 23, 2019
'


dim rtc as class using "DS1302_full.spin"
dim ser as class using "FullDuplexSerial.spin"


dim day1 as byte
dim month1 as byte
dim year1 as byte
dim dow1 as byte
dim minutes as byte
dim hour as byte
dim seconds as byte
dim dow

dim inBuff as string

rtc.init(10,11,9)
waitcnt(getcnt() + 10_000_000)

' User I/O
do
  print "> ";
  inBuff = input$(10)
  if inBuff = "dtime" then
    datetime()
  else if inBuff = "settime"
    dummy()
  else if inBuff = "setdate"
    dummy()
  else
    print "Do not understand!"
  end if
 ' pausems 3000
loop

' Subroutines and Funtions
sub datetime
  rtc.readDate(@day1, @month1, @year1, @dow1)
  rtc.readTime(@hour,@minutes,@seconds)	
  print month1;"/";day1;"/";"20";year1;"  ";hour;":";minutes;":";seconds
end sub

sub dummy
  print "Empty"
end sub

Comments

  • ersmithersmith Posts: 6,053
    edited 2019-04-23 14:52
    Rsadeika wrote: »
    First off I get the error shown below with the program as listed.
    The "unexpected end of line" is because there is no "then" at the end of the "else if" line. I'll try to figure out how to suggest that, but it's not always easy for the parser to know what the problem is.
    Next thing I noticed is a lack of a facility for doing "CRLF". I was trying to use "13" and "10", but I did not get very far.
    Are you talking about input or output? For output you can just do "print" with nothing else; that will output an end of line. For input, you can use CHR$ or ASC to try to match a character with a specific ASCII value.
    I also noticed when using input$(), there is no way to have it work with CR as the end of the input request.

    Use just plain "input", like:
    dim as string mybuf
    dim as integer year, month, day
    print "enter a string> ";
    input mybuf
    print "you entered: ["; mybuf; "]"
    if mybuf = "setdate" then
      input "enter yyyy,mm,dd"; year, month, day
      print "year="; year; " month="; month; " day="; day
    endif
    

  • Below I put together a little more formal RTC program. The stats:
    No Optimization - 13336 bytes
    Default Optimization - 9136 bytes
    Full Optimization - 8932 bytes

    I would like to add in the CM2302 code, but using the Full Optimization option would probably trash the program, I think. I might have to try it just to see what happens.

    Ray
    rem test5.bas
    '
    ' April 23, 2019
    '
    
    
    dim rtc as class using "DS1302_full.spin"
    dim ser as class using "FullDuplexSerial.spin"
    
    
    dim day1 as byte
    dim month1 as byte
    dim year1 as byte
    dim dow1 as byte
    dim minutes as byte
    dim hour as byte
    dim seconds as byte
    dim dow
    
    dim as string inBuff
    
    rtc.init(10,11,9)
    waitcnt(getcnt() + 10_000_000)
    
    print "Type 'help' for system menu."
    ' User I/O
    do
      print "> ";
      input inBuff
      if inBuff = "dtime" then
        datetime()
      else if inBuff = "setclock" then
        setclock()
      else if inBuff = "help" then
        menu()
      else if inBuff = "quit" then
        print "Program End!"
        exit do
      else
        print "Do not understand!"
      end if
     ' pausems 3000
    loop
    
    ' Subroutines and Funtions
    sub datetime
      rtc.readDate(@day1, @month1, @year1, @dow1)
      rtc.readTime(@hour,@minutes,@seconds)	
      print month1;"/";day1;"/";"20";year1;"  ";hour;":";minutes;":";seconds
    end sub
    
    sub menu
      print "Menu"
      print "help - Show the menu."
      print "dtime - Show date and time."
      print "setclock - Set the date and time."
      print "quit - End the program."
    end sub
    
    sub setclock
      print "Set date and time."
      print "dow = 0 - sun."
      input "year(yy)";year1
      input "month(mm)";month1
      input "day(dd)";day1
      input "dow(x)";dow1
      input "hour(xx)";hour
      input "minutes(xx)";minutes
      input "seconds(xx)";seconds
      rtc.setDatetime(month1,day1,year1,dow1,hour,minutes,00)
    end sub
    
    sub dummy
      print "Empty"
    end sub
    
  • Just curious: why are you including "FullDuplexSerial.spin"? You don't seem to be using it anywhere (nor do you need to, the built-in I/O for BASIC can handle everything). That's one of the reason the optimized code is so much smaller, the optimizer notices that none of the FullDuplexSerial methods are ever used and can get rid of them.
  • @ersmith, the documentation does not have an explanation for the keywords 'input' and 'sub'. I also noticed some of the examples where you list an 'if … loop' while using 'else if', it does not show a 'then'.

    The program below I added the CM2302 code.
    No Optimization - 22636 bytes
    Default Optimization - 15116 bytes Note: The values produced for the temperature are incorrect.
    Full Optimization - 14436 bytes Note: The values produced for the temperature are incorrect.

    As for including "FullDuplexSerial.spin", I really wanted to see how that was handled , in terms of program size. I will probably try pairing this up with a Raspberry Pi, using the COM, for remote access. The COM will be running in its own CPU, not sure how much program memory will be necessary for this.

    Ray
    rem test5.bas
    '
    ' April 23, 2019
    '
    /'
     program to test the RTC, CM2302, ..., with a user I/O faclity.
    '/
    
    
    dim rtc as class using "DS1302_full.spin"
    dim dht22 as class using "DHTnn_Object.spin"
    'dim ser as class using "FullDuplexSerial.spin"
    
    ' CM2302
    
    dim as single temp,humid,status
    
    ' RTC
    dim day1 as byte
    dim month1 as byte
    dim year1 as byte
    dim dow1 as byte
    dim minutes as byte
    dim hour as byte
    dim seconds as byte
    dim dow
    
    dim as string inBuff
    
    dht22.StartDHTnn(2,22,@temp,@humid,@status)
    
    rtc.init(10,11,9)
    waitcnt(getcnt() + 10_000_000)
    
    print "Type 'help' for system menu."
    ' User I/O
    do
      print "> ";
      input inBuff
      if inBuff = "dtime" then
        datetime()
      else if inBuff = "setclock" then
        setclock()
      else if inBuff = "help" then
        menu()
      else if inBuff = "quit" then
        print "Program End!"
        exit do
      else if inBuff = "htemp" then
        htemp()
      else
        print "Do not understand!"
      end if
     ' pausems 3000
    loop
    
    end
    
    ' Subroutines and Funtions
    sub datetime
      rtc.readDate(@day1, @month1, @year1, @dow1)
      rtc.readTime(@hour,@minutes,@seconds)	
      print month1;"/";day1;"/";"20";year1;"  ";hour;":";minutes;":";seconds
    end sub
    
    sub menu
      print "Menu"
      print "help - Show the menu."
      print "dtime - Show date and time."
      print "setclock - Set the date and time."
      print "quit - End the program."
    end sub
    
    sub setclock
      print "Set date and time."
      print "dow = 0-sun,1-mon,2-tue,3-wed,4-thu,5-fri,6-sat"
      input "year(yy)";year1
      input "month(mm)";month1
      input "day(dd)";day1
      input "dow(x)";dow1
      input "hour(xx)";hour
      input "minutes(xx)";minutes
      input "seconds(xx)";seconds
      rtc.setDatetime(month1,day1,year1,dow1,hour,minutes,seconds)
    end sub
    
    sub htemp
      dht22.DHTnn_Read()
      print "Temperature: ";((temp*9.0)/5.0 + 32.0);" *F";"  Humidity: ";humid;" %"
    end sub
    
    sub dummy
      print "Empty"
    end sub
    
  • Rsadeika wrote: »
    @ersmith, the documentation does not have an explanation for the keywords 'input' and 'sub'. I also noticed some of the examples where you list an 'if … loop' while using 'else if', it does not show a 'then'.
    Ah, thanks -- those examples were definitely wrong, and the missing 'sub' is an oversight. There are two different sections for 'input' though (one for the serial I/O version and one for the pin input version).

    Eric
  • I was looking at the DHTnn_Object.spin code and I noticed, in the calculate Temperature and calculate Humidity section, it uses FFloat(), FDiv(), FMul(). Would any of these complicate matters for the fastspin Basic Optimization?

    I wonder, in this case, would some kind of modification of the Spin code would resolve the Optimization problem. But it sounds like it would be creating a whole new set of problems for other Spin objects that you would try to use with fastspin Basic, modifying Spin code objects to run with fastspin Basic.

    Ray
  • For the dhtnn code, there is a line of code:
    dht22.StartDHTnn(2,22,@temp,@humid,@status)
    The 2 is for the pin number, and the 22 is for the model number. I tried to do something like:
    let dhtpin = 2
    let dhtmodel = 22
    dht22.StartDHTnn(dhtpin,dhtmodel,@temp,@humid,@status)
    
    The compiler came back with an error message. I also tried:
    #define dhtpin 2
    #define dhtmodel 22
    
    The compiler did not like that either. Is there a way to have those two items up in the declarations. Or maybe because I am doing this with a user I/O, have some thing that the user types in the pin number and the model number.

    Ray
  • Rsadeika wrote: »
    For the dhtnn code, there is a line of code:
    dht22.StartDHTnn(2,22,@temp,@humid,@status)
    The 2 is for the pin number, and the 22 is for the model number. I tried to do something like:
    let dhtpin = 2
    let dhtmodel = 22
    dht22.StartDHTnn(dhtpin,dhtmodel,@temp,@humid,@status)
    
    The compiler came back with an error message. I also tried:
    #define dhtpin 2
    #define dhtmodel 22
    
    The compiler did not like that either. Is there a way to have those two items up in the declarations. Or maybe because I am doing this with a user I/O, have some thing that the user types in the pin number and the model number.

    Ray

    What error messages did you get? Both of those approaches sound like they should work.
  • Rsadeika wrote: »
    I was looking at the DHTnn_Object.spin code and I noticed, in the calculate Temperature and calculate Humidity section, it uses FFloat(), FDiv(), FMul(). Would any of these complicate matters for the fastspin Basic Optimization?
    No, I don't think so. We're just dealing with a bug in the optimizer. I wish I could track it down, but I don't have any hardware to test with. If you could insert some prints into the code perhaps we could try to figure out where it's going wrong.

  • jmgjmg Posts: 15,173
    Rsadeika wrote: »
    I was looking at the DHTnn_Object.spin code and I noticed, in the calculate Temperature and calculate Humidity section, it uses FFloat(), FDiv(), FMul(). Would any of these complicate matters for the fastspin Basic Optimization?
    ersmith wrote: »
    ... We're just dealing with a bug in the optimizer. I wish I could track it down, but I don't have any hardware to test with.

    Since it seems to be float related, some dummy sensor values should be enough to give a simple fail-test case ?
  • @ersmith, the docs are missing an explanation for 'shared'.

    Something weird is going on, this morning I tried the:
    let dhtpin = 2
    let dhtmodel = 22
    dht22.StartDHTnn(dhtpin,dhtmodel,@temp,@humid,@status) 
    
    and this morning it worked as expected. Is the Win 10 Pro messing around with the spin2gui program?

    As for the DHTnn_Object.spin, to bad we don't have the C version for the dht22, which is in the simpletools lib, for comparison.

    As for debugging, to find the optimization problem, I am not sure as to what I can do.

    Ray
  • kwinnkwinn Posts: 8,697
    Rsadeika wrote: »
    @ersmith, the docs are missing an explanation for 'shared'.

    Something weird is going on, this morning I tried the:
    let dhtpin = 2
    let dhtmodel = 22
    dht22.StartDHTnn(dhtpin,dhtmodel,@temp,@humid,@status) 
    
    and this morning it worked as expected. Is the Win 10 Pro messing around with the spin2gui program?

    As for the DHTnn_Object.spin, to bad we don't have the C version for the dht22, which is in the simpletools lib, for comparison.

    As for debugging, to find the optimization problem, I am not sure as to what I can do.

    Ray

    LOL, drat you Ray, now I have to clean the coffee spray off my laptop and work bench. If true that would be a new bug, even for Windows.
  • The first time that I ran spin2gui, on my Win 10 Pro unit, I was getting a window that was warning me about the program that I was trying to start. In the window it had a 'more' selection, which when pressed, it opened up with more options, one of which read " Run anyway". That occurred quite a few times, now, that warning seems to have disappeared. I wondering, is spin2gui working the way it is supposed to be. Maybe Win 10 Pro has its own optimizer.

    I hope that optimizer problem gets resolved, so I can move on with the program. At the moment if I add another Spin object, like the SD code, that will take me over the top, in the memory area.

    Today, while I was trying out some different debugging things, like using the 'Full optimization' option I was getting:
    error: fit 496 failed: pc is 509
    That almost sounds like a problem with the LMM, but I am not a developer, so I could be way off target.

    Ray
  • Now we are going backwards. I decided to update the spin2gui that I was using. I did not know that there was a newer edition. The latest spin2gui compile of my program did the following. If it is finding problems in the Spin Object, then what?

    Ray
    "F:/fastspin/spin2gui/bin/fastspin" -2 -l -O0 -L "./include" "F:/fastspin/programs/test5/test5.bas"
    Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
    Version 3.9.25 Compiled on: Apr 14 2019
    test5.bas
    |-DS1302_full.spin
    |-DHTnn_Object.spin
    |-|-FloatMath.spin
    F:/fastspin/programs/test5/DHTnn_Object.spin(229) error: syntax error, unexpected identifier `ctra'
    F:/fastspin/programs/test5/DHTnn_Object.spin(234) error: syntax error, unexpected identifier `ctrb'
    F:/fastspin/programs/test5/DHTnn_Object.spin(254) error: syntax error, unexpected identifier `apinmask'
    F:/fastspin/programs/test5/DHTnn_Object.spin(264) error: syntax error, unexpected identifier `apinmask'
    child process exited abnormally

    rem test5.bas
    '
    ' April 23, 2019
    '
    /'
     program to test the RTC, CM2302, ..., with a user I/O faclity.
    '/
    
    
    dim rtc as class using "DS1302_full.spin"
    dim dht22 as class using "DHTnn_Object.spin"
    'dim ser as class using "FullDuplexSerial.spin"
    
    ' CM2302
    let dhtpin = 2     ' Set the pin number for CM2302 module.
    let dhtmodel = 22  ' Set the model number for CM2302 module.
    'dim as single temp,humid,status
    dim as short temp,humid,status
    
    ' RTC
    dim day1 as byte
    dim month1 as byte
    dim year1 as byte
    dim dow1 as byte
    dim minutes as byte
    dim hour as byte
    dim seconds as byte
    dim dow
    
    dim as string inBuff
    
    'dht22.StartDHTnn(2,22,@temp,@humid,@status)
    dht22.StartDHTnn(dhtpin,dhtmodel,@temp,@humid,@status)
    rtc.init(10,11,9)
    waitcnt(getcnt() + 10_000_000)  ' Wait for terminal screen to catch up.
    
    print "Type 'help' for system menu."
    ' User I/O
    do
      print "> ";
      input inBuff
      if inBuff = "dtime" then
        datetime()
      else if inBuff = "setclock" then
        setclock()
      else if inBuff = "help" then
        menu()
      else if inBuff = "quit" then
        print "Program End!"
        exit do
      else if inBuff = "htemp" then
        htemp()
      else
        print "Do not understand!"
      end if
     ' pausems 3000
    loop
    
    end
    
    ' Subroutines and Funtions
    sub datetime
      rtc.readDate(@day1, @month1, @year1, @dow1)
      rtc.readTime(@hour,@minutes,@seconds)	
      print month1;"/";day1;"/";"20";year1;"  ";hour;":";minutes;":";seconds
    end sub
    
    sub menu
      print "Menu"
      print "help - Show the menu."
      print "dtime - Show date and time."
      print "setclock - Set the date and time."
      print "quit - End the program."
    end sub
    
    sub setclock
      print "Set date and time."
      print "dow = 0-sun,1-mon,2-tue,3-wed,4-thu,5-fri,6-sat"
      input "year(yy)";year1
      input "month(mm)";month1
      input "day(dd)";day1
      input "dow(x)";dow1
      input "hour(xx)";hour
      input "minutes(xx)";minutes
      input "seconds(xx)";seconds
      rtc.setDatetime(month1,day1,year1,dow1,hour,minutes,seconds)
    end sub
    
    sub htemp
      print "First temp: ";temp; "  Humidity: ";humid
      dht22.DHTnn_Read()
      print "First read temp: ";temp; "  Humidity: ";humid
      print "Temperature: ";((temp*9.0)/5.0 + 32.0);" *F";"  Humidity: ";humid;" %"
    end sub
    
    sub dummy
      print "Empty"
    end sub
    
  • Sort of a false alarm, in the previous post. When I installed the latest version of spin2gui, I forgot that it defaults to P2 settings. After I switched the settings to P1, then those errors did not appear. The optimization is still not working correctly for my program.

    Ray
Sign In or Register to comment.