Symbol questions

maitchy

Member
I want to use Symbol definitions for neatness (and avoid silly mistakes if I decide to use a different pin or storage word etc). But if I want to sometimes use something like:
Code:
Symbol Handshake = 2
 high Handshake
and other times use instructions that need "pin2" instead of "2". I cannot then use the symbolic name "Handshake". I cannot even define another symbol to be pinHandshake (as far as I can tell). Is there a way to let me define in one place which pin to use and have all the program understand? it would be nice if the compiler accepted pin2 where it accepts a plain 2 (in high/low commands, etc).

Similarly, I often have two byte variables used that must always be the high and low bytes of the same word variable, so if I decide to use different registers everything changes as it should.
So I'd like to be able to program:

Code:
 Symbol  Voltage1 = w2
 Symbol  Exponent = MSB(Voltage1) ' or possibly Exponent = b{Voltage1*2+1}
 Symbol  Mantissa = LSB(Voltage2)  ' or possibly Mantissa = b{Exponent-1} ?
If I change w2 then the byte registers used for Exponent and Mantissa should automatically change.

This would require the compiler to recognize MSB and LSB functions to return the correct byte register symbol for the given word variable name (i.e. no extra code generation), or to allow some sort of expression to decide the byte register number during parsing of the Symbol command. I may end up writing a pre-processor to do this sort of thing (but I'd prefer to find some easy way to achieve it - any ideas??).
 
Last edited:
for the symbol for a pin you would use:

symbol blah = pin2

and for the exponent symbol you can't have maths in the symbol ie. you can't make it equal "symbol exponent = b1*b2" or whatever symbols you are using for the variables. you can however do this:

symbol exponent = b3
symbol voltage1 = b1
symbol msb = b2

[in the middle of a routine and one of the variables have been updated]
gosub calculate
[continue routine]

calculate: 'doing calculation for exponent now
let exponent = voltage1 * msb
return

and every time you update your voltage 1 and msb values just go into the gosub and it will quickly update the values so that exponent is updated and you only use a bit of code.

hope that helps
ak
 

hippy

Ex-Staff (retired)
It's a well known issue with the Compiler and not one which is easy to work around. Something defined as "2" is very different to something defined as "pin2" so they are not interchangeable. It is entirely possible to use two separate definitions ...

- Symbol HANDSHAKE = 2
- Symbol HANDSHAKE_PIN = pin2 ' or = outpin2

Note you have to use 'outpin2' if 'pin2' exists as an output but not also as an input ( eg pin0 on the 08/M, pin2-pin4 on 18/A/X ).

Not being able to refer to the individual bytes of a word variable is a pain and relies on coding Symbols up correctly ....

- Symbol Voltage1 = w2 ' b5:b4
- Symbol Exponent = b5
- Symbol Mantissa = b4

One mistake or a partial ( incorrect ) edit and it all goes bottom over elbow and it's often hard to spot the mistake made.

My preference would be Voltage1.msb and Voltage1.lsb as the CC5X compiler ( and my own compilers ) handle parts of words, It can be taken further and used to reference individual bits - 'Voltage1.15' - even if that is limited to variables defined as b0, b1 and w0.

Allowing the above or equivalent form would be handy in Symbol even if not allowed within other expressions.

The common way round defining something 'pin2' based on a number in other languages ( notably C ) is to use 'macro expansion string concatenation', but we don't have full macros or #Defines with the PICAXE. It should be possible to add a concatenation operator ...

- Symbol HANDSHAKE = 2
- Symbol HANDSHAKE_PIN = pin ## HANDSHAKE

or following the earlier examples of .subThing ...

- Symbol HANDSHAKE_PIN = pin2
- Symbol HANDSHAKE = HANDSHAKE_PIN.number
 
Top