Merge remote-tracking branch 'upstream/edge' into feature/filament-detector
[clinton/Smoothieware.git] / src / libs / Pin.h
1 #ifndef PIN_H
2 #define PIN_H
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string>
7
8 #include "libs/LPC17xx/sLPC17xx.h" // smoothed mbed.h lib
9 #include "PinNames.h"
10
11 namespace mbed {
12 class PwmOut;
13 class InterruptIn;
14 }
15
16 class Pin {
17 public:
18 Pin();
19
20 Pin* from_string(std::string value);
21
22 inline bool connected(){
23 return this->valid;
24 }
25
26 inline bool equals(const Pin& other) const {
27 return (this->pin == other.pin) && (this->port == other.port);
28 }
29
30 inline Pin* as_output(){
31 if (this->valid)
32 this->port->FIODIR |= 1<<this->pin;
33 return this;
34 }
35
36 inline Pin* as_input(){
37 if (this->valid)
38 this->port->FIODIR &= ~(1<<this->pin);
39 return this;
40 }
41
42 Pin* as_open_drain(void);
43
44 Pin* as_repeater(void);
45
46 Pin* pull_up(void);
47
48 Pin* pull_down(void);
49
50 Pin* pull_none(void);
51
52 inline bool get(){
53 if (!this->valid) return false;
54 return this->inverting ^ (( this->port->FIOPIN >> this->pin ) & 1);
55 }
56
57 inline void set(bool value)
58 {
59 if (!this->valid) return;
60 if ( this->inverting ^ value )
61 this->port->FIOSET = 1 << this->pin;
62 else
63 this->port->FIOCLR = 1 << this->pin;
64 }
65
66 mbed::PwmOut *hardware_pwm();
67
68 mbed::InterruptIn *interrupt_pin();
69
70 // these should be private, and use getters
71 LPC_GPIO_TypeDef* port;
72
73 unsigned char pin;
74 char port_number;
75 struct {
76 bool inverting:1;
77 bool valid:1;
78 };
79 };
80
81
82
83
84 #endif