Click here to Skip to main content
15,510,087 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch30-Jan-22 10:02
mvahoney the codewitch30-Jan-22 10:02 
GeneralRe: This is actually cleaned up! Pin
jschell17-Feb-22 11:33
jschell17-Feb-22 11:33 
JokeRe: This is actually cleaned up! Pin
Nelek15-Dec-21 11:59
protectorNelek15-Dec-21 11:59 
GeneralRe: This is actually cleaned up! Pin
Fueled By Decaff15-Dec-21 7:30
Fueled By Decaff15-Dec-21 7:30 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 7:32
mvahoney the codewitch15-Dec-21 7:32 
GeneralRe: This is actually cleaned up! Pin
Al_Brown31-Dec-21 2:44
Al_Brown31-Dec-21 2:44 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch31-Dec-21 4:14
mvahoney the codewitch31-Dec-21 4:14 
GeneralRe: This is actually cleaned up! Pin
Al_Brown31-Dec-21 5:38
Al_Brown31-Dec-21 5:38 
OK - it wasn't immediately clear that the bit mapping was constant at compile time. Personally, I'd still be inclined to do something like this but would want to confirm it was optimising as expected (which I'm sure you've done with the existing code):

C++
// Data pin GPIO allocation - purely an example
const unsigned pin_d0 = 4;
const unsigned pin_d1 = 5;
const unsigned pin_d2 = 6;
const unsigned pin_d3 = 7;
const unsigned pin_d4 = 36;
const unsigned pin_d5 = 37;
const unsigned pin_d6 = 38;
const unsigned pin_d7 = 39;

// Read our eight bit bus value from the appropriate GPIO pins
static uint8_t readInput(void)
{
    // Add delay here

    // Read the hardware bus
    uint32_t GPIOLow = gpio_input_get();
    uint32_t GPIOHigh = gpio_input_get_high();

    // Decode from GPIO to eight bit bus
    uint8_t dataRead = 0;
    dataRead |= (((pin_d0 > 31 ? GPIOHigh : GPIOLow) >> (pin_d0 & 31)) & 1) << 0;
    dataRead |= (((pin_d1 > 31 ? GPIOHigh : GPIOLow) >> (pin_d1 & 31)) & 1) << 1;
    dataRead |= (((pin_d2 > 31 ? GPIOHigh : GPIOLow) >> (pin_d2 & 31)) & 1) << 2;
    dataRead |= (((pin_d3 > 31 ? GPIOHigh : GPIOLow) >> (pin_d3 & 31)) & 1) << 3;
    dataRead |= (((pin_d4 > 31 ? GPIOHigh : GPIOLow) >> (pin_d4 & 31)) & 1) << 4;
    dataRead |= (((pin_d5 > 31 ? GPIOHigh : GPIOLow) >> (pin_d5 & 31)) & 1) << 5;
    dataRead |= (((pin_d6 > 31 ? GPIOHigh : GPIOLow) >> (pin_d6 & 31)) & 1) << 6;
    dataRead |= (((pin_d7 > 31 ? GPIOHigh : GPIOLow) >> (pin_d7 & 31)) & 1) << 7;

    return dataRead;
}


In practice, I would almost certainly encapsulate the individual lines as macros or as an inline function but it's probably clear enough what it's doing. Reads from both ports are kept in there - if a single hardware register read is really killing you then it could be eliminated.

The delay aspect would still concern me. This is presumably reading from an external device (LCD?) and there will be a defined timing between initiating a read and the data being available. You will get away with your approach on a specific hardware platform but in future it could easily fall down if the clock speed changes or the external delays change. Dropping to assembler shouldn't be necessary - but your code shouldn't depend on how long it takes to do a dummy read.

As an aside, the original code has instances of this:
C++
if(pin_d1>31) {
        b |= (((pins_h>>((pin_d1-32)&31))&1)<<1);
    } else if(pin_d1>-1) {
        b |= (((pins_l>>(pin_d1))&1)<<1);
    }

Subtracting 32 from the pin value before masking serves no purpose - you're only using the lower bits anyway.
If you ensure your pin values are non-negative (which could / should be possible at compile time) then it is also redundant to test against -1.
GeneralMessage Closed Pin
13-Dec-21 15:55
mvahoney the codewitch13-Dec-21 15:55 
GeneralRe: IME, It's a lot harder to shop for people when they get pregnant Pin
Brisingr Aerowing13-Dec-21 16:01
professionalBrisingr Aerowing13-Dec-21 16:01 
GeneralRe: IME, It's a lot harder to shop for people when they get pregnant Pin
honey the codewitch13-Dec-21 16:06
mvahoney the codewitch13-Dec-21 16:06 
GeneralI'm not sure how I feel about Forth implemented on a Cortex M-0 Pin
honey the codewitch13-Dec-21 1:09
mvahoney the codewitch13-Dec-21 1:09 
GeneralRe: I'm not sure how I feel about Forth implemented on a Cortex M-0 Pin
trønderen13-Dec-21 17:55
trønderen13-Dec-21 17:55 
GeneralRe: I'm not sure how I feel about Forth implemented on a Cortex M-0 Pin
Richard Deeming13-Dec-21 22:58
mveRichard Deeming13-Dec-21 22:58 
JokeRe: I'm not sure how I feel about Forth implemented on a Cortex M-0 Pin
englebart1-Sep-22 16:12
professionalenglebart1-Sep-22 16:12 
GeneralRe: I'm not sure how I feel about Forth implemented on a Cortex M-0 Pin
Rick York14-Dec-21 20:45
mveRick York14-Dec-21 20:45 
GeneralRe: I'm not sure how I feel about Forth implemented on a Cortex M-0 Pin
Gary Wheeler31-Dec-21 5:07
Gary Wheeler31-Dec-21 5:07 
GeneralRe: I'm not sure how I feel about Forth implemented on a Cortex M-0 Pin
Rick York31-Dec-21 8:44
mveRick York31-Dec-21 8:44 
GeneralPreparing for the future... PinPopular
Richard Deeming8-Dec-21 2:13
mveRichard Deeming8-Dec-21 2:13 
JokeRe: Preparing for the future... Pin
Daniel Pfeffer8-Dec-21 2:30
professionalDaniel Pfeffer8-Dec-21 2:30 
GeneralRe: Preparing for the future... Pin
PIEBALDconsult8-Dec-21 4:09
professionalPIEBALDconsult8-Dec-21 4:09 
JokeRe: Preparing for the future... Pin
Sander Rossel8-Dec-21 4:42
professionalSander Rossel8-Dec-21 4:42 
GeneralRe: Preparing for the future... Pin
Greg Utas9-Dec-21 15:09
mveGreg Utas9-Dec-21 15:09 
GeneralRe: Preparing for the future... Pin
Gary Wheeler10-Dec-21 3:38
Gary Wheeler10-Dec-21 3:38 
GeneralRe: Preparing for the future... Pin
thewazz10-Dec-21 8:07
professionalthewazz10-Dec-21 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.