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 <stdlib.h>
5 #include "libs/LPC17xx/sLPC17xx.h" // smoothed mbed.h lib
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 if( value.find_first_of("n")!=string::npos ? true : false ){
17 this->port_number = 0;
18 this->port = gpios[(unsigned int) this->port_number];
19 this->inverting = false;
20 this->pin = 255;
21 }else{
22 this->port_number = atoi(value.substr(0,1).c_str());
23 this->port = gpios[(unsigned int) this->port_number];
24 this->inverting = ( value.find_first_of("!")!=string::npos ? true : false );
25 this->pin = atoi( value.substr(2, value.size()-2-(this->inverting?1:0)).c_str() );
26 }
27 return this;
28 }
29
30 inline bool connected(){
31 if( this->pin == 255 ){ return false; }
32 return true;
33 }
34
35 inline Pin* as_output(){
36 this->port->FIODIR |= 1<<this->pin;
37 return this;
38 }
39
40 inline Pin* as_input(){
41 this->port->FIODIR &= ~(1<<this->pin);
42 return this;
43 }
44
45 inline Pin* as_open_drain(){
46 if( this->port_number == 0 ){ LPC_PINCON->PINMODE_OD0 |= (1<<this->pin); }
47 if( this->port_number == 1 ){ LPC_PINCON->PINMODE_OD1 |= (1<<this->pin); }
48 if( this->port_number == 2 ){ LPC_PINCON->PINMODE_OD2 |= (1<<this->pin); }
49 if( this->port_number == 3 ){ LPC_PINCON->PINMODE_OD3 |= (1<<this->pin); }
50 if( this->port_number == 4 ){ LPC_PINCON->PINMODE_OD4 |= (1<<this->pin); }
51 return this;
52 }
53
54 inline Pin* pull_up(){
55 // Set the two bits for this pin as 00
56 if( this->port_number == 0 && this->pin < 16 ){ LPC_PINCON->PINMODE0 &= ~(3<<( this->pin *2)); }
57 if( this->port_number == 0 && this->pin >= 16 ){ LPC_PINCON->PINMODE1 &= ~(3<<((this->pin-16)*2)); }
58 if( this->port_number == 1 && this->pin < 16 ){ LPC_PINCON->PINMODE2 &= ~(3<<( this->pin *2)); }
59 if( this->port_number == 1 && this->pin >= 16 ){ LPC_PINCON->PINMODE3 &= ~(3<<((this->pin-16)*2)); }
60 if( this->port_number == 2 && this->pin < 16 ){ LPC_PINCON->PINMODE4 &= ~(3<<( this->pin *2)); }
61 if( this->port_number == 3 && this->pin >= 16 ){ LPC_PINCON->PINMODE7 &= ~(3<<((this->pin-16)*2)); }
62 if( this->port_number == 4 && this->pin >= 16 ){ LPC_PINCON->PINMODE9 &= ~(3<<((this->pin-16)*2)); }
63 return this;
64 }
65
66 inline Pin* pull_down(){
67 // Set the two bits for this pin as 00
68 if( this->port_number == 0 && this->pin < 16 ){ LPC_PINCON->PINMODE0 |= (3<<( this->pin *2)); }
69 if( this->port_number == 0 && this->pin >= 16 ){ LPC_PINCON->PINMODE1 |= (3<<((this->pin-16)*2)); }
70 if( this->port_number == 1 && this->pin < 16 ){ LPC_PINCON->PINMODE2 |= (3<<( this->pin *2)); }
71 if( this->port_number == 1 && this->pin >= 16 ){ LPC_PINCON->PINMODE3 |= (3<<((this->pin-16)*2)); }
72 if( this->port_number == 2 && this->pin < 16 ){ LPC_PINCON->PINMODE4 |= (3<<( this->pin *2)); }
73 if( this->port_number == 3 && this->pin >= 16 ){ LPC_PINCON->PINMODE7 |= (3<<((this->pin-16)*2)); }
74 if( this->port_number == 4 && this->pin >= 16 ){ LPC_PINCON->PINMODE9 |= (3<<((this->pin-16)*2)); }
75 return this;
76 }
77
78 inline bool get(){
79 return this->inverting ^ (( this->port->FIOPIN >> this->pin ) & 1);
80 }
81
82 inline void set(bool value){
83 if( this->inverting ^ value ){
84 this->port->FIOSET = 1 << this->pin;
85 }else{
86 this->port->FIOCLR = 1 << this->pin;
87 }
88 }
89
90 bool inverting;
91 LPC_GPIO_TypeDef* port;
92 char port_number;
93 char pin;
94 };
95
96
97
98
99 #endif