📄 inout.h
字号:
/* InOut.h
*
* Declarations for classes that handle input and output.
*
* Normal sense for inputs is expected to be high when open,
* low when closed. To reverse this, define IN_REVERSE.
*
* Normal sense for LEDs is expected to be off when high,
* on when low. To reverse this, define LED_REVERSE.
*
* Revisions:
* 07-13-06 included in LCDSample project
* 07-05-06 version 1 in master library
*
* Written by Cathy Saxton
* robotics@idleloop.com
*/
#pragma once
/* IN -- input class, e.g. for switches */
class IN
{
public:
/* constructor for setting the register and pin (0-7),
and specifying whether to enable the pull-up resistor */
IN(volatile uint8_t *pregDDR, volatile uint8_t *pregPORT,
volatile uint8_t *pregPIN, char pin, bool fPullup);
/* returns true if pin is reading high, else false */
inline bool FHigh() const { return *m_preg & m_bit; }
/* returns true if pin is reading low, else false */
inline bool FLow() const { return !FHigh(); }
/* helpers for switches */
#ifdef IN_REVERSE // reverse sense: low when open; high when closed
inline bool FOpen() const { return FLow(); }
inline bool FClosed() const { return FHigh(); }
#else // normal sense: high when open; low when closed
inline bool FOpen() const { return FHigh(); }
inline bool FClosed() const { return FLow(); }
#endif
/* checks for button press; if not pressed, returns false; else
waits for release (including debouncing both press & release) */
bool FPressed() const;
private:
uchar m_bit;
volatile uint8_t *m_preg;
};
/* OUT -- output class, e.g. for LEDs */
class OUT
{
public:
/* constructor for setting the register and port (0-7),
and initial state (low/high output) */
OUT(volatile uint8_t *pregDDR, volatile uint8_t *pregPORT,
volatile uint8_t *pregPIN, char port, bool fStartHigh);
inline void SetHigh() const { *m_preg |= m_bit; } // set output high
inline void SetLow() const { *m_preg &= ~m_bit; } // set output low
inline void Toggle() const { *m_preg ^= m_bit; } // toggle output
/* helpers for LEDs */
#ifdef LED_REVERSE // reverse sense: on when high, off when low
inline void On() const { SetHigh(); }
inline void Off() const { SetLow(); }
#else // normal sense: on when low, off when high
inline void On() const { SetLow(); }
inline void Off() const { SetHigh(); }
#endif
void Light(char fOn) const; // set LED on or off
private:
uchar m_bit;
volatile uint8_t *m_preg;
friend class MOTOR;
friend class LCD; // needed for serial LCD
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -