Commit | Line | Data |
---|---|---|
3b1e82d2 AW |
1 | #ifndef PIN_H |
2 | #define PIN_H | |
3 | ||
70a50442 | 4 | #include <stdlib.h> |
da3a10b9 | 5 | #include "libs/LPC17xx/sLPC17xx.h" // smoothed mbed.h lib |
3b1e82d2 AW |
6 | #include "libs/Kernel.h" |
7 | #include "libs/utils.h" | |
8 | #include <string> | |
9 | ||
10 | class Pin{ | |
11 | public: | |
12 | Pin(){ } | |
13 | ||
14 | Pin* from_string(std::string value){ | |
15 | LPC_GPIO_TypeDef* gpios[5] ={LPC_GPIO0,LPC_GPIO1,LPC_GPIO2,LPC_GPIO3,LPC_GPIO4}; | |
bee725fc AW |
16 | this->port_number = atoi(value.substr(0,1).c_str()); |
17 | this->port = gpios[this->port_number]; | |
3b1e82d2 AW |
18 | this->inverting = ( value.find_first_of("!")!=string::npos ? true : false ); |
19 | this->pin = atoi( value.substr(2, value.size()-2-(this->inverting?1:0)).c_str() ); | |
20 | return this; | |
21 | } | |
22 | ||
23 | inline Pin* as_output(){ | |
24 | this->port->FIODIR |= 1<<this->pin; | |
25 | return this; | |
26 | } | |
27 | ||
81b547a1 AW |
28 | inline Pin* as_input(){ |
29 | this->port->FIODIR &= ~(1<<this->pin); | |
30 | return this; | |
31 | } | |
32 | ||
bee725fc AW |
33 | inline Pin* as_open_drain(){ |
34 | if( this->port_number == 0 ){ LPC_PINCON->PINMODE_OD0 |= (1<<this->pin); } | |
35 | if( this->port_number == 1 ){ LPC_PINCON->PINMODE_OD1 |= (1<<this->pin); } | |
36 | if( this->port_number == 2 ){ LPC_PINCON->PINMODE_OD2 |= (1<<this->pin); } | |
37 | if( this->port_number == 3 ){ LPC_PINCON->PINMODE_OD3 |= (1<<this->pin); } | |
38 | if( this->port_number == 4 ){ LPC_PINCON->PINMODE_OD4 |= (1<<this->pin); } | |
39 | return this; | |
40 | } | |
41 | ||
81b547a1 | 42 | inline bool get(){ |
da3a10b9 | 43 | return this->inverting ^ (( this->port->FIOPIN >> this->pin ) & 1); |
81b547a1 AW |
44 | } |
45 | ||
3b1e82d2 | 46 | inline void set(bool value){ |
feb204be | 47 | if( this->inverting ^ value ){ |
3b1e82d2 AW |
48 | this->port->FIOSET = 1 << this->pin; |
49 | }else{ | |
50 | this->port->FIOCLR = 1 << this->pin; | |
51 | } | |
52 | } | |
53 | ||
54 | bool inverting; | |
55 | LPC_GPIO_TypeDef* port; | |
bee725fc | 56 | char port_number; |
3b1e82d2 AW |
57 | char pin; |
58 | }; | |
59 | ||
60 | ||
61 | ||
62 | ||
3b1e82d2 | 63 | #endif |