This library is togle sw on/off edge and level decision. input sw level noise and chatting cancel. MDX-20 controler用
Fork of DigitalSw by
Diff: DigitalSw.h
- Revision:
- 0:45e3ec3004a6
- Child:
- 1:cd8bb4f0b08d
diff -r 000000000000 -r 45e3ec3004a6 DigitalSw.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DigitalSw.h Sun Oct 23 09:36:27 2016 +0000 @@ -0,0 +1,124 @@ +/** + * Digital Switch Library + * chattering cancel process. + * + * + * <scematic> + * + * +---------- mbed (pxx) (internal pullUp) + * | + * o | sw + * | + * o + * | + * -+- GND + * + * <timing chart> + * + * down Side up Side + * interrupt ! ! + * off---------+ +------------------- + * | | ! + * on +----------------+ ! + * ! ! + * edge on edge off edge + * + * + * <example program> + * @code + * #include "mbed.h" + * #include "DigitalSw.h" + * + * DigitalSw sw(p21); + * + * DigitalOut level(LED4); + * DigitalOut onEdgeToggle(LED1); + * DigitalOut offEdgeToggle(LED2); + * + * int main() { + * while(1) { + * + * level = sw.getLevel(); + * + * if(sw.getOnEdge() == 1){ + * onEdgeToggle = !onEdgeToggle; + * } + * + * if(sw.getOffEdge() == 1){ + * offEdgeToggle = !offEdgeToggle; + * } + * } + * } + * @endcode + */ + + +#ifndef DIGITALSW_H +#define DIGITALSW_H + +#include "mbed.h" + +/** + * digital switch. + */ + + +/** + * @class DigitalSw + * @brief nothine + */ +class DigitalSw +{ + +public: + + /** Create a DigitalSw port, connected to the specified sw pins + * @param sw pin + * @note nothing + */ + DigitalSw(PinName sw); + + /** + * Reset the edge and level. + * edge = off(0), level = off(0) + */ + void reset(void); + + /** + * Get the number of level by the sw. + * + * @return Number of sw level. 0:Low(off) 1:Hi(on) + */ + int16_t getLevel(void); + + /** + * Get the number of on edge by the sw. + * + * @return Number of sw on edge. 0:no edge, 1:edge + */ + int16_t getOnEdge(void); + + /** + * Get the number of off edge by the sw. + * + * @return Number of sw off edge. 0:no edge, 1:edge + */ + int16_t getOffEdge(void); + + +private: + + void swLevelUp(void); + void swLevelDown(void); + + + InterruptIn _sw; + + volatile int16_t _onEdge; + volatile int16_t _offEdge; + volatile int16_t _levelNow; + volatile int16_t _levelOld; + +}; + +#endif /* DIGITALSW_H */