PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
pin.h
Go to the documentation of this file.
1 
26 #pragma once
27 
28 #include <PropWare/PropWare.h>
29 #include <PropWare/gpio/port.h>
30 
31 namespace PropWare {
32 
36 class Pin: public Port {
37  public:
38  enum class Channel {
39  A,
40  B
41  };
42 
43  public:
51  static void flash_pin (const Pin::Mask pinMask, const unsigned int iterations = 10) {
52  flash_port(pinMask, iterations);
53  }
54 
55  public:
62  : Port(mask),
63  m_channel(Channel::A) {
64  this->update_pin_number();
65  }
66 
71  Pin (const Pin::Mask mask, const Pin::Dir direction)
72  : Port(mask, direction),
73  m_channel(Channel::A) {
74  this->update_pin_number();
75  }
76 
80  void set_mask (const Pin::Mask mask) {
81  this->Port::set_mask(mask);
82  this->update_pin_number();
83  }
84 
90  void set_pin_number (const uint8_t pinNum) {
91  this->set_mask(Pin::to_mask(pinNum));
92  }
93 
97  uint_fast8_t get_pin_number () const {
98  return this->m_pinNumber;
99  }
100 
101  Pin::Mask get_mask () const {
102  return static_cast<Pin::Mask>(this->m_mask);
103  }
104 
105  Channel get_channel () const {
106  return this->m_channel;
107  }
108 
109  void set_channel (const Channel channel) {
110  this->m_channel = channel;
111  }
112 
118  void write (const bool value) const {
119  if (value)
120  this->set();
121  else
122  this->clear();
123  }
124 
130  bool read () const {
131  return static_cast<bool>(this->read_fast());
132  }
133 
140  void wait_until_high () const {
141  waitpeq(this->m_mask, this->m_mask);
142  }
143 
150  void wait_until_low () const {
151  waitpeq(0, this->m_mask);
152  }
153 
160  void wait_until_toggle () const {
161  waitpne(this->read_fast(), this->m_mask);
162  }
163 
171  bool is_switch_low (const uint16_t debounceDelayInMillis = 3) const {
172  this->set_dir(Dir::IN); // Set the pin as input
173 
174  if (!(this->read())) { // If pin is grounded (aka, pressed)
175  waitcnt(debounceDelayInMillis * MILLISECOND + CNT);
176 
177  return !(this->read()); // Check if it's still pressed
178  }
179 
180  return false;
181  }
182 
198  int rc_time (const bool state, const uint32_t timeout = SECOND / 4) {
199  // Taken from Simple's rc_time(int pin, int state) in rcTime.C
200  /*
201  if(iodt == 0) // If dt not initialized
202  {
203  set_io_dt(CLKFREQ/1000000); // Set up timed I/O time increment
204  set_io_timeout(CLKFREQ/4); // Set up timeout
205  }
206  */
207  uint32_t ctr = static_cast<uint32_t>((8 + ((!state & 1) * 4)) << 26); // POS detector counter setup
208  ctr += this->m_pinNumber; // Add pin to setup
209  const uint32_t startTime = CNT; // Mark current time
210  if (CTRA == 0) {
211  // If CTRA unused
212  CTRA = ctr; // Configure CTRA
213  FRQA = 1; // FRQA increments PHSA by 1
214  this->set_dir_in();
215  PHSA = 0; // Clear PHSA
216  // Wait for decay or timeout
217  while (state == this->read() && (CNT - startTime <= timeout));
218  CTRA = 0; // Stop the counter module
219  return PHSA;
220  } else if (CTRB == 0) {
221  // If CTRA used, try CTRB
222  CTRB = ctr; // Same procedure as for CTRA
223  FRQB = 1;
224  this->set_dir_in();
225  PHSB = 0;
226  while (state == this->read() && (CNT - startTime <= timeout));
227  CTRB = 0;
228  return PHSB;
229  } else {
230  // If CTRA & CTRB in use
231  return -1;
232  }
233  }
234 
243  void start_hardware_pwm (const uint32_t frequency) const {
244  this->stop_hardware_pwm();
245 
246  const uint32_t frq = static_cast<uint32_t>((UINT32_MAX + 1ULL) * frequency / CLKFREQ);
247  const uint32_t ctr = (4 << 26) | this->m_pinNumber;
248  if (Channel::A == this->m_channel) {
249  FRQA = frq;
250  PHSA = 0;
251  CTRA = ctr;
252  } else if (Channel::B == this->m_channel) {
253  FRQB = frq;
254  PHSB = 0;
255  CTRB = ctr;
256  }
257  }
258 
262  void stop_hardware_pwm () const {
263  if (Channel::A == this->m_channel) {
264  CTRA = 0;
265  } else if (Channel::B == this->m_channel) {
266  CTRB = 0;
267  }
268  }
269 
270  private:
271  void update_pin_number () {
272  this->m_pinNumber = Pin::from_mask(static_cast<Mask>(this->m_mask));
273  }
274 
275 
276  /****************************************
277  *** Nonsensical functions for a pin ***
278  ****************************************/
282  void set_mask (const uint32_t mask) {
283  }
284 
288  void add_pins (const uint32_t mask) {
289  }
290 
294  uint32_t read_fast () const {
295  return this->Port::read_fast();
296  }
297 
298  private:
299  Channel m_channel;
300  uint_fast8_t m_pinNumber;
301 };
302 
303 }
PHSB
#define PHSB
Counter B phase accumulation register.
Definition: propeller1.h:175
PropWare::Pin::stop_hardware_pwm
void stop_hardware_pwm() const
Stop the hardware counter.
Definition: pin.h:262
PropWare::Port::NULL_PIN
@ NULL_PIN
Definition: port.h:76
PropWare::Port::from_mask
static uint8_t from_mask(const Mask mask)
Determine which pin number based on a pin mask.
Definition: port.h:98
PropWare::Pin::rc_time
int rc_time(const bool state, const uint32_t timeout=SECOND/4)
Set to input and measure the time it takes a signal to transition from a start state to the opposite ...
Definition: pin.h:198
PropWare::Port::read_fast
uint32_t read_fast() const
Allow easy reading of only selected pins from a port.
Definition: port.h:291
PropWare::Port::Dir::IN
@ IN
PropWare::Pin::get_pin_number
uint_fast8_t get_pin_number() const
Get the pin's number (an integer, 0 through 31)
Definition: pin.h:97
PropWare::Pin::set_mask
void set_mask(const Pin::Mask mask)
Definition: pin.h:80
waitpeq
#define waitpeq(state, mask)
Wait until INA equal state & mask.
Definition: propeller.h:190
PHSA
#define PHSA
Counter A phase accumulation register.
Definition: propeller1.h:173
PropWare::Pin::set_pin_number
void set_pin_number(const uint8_t pinNum)
Set a Pin's mask based on the pin number (an integer, 0 through 31)
Definition: pin.h:90
PropWare::Port::Mask
Mask
Definition: port.h:43
port.h
FRQA
#define FRQA
Counter A frequency register.
Definition: propeller1.h:169
timeout
int timeout(int time)
Compares the time against the time elapsed since mark (deprecated).
Definition: timeout.c:19
PropWare::Port::flash_port
static void flash_port(const uint32_t pinMask, unsigned int iterations=10)
Great for quick debugging to ensure a line of code is executed, this will quickly flash a given set o...
Definition: port.h:129
PropWare::Pin::wait_until_low
void wait_until_low() const
Hold cog execution until an input pin goes low.
Definition: pin.h:150
PropWare::Pin::Pin
Pin(const PropWare::Pin::Mask mask=NULL_PIN)
Create a Pin variable.
Definition: pin.h:61
PropWare::Port::set_dir_in
void set_dir_in() const
Set the port for input.
Definition: port.h:217
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::Port::set_dir
void set_dir(const PropWare::Port::Dir direction) const
Set port as either input or output.
Definition: port.h:191
PropWare::Pin::start_hardware_pwm
void start_hardware_pwm(const uint32_t frequency) const
Output a PWM signal on this pin.
Definition: pin.h:243
PropWare::Port::Dir
Dir
Definition: port.h:82
PropWare::Pin::read
bool read() const
Read the value from a single pin and return its state.
Definition: pin.h:130
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
FRQB
#define FRQB
Counter B frequency register.
Definition: propeller1.h:171
PropWare::Pin::write
void write(const bool value) const
Set or clear the pin programmatically.
Definition: pin.h:118
PropWare::Pin::is_switch_low
bool is_switch_low(const uint16_t debounceDelayInMillis=3) const
Allow easy switch-press detection of any pin; Includes de-bounce protection.
Definition: pin.h:171
PropWare::Pin::Pin
Pin(const Pin::Mask mask, const Pin::Dir direction)
Definition: pin.h:71
PropWare.h
PropWare::Pin::wait_until_toggle
void wait_until_toggle() const
Hold cog execution until an input pin toggles.
Definition: pin.h:160
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
PropWare::Port::set
void set() const
Set selected output port high (set all pins to 1)
Definition: port.h:226
CTRB
#define CTRB
Counter B control register.
Definition: propeller1.h:167
PropWare::Port::set_mask
void set_mask(const uint32_t mask)
Set the mask for this port.
Definition: port.h:163
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
PropWare::Pin::flash_pin
static void flash_pin(const Pin::Mask pinMask, const unsigned int iterations=10)
Great for quick debugging to ensure a line of code is executed, this will quickly flash a given pin a...
Definition: pin.h:51
PropWare::Pin::wait_until_high
void wait_until_high() const
Hold cog execution until an input pin goes high.
Definition: pin.h:140
waitpne
#define waitpne(state, mask)
Wait until INA not equal state & mask.
Definition: propeller.h:197
PropWare::Port::to_mask
static Mask to_mask(const uint8_t pinNum)
Return a Mask type based on a pin number.
Definition: port.h:115
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Port::clear
void clear() const
Clear selected output port (set it to 0)
Definition: port.h:249
CTRA
#define CTRA
Counter A control register.
Definition: propeller1.h:165