GROVE LEDバーのライブラリ
LEDBar.cpp@0:a2b97475d3af, 2013-11-30 (annotated)
- Committer:
- jksoft
- Date:
- Sat Nov 30 03:04:10 2013 +0000
- Revision:
- 0:a2b97475d3af
Rev1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:a2b97475d3af | 1 | #include "mbed.h" |
jksoft | 0:a2b97475d3af | 2 | #include "LEDBar.h" |
jksoft | 0:a2b97475d3af | 3 | |
jksoft | 0:a2b97475d3af | 4 | LED_Bar::LED_Bar(PinName clk_pin, PinName data_pin) :_clk_pin(clk_pin), _data_pin(data_pin) |
jksoft | 0:a2b97475d3af | 5 | { |
jksoft | 0:a2b97475d3af | 6 | // _clk=clk; |
jksoft | 0:a2b97475d3af | 7 | // _dat=dat; |
jksoft | 0:a2b97475d3af | 8 | //DDR_Data |= BIT_Data; |
jksoft | 0:a2b97475d3af | 9 | //DDR_Clk |= BIT_Clk; |
jksoft | 0:a2b97475d3af | 10 | // pinMode(_clk,OUTPUT); //Data pin |
jksoft | 0:a2b97475d3af | 11 | // pinMode(_dat,OUTPUT); //CLK pin |
jksoft | 0:a2b97475d3af | 12 | } |
jksoft | 0:a2b97475d3af | 13 | |
jksoft | 0:a2b97475d3af | 14 | void LED_Bar::set_LED_Index(unsigned int index) |
jksoft | 0:a2b97475d3af | 15 | //index: range 0b000000000000~0b001111111111, 1 stands light whlie 0 stands off; |
jksoft | 0:a2b97475d3af | 16 | { |
jksoft | 0:a2b97475d3af | 17 | unsigned char i; |
jksoft | 0:a2b97475d3af | 18 | send16bitData(CmdMode); |
jksoft | 0:a2b97475d3af | 19 | for (i=0;i<12;i++) |
jksoft | 0:a2b97475d3af | 20 | { |
jksoft | 0:a2b97475d3af | 21 | if (index&0x0001) |
jksoft | 0:a2b97475d3af | 22 | send16bitData(ON); |
jksoft | 0:a2b97475d3af | 23 | else |
jksoft | 0:a2b97475d3af | 24 | send16bitData(SHUT); |
jksoft | 0:a2b97475d3af | 25 | index= index>>1; |
jksoft | 0:a2b97475d3af | 26 | } |
jksoft | 0:a2b97475d3af | 27 | latchData(); |
jksoft | 0:a2b97475d3af | 28 | } |
jksoft | 0:a2b97475d3af | 29 | void LED_Bar::set_LED_Index(unsigned int index1,unsigned int index2) |
jksoft | 0:a2b97475d3af | 30 | //index: range 0b000000000000~0b001111111111, 1 stands light whlie 0 stands off; |
jksoft | 0:a2b97475d3af | 31 | { |
jksoft | 0:a2b97475d3af | 32 | unsigned char i; |
jksoft | 0:a2b97475d3af | 33 | for (unsigned char j =0;j<2;j++) |
jksoft | 0:a2b97475d3af | 34 | { |
jksoft | 0:a2b97475d3af | 35 | send16bitData(CmdMode); |
jksoft | 0:a2b97475d3af | 36 | if (j==0) |
jksoft | 0:a2b97475d3af | 37 | { |
jksoft | 0:a2b97475d3af | 38 | for (i=0;i<12;i++) |
jksoft | 0:a2b97475d3af | 39 | { |
jksoft | 0:a2b97475d3af | 40 | if (index1&0x0001) |
jksoft | 0:a2b97475d3af | 41 | send16bitData(ON); |
jksoft | 0:a2b97475d3af | 42 | else |
jksoft | 0:a2b97475d3af | 43 | send16bitData(SHUT); |
jksoft | 0:a2b97475d3af | 44 | index1= index1>>1; |
jksoft | 0:a2b97475d3af | 45 | } |
jksoft | 0:a2b97475d3af | 46 | } |
jksoft | 0:a2b97475d3af | 47 | if (j==1) |
jksoft | 0:a2b97475d3af | 48 | { |
jksoft | 0:a2b97475d3af | 49 | for (i=0;i<12;i++) |
jksoft | 0:a2b97475d3af | 50 | { |
jksoft | 0:a2b97475d3af | 51 | if (index2&0x0001) |
jksoft | 0:a2b97475d3af | 52 | send16bitData(ON); |
jksoft | 0:a2b97475d3af | 53 | else |
jksoft | 0:a2b97475d3af | 54 | send16bitData(SHUT); |
jksoft | 0:a2b97475d3af | 55 | index2= index2>>1; |
jksoft | 0:a2b97475d3af | 56 | } |
jksoft | 0:a2b97475d3af | 57 | } |
jksoft | 0:a2b97475d3af | 58 | } |
jksoft | 0:a2b97475d3af | 59 | latchData(); |
jksoft | 0:a2b97475d3af | 60 | } |
jksoft | 0:a2b97475d3af | 61 | void LED_Bar::set_LED_Range(unsigned int indexTo) |
jksoft | 0:a2b97475d3af | 62 | //indexto : range 1~10.for example: 5, the 1~5 would be light |
jksoft | 0:a2b97475d3af | 63 | { |
jksoft | 0:a2b97475d3af | 64 | unsigned char i; |
jksoft | 0:a2b97475d3af | 65 | |
jksoft | 0:a2b97475d3af | 66 | send16bitData(CmdMode); |
jksoft | 0:a2b97475d3af | 67 | for(i=0;i<12;i++) |
jksoft | 0:a2b97475d3af | 68 | { |
jksoft | 0:a2b97475d3af | 69 | if(i<indexTo) |
jksoft | 0:a2b97475d3af | 70 | { |
jksoft | 0:a2b97475d3af | 71 | send16bitData(ON); |
jksoft | 0:a2b97475d3af | 72 | } |
jksoft | 0:a2b97475d3af | 73 | else |
jksoft | 0:a2b97475d3af | 74 | { |
jksoft | 0:a2b97475d3af | 75 | send16bitData(SHUT); |
jksoft | 0:a2b97475d3af | 76 | } |
jksoft | 0:a2b97475d3af | 77 | // indexTo=indexTo>>1; |
jksoft | 0:a2b97475d3af | 78 | } |
jksoft | 0:a2b97475d3af | 79 | |
jksoft | 0:a2b97475d3af | 80 | latchData(); |
jksoft | 0:a2b97475d3af | 81 | } |
jksoft | 0:a2b97475d3af | 82 | void LED_Bar::set_LED_Range(unsigned int indexTo1,unsigned int indexTo2) |
jksoft | 0:a2b97475d3af | 83 | //indexto : range 1~10.for example: 5, the 1~5 would be light |
jksoft | 0:a2b97475d3af | 84 | { |
jksoft | 0:a2b97475d3af | 85 | unsigned char i; |
jksoft | 0:a2b97475d3af | 86 | |
jksoft | 0:a2b97475d3af | 87 | for (unsigned char j =0;j<2;j++) |
jksoft | 0:a2b97475d3af | 88 | { |
jksoft | 0:a2b97475d3af | 89 | send16bitData(CmdMode); |
jksoft | 0:a2b97475d3af | 90 | if (j==0) |
jksoft | 0:a2b97475d3af | 91 | { |
jksoft | 0:a2b97475d3af | 92 | for(i=0;i<12;i++) |
jksoft | 0:a2b97475d3af | 93 | { |
jksoft | 0:a2b97475d3af | 94 | if(i<indexTo1) |
jksoft | 0:a2b97475d3af | 95 | { |
jksoft | 0:a2b97475d3af | 96 | send16bitData(ON); |
jksoft | 0:a2b97475d3af | 97 | } |
jksoft | 0:a2b97475d3af | 98 | else |
jksoft | 0:a2b97475d3af | 99 | { |
jksoft | 0:a2b97475d3af | 100 | send16bitData(SHUT); |
jksoft | 0:a2b97475d3af | 101 | } |
jksoft | 0:a2b97475d3af | 102 | } |
jksoft | 0:a2b97475d3af | 103 | } |
jksoft | 0:a2b97475d3af | 104 | if (j==1) |
jksoft | 0:a2b97475d3af | 105 | { |
jksoft | 0:a2b97475d3af | 106 | for(i=0;i<12;i++) |
jksoft | 0:a2b97475d3af | 107 | { |
jksoft | 0:a2b97475d3af | 108 | if(i<indexTo2) |
jksoft | 0:a2b97475d3af | 109 | { |
jksoft | 0:a2b97475d3af | 110 | send16bitData(ON); |
jksoft | 0:a2b97475d3af | 111 | } |
jksoft | 0:a2b97475d3af | 112 | else |
jksoft | 0:a2b97475d3af | 113 | { |
jksoft | 0:a2b97475d3af | 114 | send16bitData(SHUT); |
jksoft | 0:a2b97475d3af | 115 | } |
jksoft | 0:a2b97475d3af | 116 | } |
jksoft | 0:a2b97475d3af | 117 | } |
jksoft | 0:a2b97475d3af | 118 | |
jksoft | 0:a2b97475d3af | 119 | } |
jksoft | 0:a2b97475d3af | 120 | latchData(); |
jksoft | 0:a2b97475d3af | 121 | } |
jksoft | 0:a2b97475d3af | 122 | void LED_Bar::send16bitData(unsigned int data) |
jksoft | 0:a2b97475d3af | 123 | { |
jksoft | 0:a2b97475d3af | 124 | for(unsigned char i=0;i<16;i++) |
jksoft | 0:a2b97475d3af | 125 | { |
jksoft | 0:a2b97475d3af | 126 | if(data&0x8000) |
jksoft | 0:a2b97475d3af | 127 | { |
jksoft | 0:a2b97475d3af | 128 | _data_pin = 1; |
jksoft | 0:a2b97475d3af | 129 | //PORT_Data |= BIT_Data; |
jksoft | 0:a2b97475d3af | 130 | } |
jksoft | 0:a2b97475d3af | 131 | else |
jksoft | 0:a2b97475d3af | 132 | { |
jksoft | 0:a2b97475d3af | 133 | _data_pin = 0; |
jksoft | 0:a2b97475d3af | 134 | //PORT_Data &=~ BIT_Data; |
jksoft | 0:a2b97475d3af | 135 | } |
jksoft | 0:a2b97475d3af | 136 | _clk_pin = !_clk_pin; |
jksoft | 0:a2b97475d3af | 137 | //PORT_Clk ^= BIT_Clk; |
jksoft | 0:a2b97475d3af | 138 | data <<= 1; |
jksoft | 0:a2b97475d3af | 139 | } |
jksoft | 0:a2b97475d3af | 140 | } |
jksoft | 0:a2b97475d3af | 141 | void LED_Bar::latchData(void) |
jksoft | 0:a2b97475d3af | 142 | { |
jksoft | 0:a2b97475d3af | 143 | _data_pin = 0; |
jksoft | 0:a2b97475d3af | 144 | //PORT_Data &=~ BIT_Data; |
jksoft | 0:a2b97475d3af | 145 | wait_us(10); |
jksoft | 0:a2b97475d3af | 146 | //delayMicroseconds(10); |
jksoft | 0:a2b97475d3af | 147 | for(unsigned char i=0;i<8;i++) |
jksoft | 0:a2b97475d3af | 148 | { |
jksoft | 0:a2b97475d3af | 149 | _data_pin = !_data_pin; |
jksoft | 0:a2b97475d3af | 150 | //PORT_Data ^= BIT_Data; |
jksoft | 0:a2b97475d3af | 151 | } |
jksoft | 0:a2b97475d3af | 152 | } |