Bidirectional IO

16 Dec 2009

DigitaIn and DigitalOut are great, but I need to implement a bidirectional IO pin.

I would expect an API with methods like those:

 

DigitalInOut pin(p1);
...
pin.input();
if ( pin.read() ) {
  // external device is not driving it low, so we can send clock
  pin.output(1)   // enable the output driver and set to 1
  pin.output(0);  wait(0.1);
  pin.output(1);  wait(0.1);
  pin.output(0);  wait(0.1);
  pin.output(1);  wait(0.1);
  pin.output(0);  wait(0.1);
  pin.output(1);  wait(0.1);
  pin.input();    // return to being an input
}

 

What is the MBED way of doing it?

16 Dec 2009

Well, you're not going to believe it...

16 Dec 2009

Great, thanks!

Somebody update the Handbook!

17 Dec 2009 . Edited: 17 Dec 2009

Hi Ilya,

Ilya I wrote:
DigitaIn and DigitalOut are great, but I need to implement a bidirectional IO pin.

DigitalInOut now added to the Handbook, along with BusIn, BusOut and BusInOut.

Simon

17 Dec 2009

Excellent! Let there be more light!

18 Dec 2009

Is there likely to be an analog version of the bidirectional I/O as I have got hold of a Nintendo DS touch screen (£3 on e-bay) and it requires cunning analog switchery to drive it, it seems?

Pip

P.S. At least one of us has passed the 60 mark and still fighting with C.

18 Dec 2009

I'm not 100% sure (don't have a touch screen to test), but the following approach might work:

class NdsTouchScreen
{
    DigitalInOut _bottom, _left, _top, _right;
    PinName _bottomPin, _leftPin, _topPin, _rightPin;
public:    
    NdsTouchScreen(PinName bottom, PinName left, PinName top, PinName right)
        : _bottom(bottom), _left(left), _top(top), _right(right),
          _bottomPin(bottom), _leftPin(left), _topPin(top), _rightPin(right)
    {};
    
    float readX()
    {
        // apply 0-Vcc to top-bottom pair
        _top.output();
        _top = 0;
        _bottom.output();
        _bottom = 1;
        // set left and right to input mode
        _left.input();
        _right.input();
        // wait a bit
        wait_ms(10);
        // return analog value of left as X
        return AnalogIn(_leftPin).read();
    }

    float readY()
    {
        // apply 0-Vcc to left-right pair
        _left.output();
        _left = 0;
        _right.output();
        _right = 1;
        // set top and bottom to input mode
        _top.input();
        _bottom.input();
        // wait a bit
        wait_ms(10);
        // return analog value of top as Y
        return AnalogIn(_topPin).read();
    }
};

NdsTouchScreen s(p9, p10, p11, p12);