started hacking on the new conveyor model
[clinton/Smoothieware.git] / src / libs / Pin.h
CommitLineData
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 8#include "libs/LPC17xx/sLPC17xx.h" // smoothed mbed.h lib
1c73b86f
PA
9#include "PinNames.h"
10
11namespace mbed {
12 class PwmOut;
d1097ef0 13 class InterruptIn;
1c73b86f 14}
46106f58 15
ec764d32 16class Pin {
3b1e82d2 17 public:
46106f58 18 Pin();
3b1e82d2 19
46106f58 20 Pin* from_string(std::string value);
3b1e82d2 21
750277f8 22 inline bool connected(){
e2c1da4b 23 return this->valid;
750277f8 24 }
e558f082
JM
25
26 inline bool equals(const Pin& other) const {
27 return (this->pin == other.pin) && (this->port == other.port);
28 }
750277f8
AW
29
30 inline Pin* as_output(){
e2c1da4b 31 if (this->valid)
46106f58 32 this->port->FIODIR |= 1<<this->pin;
3b1e82d2 33 return this;
df27a6a3 34 }
3b1e82d2 35
750277f8 36 inline Pin* as_input(){
e2c1da4b 37 if (this->valid)
46106f58 38 this->port->FIODIR &= ~(1<<this->pin);
81b547a1 39 return this;
df27a6a3 40 }
81b547a1 41
46106f58 42 Pin* as_open_drain(void);
bee725fc 43
1d7df31d
JM
44 Pin* as_repeater(void);
45
46106f58 46 Pin* pull_up(void);
c52ce509 47
46106f58 48 Pin* pull_down(void);
c52ce509 49
2b7aefce
JM
50 Pin* pull_none(void);
51
9e6014a6 52 inline bool get() const{
e2c1da4b 53 if (!this->valid) return false;
da3a10b9 54 return this->inverting ^ (( this->port->FIOPIN >> this->pin ) & 1);
81b547a1
AW
55 }
56
ec764d32
MM
57 inline void set(bool value)
58 {
e2c1da4b 59 if (!this->valid) return;
ec764d32 60 if ( this->inverting ^ value )
3b1e82d2 61 this->port->FIOSET = 1 << this->pin;
ec764d32 62 else
3b1e82d2 63 this->port->FIOCLR = 1 << this->pin;
ec764d32
MM
64 }
65
1c73b86f 66 mbed::PwmOut *hardware_pwm();
9a3add12 67
d1097ef0
JM
68 mbed::InterruptIn *interrupt_pin();
69
39f1d9bd
JM
70 bool is_inverting() const { return inverting; }
71 void set_inverting(bool f) { inverting= f; }
72
e2c1da4b 73 // these should be private, and use getters
3b1e82d2 74 LPC_GPIO_TypeDef* port;
e2c1da4b 75
efbce4f6
JM
76 unsigned char pin;
77 char port_number;
39f1d9bd
JM
78
79 private:
e2c1da4b 80 struct {
e2c1da4b
JM
81 bool inverting:1;
82 bool valid:1;
83 };
3b1e82d2
AW
84};
85
86
87
88
3b1e82d2 89#endif