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