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

Fix unsigned/signed comparison warning in Metronome.cpp #669

Conversation

NeroBurner
Copy link
Contributor

xTaskGetTickCount() returns a TickType_t, which is defined as an
uint32_t. This is compared to the bpm variable, which is a int16_t
in the range of 40 to 220 as defined in the constructor.

  lv_arc_set_range(bpmArc, 40, 220);

Just to be sure also add an assert that bpm is greater than 0, as this
would result in a division by zero or negative values, which would
unintentionally underflow to a very large number.


Not flashed, I have only a sealed watch and I'm afraid of bricking it

@Avamander
Copy link
Collaborator

The assert is not ideal in this scenario. It would be better to turn BPM into an uint16_t.

@evergreen22
Copy link
Contributor

The best solution IMO would be to promote bpm to a uint32_t before the comparision. There would be no loss of information this way and it would eliminate the warning.

@Avamander
Copy link
Collaborator

@evergreen22 why the unnecessary precision?

@evergreen22
Copy link
Contributor

evergreen22 commented Sep 14, 2021 via email

@Avamander
Copy link
Collaborator

The best choice is the last one. However, if we don't want to do that I think the next safest choice is the second one.

Well it kinda is the only choice if I'm being totally honest. The goal of the warning or removal of it shouldn't be simply getting rid of the warning, but to warn against and fix inconsistencies in integer typing.

@NeroBurner
Copy link
Contributor Author

as clarification: the warning is about the signed int (bpm as int16_t) and unsigned int (TickType_t aka uint32_t) comparison, not the different bit-sizes

I've change the bpm type to be uin32_t.

When getting the bpm value from the arc convert the returned value from int16_t to unit32_t without a check (should I add a comment, that we assume the return value to be strictly positive?).

int16_t lv_arc_get_value(const lv_obj_t * arc)

When setting the arc-value convert from uint32_t to the expected int16_t (as we got the value from the arc in the first place I think it ok to assume the bpm value is still in the int16_t range)

void lv_arc_set_value(lv_obj_t * arc, int16_t value)

@NeroBurner NeroBurner force-pushed the fix_unsigned_comparison_warning_metronome branch from 757df27 to 78740b1 Compare September 14, 2021 20:18
@Riksu9000
Copy link
Contributor

I don't think bpm type should be changed. LVGL works with 16 bit ints and TickType_t is 32 bit uint, so they have to mix at some point. If we change bpm type, we have to cast it everywhere instead of just at the one point. The first commit was fine, just without the assert.

`xTaskGetTickCount()` returns a `TickType_t`, which is defined as an
`uint32_t`. This is compared to the `bpm` variable, which is a `int16_t`
in the range of 40 to 220 as defined in the constructor.

```cpp
  lv_arc_set_range(bpmArc, 40, 220);
```

Just assume that `bpm` is greater than 0, as this
would result in a divison by zero or negative values, which would
unintentionally underflow to a very large number.
@NeroBurner NeroBurner force-pushed the fix_unsigned_comparison_warning_metronome branch from 78740b1 to 63477fc Compare September 16, 2021 08:48
@NeroBurner
Copy link
Contributor Author

@Riksu9000 updated

@JF002 JF002 added this to the 1.5.0 milestone Sep 18, 2021
@JF002
Copy link
Collaborator

JF002 commented Sep 18, 2021

Thanks!

@JF002 JF002 merged commit 52eb94c into InfiniTimeOrg:develop Sep 18, 2021
@NeroBurner NeroBurner deleted the fix_unsigned_comparison_warning_metronome branch September 18, 2021 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants