standardisation for code development

toxicmouse

Senior Member
i was wondering if there has been a general agreement or discussion on the development of software for universal use. what i have in mind now is making subroutines for some useful maths functions. if there is a standard which will make it easier for others to interface it with their code, then i may as well follow that.

this may save a lot of legwork in the future and may make it possible to make a maths library.

have there been such discussions?
 

BeanieBots

Moderator
It has been mentioned once or twice before.
No real conclusion but a common theme has gone more along of the lines of where to keep such routines in a 'common' library rather than having to painfully search this forum each time. There are some truly ingeniuos snippets of code buried away here probably never to be seen again, which is a great shame.

As a starter, probably some sort of 'standard' header to indicate variable useage, bytes used and target device(s) as an absolute minimum.
Hopefully, some of the 'professional' software writers will have some good input on this because I think it is an excellent idea that would be of great benefit to many.
 

demonicpicaxeguy

Senior Member
i think rev-ed should have a library section where you can submit code snipets and schematics of circuits involving the picaxe and various other usefull circuit
also being able to comment on code would be another bonus

this would be highly benificial especially as an educational feature and would also encourage more people to use the picaxe

just my $2Au
 

manuka

Senior Member
I'll go along with this, especially as I'm a "header & sidebar" comment fanatic. Program styles however are as unique & personal as music - one mans Hip Hop is another's Beethoven- so some agreed common ground could first be in order! Stan
 

hippy

Technical Support
Staff member
Without auto-allocation of variable names so a library can use variables not used in the end-user's program, local variable naming and a mechanism for code to call a library built into the Programming Editor there are going to be some difficulties in 'doing it properly' or how it would be done in other programming environments.

The PICAXE isn't stack-based and end-users have to define what variables are used where which can make it complex, and especially when one library calls another.

The primary three rules are likely to be -

* Only use Symbol defined named variables in the code so an end-user can edit variables to overcome conflicts ( use those with highest numbers first to reduce occurrences ), and never use an explicit variable name ( eg 'b0' ).

* Subroutine and label names should be chosen to be unlikely to conflict with those used by the end user.

* Make each individual library as small, specific and as standalone as possible to reduce code bloat. #IfDef can be used to only include what is needed. Unfortunately the Programming Editor doesn't support nested #IfDef/#IfNDef at present.

The Programming Editor doesn't, but is intended to, support #Include files, so it's probably best to write each library as self-contained as possible in a separate file which can be cut and pasted now, #included later. For exaple ...<code><pre><font size=2 face='Courier'>#IfNDef LIB_SQUAREROOT_DEFINED
#Define LIB_SQUAREROOT_DEFINED

Symbol Lib_SquareRoot_Arg = w5 ' b11:b10
Symbol Lib_SquareRoot_Guess = b9
Symbol Lib_SquareRoot_Temp = w6 ' b13:b12
Symbol Lib_SquareRoot_Result = b13

Lib_SquareRoot:
Lib_SquareRoot_Guess = 0
Do
Lib_SquareRoot_Guess = Lib_SquareRoot_Guess+1
Lib_SquareRoot_Temp =
Lib_SquareRoot_Guess * Lib_SquareRoot_Guess
Loop Until Lib_SquareRoot_Temp &gt; Lib_SquareRoot_Arg
Lib_SquareRoot_Result = Lib_SquareRoot_Guess-1
Return

#EndIf </font></pre></code> Even with #include and using Symbol to define variables there's a problem if those variables need to be changed to make an end-user program work, as that change may break other code which also uses the library but needs different variable definitions.

I've looked into how to do libraries and there seems to be no easy solution. Even with an external preprocessor it's not simple and there is a lot of work required to do it properly and efficiently - Identifying variables used isn't hard, auto-allocating isn't either, but recycling variables so different libraries can use the same variables is more difficult. It is possible to poke to SFR or scratchpad to create a stack-based environment, providing the end-user program doesn't use them, but it all adds up to having to deal with multiple scenarios.

All mechanisms to add libraries simply on top of what exists run into some problem sooner or later, and the best option at present may be to provide the library as a code snippet ( as per above ) leaving the end-user to cut-paste-and-edit as appropriate.

