How to alter running code?
T Chap
Posts: 4,223
If I have 4 possible hardware options that might be connected, but all may not be connected if the user chooses not to, is there a way to alter code to remove the IF statements that would attempt to poll the devices?
An example:
PUB Poll
Repeat 'poll devices for a response
if device 1
'something
if device 2
'something
if device 3
'something
if device 4
'something
whereas polling or attempting to poll all 4 would affect the speed and performance.
I am just curious if there is some software equivalent of commenting out the unwanted code, that can be set within the same program by the user. Something like:
PUB SetupDevices
If device1NotPresent
'set Poll to NOT read the device
If device2NotPresent
'set Poll to NOT read the device
If device3NotPresent
'set Poll to NOT read the device
If device4NotPresent
'set Poll to NOT read the device
The result would save the loop having to devote time to each IF. In a case where there are just a few like shown it is not a big deal, but when I have possibly 20 options to poll, the performance takes a hit with the multiple If statements.
An example:
PUB Poll
Repeat 'poll devices for a response
if device 1
'something
if device 2
'something
if device 3
'something
if device 4
'something
whereas polling or attempting to poll all 4 would affect the speed and performance.
I am just curious if there is some software equivalent of commenting out the unwanted code, that can be set within the same program by the user. Something like:
PUB SetupDevices
If device1NotPresent
'set Poll to NOT read the device
If device2NotPresent
'set Poll to NOT read the device
If device3NotPresent
'set Poll to NOT read the device
If device4NotPresent
'set Poll to NOT read the device
The result would save the loop having to devote time to each IF. In a case where there are just a few like shown it is not a big deal, but when I have possibly 20 options to poll, the performance takes a hit with the multiple If statements.
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit the home of pPropQL, pPropQL020 and OMU for the pPropQL/020 at omnibus.uni-freiburg.de/~rp92
If the parameter is a command line PARM then the CASE code uses parm in the CASE code. Ale's method works fine, also.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
JMH
Post Edited (James Michael Huselton) : 5/25/2009 5:01:25 PM GMT
Given that, another way besides the good ones already mentioned would be to use an array of objects and indicies where each object has a method you can invoke depending on the device type. In the case of "missing hardware" the indexed object would be an "NullDevice" object where method does nothing or whatever. Of course that requires using the object boundary violation trick which means all objects must have the same set of methods (and some members loathe), but your code can be greatly simplified (more maintainable).
I've included an example for you (a little more complicated than you need I guess). As you can see in the output below, object 1's function was dynamically altered during program execution. There is no chain of IF or other conditional constructs in the demo ... everything is controlled by repeat for loops and array manipulation.
Hope this is helpful to you or someone else.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
Jazzed, Thanks for that example to study. I am after something something similar. The end user does not have access to a compiler to turn off device scans, and the end user may choose what devices to connect, and these devices can be added later. That being the case, I want to have a menu where the user first manually configures by a menu driven method the actual devices are hooked up, and then the main repeat polling loop scans ONLY the devices that are present, without spending time determining (within each iteration) if the device is present. Using Case or IF to determine if a variable is present that stores the presence or absence of a device is the current method, but this still requires time to read the vars that precede each objects scan. In SPIN multiple layers of checks start affecting the repeat speed dramatically.
In other words:
But these layers of extra IFs add time to the loop and decreased reaction time to reading the present devices.
To my understanding, either Case or IF requires similar time to look at every possible device state (to see if a variable was set to present or missing), meaning that you still have to allocate resources to ALL the possible connected devices, regardless of whether they are present or not. Your concept where the main program on boot checks for devices, and stores a value in an array of what is present or not is great, although in my case, I will have the 'user selected' devices get scanned on boot and a warning issued if the device(s) that should be detected are not.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
links:
My band's website
Our album on the iTunes Music Store
ReadDevice looks for me like you could possibly change the device driver to not wait for your SPIN-code call a read function to retrieve the value, but per default write the next value to a fixed buffer-adress. If the buffer of all the devices look the same (say it's one long for each device) that would be best. For each device that's physically present (or selected by the user) you use the next free long in an array of longs and you increase the number of devices. Then you only have a loop that iterates over all buffer-adresses to see if the driver put a value there. If the loop finds a value you need to branch to your <something> code with a case statement. The gain of speed in this case comes from looping over existing driver-buffers only and the lack of a ReadDevice code.
Oversimplified but the idea suggests that the repeat loop only spends time on devices that are present and not unneccessary IFs or Case statements querrying a list of variables.
Thanks for the suggestions guys, very helpful!
Post Edited (TChapman) : 5/26/2009 4:17:20 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230