Commit | Line | Data |
---|---|---|
3b1e82d2 AW |
1 | #ifndef PIN_H |
2 | #define PIN_H | |
3 | ||
70a50442 | 4 | #include <stdlib.h> |
ae4a7dfc | 5 | #include <stdio.h> |
3b1e82d2 AW |
6 | #include <string> |
7 | ||
46106f58 MM |
8 | #include "libs/LPC17xx/sLPC17xx.h" // smoothed mbed.h lib |
9 | ||
ec764d32 | 10 | class Pin { |
3b1e82d2 | 11 | public: |
46106f58 | 12 | Pin(); |
3b1e82d2 | 13 | |
46106f58 | 14 | Pin* from_string(std::string value); |
3b1e82d2 | 15 | |
750277f8 | 16 | inline bool connected(){ |
46106f58 | 17 | return this->pin < 32; |
750277f8 AW |
18 | } |
19 | ||
20 | inline Pin* as_output(){ | |
46106f58 MM |
21 | if (this->pin < 32) |
22 | this->port->FIODIR |= 1<<this->pin; | |
3b1e82d2 | 23 | return this; |
df27a6a3 | 24 | } |
3b1e82d2 | 25 | |
750277f8 | 26 | inline Pin* as_input(){ |
46106f58 MM |
27 | if (this->pin < 32) |
28 | this->port->FIODIR &= ~(1<<this->pin); | |
81b547a1 | 29 | return this; |
df27a6a3 | 30 | } |
81b547a1 | 31 | |
46106f58 | 32 | Pin* as_open_drain(void); |
bee725fc | 33 | |
1d7df31d JM |
34 | Pin* as_repeater(void); |
35 | ||
46106f58 | 36 | Pin* pull_up(void); |
c52ce509 | 37 | |
46106f58 | 38 | Pin* pull_down(void); |
c52ce509 | 39 | |
2b7aefce JM |
40 | Pin* pull_none(void); |
41 | ||
81b547a1 | 42 | inline bool get(){ |
d4ee6ee2 | 43 | |
46106f58 | 44 | if (this->pin >= 32) return false; |
da3a10b9 | 45 | return this->inverting ^ (( this->port->FIOPIN >> this->pin ) & 1); |
81b547a1 AW |
46 | } |
47 | ||
ec764d32 MM |
48 | inline void set(bool value) |
49 | { | |
46106f58 | 50 | if (this->pin >= 32) return; |
ec764d32 | 51 | if ( this->inverting ^ value ) |
3b1e82d2 | 52 | this->port->FIOSET = 1 << this->pin; |
ec764d32 | 53 | else |
3b1e82d2 | 54 | this->port->FIOCLR = 1 << this->pin; |
ec764d32 MM |
55 | } |
56 | ||
3b1e82d2 | 57 | LPC_GPIO_TypeDef* port; |
c3795b32 | 58 | bool inverting; |
bee725fc | 59 | char port_number; |
11f8ba4e | 60 | unsigned char pin; |
3b1e82d2 AW |
61 | }; |
62 | ||
63 | ||
64 | ||
65 | ||
3b1e82d2 | 66 | #endif |