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