PDA

View Full Version : Concise code



ScottL
05-15-2006, 09:36 PM
Well my first project is in danger of running out of RAM on the BS2px, out of variable space and 76% on program storage. Maybe my application is too ambitious. Is there a guide like "The Elements of PBASIC style" to·advise me how to write more concise code? Tips & tricks? -Scott

Chris Savage
05-15-2006, 10:40 PM
Scott,

·· When you say 76% of program space, are you referring to all 8 slots or just slot 0?· In terms of variables, are you making the best use of your variables and re-using them when possible?· Are you declaring the smallest variable for what you need to store?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Lee Harker
05-16-2006, 03:51 AM
Scott,
It may be a bit more likely to get detailed help if you would post an attachment of your code. If there is proprietary info, you could just post the area that is giving you the most trouble.

Lee

Chris Savage
05-16-2006, 03:59 AM
The following article should help.

http://www.parallax.com/dl/docs/cols/nv/vol3/col/nv87.pdf

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Jon Williams
05-16-2006, 07:05 AM
I've only filled one 2K slot in all the years of the BS2 and its siblings, so there may be opportunites to save code -- but we'd have to see it to make specific recommendations.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

ScottL
05-16-2006, 07:18 AM
Thanks Chris,
When you say 76% of program space, are you referring to all 8 slots or just slot 0?· I am using the BS2px, according to the article you referenced, only the 2e,2sx and 2p have this. Does the 2px have this?

In terms of variables, are you making the best use of your variables and re-using them when possible? I removed all references to constants and replaced each occurance with the value. I also removed variables that stored intermediate values. I'll look again.

Are you declaring the smallest variable for what you need to store? I reviewed these last night but I'll look again in daylight. -Scott



p.s. code attached

SSteve
05-16-2006, 11:03 AM
Replacing constants with literal values doesn't save you any variable or code space. It only makes the code harder to understand and much harder to change. You should undo that particular change.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows

Jon Williams
05-16-2006, 11:16 AM
You have a lot of redundant code in the Write_X subroutines -- you might consider offloading those values into tables and then indexing through with an offset.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Chris Savage
05-16-2006, 11:28 AM
ScottL said...(trimmed)
I am using the BS2px, according to the article you referenced, only the 2e,2sx and 2p have this. Does the 2px have this?
I removed all references to constants and replaced each occurance with the value. I also removed variables that stored intermediate values. I'll look again.
Scott,

·· The BS2px has the same memory features as the BS2p, it's just faster and has 2 additional commands.· Otherwise it is a BS2p.· Removing references to constants doesn't save any memory.· You should keep your constants as they're only compiled when referenced in the code and make it cleaner and easier to follow.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Gadgetman
05-16-2006, 06:23 PM
Split the code in two parts:

In Bank 0 you place the main loop and reading sensors.

In Bank 1 you place everything to do with the LCD.

Variables needed by both parts can be placed in Scratchpad RAM.

It'll take a bit of restructuring, but it will allow for easier expansion later, when you decide to add temperatures, liquid-levels, GPS....

Oh, and read up on Multibank programming in Nuts & Volts Column# 87 Multi Bank Programming (http://www.parallax.com/dl/docs/cols/nv/vol3/code/nv87.zip)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't visit my new website...

ScottL
05-16-2006, 08:46 PM
Thanks guys, I really appreciate your help. -Scott