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