| 1 | /* mbed Microcontroller Library - DigitalInOut |
|---|
| 2 | * Copyright (c) 2006-2011 ARM Limited. All rights reserved. |
|---|
| 3 | */ |
|---|
| 4 | |
|---|
| 5 | #ifndef MBED_DIGITALINOUT_H |
|---|
| 6 | #define MBED_DIGITALINOUT_H |
|---|
| 7 | |
|---|
| 8 | #include "platform.h" |
|---|
| 9 | #include "PinNames.h" |
|---|
| 10 | #include "PeripheralNames.h" |
|---|
| 11 | #include "Base.h" |
|---|
| 12 | |
|---|
| 13 | namespace mbed { |
|---|
| 14 | |
|---|
| 15 | /* Class: DigitalInOut |
|---|
| 16 | * A digital input/output, used for setting or reading a bi-directional pin |
|---|
| 17 | */ |
|---|
| 18 | class DigitalInOut : public Base { |
|---|
| 19 | |
|---|
| 20 | public: |
|---|
| 21 | |
|---|
| 22 | /* Constructor: DigitalInOut |
|---|
| 23 | * Create a DigitalInOut connected to the specified pin |
|---|
| 24 | * |
|---|
| 25 | * Variables: |
|---|
| 26 | * pin - DigitalInOut pin to connect to |
|---|
| 27 | */ |
|---|
| 28 | DigitalInOut(PinName pin, const char* name = NULL); |
|---|
| 29 | |
|---|
| 30 | /* Function: write |
|---|
| 31 | * Set the output, specified as 0 or 1 (int) |
|---|
| 32 | * |
|---|
| 33 | * Variables: |
|---|
| 34 | * value - An integer specifying the pin output value, |
|---|
| 35 | * 0 for logical 0 and 1 (or any other non-zero value) for logical 1 |
|---|
| 36 | */ |
|---|
| 37 | void write(int value) { |
|---|
| 38 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
|---|
| 39 | |
|---|
| 40 | if(value) { |
|---|
| 41 | _gpio->FIOSET = _mask; |
|---|
| 42 | } else { |
|---|
| 43 | _gpio->FIOCLR = _mask; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | #elif defined(TARGET_LPC11U24) |
|---|
| 47 | |
|---|
| 48 | if(value) { |
|---|
| 49 | LPC_GPIO->SET[_index] = _mask; |
|---|
| 50 | } else { |
|---|
| 51 | LPC_GPIO->CLR[_index] = _mask; |
|---|
| 52 | } |
|---|
| 53 | #endif |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /* Function: read |
|---|
| 57 | * Return the output setting, represented as 0 or 1 (int) |
|---|
| 58 | * |
|---|
| 59 | * Variables: |
|---|
| 60 | * returns - An integer representing the output setting of the pin if it is an output, |
|---|
| 61 | * or read the input if set as an input |
|---|
| 62 | */ |
|---|
| 63 | int read() { |
|---|
| 64 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
|---|
| 65 | |
|---|
| 66 | return ((_gpio->FIOPIN & _mask) ? 1 : 0); |
|---|
| 67 | #elif defined(TARGET_LPC11U24) |
|---|
| 68 | return ((LPC_GPIO->PIN[_index] & _mask) ? 1 : 0); |
|---|
| 69 | #endif |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /* Function: output |
|---|
| 74 | * Set as an output |
|---|
| 75 | */ |
|---|
| 76 | void output(); |
|---|
| 77 | |
|---|
| 78 | /* Function: input |
|---|
| 79 | * Set as an input |
|---|
| 80 | */ |
|---|
| 81 | void input(); |
|---|
| 82 | |
|---|
| 83 | /* Function: mode |
|---|
| 84 | * Set the input pin mode |
|---|
| 85 | * |
|---|
| 86 | * Variables: |
|---|
| 87 | * mode - PullUp, PullDown, PullNone, OpenDrain |
|---|
| 88 | */ |
|---|
| 89 | void mode(PinMode pull); |
|---|
| 90 | |
|---|
| 91 | #ifdef MBED_OPERATORS |
|---|
| 92 | /* Function: operator= |
|---|
| 93 | * A shorthand for <write> |
|---|
| 94 | */ |
|---|
| 95 | DigitalInOut& operator= (int value) { |
|---|
| 96 | write(value); |
|---|
| 97 | return *this; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | DigitalInOut& operator= (DigitalInOut& rhs) { |
|---|
| 101 | write(rhs.read()); |
|---|
| 102 | return *this; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /* Function: operator int() |
|---|
| 106 | * A shorthand for <read> |
|---|
| 107 | */ |
|---|
| 108 | operator int() { |
|---|
| 109 | return read(); |
|---|
| 110 | } |
|---|
| 111 | #endif |
|---|
| 112 | |
|---|
| 113 | #ifdef MBED_RPC |
|---|
| 114 | virtual const struct rpc_method *get_rpc_methods(); |
|---|
| 115 | static struct rpc_class *get_rpc_class(); |
|---|
| 116 | #endif |
|---|
| 117 | |
|---|
| 118 | protected: |
|---|
| 119 | |
|---|
| 120 | PinName _pin; |
|---|
| 121 | |
|---|
| 122 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
|---|
| 123 | LPC_GPIO_TypeDef *_gpio; |
|---|
| 124 | #elif defined(TARGET_LPC11U24) |
|---|
| 125 | int _index; |
|---|
| 126 | #endif |
|---|
| 127 | |
|---|
| 128 | uint32_t _mask; |
|---|
| 129 | |
|---|
| 130 | }; |
|---|
| 131 | |
|---|
| 132 | } // namespace mbed |
|---|
| 133 | |
|---|
| 134 | #endif |
|---|