Merge branch 'edge' of github.com:arthurwolf/Smoothie into edge
[clinton/Smoothieware.git] / src / libs / Pin.h
1 #ifndef PIN_H
2 #define PIN_H
3
4 #include "mbed.h" //Required for LPC_GPIO* . can probably be found in one othe the files mbed.h includes. TODO
5 //#include "../gcc4mbed/external/mbed/LPC1768/LPC17xx.h"
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};
16 this->port_number = atoi(value.substr(0,1).c_str());
17 this->port = gpios[this->port_number];
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
28 inline Pin* as_input(){
29 this->port->FIODIR &= ~(1<<this->pin);
30 return this;
31 }
32
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
42 inline bool get(){
43 if( this->inverting ){
44 return ~(( this->port->FIOPIN >> this->pin ) & 1);
45 }else{
46 return (( this->port->FIOPIN >> this->pin ) & 1);
47 }
48 }
49
50 inline void set(bool value){
51 // TODO : This should be bitmath
52 if( this->inverting ){ value = !value; }
53 if( value ){
54 this->port->FIOSET = 1 << this->pin;
55 }else{
56 this->port->FIOCLR = 1 << this->pin;
57 }
58 }
59
60 bool inverting;
61 LPC_GPIO_TypeDef* port;
62 char port_number;
63 char pin;
64 };
65
66
67
68
69 #endif