Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/Pin.h@0:31e91bb0ef3c, 2012-07-31 (annotated)
- Committer:
- scachat
- Date:
- Tue Jul 31 21:11:18 2012 +0000
- Revision:
- 0:31e91bb0ef3c
- Child:
- 1:ab59fc9af055
smoothie port to mbed online compiler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scachat | 0:31e91bb0ef3c | 1 | #ifndef PIN_H |
scachat | 0:31e91bb0ef3c | 2 | #define PIN_H |
scachat | 0:31e91bb0ef3c | 3 | |
scachat | 0:31e91bb0ef3c | 4 | #include <stdlib.h> |
scachat | 0:31e91bb0ef3c | 5 | #include "mbed.h" // smoothed mbed.h lib |
scachat | 0:31e91bb0ef3c | 6 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 7 | #include "libs/utils.h" |
scachat | 0:31e91bb0ef3c | 8 | #include <string> |
scachat | 0:31e91bb0ef3c | 9 | |
scachat | 0:31e91bb0ef3c | 10 | class Pin{ |
scachat | 0:31e91bb0ef3c | 11 | public: |
scachat | 0:31e91bb0ef3c | 12 | Pin(){ } |
scachat | 0:31e91bb0ef3c | 13 | |
scachat | 0:31e91bb0ef3c | 14 | Pin* from_string(std::string value){ |
scachat | 0:31e91bb0ef3c | 15 | LPC_GPIO_TypeDef* gpios[5] ={LPC_GPIO0,LPC_GPIO1,LPC_GPIO2,LPC_GPIO3,LPC_GPIO4}; |
scachat | 0:31e91bb0ef3c | 16 | this->port_number = atoi(value.substr(0,1).c_str()); |
scachat | 0:31e91bb0ef3c | 17 | this->port = gpios[this->port_number]; |
scachat | 0:31e91bb0ef3c | 18 | this->inverting = ( value.find_first_of("!")!=string::npos ? true : false ); |
scachat | 0:31e91bb0ef3c | 19 | this->pin = atoi( value.substr(2, value.size()-2-(this->inverting?1:0)).c_str() ); |
scachat | 0:31e91bb0ef3c | 20 | return this; |
scachat | 0:31e91bb0ef3c | 21 | } |
scachat | 0:31e91bb0ef3c | 22 | |
scachat | 0:31e91bb0ef3c | 23 | inline Pin* as_output(){ |
scachat | 0:31e91bb0ef3c | 24 | this->port->FIODIR |= 1<<this->pin; |
scachat | 0:31e91bb0ef3c | 25 | return this; |
scachat | 0:31e91bb0ef3c | 26 | } |
scachat | 0:31e91bb0ef3c | 27 | |
scachat | 0:31e91bb0ef3c | 28 | inline Pin* as_input(){ |
scachat | 0:31e91bb0ef3c | 29 | this->port->FIODIR &= ~(1<<this->pin); |
scachat | 0:31e91bb0ef3c | 30 | return this; |
scachat | 0:31e91bb0ef3c | 31 | } |
scachat | 0:31e91bb0ef3c | 32 | |
scachat | 0:31e91bb0ef3c | 33 | inline Pin* as_open_drain(){ |
scachat | 0:31e91bb0ef3c | 34 | if( this->port_number == 0 ){ LPC_PINCON->PINMODE_OD0 |= (1<<this->pin); } |
scachat | 0:31e91bb0ef3c | 35 | if( this->port_number == 1 ){ LPC_PINCON->PINMODE_OD1 |= (1<<this->pin); } |
scachat | 0:31e91bb0ef3c | 36 | if( this->port_number == 2 ){ LPC_PINCON->PINMODE_OD2 |= (1<<this->pin); } |
scachat | 0:31e91bb0ef3c | 37 | if( this->port_number == 3 ){ LPC_PINCON->PINMODE_OD3 |= (1<<this->pin); } |
scachat | 0:31e91bb0ef3c | 38 | if( this->port_number == 4 ){ LPC_PINCON->PINMODE_OD4 |= (1<<this->pin); } |
scachat | 0:31e91bb0ef3c | 39 | return this; |
scachat | 0:31e91bb0ef3c | 40 | } |
scachat | 0:31e91bb0ef3c | 41 | |
scachat | 0:31e91bb0ef3c | 42 | inline bool get(){ |
scachat | 0:31e91bb0ef3c | 43 | return this->inverting ^ (( this->port->FIOPIN >> this->pin ) & 1); |
scachat | 0:31e91bb0ef3c | 44 | } |
scachat | 0:31e91bb0ef3c | 45 | |
scachat | 0:31e91bb0ef3c | 46 | inline void set(bool value){ |
scachat | 0:31e91bb0ef3c | 47 | value = this->inverting ^ value; |
scachat | 0:31e91bb0ef3c | 48 | if( value ){ |
scachat | 0:31e91bb0ef3c | 49 | this->port->FIOSET = 1 << this->pin; |
scachat | 0:31e91bb0ef3c | 50 | }else{ |
scachat | 0:31e91bb0ef3c | 51 | this->port->FIOCLR = 1 << this->pin; |
scachat | 0:31e91bb0ef3c | 52 | } |
scachat | 0:31e91bb0ef3c | 53 | } |
scachat | 0:31e91bb0ef3c | 54 | |
scachat | 0:31e91bb0ef3c | 55 | bool inverting; |
scachat | 0:31e91bb0ef3c | 56 | LPC_GPIO_TypeDef* port; |
scachat | 0:31e91bb0ef3c | 57 | char port_number; |
scachat | 0:31e91bb0ef3c | 58 | char pin; |
scachat | 0:31e91bb0ef3c | 59 | }; |
scachat | 0:31e91bb0ef3c | 60 | |
scachat | 0:31e91bb0ef3c | 61 | |
scachat | 0:31e91bb0ef3c | 62 | |
scachat | 0:31e91bb0ef3c | 63 | |
scachat | 0:31e91bb0ef3c | 64 | #endif |