Modified InOut library
DigitalInOut2.h@0:1d15d1ea182e, 2014-10-12 (annotated)
- Committer:
- jmar11
- Date:
- Sun Oct 12 23:47:17 2014 +0000
- Revision:
- 0:1d15d1ea182e
- Child:
- 1:f3a86fb2a054
null
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmar11 | 0:1d15d1ea182e | 1 | #ifndef MBED_DIGITALINOUT_H |
jmar11 | 0:1d15d1ea182e | 2 | #define MBED_DIGITALINOUT_H |
jmar11 | 0:1d15d1ea182e | 3 | |
jmar11 | 0:1d15d1ea182e | 4 | #include "platform.h" |
jmar11 | 0:1d15d1ea182e | 5 | |
jmar11 | 0:1d15d1ea182e | 6 | #include "gpio_api.h" |
jmar11 | 0:1d15d1ea182e | 7 | |
jmar11 | 0:1d15d1ea182e | 8 | namespace mbed { |
jmar11 | 0:1d15d1ea182e | 9 | |
jmar11 | 0:1d15d1ea182e | 10 | /** A digital input/output, used for setting or reading a bi-directional pin |
jmar11 | 0:1d15d1ea182e | 11 | */ |
jmar11 | 0:1d15d1ea182e | 12 | class DigitalInOut2 { |
jmar11 | 0:1d15d1ea182e | 13 | |
jmar11 | 0:1d15d1ea182e | 14 | public: |
jmar11 | 0:1d15d1ea182e | 15 | /** Create a DigitalInOut connected to the specified pin |
jmar11 | 0:1d15d1ea182e | 16 | * |
jmar11 | 0:1d15d1ea182e | 17 | * @param pin DigitalInOut pin to connect to |
jmar11 | 0:1d15d1ea182e | 18 | */ |
jmar11 | 0:1d15d1ea182e | 19 | DigitalInOut(PinName pin) { |
jmar11 | 0:1d15d1ea182e | 20 | gpio_init(&gpio, pin, PIN_INPUT); |
jmar11 | 0:1d15d1ea182e | 21 | } |
jmar11 | 0:1d15d1ea182e | 22 | |
jmar11 | 0:1d15d1ea182e | 23 | /** Set the output, specified as 0 or 1 (int) |
jmar11 | 0:1d15d1ea182e | 24 | * |
jmar11 | 0:1d15d1ea182e | 25 | * @param value An integer specifying the pin output value, |
jmar11 | 0:1d15d1ea182e | 26 | * 0 for logical 0, 1 (or any other non-zero value) for logical 1 |
jmar11 | 0:1d15d1ea182e | 27 | */ |
jmar11 | 0:1d15d1ea182e | 28 | void write(int value) { |
jmar11 | 0:1d15d1ea182e | 29 | gpio_write(&gpio, value); |
jmar11 | 0:1d15d1ea182e | 30 | } |
jmar11 | 0:1d15d1ea182e | 31 | |
jmar11 | 0:1d15d1ea182e | 32 | /** Return the output setting, represented as 0 or 1 (int) |
jmar11 | 0:1d15d1ea182e | 33 | * |
jmar11 | 0:1d15d1ea182e | 34 | * @returns |
jmar11 | 0:1d15d1ea182e | 35 | * an integer representing the output setting of the pin if it is an output, |
jmar11 | 0:1d15d1ea182e | 36 | * or read the input if set as an input |
jmar11 | 0:1d15d1ea182e | 37 | */ |
jmar11 | 0:1d15d1ea182e | 38 | int read() { |
jmar11 | 0:1d15d1ea182e | 39 | return gpio_read(&gpio); |
jmar11 | 0:1d15d1ea182e | 40 | } |
jmar11 | 0:1d15d1ea182e | 41 | |
jmar11 | 0:1d15d1ea182e | 42 | /** Set as an output |
jmar11 | 0:1d15d1ea182e | 43 | */ |
jmar11 | 0:1d15d1ea182e | 44 | void output() { |
jmar11 | 0:1d15d1ea182e | 45 | gpio_dir(&gpio, PIN_OUTPUT); |
jmar11 | 0:1d15d1ea182e | 46 | } |
jmar11 | 0:1d15d1ea182e | 47 | |
jmar11 | 0:1d15d1ea182e | 48 | /** Set as an input |
jmar11 | 0:1d15d1ea182e | 49 | */ |
jmar11 | 0:1d15d1ea182e | 50 | void input() { |
jmar11 | 0:1d15d1ea182e | 51 | gpio_dir(&gpio, PIN_INPUT); |
jmar11 | 0:1d15d1ea182e | 52 | } |
jmar11 | 0:1d15d1ea182e | 53 | |
jmar11 | 0:1d15d1ea182e | 54 | /** Set the input pin mode |
jmar11 | 0:1d15d1ea182e | 55 | * |
jmar11 | 0:1d15d1ea182e | 56 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
jmar11 | 0:1d15d1ea182e | 57 | */ |
jmar11 | 0:1d15d1ea182e | 58 | void mode(PinMode pull) { |
jmar11 | 0:1d15d1ea182e | 59 | gpio_mode(&gpio, pull); |
jmar11 | 0:1d15d1ea182e | 60 | } |
jmar11 | 0:1d15d1ea182e | 61 | |
jmar11 | 0:1d15d1ea182e | 62 | #ifdef MBED_OPERATORS |
jmar11 | 0:1d15d1ea182e | 63 | /** A shorthand for write() |
jmar11 | 0:1d15d1ea182e | 64 | */ |
jmar11 | 0:1d15d1ea182e | 65 | DigitalInOut& operator= (int value) { |
jmar11 | 0:1d15d1ea182e | 66 | write(value); |
jmar11 | 0:1d15d1ea182e | 67 | return *this; |
jmar11 | 0:1d15d1ea182e | 68 | } |
jmar11 | 0:1d15d1ea182e | 69 | |
jmar11 | 0:1d15d1ea182e | 70 | DigitalInOut& operator= (DigitalInOut& rhs) { |
jmar11 | 0:1d15d1ea182e | 71 | write(rhs.read()); |
jmar11 | 0:1d15d1ea182e | 72 | return *this; |
jmar11 | 0:1d15d1ea182e | 73 | } |
jmar11 | 0:1d15d1ea182e | 74 | |
jmar11 | 0:1d15d1ea182e | 75 | /** A shorthand for read() |
jmar11 | 0:1d15d1ea182e | 76 | */ |
jmar11 | 0:1d15d1ea182e | 77 | operator int() { |
jmar11 | 0:1d15d1ea182e | 78 | return read(); |
jmar11 | 0:1d15d1ea182e | 79 | } |
jmar11 | 0:1d15d1ea182e | 80 | #endif |
jmar11 | 0:1d15d1ea182e | 81 | |
jmar11 | 0:1d15d1ea182e | 82 | gpio_t gpio; |
jmar11 | 0:1d15d1ea182e | 83 | }; |
jmar11 | 0:1d15d1ea182e | 84 | |
jmar11 | 0:1d15d1ea182e | 85 | } // namespace mbed |
jmar11 | 0:1d15d1ea182e | 86 | |
jmar11 | 0:1d15d1ea182e | 87 | #endif |