Merge pull request #96 from wolfmanjm/upstreamedge
[clinton/Smoothieware.git] / src / libs / Pin.cpp
1 #include "Pin.h"
2
3 #include "utils.h"
4
5 Pin::Pin(){}
6
7 Pin* Pin::from_string(std::string value)
8 {
9 LPC_GPIO_TypeDef* gpios[5] ={LPC_GPIO0,LPC_GPIO1,LPC_GPIO2,LPC_GPIO3,LPC_GPIO4};
10
11 // cs is the current position in the string
12 const char* cs = value.c_str();
13 // cn is the position of the next char after the number we just read
14 char* cn = NULL;
15
16 this->port_number = strtol(cs, &cn, 10);
17 if ((cn > cs) && (port_number <= 4))
18 {
19 this->port = gpios[(unsigned int) this->port_number];
20 if (*cn == '.')
21 {
22 cs = ++cn;
23 this->pin = strtol(cs, &cn, 10);
24 if ((cn > cs) & (pin < 32))
25 {
26 while (is_whitespace(*cn)) cn++;
27 this->inverting = (*cn == '!');
28
29 this->port->FIOMASK &= ~(1 << this->pin);
30
31 return this;
32 }
33 }
34 }
35
36 port_number = 0;
37 port = gpios[0];
38 pin = 255;
39 inverting = false;
40 return this;
41 }
42
43 Pin* Pin::as_open_drain()
44 {
45 if (this->pin >= 32) return this;
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 Pin* Pin::pull_up()
55 {
56 if (this->pin >= 32) return this;
57 // Set the two bits for this pin as 00
58 if( this->port_number == 0 && this->pin < 16 ){ LPC_PINCON->PINMODE0 &= ~(3<<( this->pin *2)); }
59 if( this->port_number == 0 && this->pin >= 16 ){ LPC_PINCON->PINMODE1 &= ~(3<<((this->pin-16)*2)); }
60 if( this->port_number == 1 && this->pin < 16 ){ LPC_PINCON->PINMODE2 &= ~(3<<( this->pin *2)); }
61 if( this->port_number == 1 && this->pin >= 16 ){ LPC_PINCON->PINMODE3 &= ~(3<<((this->pin-16)*2)); }
62 if( this->port_number == 2 && this->pin < 16 ){ LPC_PINCON->PINMODE4 &= ~(3<<( this->pin *2)); }
63 if( this->port_number == 3 && this->pin >= 16 ){ LPC_PINCON->PINMODE7 &= ~(3<<((this->pin-16)*2)); }
64 if( this->port_number == 4 && this->pin >= 16 ){ LPC_PINCON->PINMODE9 &= ~(3<<((this->pin-16)*2)); }
65 return this;
66 }
67
68 Pin* Pin::pull_down()
69 {
70 if (this->pin >= 32) return this;
71 // Set the two bits for this pin as 11
72 if( this->port_number == 0 && this->pin < 16 ){ LPC_PINCON->PINMODE0 |= (3<<( this->pin *2)); }
73 if( this->port_number == 0 && this->pin >= 16 ){ LPC_PINCON->PINMODE1 |= (3<<((this->pin-16)*2)); }
74 if( this->port_number == 1 && this->pin < 16 ){ LPC_PINCON->PINMODE2 |= (3<<( this->pin *2)); }
75 if( this->port_number == 1 && this->pin >= 16 ){ LPC_PINCON->PINMODE3 |= (3<<((this->pin-16)*2)); }
76 if( this->port_number == 2 && this->pin < 16 ){ LPC_PINCON->PINMODE4 |= (3<<( this->pin *2)); }
77 if( this->port_number == 3 && this->pin >= 16 ){ LPC_PINCON->PINMODE7 |= (3<<((this->pin-16)*2)); }
78 if( this->port_number == 4 && this->pin >= 16 ){ LPC_PINCON->PINMODE9 |= (3<<((this->pin-16)*2)); }
79 return this;
80 }