-
Notifications
You must be signed in to change notification settings - Fork 33
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 floats #130
Fix unsigned floats #130
Conversation
Cast "signed raw bytes" to signed integer first, before casting to float and applying a divider. Example: datatype = 0x92 (1-0-010-010) datatype & 0x80 (bit 8) => signed value datatype & 0x38 (bits 654) => length => 2 bytes datatype & 0x07 (bits 321) => divider => 2 datatype 0x92 value 0xFC18 datatype signed, length 2, divider 2. signed int value -1000: value -10.00
I don't think this PR caused the CodeFactor failures. |
Itho datatype (single byte) 7-654-3210 bit 7: signed (1), unsigned (0) 654: length of raw data 3210: (4 LSBs): divider Exception 0x5B: two byte unsigned int.
2174e9a
to
f7b914a
Compare
I refactorted the Itho datatype handling to reflect the behaviour of the latest version of the Itho servicetool. (Maybe document the datatype format on a wiki page?) The logic is somewhat different from the old source: https://github.com/rustyx/itho-esp/blob/main/Specs.md#message-a4-00-query-status-format. The special cases Special case |
Is there a way around having the divider as float datatype? Comparing floats is tricky (ie. when divider = 1.0 the comparison if(divider == 1.0) is not guaranteed to result true) |
I agree that comparing floats is scary and a certain source of bugs in the future. From my observations of the Itho servicetool the dividers We can use an I'll push a commit (but not today). |
That sounds like a workable solution as it is not implemented now indeed. Another take might be to change it to a 2 step approach? and use that index to retrieve the divider float value from an array using the index at the time it is needed.
|
Dividers index 9 and 10 should 0.1 and 0.01 These seem to be never used. We set them to 1 so the divider can be integer.
I was even thinking of implementing a I tested |
For testing purposes only! Changes since 2.4.4-beta3: - feat: add ability to enable/disable various i2c commands - fix: check if i2c returned buffer is reply of requested command - fix: mqtt web interface issues - fix: change syslog queue from FreeRTOS queue to stl deque - fix: Merge pull request #130 from tomkooij/fix_signed_floats - fix: missing settings link for WPU version 9 and make code in these cases more resilient - fix: update arduino build info - fix: buffer size mismatch - fix: stop i2c bus only if i2c debug function is active - Update supporting libs Firmware binary (CVE HW rev.2 and NON-CVE): https://github.com/arjenhiemstra/ithowifi/raw/master/compiled_firmware_files/hardware_rev_2/nrgitho-hw2-v2.4.4-beta4.bin
Fixes #123
This is a bit of a hack, as it implements a "is_signed" flag for "floats" but leaves ints and uints in place.
In reality all "itho raw bytes" are either signed/unsigned and have some divider. Ints are just floats with divider 1.
This should be refactored with
IthoDeviceStatus::type
(int/uints/float) removed and only theis_signed
flag in place.But this works:
Cast "signed raw bytes" to signed integer first, before casting to float and applying a divider.
Example: datatype = 0x92 (1-0-010-010)
datatype & 0x80 (bit 8) => signed value
datatype & 0x38 (bits 654) => length => 2 bytes
datatype & 0x07 (bits 321) => divider => 10^2
datatype 0x92 value 0xFC18
datatype signed, length 2, divider 100.
signed int value -1000: value -10.00