Let's talk in "object oriented" way but we apply a non class based approach in order to be easier to understand.
Let's call our approach "object based" even if this is not 100% correct.
We will write our code in Object Pascal using Lazarus, later we will try to port the project in Linux and MacOSX.
A multimeter has a code and a status.
The code has a 14 byte value and can be valid or not.
From the table of our multimeter we can find the attributes which characterize it's status.
- IsRS232:boolean; //The multimeter can have RS232 on or off
- IsAuto:boolean; //auto can be on or off
- AC_DC_None : TAC_DC_None; //this can have values AC, DC and None
- ValueNoPrefix : string; //Value without prefix
- UnitValue : string; //Unit value : Ohm Hz V A F
- UnitPrefix : string; //Prefix : K,M,n,u,m
- ValuePrefix : string; //Value with prefix
- IsDiode : boolean; //The diode mode is on or off
- IsSound: boolean; //The continuity mode is on or off
- IsRelative : boolean; //The relative sign is on or off
- IsHold : boolean; //The hold sign is on or off
- IsBattery : boolean; //The battery sign is on or off
So the code below is fully understandable :
type
TAC_DC_None = (AC, DC, None);
type
TStatus = record
IsRS232:boolean;
IsAuto:boolean;
AC_DC_None : TAC_DC_None;
ValueNoPrefix : string; //Value without prefix
UnitValue : string; //Ohm Hz V A F
UnitPrefix : string; //K,M,n,u,m
ValuePrefix : string; //Value with prefix
IsDiode : boolean;
IsSound: boolean;
IsRelative : boolean;
IsHold : boolean;
IsBattery : boolean;
end;
type TCode = record
Value : array [0..13] of byte; //14 bytes
IsValid : boolean;
end;
type
TMultimeter = record
Code : TCode;
Status : TStatus;
end;
Notice how easily can program if you define well your working environment !
Pretty easy don't you think ?
Always ask yourself ... does my structure define all of my working environment ?
The answer here is yes this structure defines completely the status of our multimeter.
Notice that when the multimeter shows 2KOhms we have
ValueNoPrefix = 2000
UnitValue = Ohm
UnitPrefix = K
ValuePrefix = 2
So in order to write correctly the value we can do ValueNoPrefix + UnitValue = 2000Ohm
Or alternatively ValuePrefix + UnitPrefix + UnitValue = 2KOhm
We define as : My_Multimeter : TMultimeter;
Once defined our structure we pass at the code validation.
A valid code is 1X 2X 3X 4X 5X 6X 7X 8X 9X AX BX CX DX EX so we ignore the 4 last bits of each byte.
To do so we use the shift right technique : XXXXYYYY shr 4 -> 0000XXXX
function TFormMain.IsValidCode():boolean;
begin
result := false;
if (My_Multimeter.Code.Value[0] shr 4 = 1) and
(My_Multimeter.Code.Value[1] shr 4 = 2) and
(My_Multimeter.Code.Value[2] shr 4 = 3) and
(My_Multimeter.Code.Value[3] shr 4 = 4) and
(My_Multimeter.Code.Value[4] shr 4 = 5) and
(My_Multimeter.Code.Value[5] shr 4 = 6) and
(My_Multimeter.Code.Value[6] shr 4 = 7) and
(My_Multimeter.Code.Value[7] shr 4 = 8) and
(My_Multimeter.Code.Value[8] shr 4 = 9) and
(My_Multimeter.Code.Value[9] shr 4 = 10) and
(My_Multimeter.Code.Value[10] shr 4 = 11) and
(My_Multimeter.Code.Value[11] shr 4 = 12) and
(My_Multimeter.Code.Value[12] shr 4 = 13) and
(My_Multimeter.Code.Value[13] shr 4 = 14) then
result := true;
My_Multimeter.Code.IsValid:=result;
end;