No matter what the mechanism used, the key to producing a successful and usable library is in defining and documenting exactly what it does, what it expects as input and what it gives as output. It's also good to provide documentation on how a library works and some test program(s) to show how it should be used and to demonstrate it actually does do what it claims. It should also be obvious that a library must do what it says, have been rigorously tested, and any special restrictions, failure cases or issues are documented. All this can take far longer than writing the code.

Edited by - hippy on 26/04/2007 13:43:08
 

benryves

Senior Member
Evidently there are some registers (or rather, variables) that can do some things that others don't; I fully agree that having symbol defined variable names is in most cases the best solution, but if you have a function that needs to manipulate bits via the bitX variables you're a little more limited, and for these cases it might be an idea to agree on what would be &quot;typical&quot; use of variables (eg B0 is the accumulator and so is the typical input or output of a function).

Clear, standardised documentation (&quot;Inputs:&quot;, &quot;Outputs:&quot;, &quot;Destroys:&quot;, &quot;Remarks:&quot;) on each function would probably help to some extent.

Breaking code into modules could also help with name collisions; eg
<code><pre><font size=2 face='Courier'>#Module SomeProtocol

;;Summary: used to count incoming bits.
Symbol BitCount = B13

;;Summary: Sends a byte using SomeProtocol.
;;Inputs: B0 = byte to send.
;;Outputs: None.
;;Destroyed: BitCount.
Send:
; * send a byte *
Return

;;Summary: Receives a byte using SomeProtocol.
;;Inputs: None.
;;Outputs: B0 = received byte.
;;Destroyed: BitCount.
Receive:
; * receive a byte *
Return

#EndModule </font></pre></code>
Called from another file as SomeProtocol.Send and SomeProtocol.Receive; BitCount would be SomeProtocol.BitCount when accessed from outside the module.

On a related note, is it possible to detect which PICAXE 'model' the code is being compiled for, to cater for slight hardware differences?

 
 

hippy

Technical Support
Staff member
Yes, bit variables are a special case / nuisance. Although wasteful and needs care, it is possible to use byte variables for single bit storage.

And yes, it is possible to determine the type of PICAXE the compilation is for ...<code><pre><font size=2 face='Courier'> #IfDef 08m
#error Using 08M
#Else
#error Using non-08M
#EndIf </font></pre></code> Without nested #IfDef's it's a little tortuous and error prone at times though.
 

Jeremy Leach

Senior Member
I've thought about this quite a lot before, and my conclusion is that it's really difficult to set software standards here.

One of the biggest issues I see is that a great deal of the software written for picaxes (especially the 08M) relies on optimisation to get the most out of storage and execution speed - and this doesn't lend itself to standardised coding techniques. So much code is hand-crafted to suit the specific circumstances and application.

There might be more scope on the bigger picaxes though. One thing I'm tending to do more is to make as much use of RAM as possible to store variable values, just keeping the b0 to b13 as a sort of 'scratch pad'. Doing this adds a lot more pokes and peeks, but if you've got the code space and routines aren't too time-critical then it brings a huge benefit of not having to track the use and re-use of the b0-b13 variables all the time. But there's a balance here and it gets silly to try to Poke/Peek everything !
 

toxicmouse

Senior Member
great inputs, thanks.

i doubt rev-ed would be too keen about taking on the responsibility for the libraries. perhaps a simple solution for a location would simply be to put in an unusual word at the top of the code so that a search on this forum for that word would yield all the library posts.

as for the variables, i don't really see a problem here. the user would allocate which bytes are to be used and ensures there would not be conflicts in the rest of the code, by peek-ing if necessary.

 

hippy

Technical Support
Staff member
Also Rev-Ed are planning a Project Showcase sub-site which will allow all PICAXE related things to be put online, and a new forum &quot;Code Examples&quot; could be created in the meantime.

There's no ETA for the showcase site, and I expect Rev-Ed are somewhat pre-occupied with finalising the 14M, 28X1 and 40X1 releases at present ( just a few days now ! ), and the 28X2 and 40X2 are on the radar.
 
Top