Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed is_float_conversion() #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 77 additions & 30 deletions cantools/database/can/c_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@
*
* @return Encoded signal.
*/
{type_name} {database_name}_{message_name}_{signal_name}_encode({floating_point_type} value);
{type_name} {database_name}_{message_name}_{signal_name}_encode({conversion_type} value);

'''

Expand Down Expand Up @@ -633,7 +633,7 @@
*
* @return Decoded signal.
*/
{floating_point_type} {database_name}_{message_name}_{signal_name}_decode({type_name} value);
{conversion_type} {database_name}_{message_name}_{signal_name}_decode({type_name} value);

'''

Expand Down Expand Up @@ -771,13 +771,13 @@
SIGNAL_DECLARATION_TO_ = ''' {signal_type} {signal_name},
'''

SIGNAL_DEFINITION_RAW_TO_CONVERT_STRUCT_FLOAT = ''' conversion->{signal_name} = {database_name}_{message_name}_{signal_name}_decode(raw->{signal_name});
SIGNAL_DEFINITION_RAW_TO_CONVERT_STRUCT_DECODE = ''' conversion->{signal_name} = {database_name}_{message_name}_{signal_name}_decode(raw->{signal_name});
'''
SIGNAL_DEFINITION_CONVERT_TO_RAW_STRUCT_FLOAT = ''' raw->{signal_name} = {database_name}_{message_name}_{signal_name}_encode(conversion->{signal_name});
SIGNAL_DEFINITION_CONVERT_TO_RAW_STRUCT_ENCODE = ''' raw->{signal_name} = {database_name}_{message_name}_{signal_name}_encode(conversion->{signal_name});
'''
SIGNAL_DEFINITION_RAW_TO_CONVERT_FLOAT = ''' conversion->{signal_name} = {database_name}_{message_name}_{signal_name}_decode({signal_name});
SIGNAL_DEFINITION_RAW_TO_CONVERT_DECODE = ''' conversion->{signal_name} = {database_name}_{message_name}_{signal_name}_decode({signal_name});
'''
SIGNAL_DEFINITION_CONVERT_TO_RAW_FLOAT = ''' raw->{signal_name} = {database_name}_{message_name}_{signal_name}_encode({signal_name});
SIGNAL_DEFINITION_CONVERT_TO_RAW_ENCODE = ''' raw->{signal_name} = {database_name}_{message_name}_{signal_name}_encode({signal_name});
'''

SIGNAL_DEFINITION_RAW_TO_CONVERT_STRUCT = ''' conversion->{signal_name} = raw->{signal_name};
Expand All @@ -790,15 +790,15 @@
'''

SIGNAL_DEFINITION_ENCODE_FMT = '''\
{type_name} {database_name}_{message_name}_{signal_name}_encode({floating_point_type} value)
{type_name} {database_name}_{message_name}_{signal_name}_encode({conversion_type} value)
{{
return ({type_name})({encode});
}}

'''

