In CASE of mystery
Erlend
Posts: 612
in Propeller 1
Dear Forum, I have gone code-blind. What is wrong with below CASE section? - the compiler stops at 64 with "Expected and instruction or variable"
Erlend
PUB Gain(gainSel) | bGain, regbyte CASE gainSel 128: bGain:= 7 'gain selected - in dB 64: bGain:= 6 32: bGain:= 5 16: bGain:= 4 8: bGain:= 3 4: bGain:= 2 2: bGain:= 1 1: bGain:= 0 OTHER: RETURN FALSE regByte:= i2c.ReadByteA8(ChipAddr, CTRL1_CRP) & !bGAINselCTRL1 'blank out the old Gain bits i2c.WriteByteA8(ChipAddr, PWR_CTRL, regByte | bGain) 'write byte with new lower 3 bits representing Gain Recal 'always do re-cal after RETURN bGain
Erlend
Comments
Since the outcome is a single variable update, I suggest you replace case with this line: (corrected) IF gainSel is in the list you'll get 0..7 back, otherwise you'll get 0 -- this is exactly what your case is doing.
I like this solution a lot. I have several of these parameter list situations, and not all of them are regular like this one.
Erlend
Now I have read the manual (again) and gotten to know that LOOKUP(Z) takes an index and returns the corresponding number in the list, whereas LOOKDOWN(Z) takes a number and returns its index in the list. I need the latter. Thanks @JonnyMac and @Wuerfel_21