Skip to content

Commit

Permalink
Adds a parseblink method to turn a blinker code into a numeric displa…
Browse files Browse the repository at this point in the history
…y. (#516)

* Adds a parseblink method to turn a blinker code into a numeric display.

* Clarify the bit order.
  • Loading branch information
balazsracz authored Feb 28, 2021
1 parent 01081b4 commit 61e89e7
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/utils/Blinker.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/** \copyright
* Copyright (c) 2021, Balazs Racz
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \file Blinker.cxx
*
* Implementations of blinker routines
*
* @author Balazs Racz
* @date 27 Feb 2021
*/

#include "utils/blinker.h"

extern "C"
{

unsigned parseblink(uint32_t pattern)
{
unsigned ret = 0;
if (!pattern)
{
return ret;
}
// Finds the top bit.
uint32_t tb = 1U << 31;
while ((tb & pattern) == 0)
{
tb >>= 1;
}
unsigned nibble_shift = 1;
unsigned last_len = 1;
unsigned curr_len = 1;
pattern &= ~tb;
while (true)
{
if (curr_len)
{
if (pattern & 1)
{
curr_len++;
}
else
{
// end of lit period
if (last_len != curr_len)
{
// new blink length
nibble_shift <<= 4;
last_len = curr_len;
}
ret += nibble_shift;
curr_len = 0;
}
}
else
{
// we are in unlit
if (pattern & 1)
{
// start of a new lit period.
curr_len = 1;
}
}
if (!pattern)
{
break;
}
pattern >>= 1;
}
return ret;
}

}
14 changes: 14 additions & 0 deletions src/utils/Blinker.cxxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "utils/blinker.h"

#include "utils/test_main.hxx"

TEST(BlinkerTest, parse_test)
{
EXPECT_EQ(0u, parseblink(0));
EXPECT_EQ(0x33u, parseblink(BLINK_DIE_ABORT));
EXPECT_EQ(0x213u, parseblink(BLINK_DIE_HARDFAULT));
EXPECT_EQ(0x123u, parseblink(BLINK_DIE_OUTOFMEM));
EXPECT_EQ(0x313u, parseblink(BLINK_DIE_NMI));
EXPECT_EQ(0x223u, parseblink(BLINK_DIE_ASSERT));
EXPECT_EQ(0x133u, parseblink(BLINK_DIE_WATCHDOG));
};
8 changes: 8 additions & 0 deletions src/utils/blinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ extern uint32_t blinker_pattern;
/** Sets a blinking pattern and never returns. */
extern void diewith(uint32_t);

/** Turns a blinker pattern into an error code in BCD.
* @param pattern a blinker pattern from the above examples.
* @return a nonnegative integer with each 4-bit nibble representing the number
* of repeats in the block of blinks, in LSB-first order. So a blink 3-1-2 will
* be represented as 0x213.
*/
extern unsigned parseblink(uint32_t pattern);

#ifdef __cplusplus
}
#endif // cplusplus
Expand Down
1 change: 1 addition & 0 deletions src/utils/sources
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CSRCS += errno_exit.c \

CXXSRCS += \
Base64.cxx \
Blinker.cxx \
CanIf.cxx \
Crc.cxx \
StringPrintf.cxx \
Expand Down

0 comments on commit 61e89e7

Please sign in to comment.