SIGNAL_DEFINITION_DECODE_FMT = '''\
{floating_point_type} {database_name}_{message_name}_{signal_name}_decode({type_name} value)
{conversion_type} {database_name}_{message_name}_{signal_name}_decode({type_name} value)
{{
return ({decode});
}}
Expand Down Expand Up @@ -1121,7 +1121,10 @@ def unique_choices(self):

@property
def is_float_conversion(self):
return self.is_float or _get(self.scale, '-') % 1 != 0 or self.minimum_value % 1 != 0 or self.maximum_value % 1 != 0
return self.is_float or _get(self.scale, '1') != 1 or _get(self.offset, '0') % 1 != 0 or self.minimum_value % 1 != 0 or self.maximum_value % 1 != 0
@property
def is_integer_conversion(self):
return (not self.is_float_conversion) and _get(self.offset, '0') != 0

@property
def minimum_type_value(self):
Expand Down Expand Up @@ -1236,7 +1239,7 @@ def __init__(self, message, database_name):
self.signals = [Signal(signal, self.snake_name, database_name)for signal in message.signals]
self.has_conversions = False
for sig in self.signals:
if sig.is_float_conversion:
if sig.is_float_conversion or sig.is_integer_conversion:
self.has_conversions = True
break

Expand Down Expand Up @@ -1794,11 +1797,12 @@ def _generate_encode_decode(message, use_float):
if offset == 0 and scale == 1:
encoding = 'value'
decoding = f'({floating_point_type})value'
elif scale == 1 and offset != 0 and offset % 1 == 0:
encoding = f'value - {offset}'
decoding = f'value + {offset}'
elif offset != 0 and scale != 1:
encoding = '(value - {}) / {}'.format(formatted_offset,
formatted_scale)
decoding = '(({})value * {}) + {}'.format(floating_point_type, formatted_scale,
formatted_offset)
encoding = f'(value - {formatted_offset}) / {formatted_scale}'
decoding = f'(({floating_point_type})value * {formatted_scale}) + {formatted_offset}'
elif offset != 0:
encoding = f'value - {formatted_offset}'
decoding = f'({floating_point_type})value + {formatted_offset}'
Expand Down Expand Up @@ -2092,14 +2096,29 @@ def _generate_declarations(database_name, messages: List[Message], floating_poin
message_name=message.snake_name,
signal_name=signal.snake_name,
type_name=signal.type_name,
floating_point_type=_get_floating_point_type(use_float))
conversion_type=_get_floating_point_type(use_float))
if node_name is None or _is_receiver(signal, node_name):
signal_declaration += SIGNAL_DECLARATION_DECODE_FMT.format(
database_name=database_name,
message_name=message.snake_name,
signal_name=signal.snake_name,
type_name=signal.type_name,
conversion_type=_get_floating_point_type(use_float))
if signal.is_integer_conversion:
if is_sender:
signal_declaration += SIGNAL_DECLARATION_ENCODE_FMT.format(
database_name=database_name,
message_name=message.snake_name,
signal_name=signal.snake_name,
type_name=signal.type_name,
conversion_type=signal.type_name)
if node_name is None or _is_receiver(signal, node_name):
signal_declaration += SIGNAL_DECLARATION_DECODE_FMT.format(
database_name=database_name,
message_name=message.snake_name,
signal_name=signal.snake_name,
type_name=signal.type_name,
floating_point_type=_get_floating_point_type(use_float))
conversion_type=signal.type_name)
if is_sender or _is_receiver(signal, node_name):
signal_declaration += SIGNAL_DECLARATION_IS_IN_RANGE_FMT.format(
database_name=database_name,
Expand Down Expand Up @@ -2233,13 +2252,7 @@ def _generate_definitions(database_name, messages: List[Message], floating_point
database_name=database_name,
message_length=message.length,
conversion_component=conversion_comp)
if message.has_conversions:
devices_serialize += DEVICE_MESSAGE_SERIALIZE_CONVERSION.format(id=message._message._frame_id,
message_name=message.snake_name,
database_name=database_name,
message_length=message.length)
else:
devices_serialize += DEVICE_MESSAGE_SERIALIZE_RAW.format(id=message._message._frame_id,
devices_serialize += DEVICE_MESSAGE_SERIALIZE_CONVERSION.format(id=message._message._frame_id,
message_name=message.snake_name,
database_name=database_name,
message_length=message.length)
Expand Down Expand Up @@ -2323,18 +2336,52 @@ def _generate_definitions(database_name, messages: List[Message], floating_point

if floating_point_numbers and signal.is_float_conversion:
signals_specifiers += SIGNAL_DEFINITION_SPECIFIER.format(specifier = type_to_specifier["float"])
message_raw_to_conversion += SIGNAL_DEFINITION_RAW_TO_CONVERT_FLOAT.format(signal_name=signal.snake_name,
message_raw_to_conversion += SIGNAL_DEFINITION_RAW_TO_CONVERT_DECODE.format(signal_name=signal.snake_name,
database_name=database_name,
message_name=message.snake_name)
message_conversion_to_raw += SIGNAL_DEFINITION_CONVERT_TO_RAW_ENCODE.format(signal_name=signal.snake_name,
database_name=database_name,
message_name=message.snake_name)

message_raw_to_conversion_struct += SIGNAL_DEFINITION_RAW_TO_CONVERT_STRUCT_DECODE.format(signal_name=signal.snake_name,
database_name=database_name,
message_name=message.snake_name)

message_conversion_to_raw_struct += SIGNAL_DEFINITION_CONVERT_TO_RAW_STRUCT_ENCODE.format(signal_name=signal.snake_name,
database_name=database_name,
message_name=message.snake_name)
if is_sender:

signal_definition += SIGNAL_DEFINITION_ENCODE_FMT.format(
database_name=database_name,
message_name=message.snake_name,
signal_name=signal.snake_name,
type_name=signal.type_name,
encode=encode,
conversion_type=_get_floating_point_type(use_float))
if node_name is None or _is_receiver(signal, node_name):
signal_definition += SIGNAL_DEFINITION_DECODE_FMT.format(
database_name=database_name,
message_name=message.snake_name,
signal_name=signal.snake_name,
type_name=signal.type_name,
decode=decode,
conversion_type=_get_floating_point_type(use_float))

if signal.is_integer_conversion:
signals_specifiers += SIGNAL_DEFINITION_SPECIFIER.format(specifier = type_to_specifier[signal.type_name])
message_raw_to_conversion += SIGNAL_DEFINITION_RAW_TO_CONVERT_DECODE.format(signal_name=signal.snake_name,
database_name=database_name,
message_name=message.snake_name)
message_conversion_to_raw += SIGNAL_DEFINITION_CONVERT_TO_RAW_FLOAT.format(signal_name=signal.snake_name,
message_conversion_to_raw += SIGNAL_DEFINITION_CONVERT_TO_RAW_ENCODE.format(signal_name=signal.snake_name,
database_name=database_name,
message_name=message.snake_name)

message_raw_to_conversion_struct += SIGNAL_DEFINITION_RAW_TO_CONVERT_STRUCT_FLOAT.format(signal_name=signal.snake_name,
message_raw_to_conversion_struct += SIGNAL_DEFINITION_RAW_TO_CONVERT_STRUCT_DECODE.format(signal_name=signal.snake_name,
database_name=database_name,
message_name=message.snake_name)

message_conversion_to_raw_struct += SIGNAL_DEFINITION_CONVERT_TO_RAW_STRUCT_FLOAT.format(signal_name=signal.snake_name,
message_conversion_to_raw_struct += SIGNAL_DEFINITION_CONVERT_TO_RAW_STRUCT_ENCODE.format(signal_name=signal.snake_name,
database_name=database_name,
message_name=message.snake_name)
if is_sender:
Expand All @@ -2345,17 +2392,17 @@ def _generate_definitions(database_name, messages: List[Message], floating_point
signal_name=signal.snake_name,
type_name=signal.type_name,
encode=encode,
floating_point_type=_get_floating_point_type(use_float))
conversion_type=signal.type_name)
if node_name is None or _is_receiver(signal, node_name):
signal_definition += SIGNAL_DEFINITION_DECODE_FMT.format(
database_name=database_name,
message_name=message.snake_name,
signal_name=signal.snake_name,
type_name=signal.type_name,
decode=decode,
floating_point_type=_get_floating_point_type(use_float))
conversion_type=signal.type_name)

if not signal.is_float_conversion:
if not (signal.is_float_conversion or signal.is_integer_conversion):
signals_specifiers += SIGNAL_DEFINITION_SPECIFIER.format(specifier = type_to_specifier[signal.type_name])
message_raw_to_conversion_struct += SIGNAL_DEFINITION_RAW_TO_CONVERT_STRUCT.format(signal_name=signal.snake_name)
message_conversion_to_raw_struct += SIGNAL_DEFINITION_CONVERT_TO_RAW_STRUCT.format(signal_name=signal.snake_name)
Expand Down