GROVE LEDバーのライブラリ
Revision 0:a2b97475d3af, committed 2013-11-30
- Comitter:
- jksoft
- Date:
- Sat Nov 30 03:04:10 2013 +0000
- Commit message:
- Rev1
Changed in this revision
LEDBar.cpp | Show annotated file Show diff for this revision Revisions of this file |
LEDBar.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LEDBar.cpp Sat Nov 30 03:04:10 2013 +0000 @@ -0,0 +1,152 @@ +#include "mbed.h" +#include "LEDBar.h" + +LED_Bar::LED_Bar(PinName clk_pin, PinName data_pin) :_clk_pin(clk_pin), _data_pin(data_pin) +{ +// _clk=clk; +// _dat=dat; + //DDR_Data |= BIT_Data; + //DDR_Clk |= BIT_Clk; + // pinMode(_clk,OUTPUT); //Data pin +// pinMode(_dat,OUTPUT); //CLK pin +} + +void LED_Bar::set_LED_Index(unsigned int index) +//index: range 0b000000000000~0b001111111111, 1 stands light whlie 0 stands off; +{ + unsigned char i; + send16bitData(CmdMode); + for (i=0;i<12;i++) + { + if (index&0x0001) + send16bitData(ON); + else + send16bitData(SHUT); + index= index>>1; + } + latchData(); +} +void LED_Bar::set_LED_Index(unsigned int index1,unsigned int index2) +//index: range 0b000000000000~0b001111111111, 1 stands light whlie 0 stands off; +{ + unsigned char i; + for (unsigned char j =0;j<2;j++) + { + send16bitData(CmdMode); + if (j==0) + { + for (i=0;i<12;i++) + { + if (index1&0x0001) + send16bitData(ON); + else + send16bitData(SHUT); + index1= index1>>1; + } + } + if (j==1) + { + for (i=0;i<12;i++) + { + if (index2&0x0001) + send16bitData(ON); + else + send16bitData(SHUT); + index2= index2>>1; + } + } + } + latchData(); +} +void LED_Bar::set_LED_Range(unsigned int indexTo) +//indexto : range 1~10.for example: 5, the 1~5 would be light +{ + unsigned char i; + + send16bitData(CmdMode); + for(i=0;i<12;i++) + { + if(i<indexTo) + { + send16bitData(ON); + } + else + { + send16bitData(SHUT); + } + // indexTo=indexTo>>1; + } + + latchData(); +} +void LED_Bar::set_LED_Range(unsigned int indexTo1,unsigned int indexTo2) +//indexto : range 1~10.for example: 5, the 1~5 would be light +{ + unsigned char i; + + for (unsigned char j =0;j<2;j++) + { + send16bitData(CmdMode); + if (j==0) + { + for(i=0;i<12;i++) + { + if(i<indexTo1) + { + send16bitData(ON); + } + else + { + send16bitData(SHUT); + } + } + } + if (j==1) + { + for(i=0;i<12;i++) + { + if(i<indexTo2) + { + send16bitData(ON); + } + else + { + send16bitData(SHUT); + } + } + } + + } + latchData(); +} +void LED_Bar::send16bitData(unsigned int data) +{ + for(unsigned char i=0;i<16;i++) + { + if(data&0x8000) + { + _data_pin = 1; + //PORT_Data |= BIT_Data; + } + else + { + _data_pin = 0; + //PORT_Data &=~ BIT_Data; + } + _clk_pin = !_clk_pin; + //PORT_Clk ^= BIT_Clk; + data <<= 1; + } +} +void LED_Bar::latchData(void) +{ + _data_pin = 0; + //PORT_Data &=~ BIT_Data; + wait_us(10); + //delayMicroseconds(10); + for(unsigned char i=0;i<8;i++) + { + _data_pin = !_data_pin; + //PORT_Data ^= BIT_Data; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LEDBar.h Sat Nov 30 03:04:10 2013 +0000 @@ -0,0 +1,36 @@ +#ifndef LED_Bar_H +#define LED_Bar_H +#include "mbed.h" + +#if 0 +#define DDR_Data DDRB +#define DDR_Clk DDRB +#define PORT_Data PORTB +#define PORT_Clk PORTB + +#define BIT_Data 0x01 +#define BIT_Clk 0x02 +#endif + +#define CmdMode 0x0000 //Work on 8-bit mode +#define ON 0x00ff //8-bit 1 data +#define SHUT 0x0000 //8-bit 0 data + +class LED_Bar +{ + public: + LED_Bar(PinName clk_pin, PinName data_pin); + void set_LED_Index(unsigned int index); + void set_LED_Index(unsigned int index1,unsigned int index2); + void set_LED_Range(unsigned int indexTo); + void set_LED_Range(unsigned int indexTo1,unsigned int indexTo2); + + private: + DigitalOut _clk_pin; + DigitalOut _data_pin; + void send16bitData(unsigned int data); + void latchData(void); + int _clk; + int _dat; +}; +#endif; \ No newline at end of file