Skip to content

Conversation

miri64
Copy link
Member

@miri64 miri64 commented Jul 4, 2017

This imports a simple BCD en-/decoder. I require that for a driver I'm currently porting.

@miri64 miri64 added the Type: new feature The issue requests / The PR implemements a new feature for RIOT label Jul 4, 2017
@miri64 miri64 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jul 4, 2017
@miri64 miri64 force-pushed the bcd/feat/initial branch from d15b039 to 9193a5f Compare July 4, 2017 11:28
static void test_bcd_to_byte__greater_0x99(void)
{
TEST_ASSERT_EQUAL_INT(100, bcd_to_byte(0xa0));
TEST_ASSERT_EQUAL_INT(100, bcd_to_byte(0xa0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate test for 100, and shouldn't this be the opposite version of test_bcd_from_byte__greater_99 - meaning both should test same values?!

Copy link
Member Author

@miri64 miri64 Jul 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the duplicate line, but they don't have the same value, since they are out of range => garbled "input", which is the whole point of this test.

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

(in fact the output of bcd_to_byte() makes more sense mathematically than the output of bcd_from_byte() since there is a cut-of at some-point for the latter (165 or 0xff as BCD))

@smlng
Copy link
Member

smlng commented Jul 4, 2017

interesting encoding, but which driver is (seriously) using this - and why 😕 ❓

@smlng
Copy link
Member

smlng commented Jul 4, 2017

ah oversaw your reference ...

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

interesting encoding, but which driver is (seriously) using this - and why 😕 ❓

Among others (like the DS1307) the x86 RTC

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

The mensa-cards at FU also store the current account balance as a BCD, IIRC.

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

As to why according to my quick research mainly a) legacy reasons (see x86 RTC) and simplified ASCII representation. Since the DS1307 is mainly used by the Arduino community I guess the latter applies for this device.

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

(not only ASCII, but also e.g. 7-segment displays).

@miri64
Copy link
Member Author

miri64 commented Jul 4, 2017

@smlng
Copy link
Member

smlng commented Jul 4, 2017

8-segment displays

that sounds reasonable, indeed! Wasn't aware that it might still have a good use-case ... mostly read articles stating it is legacy stuff and "not supported by modern CPUs" bla bla ...

*/
static inline uint8_t bcd_from_byte(uint8_t byte)
{
return byte + (6 * (byte / 10));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this is (more) elegant, it's not as intuitive as ((byte / 10) << 4) | (byte % 10), which makes the conversion IMHO quite explicit. I don't suggest to change this, but it might be worth a comment (or example)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only is it more elegant, but also more efficient ;-), because you only have one costly division op (counting mod as a division).

Copy link
Member

@smlng smlng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise ACK

@smlng
Copy link
Member

smlng commented Jul 6, 2017

please squash, too

@miri64 miri64 force-pushed the bcd/feat/initial branch from f8adbc2 to d09999a Compare July 6, 2017 18:44
@miri64
Copy link
Member Author

miri64 commented Jul 6, 2017

Squashed

@miri64 miri64 merged commit 17a35a0 into RIOT-OS:master Jul 6, 2017
@miri64 miri64 deleted the bcd/feat/initial branch July 6, 2017 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants