GROVE LEDバーのライブラリ

Revision:
0:a2b97475d3af
--- /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