libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
pin.h
Go to the documentation of this file.
1 #ifndef LIBPROPELLER_PIN_H_
2 #define LIBPROPELLER_PIN_H_
3 
4 // Comment out one definition of INLINE.
5 #define INLINE __attribute__((always_inline)) inline
6 //#define INLINE inline
7 
8 #include <propeller.h>
9 
41 class Pin {
42 public:
46  Pin();
47 
48 
53  Pin(int pin);
54 
58  int getPin(void);
59 
60 
63  void high(void);
64 
67  void low(void);
68 
72  void toggle(void);
73 
78  int input(void);
79 
84  void output(int setting);
85 
92  bool isOutput(void);
93 
94 
95 
110  void pwm(const int decihz, const bool useCTRA = true, Pin * alternatePin = NULL) {
111 
112  low();
113 
114  const int frq = (decihz * (((1 << 30) / CLKFREQ) << 2)) / 10;
115  int ctr = (0b00101000 << 23) + pinNumber;
116 
117  if(alternatePin != NULL){
118  ctr += alternatePin->getPin() << 9;
119  alternatePin->setOutput();
120  alternatePin->low();
121  }
122 
123 
124  if(decihz == 0){
125  ctr = 0;
126  }
127 
128  if (useCTRA) {
129  FRQA = frq;
130  CTRA = ctr;
131  } else {
132  FRQB = frq;
133  CTRB = ctr;
134  }
135 
136  setOutput();
137  }
138 
139 
140 private:
141  unsigned int pin_mask;
142  int pinNumber;
143  void setOutput();
144 };
145 
146 INLINE Pin::Pin() : pin_mask(0) {
147  pinNumber = -1;
148 }
149 
150 INLINE Pin::Pin(int pin) : pin_mask(1 << pin) {
151  pinNumber = pin;
152 }
153 
154 INLINE int Pin::getPin(void) {
155  return pinNumber;
156 }
157 
159  OUTA |= pin_mask;
160  setOutput();
161 }
162 
163 INLINE void Pin::low() {
164  OUTA &= ~pin_mask;
165  setOutput();
166 }
167 
169  OUTA ^= pin_mask;
170  setOutput();
171 }
172 
173 INLINE void Pin::output(int setting) {
174  if (setting == 1)
175  high();
176  else
177  low();
178 }
179 
181  return (DIRA & pin_mask) ? true : false;
182 }
183 
185  DIRA &= ~pin_mask;
186  return (INA & pin_mask) != 0;
187 }
188 
189 INLINE void Pin::setOutput() {
190  DIRA |= pin_mask;
191 }
192 
193 
194 #endif // LIBPROPELLER_PIN_H_
195