Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
Diff: libs/Pin.cpp
- Revision:
- 3:f151d08d335c
- Parent:
- 2:1df0b61d3b5a
--- a/libs/Pin.cpp Fri Feb 28 18:52:52 2014 -0800 +++ b/libs/Pin.cpp Sun Mar 02 06:33:08 2014 +0000 @@ -1,6 +1,7 @@ #include "Pin.h" #include "utils.h" +#include "Targets/Target.h" Pin::Pin(){ this->inverting= false; @@ -8,85 +9,53 @@ // Make a new pin object from a string Pin* Pin::from_string(std::string value){ - LPC_GPIO_TypeDef* gpios[5] ={LPC_GPIO0,LPC_GPIO1,LPC_GPIO2,LPC_GPIO3,LPC_GPIO4}; - - // cs is the current position in the string - const char* cs = value.c_str(); - // cn is the position of the next char after the number we just read - char* cn = NULL; - - // grab first integer as port. pointer to first non-digit goes in cn - this->port_number = strtol(cs, &cn, 10); - // if cn > cs then strtol read at least one digit - if ((cn > cs) && (port_number <= 4)){ - // translate port index into something useful - this->port = gpios[(unsigned int) this->port_number]; - // if the char after the first integer is a . then we should expect a pin index next - if (*cn == '.'){ - // move pointer to first digit (hopefully) of pin index - cs = ++cn; - - // grab pin index. - this->pin = strtol(cs, &cn, 10); - - // if strtol read some numbers, cn will point to the first non-digit - if ((cn > cs) && (pin < 32)){ - this->port->FIOMASK &= ~(1 << this->pin); + // Find the end of the pin name. + std::string::size_type e = value.find_first_of("!o^V-@\n\r\t "); + name = pin_name_from_string(value.substr(0, e)); + if (name == NC) + return this; + + gpio_init(&pin, name, PIN_INPUT); + + // The current position in the string + const char* cn = value.c_str() + e; - // now check for modifiers:- - // ! = invert pin - // o = set pin to open drain - // ^ = set pin to pull up - // v = set pin to pull down - // - = set pin to no pull up or down - // @ = set pin to repeater mode - for (;*cn;cn++) { - switch(*cn) { - case '!': - this->inverting = true; - break; - case 'o': - as_open_drain(); - break; - case '^': - pull_up(); - break; - case 'v': - pull_down(); - break; - case '-': - pull_none(); - break; - case '@': - as_repeater(); - break; - default: - // skip any whitespace following the pin index - if (!is_whitespace(*cn)) - return this; - } - } - return this; - } + for (;*cn;cn++) { + switch(*cn) { + case '!': + this->inverting = true; + break; + case 'o': + as_open_drain(); + break; + case '^': + pull_up(); + break; + case 'v': + pull_down(); + break; + case '-': + pull_none(); + break; + case '@': + as_repeater(); + break; + default: + // skip any whitespace following the pin index + if (!is_whitespace(*cn)) + return this; } } // from_string failed. TODO: some sort of error - port_number = 0; - port = gpios[0]; - pin = 255; inverting = false; return this; } // Configure this pin as OD Pin* Pin::as_open_drain(){ - if (this->pin >= 32) return this; - if( this->port_number == 0 ){ LPC_PINCON->PINMODE_OD0 |= (1<<this->pin); } - if( this->port_number == 1 ){ LPC_PINCON->PINMODE_OD1 |= (1<<this->pin); } - if( this->port_number == 2 ){ LPC_PINCON->PINMODE_OD2 |= (1<<this->pin); } - if( this->port_number == 3 ){ LPC_PINCON->PINMODE_OD3 |= (1<<this->pin); } - if( this->port_number == 4 ){ LPC_PINCON->PINMODE_OD4 |= (1<<this->pin); } + if (!connected()) return this; + gpio_mode(&pin, OpenDrain); pull_none(); // no pull up by default return this; } @@ -94,56 +63,28 @@ // Configure this pin as a repeater Pin* Pin::as_repeater(){ - if (this->pin >= 32) return this; - // Set the two bits for this pin as 01 - if( this->port_number == 0 && this->pin < 16 ){ LPC_PINCON->PINMODE0 |= (1<<( this->pin*2)); LPC_PINCON->PINMODE0 &= ~(2<<( this->pin *2)); } - if( this->port_number == 0 && this->pin >= 16 ){ LPC_PINCON->PINMODE1 |= (1<<( this->pin*2)); LPC_PINCON->PINMODE1 &= ~(2<<((this->pin-16)*2)); } - if( this->port_number == 1 && this->pin < 16 ){ LPC_PINCON->PINMODE2 |= (1<<( this->pin*2)); LPC_PINCON->PINMODE2 &= ~(2<<( this->pin *2)); } - if( this->port_number == 1 && this->pin >= 16 ){ LPC_PINCON->PINMODE3 |= (1<<( this->pin*2)); LPC_PINCON->PINMODE3 &= ~(2<<((this->pin-16)*2)); } - if( this->port_number == 2 && this->pin < 16 ){ LPC_PINCON->PINMODE4 |= (1<<( this->pin*2)); LPC_PINCON->PINMODE4 &= ~(2<<( this->pin *2)); } - if( this->port_number == 3 && this->pin >= 16 ){ LPC_PINCON->PINMODE7 |= (1<<( this->pin*2)); LPC_PINCON->PINMODE7 &= ~(2<<((this->pin-16)*2)); } - if( this->port_number == 4 && this->pin >= 16 ){ LPC_PINCON->PINMODE9 |= (1<<( this->pin*2)); LPC_PINCON->PINMODE9 &= ~(2<<((this->pin-16)*2)); } + if (!connected()) return this; + // ? return this; } // Configure this pin as no pullup or pulldown Pin* Pin::pull_none(){ - if (this->pin >= 32) return this; - // Set the two bits for this pin as 10 - if( this->port_number == 0 && this->pin < 16 ){ LPC_PINCON->PINMODE0 |= (2<<( this->pin*2)); LPC_PINCON->PINMODE0 &= ~(1<<( this->pin *2)); } - if( this->port_number == 0 && this->pin >= 16 ){ LPC_PINCON->PINMODE1 |= (2<<( this->pin*2)); LPC_PINCON->PINMODE1 &= ~(1<<((this->pin-16)*2)); } - if( this->port_number == 1 && this->pin < 16 ){ LPC_PINCON->PINMODE2 |= (2<<( this->pin*2)); LPC_PINCON->PINMODE2 &= ~(1<<( this->pin *2)); } - if( this->port_number == 1 && this->pin >= 16 ){ LPC_PINCON->PINMODE3 |= (2<<( this->pin*2)); LPC_PINCON->PINMODE3 &= ~(1<<((this->pin-16)*2)); } - if( this->port_number == 2 && this->pin < 16 ){ LPC_PINCON->PINMODE4 |= (2<<( this->pin*2)); LPC_PINCON->PINMODE4 &= ~(1<<( this->pin *2)); } - if( this->port_number == 3 && this->pin >= 16 ){ LPC_PINCON->PINMODE7 |= (2<<( this->pin*2)); LPC_PINCON->PINMODE7 &= ~(1<<((this->pin-16)*2)); } - if( this->port_number == 4 && this->pin >= 16 ){ LPC_PINCON->PINMODE9 |= (2<<( this->pin*2)); LPC_PINCON->PINMODE9 &= ~(1<<((this->pin-16)*2)); } + if (!connected()) return this; + gpio_mode(&pin, PullNone); return this; } // Configure this pin as a pullup Pin* Pin::pull_up(){ - if (this->pin >= 32) return this; - // Set the two bits for this pin as 00 - if( this->port_number == 0 && this->pin < 16 ){ LPC_PINCON->PINMODE0 &= ~(3<<( this->pin *2)); } - if( this->port_number == 0 && this->pin >= 16 ){ LPC_PINCON->PINMODE1 &= ~(3<<((this->pin-16)*2)); } - if( this->port_number == 1 && this->pin < 16 ){ LPC_PINCON->PINMODE2 &= ~(3<<( this->pin *2)); } - if( this->port_number == 1 && this->pin >= 16 ){ LPC_PINCON->PINMODE3 &= ~(3<<((this->pin-16)*2)); } - if( this->port_number == 2 && this->pin < 16 ){ LPC_PINCON->PINMODE4 &= ~(3<<( this->pin *2)); } - if( this->port_number == 3 && this->pin >= 16 ){ LPC_PINCON->PINMODE7 &= ~(3<<((this->pin-16)*2)); } - if( this->port_number == 4 && this->pin >= 16 ){ LPC_PINCON->PINMODE9 &= ~(3<<((this->pin-16)*2)); } + if (!connected()) return this; + gpio_mode(&pin, PullUp); return this; } // Configure this pin as a pulldown Pin* Pin::pull_down(){ - if (this->pin >= 32) return this; - // Set the two bits for this pin as 11 - if( this->port_number == 0 && this->pin < 16 ){ LPC_PINCON->PINMODE0 |= (3<<( this->pin *2)); } - if( this->port_number == 0 && this->pin >= 16 ){ LPC_PINCON->PINMODE1 |= (3<<((this->pin-16)*2)); } - if( this->port_number == 1 && this->pin < 16 ){ LPC_PINCON->PINMODE2 |= (3<<( this->pin *2)); } - if( this->port_number == 1 && this->pin >= 16 ){ LPC_PINCON->PINMODE3 |= (3<<((this->pin-16)*2)); } - if( this->port_number == 2 && this->pin < 16 ){ LPC_PINCON->PINMODE4 |= (3<<( this->pin *2)); } - if( this->port_number == 3 && this->pin >= 16 ){ LPC_PINCON->PINMODE7 |= (3<<((this->pin-16)*2)); } - if( this->port_number == 4 && this->pin >= 16 ){ LPC_PINCON->PINMODE9 |= (3<<((this->pin-16)*2)); } + if (!connected()) return this; + gpio_mode(&pin, PullDown); return this; }