half-way commit to explain some of what is going on
[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 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 return this->inverting ^ (( this->port->FIOPIN >> this->pin ) & 1);
44 }
45
46 inline void set(bool value){
47 if( this->inverting ^ value ){
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;
56 char port_number;
57 char pin;
58 };
59
60
61
62
63 #endif