ShiftReg library (74HC595, NUJ3711)
Revision 1:3741bc0fbba8, committed 2015-10-24
- Comitter:
- kysiki
- Date:
- Sat Oct 24 17:44:30 2015 +0000
- Parent:
- 0:5f1670338bef
- Commit message:
- 8bit, 16bit, 32bit???
Changed in this revision
ShiftReg.cpp | Show annotated file Show diff for this revision Revisions of this file |
ShiftReg.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ShiftReg.cpp Sat Oct 24 16:30:11 2015 +0000 +++ b/ShiftReg.cpp Sat Oct 24 17:44:30 2015 +0000 @@ -1,7 +1,8 @@ #include "ShiftReg.h" #include "mbed.h" - -ShiftReg::ShiftReg(PinName dataPin, PinName stbPin, + +template <class T_bit> +ShiftReg<T_bit>::ShiftReg(PinName dataPin, PinName stbPin, PinName clkPin, PinName clrPin) : DATA(dataPin), @@ -14,9 +15,10 @@ CLK = 1; CLR = 1; } - -void ShiftReg::send_data(uint8_t data) { - shiftOut(data); + +template <class T_bit> +void ShiftReg<T_bit>::send_data(T_bit data, uint8_t size) { + shiftOut(data, size); wait_us(1); #ifdef HC595 STB = 0; @@ -32,10 +34,14 @@ wait_us(1); } -void ShiftReg::shiftOut(uint8_t data) { - for(int i = 0; i < 8; i++) { +template <class T_bit> +void ShiftReg<T_bit>::shiftOut(T_bit data, uint8_t size) { + for(int i = 0; i < size; i++) { DATA = (data & (1 << i)); CLK = 0; CLK = 1; } -} \ No newline at end of file +} +template class ShiftReg<uint8_t>; +template class ShiftReg<uint16_t>; +template class ShiftReg<uint32_t>; \ No newline at end of file
--- a/ShiftReg.h Sat Oct 24 16:30:11 2015 +0000 +++ b/ShiftReg.h Sat Oct 24 17:44:30 2015 +0000 @@ -3,18 +3,23 @@ #define HC595 //#define NJU3711 #include "mbed.h" - + +template <class T_bit> class ShiftReg { public: ShiftReg(PinName dataPin, PinName stbPin, PinName clkPin, PinName clrPin); - void send_data(uint8_t data); + void send_data(T_bit data, uint8_t size); + typedef ShiftReg<uint8_t> ShiftReg8; + typedef ShiftReg<uint16_t> ShiftReg16; + typedef ShiftReg<uint32_t> ShiftReg32; + private: DigitalOut DATA; DigitalOut STB; DigitalOut CLK; DigitalOut CLR; - void shiftOut(uint8_t data); + void shiftOut(T_bit data, uint8_t size); }; #endif \ No newline at end of file