Can some Please Explain this some more
sam_sam_sam
Posts: 2,286
Methods and Cogs Lab on page· 75
One handy use of this feature allows us to take a value defined by processes in one method and make it available for use by other methods. Our example ButtonBlink.spin uses three methods to demonstrate: [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]Main[/font][/font], [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]Blink[/font][/font], and [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]ButtonTime[/font][/font]. In this application, pressing and then releasing a pushbutton on P23 will cause an LED on P4 to blink 10 times (using the [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]Blink [/font][/font]method), and the blink rate is determined by how long the pushbutton was held down (using the [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]ButtonTime [/font][/font]method).
Is there a way to see the value In the amout of time the button is held down In a DEBUG window
Methods and Cogs Lab on page 76
Specifying Return Values
[font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]Public and private method declarations offer the option to name a return value ([/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]Rvalue [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]in the [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]PUB [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]and [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]PRI [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]syntax definitions in the Propeller Manual). When a return value is specified, it actually just provides an alias to the method’s [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]result [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]variable. This alias name is useful, especially for making the code self-documenting, but it is not required.
Below is a modified version of the [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]ButtonTime [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]method that demonstrates how a return value can be used instead of the [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]result [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]variable. Here, [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]:dt [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]has been added to the method declaration, and the last line now reads [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]dt := t2 [/font][/font][font=Courier New,Courier New][font=Courier New,Courier New]– [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]t1 [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]instead of [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]result := t2 [/font][/font][font=Courier New,Courier New][font=Courier New,Courier New]– [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]t1[/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]. Keep in mind that [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]dt [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]is really just an alias to the [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]result [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]local variable. So, from the method call’s standpoint, this revised method still functions identically to the one in the original ButtonBlink object.
[/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]PUB ButtonTime(pin) : dt | t1, t2 ' Optional return value alias specified
repeat until ina[noparse][[/noparse]pin]
t1 := cnt
repeat while ina[noparse][[/noparse]pin]
t2 := cnt
dt := t2 - t1 ' Value stored by dt is automatically returned Is there a way to see this Vaule By DEBUG it
In the modified version of ButtonBlink, you should see the return value [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]dt [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]included in the Summary and Documentation views. Making a habit of defining return values when declaring methods that will be called inside expressions will make your objects easier to understand and reuse.
[/font][/font]▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·Now wanting to learn Spin· Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
One handy use of this feature allows us to take a value defined by processes in one method and make it available for use by other methods. Our example ButtonBlink.spin uses three methods to demonstrate: [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]Main[/font][/font], [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]Blink[/font][/font], and [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]ButtonTime[/font][/font]. In this application, pressing and then releasing a pushbutton on P23 will cause an LED on P4 to blink 10 times (using the [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]Blink [/font][/font]method), and the blink rate is determined by how long the pushbutton was held down (using the [font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]ButtonTime [/font][/font]method).
Is there a way to see the value In the amout of time the button is held down In a DEBUG window
Methods and Cogs Lab on page 76
Specifying Return Values
[font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]Public and private method declarations offer the option to name a return value ([/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]Rvalue [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]in the [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]PUB [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]and [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]PRI [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]syntax definitions in the Propeller Manual). When a return value is specified, it actually just provides an alias to the method’s [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]result [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]variable. This alias name is useful, especially for making the code self-documenting, but it is not required.
Below is a modified version of the [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]ButtonTime [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]method that demonstrates how a return value can be used instead of the [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]result [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]variable. Here, [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]:dt [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]has been added to the method declaration, and the last line now reads [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]dt := t2 [/font][/font][font=Courier New,Courier New][font=Courier New,Courier New]– [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]t1 [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]instead of [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]result := t2 [/font][/font][font=Courier New,Courier New][font=Courier New,Courier New]– [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]t1[/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]. Keep in mind that [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]dt [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]is really just an alias to the [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]result [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]local variable. So, from the method call’s standpoint, this revised method still functions identically to the one in the original ButtonBlink object.
[/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]PUB ButtonTime(pin) : dt | t1, t2 ' Optional return value alias specified
repeat until ina[noparse][[/noparse]pin]
t1 := cnt
repeat while ina[noparse][[/noparse]pin]
t2 := cnt
dt := t2 - t1 ' Value stored by dt is automatically returned Is there a way to see this Vaule By DEBUG it
-
[/font][/font]
In the modified version of ButtonBlink, you should see the return value [/font][/font][font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]dt [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]included in the Summary and Documentation views. Making a habit of defining return values when declaring methods that will be called inside expressions will make your objects easier to understand and reuse.
[/font][/font]▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·Now wanting to learn Spin· Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Comments
Then inside your method, you could say the following:
I am presuming that your DEBUG code is working inside your Main program, so you could have the DEBUG code display the value of GlobalVariableDT.
Include the following·OBJ. And then the·statements from Debug.start on, in your "method" which would be after the PUB statement...
OBJ
· Debug : "FullDuplexSerial"
Debug.start(31, 30, 0, 19200)
waitcnt(6_000_000 + cnt)
Debug.dec(dt)
...then you would use this along with the Parallax Serial Terminal. That would be your debug window. Have the serial terminal up and running, set to the same com port as your propeller and set to baud 19200. Then run your program and quickly switch to the serial terminal and click on enable. In a moment your value should appear.
This is all explained further along in your book, but a little on what the above does...
OBJ
· Debug : "FullDuplexSerial"
(This includes a program called FullDuplexSerial.spin in your program and renames it to "Debug".)
Debug.start(31, 30, 0, 19200)
(This gets the FullDuplexSerial program started and tells it to use 19200 baud.)
waitcnt(6_000_000 + cnt)
(This adds a delay before sending the data so you will have time to switch to the serial terminal. Otherwise the data would be lost. You can also write your program to EEPROM, switch to the serical terminal, click enable, then press the reset button on the propeller (and not use this delay), then the value would appear instantly.
Debug.dec(dt)
(This displays your value as a decimal value.)
Note. There are different PUB sections in the·FullDuplexSerial.spin file (located in the library - same location as editor). Each if·its PUB sections is a "method" or does something different. So the .start will do one thing, the .dec something else. FullDuplexSerial will do a whole lot more than this. For example you can send terminal control codes like this...
Debug.tx(Debug#CR)
(Sends Carrage Return)
Here are more terminal control codes...
· HOME···· =·· 1
· CRSRXY·· =·· 2
· CRSRLF·· =·· 3
· CRSRRT·· =·· 4
· CRSRUP·· =·· 5
· CRSRDN·· =·· 6
· BELL···· =·· 7
· BKSP···· =·· 8
· TAB····· =·· 9
· LF······ =·· 10
· CLREOL·· =·· 11
· CLRDN··· =·· 12
· CR······ =·· 13
· CRSRX··· =·· 14
· CRSRY··· =·· 15
· CLS····· =·· 16
There is more to this FullDuplexSerial business such as setting the system clock, but I will not include that. That will be in·your book later on. The baud rate above is slow, so should work without a clock setting. If you get garbage at the serial terminal, then include the following·at the top of your program.·This sets the system clock to an "accurate" speed of 5 MHz...
CON
· _xinfreq = 5_000_000····················
· _clkmode = xtal1 + pll1x