📄 formsendcommand.pas
字号:
function TranslateToID(const ID, ItemType: TW_UINT16; Value: String): String;
var
i, j: Integer;
UpperValue: String;
begin
{True/False for boolean}
if ItemType = TWTY_BOOL then
begin
{TRUE is '1' and FALSE '0'}
if Uppercase(Value) = 'TRUE' then Result := '1' else Result := '0';
exit;
end;
{In case not found, return same value}
Result := Value;
{Use search table}
FOR i := low(CapabilityTable) to high(CapabilityTable) do
if CapabilityTable[i].Cap = ID then
begin
UpperValue := Uppercase(Value); {Value in uppercase for speed}
{Match found, look in the new table}
FOR j := 0 TO CapabilityTable[i].Count - 1 DO
if CapabilityTable[i].PTR^[j].STR = UpperValue then
Result := IntToStr(CapabilityTable[i].PTR^[j].ID);
end {if CapabilityTable[i].Cap = Capability};
end;
{Converts string into type and return pointer to new type}
function GetID(const ID, ItemType: TW_UINT16; Value: String): Pointer;
var
ItemSize: Integer;
begin
{Translate constant string to string integer if needed}
Value := TranslateToID(ID, ItemType, Value);
{Allocate memory}
ItemSize := TWTypeSize(ItemType);
Result := AllocMem(ItemSize);
Fillchar(Result^, ItemSize, #0);
{Fill value depending on the item type}
case ItemType of
TWTY_INT8 : pTW_INT8(Result)^ := StrToIntDef(Value, 0);
TWTY_UINT8 : pTW_UINT8(Result)^ := StrToIntDef(Value, 0);
TWTY_INT16 : pTW_INT16(Result)^ := StrToIntDef(Value, 0);
TWTY_UINT16: pTW_UINT16(Result)^ := StrToIntDef(Value, 0);
TWTY_INT32 : pTW_INT32(Result)^ := StrToIntDef(Value, 0);
TWTY_UINT32: pTW_UINT32(Result)^ := StrToIntDef(Value, 0);
{Floating type numbers}
TWTY_FIX32 :
try
pTW_FIX32(Result)^ := FloatToFix32(StrToFloat(Value));
except
pTW_FIX32(Result)^ := FloatToFix32(0);
end;
TWTY_FRAME : begin end;
{Special for text}
TWTY_STR32,
TWTY_STR64,
TWTY_STR128,
TWTY_STR255:
if Length(Value) > ItemSize - 1 then
CopyMemory(Result, @Value[1], ItemSize - 1)
else
CopyMemory(Result, @Value[1], Length(Value))
end;
end;
{Set value for the selected capability}
procedure TSendCommandForm.SetValue;
var
Capability: TTableInfo;
i: Integer;
{For one value}
Value: Pointer;
{For range}
RMin, RStep, RMax, RCurrent: TW_FIX32;
{Used with enumeration and array}
SetList: TSetCapabilityList;
begin
{Obtain current capability}
Capability := CurrentCapability();
{Test the container type}
case TW_UINT16(Combo2.ItemIndex + 3) of
{One value types}
TWON_ONEVALUE:
begin
{Allocate value}
Value := GetID(Capability.ID, CurrentItemType(), SetCombo.Text);
{Set value}
if TestResult(CurSource.SetOneValue(CurrentCapability.ID,
CurrentItemType, Value)) then
{If everything went ok, display message}
ShowMessage(Combo1.Items[Combo1.ItemIndex] + ' set ok as ' +
SetCombo.Text + '.');
{Free memory}
FreeMem(Value);
end {TWON_ONEVALUE};
{Enumeration}
TWON_ENUMERATION:
begin
SetLength(SetList, EnumerationList.Items.Count); {Set list size}
{Build list}
FOR i := 0 TO EnumerationList.Items.Count - 1 DO
SetList[i] := GetID(Capability.ID, CurrentItemType(),
EnumerationList.Items[i]);
{Call method to set}
if TestResult(CurSource.SetEnumerationValue(CurrentCapability.ID,
CurrentItemType, EnumDefaultValue.Position, SetList)) then
ShowMessage(Combo1.Items[Combo1.ItemIndex] + ' enumeration set ok.')
end;
{Array}
TWON_ARRAY:
begin
SetLength(SetList, ArrayList.Items.Count); {Set list size}
{Build list}
FOR i := 0 TO ArrayList.Items.Count - 1 DO
SetList[i] := GetID(Capability.ID, CurrentItemType(),
ArrayList.Items[i]);
{Call method to set}
if TestResult(CurSource.SetArrayValue(CurrentCapability.ID,
CurrentItemType, SetList)) then
ShowMessage(Combo1.Items[Combo1.ItemIndex] + ' array set ok.')
end;
{Range values}
TWON_RANGE:
begin
{Try obtaining values from range}
try
RMin := FloatToFix32(StrToFloat(TranslateToID(Capability.ID,
CurrentItemType(), RangeMin.Text)));
RStep := FloatToFix32(StrToFloat(TranslateToID(Capability.ID,
CurrentItemType(), RangeStep.Text)));
RMax := FloatToFix32(StrToFloat(TranslateToID(Capability.ID,
CurrentItemType(), RangeMax.Text)));
RCurrent := FloatToFix32(StrToFloat(TranslateToID(Capability.ID,
CurrentItemType(), SetCombo.Text)));
except
{In case one or more values could not be converted to integer}
ShowMessage('One or more values from the range are not valid ' +
'integer values.'#13#10#13#10 + 'Tip: Use comma for floating type ' +
'numbers instead of dots');
exit;
end;
{Set value}
if CurrentItemType = TWTY_FIX32 then
if TestResult(CurSource.SetRangeValue(CurrentCapability.ID,
CurrentItemType, pInteger(@RMin)^, pInteger(@RMax)^,
PInteger(@RStep)^, pInteger(@RCurrent)^)) then
{If everything went ok, display message}
ShowMessage(Combo1.Items[Combo1.ItemIndex] + ' range set ok.')
else
if TestResult(CurSource.SetRangeValue(CurrentCapability.ID,
CurrentItemType, Integer(RMin.Whole), Integer(RMax.Whole),
Integer(RStep.Whole), Integer(RCurrent.Whole))) then
{If everything went ok, display message}
ShowMessage(Combo1.Items[Combo1.ItemIndex] + ' range set ok.')
end {TWON_RANGE};
end {case};
{Free pointer list if needed}
FOR i := 0 TO Length(SetList) - 1 DO
FreeMem(SetList[i]);
{Clear all values}
EnumerationList.Items.Clear;
ArrayList.Items.Clear;
EnumDefaultValue.Position := 0;
SetCombo.Text := '';
end;
{Test the result, and display messages in case of error}
function TSendCommandForm.TestResult(Return: TCapabilityRet): Boolean;
const
Messages: Array[TCapabilityRet] of String = ('', 'Unsupported capability.',
'Bad operation performed, not supported.', 'Capability depends on another.',
'Low memory avaliable for this operation.', 'Invalid source or manager ' +
'state to perform operation.', 'Invalid container used for operation.');
begin
{Only successful if value is crSuccess}
Result := (Return = crSuccess);
{Show appropriate message}
if not Result then ShowMessage(Messages[Return]);
end;
{Changing container, updates set page}
procedure TSendCommandForm.ComboSetChange(Sender: TObject);
var
i, j: Integer;
CurCapability: TTableInfo;
begin
{Call method to update the set pages}
UpdateSetPages;
{Clear combo values}
SetCombo.Items.Clear;
SetCombo.Text := '';
CurCapability := CurrentCapability();
{If it's a boolean, add boolean types to combo}
if CurrentItemType = TWTY_BOOL then
begin
SetCombo.Style := csDropDownList;
SetCombo.Items.Add('True');
SetCombo.Items.Add('False');
SetCombo.ItemIndex := 0;
exit;
end;
FOR i := 0 TO High(CapabilityTable) DO
if CapabilityTable[i].Cap = CurCapability.ID then
begin
{Set combo style}
SetCombo.Style := csDropDown;
{Add each item}
FOR j := 0 TO CapabilityTable[i].Count - 1 DO
SetCombo.Items.Add(CapabilityTable[i].PTR^[j].STR);
SetCombo.ItemIndex := 0;
exit;
end;
{No definied constant}
SetCombo.Style := csSimple;
end;
{Update the set pages}
procedure TSendCommandForm.UpdateSetPages;
begin
SetPages.PageIndex := Combo2.ItemIndex;
{Set return type for this capability}
Combo4.ItemIndex := CurrentCapability.DefType;
end;
{Add an item to the array or to the enumeration}
procedure TSendCommandForm.GoAddItem(Sender: TObject);
begin
{First check if there is content in the edit box}
if SetCombo.Text <> '' then
{Add the item to the list being displayed, array or enumeration}
if SetPages.PageIndex = 0 then
ArrayList.Items.Add(SetCombo.Text)
else
EnumerationList.Items.Add(SetCombo.Text)
else ShowMessage('Set some value before trying to add to the list.');
end;
{Removes an item from the array or enumeration}
procedure TSendCommandForm.GoRemoveItem(Sender: TObject);
begin
{Remove item from the list being displayed}
if SetPages.PageIndex = 0 then
if ArrayList.ItemIndex = -1 then
ShowMessage('There is no items selected in the list')
else
ArrayList.Items.Delete(ArrayList.ItemIndex)
else
{Enumeration}
if EnumerationList.ItemIndex = -1 then
ShowMessage('There is no items selected in the list')
else
EnumerationList.Items.Delete(EnumerationList.ItemIndex);
end;
{Changing the mode}
procedure TSendCommandForm.Combo3Change(Sender: TObject);
begin
{If user choose setting a capability value, select TWON_ONEVALUE by default }
if Combo3.ItemIndex = Combo3.Items.Count - 1 then
Combo2.ItemIndex := Combo2.Items.IndexOf('TWON_ONEVALUE');
UpdateSetPages;
end;
{Set the current selected item in the enumeration as default}
procedure TSendCommandForm.SetEnumDefaultClick(Sender: TObject);
begin
if EnumerationList.ItemIndex <> -1 then
EnumDefaultValue.Position := EnumerationList.ItemIndex
else
showmessage('You must select an item in the list first.');
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -