More GCE (Gross conceptual Error)

In the following I am endeavoring to provide dot syntax access to individual bit locations in a byte returned from a port read.

class Port
{	
private:
  uint8_t *_PortDev;
public:
  Port();
  Port(volatile uint8_t *PortDev)
  {
    *_PortDev = *PortDev;
  }
  void SetPort(volatile uint8_t *PortDev)
  {
    *_PortDev = *PortDev;
  }
  char Bit(int idxBit, bool AsNumber)
  {
    if (AsNumber)
    {
      return (((*_PortDev) << (8-idxBit)) >> (8-idxBit));
    }
    else
    {
      return (((*_PortDev) << (8-idxBit)) >> 7);
    }
  }
};

Port DAC1(&PORTB);

I suppose I am over complicating things by trying to send a reference to the port its self when I could make it more generic by accepting any byte.

Maybe something like:

#define PortBit(port,bit) (((port) >> (bit)) & 1)

    uint8_t mybit = PortBit(PORTB, 3);

A class for this seems like overkill.

Not really I'm controlling an R2R array and I need bit addressability. But never the less the question was 'is the code correct', not as to whether it is overkill or not.

No.

      return (((*_PortDev) << (8-idxBit)) >> (8-idxBit));

Don’t you need to AND it with 1 before shifting it back?

Anyway, doesn’t the _BV macro do that anyway? Or at least, if you tested the result for non-zero.

    *_PortDev = *PortDev;

Why? I think this should be:

    _PortDev = PortDev;

@dhunt A define won't work as I need to be able to change ports at will while running.

I have played with class in MSE2003 in a console application, which was educational on the line of core dependancies and have gotten alot of different outputs other than what I wanted. And as for class overkill, possibly, but what would I learn in the process? It is as much an excercise in bit shift mathmatics as anything else. The goal was to iterate the port bits of the last port write and alter the bit pattern based on the comparison with the port value and a measured value. Say the measured value equated to B110101110, conversion begins the port is set to B10000000. Port is compared to value, in this case value is greater than Port so compare is 1, Port isn't great enough yet so I add B01000000 to Port making B11000000 Comparison is still 1 so now B00100000 is added, Port now B11100000, Comparison is now 0 as Port exceeded value, so I have to strip the right 6 bits and replace with 0's and then add B00011111 to port. I now srtip 5 bits and do a similar process now only with 5 bits. This continues up to and including the LSB. The end goal is a short hand method to achieve successive aproximation without going through each bit count consecutively until value match. The end goal if the class will be to do the bit stripping. In my dabblings I discovered I was shifting the wrong direction first. Oddly in the process of explaining it here what I want to do is a whole lot simpler than I had been making it. A Right shift will dump the lesser bits up to and including the bit number coinciding to number of shifts, and the return shift will leave 0's in those locations.

ajofscott:
@dhunt A define won’t work as I need to be able to change ports at will while running.

Yes that will work.

Something like his define, or:

if (_BV (foo) != 0) 
  // blah blah

Given this define:

#define _BV(bit) (1 << (bit))

The compiler will emit the code:

if ((1 << (foo)) != 0) 
  // blah blah

That can be evaluated at runtime.

To coin a punn, "I'm a macro moron!"

I think the macro is fairly flexible. For example you can change its use at run time like this:

#define PortBit(port,bit) (((port) >> (bit)) & 1)

volatile uint8_t *myport = &PORTB;
int count = 0;

loop()
{
    uint8_t mybit;

    mybit = PortBit(*myport, 2);

    if (mybit == 0) {
        // some action when the bit is 0
    } else {
        // some other action when the bit is 1
    }

    count++;

    if (count == 10) {
        // Switch to PORTC
        myport = &PORTC;
    } else if (count == 20) {
        // Switch back to PORTB
        myport = &PORTB;
        count = 0;
    }
}

Could you describe the flexibility you need for your project? Maybe some pseudo code to explain it?

If i get off work on time tomorrow I will draw a schematic. I gave a description of my end goal in this thread.