gusucode.com > vnt工具箱matlab源码程序 > vnt/vnt/+can/Utility.m
classdef (Hidden) Utility % Utility Supporting functionality for toolbox operations. % % This class implements internal functionality required for toolbox % operation. These methods are not intended for external use. % Authors: JDP % Copyright 2007-2015 The MathWorks, Inc. methods (Static) function dt = calculateInitialTimestamp(offset) % calculateInitialTimestamp Makes a datetime out of a seconds offset. % % This function is used to convert the seconds offset from a channel % start time as received from asyncio to a MATLAB datetime object of % the current time and locale. % Make a datetime object at the base time specified by the tamutil % timestamp from asyncio. dt = datetime(1970, 1, 1, 0, 0, 0); dt.TimeZone = 'UTC'; % Add the time offset in seconds. dt = dt + seconds(offset); % Adjust the timezone of the object to the current time % zone of the system. dt.TimeZone = datetime().SystemTimeZone; end function key = deriveIDKey(message) % deriveIDKey Makes a string key value from a message ID. % % This function is used to turn a numeric ID and Extended flag % into a string value, generally use as a key value for maps. The % string is the numeric ID with an 'x' on the end for extended % identifiers. % Turn the numeric ID into a string. key = num2str(message.ID); % Add the postfix onto the string if it is an extended ID. if message.Extended key = [key 'x']; end end function [code, mask] = filterCalc(id, idType) % filterCalc Calculates filter settings for a set of identifiers. % % This function is used interally to perform the calculations % necessary to determine the CODE and MASK filter settings for % a given ID or set of IDs of the specified IDTYPE. % Cast the input IDs as uint32 for proper bit handling and to % enable use of the MATLAB bit functions. id = uint32(id); % If only one ID is provided, the code and mask can be directly set. % There is no need to calculate them. Just set them and return. if numel(id) == 1 % For one ID, code is equal to the exact bit field of the ID. code = id; % For one ID, mask is simply all 1s to indicate that every bit % of the field is relevant. mask = uint32(hex2dec('FFFFFFFF')); % Trim the unused bits. zeroBits(); return; end % Bit complement the IDs. This is done to find the common % positions in each ID where the values are 0. idcmp = bitcmp(id); % Calculating the code and mask for 2 or more IDs involves % constantly ANDing each ID value together with the ongoing % result. First, we do the AND on the first 2 IDs as well % as AND the bit complement of the first 2 IDs together. runningCode = bitand(id(1), id(2)); runningbandcmp = bitand(idcmp(1), idcmp(2)); % Loop through each remaining ID to AND it with the running % result for the regular ID and bit complemented value too. for ii = 3:numel(id) runningCode = bitand(runningCode, id(ii)); runningbandcmp = bitand(runningbandcmp, idcmp(ii)); end % Set the code. code = runningCode; % Calculate the mask by ORing together the code along with % the ANDing of the one's complement of the IDs. This sets % a 1 in every bit position of the mask where all IDs have % either a 1 or a 0 in the same position. mask = bitor(code, runningbandcmp); % Trim the unused bits. zeroBits(); function zeroBits() % zeroBits Clears the unused bits from the code and mask. % % This function is used locally to zero out to preceding, % unused bits of the code and mask that are still part of % the uint32 bit field. For standard IDs, the function will % zero the first 21 bits. For extended IDs, it will zero out % just the first 3 bits. % Adjust the code and mask values according to the type of ID for % which the values are being calculated. switch lower(idType) case 'standard' code = bitand(code, uint32(hex2dec('7FF'))); mask = bitand(mask, uint32(hex2dec('7FF'))); case 'extended' code = bitand(code, uint32(hex2dec('1FFFFFFF'))); mask = bitand(mask, uint32(hex2dec('1FFFFFFF'))); end end end function passed = validateCallbackFcn(func) % validateCallbackFcn Validate a callback function. % % This method is used internally to validate an intended value % for the message received callback functionality of the % Channel class. Input may be a function handle, a string, or a % cell array. In the case of a cell array, the first element in % the array is expected to be a function handle. % A valid callback value must either be a string, function % handle, or cell array. If the type of func is none of % these, then error. if ~isempty(func) &&... ~ischar(func) &&... ~isa(func, 'function_handle') &&... ~iscell(func) passed = false; return; end % Extra validation is required for cell arrays. if iscell(func) % Check the type of the first element of the cell array. % The first element must be a function handle. if ~isa(func{1}, 'function_handle') passed = false; return; end end % Return passed as true. passed = true; end function executeCallbackFcn(func, channel) % executeCallbackFcn Execute a callback function. % % This method is used internally to execute a set message % received callback function. It activates the callback according % to the type of value set to represent the callback. % Check a string. if ischar(func) % Eval the string to execute it. eval(func); % Check for a function handle. elseif isa(func, 'function_handle') % Call the function directly. feval(func, channel); % Check a cell array. elseif iscell(func) % Set the function. funcHandle = func{1}; % Extract the remainder of the cell array, exclusive of % the first element, which was the function handle. args = func(2:end); % Invoke the callback function passing any extra arguments % along with the call. feval(funcHandle, channel, args{:}); end end function out = callbackFcnToString(func) % callbackFcnToString Convert a callback function name to a string. % % This method is used internally to convert a callback function % name to a string for used in screen displaying. It handles % converting the name for types of empty, string, function % handle, and cell array. % Parse the func name to convert it properly. if (isempty(func)) % When empty, return empty brackets. out = '[]'; elseif (ischar(func)) % When a string type already, return it with quotes. out = ['''' func '''']; elseif (isa(func, 'function_handle')) % When a function handle, convert with an @ symbol. out = ['@' func2str(func)]; elseif (iscell(func)) % When a cell, obtain the size and class and return in curly braces. out = ['{' num2str(size(func, 1)) 'x' num2str(size(func, 2)) ' ' class(func) '}']; end end end end