LEDMatrixDisplay Program 文字ごとに色を変更できるように修正

Dependencies:   mbed

Fork of LEDMatrix_Master by en 129

Revision:
1:d4d1951a4202
Parent:
0:64f46cf1b2b4
Child:
2:402191724e28
--- a/main.cpp	Mon Oct 07 14:19:46 2013 +0000
+++ b/main.cpp	Sat Nov 02 23:49:55 2013 +0000
@@ -5,11 +5,155 @@
 #include "kfont8.h"
 #include "process_fonts.h"
 
-SPI spi(p5, p6, p7); // mosi, miso, sclk
+unsigned char matrixdata[32];
+SPI spi(p5, p6, p7); // mosi, miso, sclk 
+
 BusOut bords(p15,p16,p17,p18,p19,p20);
 Serial pc(USBTX, USBRX); // tx, rx
 
-unsigned int ImageBuf[3][16*9];//16*6
+SPI spi2(p11, p12, p13); // mosi, miso, sclk
+DigitalOut cs(p14);
+
+#define DISPLAY_XSIZE   (96)
+unsigned int ImageBuf[3][DISPLAY_XSIZE];//16*6
+unsigned int ColerMap[3][DISPLAY_XSIZE];
+
+void read_font(unsigned short code) {
+    unsigned char  c1, c2, MSB,LSB;
+    uint32_t Address, seq;
+
+    // SJIS to kuten code conversion
+    c1 = (code>>8);
+    c2 = (code & 0xFF);
+    seq = (c1<=159 ? c1-129 : c1-193)*188 + (c2<=126 ? c2-64 : c2-65);
+    MSB = seq / 94 + 1;
+    LSB = seq % 94 + 1;
+    Address = 0;
+        
+    if(     MSB >=  1 && MSB <= 15 && LSB >= 1 && LSB <= 94)
+        Address =( (MSB -  1) * 94 + (LSB - 1))*32;
+    else if(MSB >= 16 && MSB <= 47 && LSB >= 1 && LSB <= 94)
+        Address =( (MSB - 16) * 94 + (LSB - 1))*32 + 0x0AA40L;
+    else if(MSB >= 48 && MSB <= 84 && LSB >= 1 && LSB <= 94)
+        Address = ((MSB - 48) * 94 + (LSB - 1))*32 + 0x21CDFL;
+    else if(MSB == 85 &&                LSB >= 1 && LSB <= 94)
+        Address = ((MSB - 85) * 94 + (LSB - 1))*32 + 0x3C4A0L;
+    else if(MSB >= 88 && MSB <= 89 && LSB >= 1 && LSB <= 94)
+        Address = ((MSB - 88) * 94 + (LSB - 1))*32 + 0x3D060L;
+    
+    // Deselect the device
+    cs = 1;
+
+    // Setup the spi for 8 bit data, high steady state clock
+    spi2.format(8,3);
+    spi2.frequency(1000000); 
+    
+    // Select the device by seting chip select low
+    cs = 0;
+    spi2.write(0x03);    // Read data byte
+    spi2.write(Address>>16 & 0xff);
+    spi2.write(Address>>8 & 0xff);
+    spi2.write(Address & 0xff);
+    
+    // Send a dummy byte to receive the contents of the WHOAMI register
+    for(int i=0; i<32; i++)
+    {
+      matrixdata[i]=spi2.write(0x00);
+    }
+
+    // Deselect the device
+    cs = 1;
+}
+
+void draw_kanji_15x16(int pos_x,unsigned char color)
+{
+    int i = 0;
+    for(i=0;i<16;i++)
+    {
+        if( ((signed int)(15-i+pos_x) >= 0) && ((15-i+pos_x) <= (DISPLAY_XSIZE-1)) )
+        {
+            if(color == COLOR_G || color == COLOR_C || color == COLOR_Y || color == COLOR_W)
+            {
+                ImageBuf[0][15-i+pos_x] =  matrixdata[i];
+                ImageBuf[0][15-i+pos_x] |= matrixdata[i+16]<<8;
+            }
+            if(color == COLOR_R || color == COLOR_Y || color == COLOR_M || color == COLOR_W)
+            {
+                ImageBuf[1][15-i+pos_x] =  matrixdata[i];
+                ImageBuf[1][15-i+pos_x] |= matrixdata[i+16]<<8;
+            }
+            if(color == COLOR_B || color == COLOR_C || color == COLOR_M || color == COLOR_W)
+            {
+                ImageBuf[2][15-i+pos_x] =  matrixdata[i];
+                ImageBuf[2][15-i+pos_x] |= matrixdata[i+16]<<8;
+            }
+        }
+    }
+}
+
+unsigned int CountChar(char *str)
+{
+    unsigned char   f_SJISChar = 0;
+    unsigned char   c = 0;
+    unsigned int    CountChar = 0;
+
+    c = *str;
+    while(c != '\0')
+    {
+        //2バイト文字の判定
+        if( ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc)) && f_SJISChar != 1 )
+        {
+            f_SJISChar = 1;
+        }
+        else
+        {
+            f_SJISChar = 0;
+            CountChar++;
+        }
+        str++;
+        c = *str;
+    }
+    return CountChar;
+}
+
+void drawStr15x16(char *str ,int pos_x,unsigned char color)
+{
+    unsigned char   f_SJISChar = 0;
+    unsigned char   c = 0;
+    unsigned int    SJISChar = 0;
+    unsigned int    CountChar = 0;
+
+    c = *str;
+    while(c != '\0')
+    {
+        //2バイト文字の判定
+        if( ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc)) && f_SJISChar != 1 )
+        {
+            SJISChar = c;
+            f_SJISChar = 1;
+        }
+        else if(f_SJISChar == 1)
+        {
+            SJISChar = (SJISChar<<8) | c;
+            f_SJISChar = 0;
+            read_font(SJISChar);
+            draw_kanji_15x16(pos_x-CountChar*16,color);
+            CountChar++;
+        }
+        else  //ASCII文字
+        {
+            SJISChar = c;
+            f_SJISChar = 0;
+            read_font(SJISChar);
+            draw_kanji_15x16(pos_x-CountChar*16,color);
+            CountChar++;
+        }
+        str++;
+        c = *str;
+    }
+}
+
+
 
 void SPILineOut(unsigned char setLine)
 {
@@ -49,49 +193,182 @@
     spi.write(data[2]);
 }
 
-void outBordData(unsigned char ch)
+void outBordData()
 {
+    unsigned char ch = 0,Max_ch=0;
     unsigned int i = 0;
-    wait_us(10);
-    bords = 0x01 << ch;
-    for(i=(ch*16);i<(ch*16+16);i++)
+    Max_ch = (DISPLAY_XSIZE-16)/16;
+    for(ch=0;ch<=Max_ch;ch++)
     {
-        SPILineOut(i);
+        wait_us(10);
+        bords = 0x01 << ch;
+        for(i=(ch*16);i<(ch*16+16);i++)
+        {
+            SPILineOut(i);
+        }
     }
 }
 
+void bufLeftShift_Loop(void)
+{
+    signed int i = 0;
+   
+    for(i=(DISPLAY_XSIZE-1);i>=1;i--)
+    {
+        ImageBuf[0][i] = ImageBuf[0][i-1];
+        ImageBuf[1][i] = ImageBuf[1][i-1];
+        ImageBuf[2][i] = ImageBuf[2][i-1];
+    }
+    ImageBuf[0][0] = ImageBuf[0][(DISPLAY_XSIZE-1)];
+    ImageBuf[1][0] = ImageBuf[1][(DISPLAY_XSIZE-1)];
+    ImageBuf[2][0] = ImageBuf[2][(DISPLAY_XSIZE-1)];
+
+}
+
 void bufLeftShift(void)
-{  
+{
     signed int i = 0;
 
-    for(i=95;i>=0;i--)
+    for(i=(DISPLAY_XSIZE-1);i>=1;i--)
+    {
+        ImageBuf[0][i] = ImageBuf[0][i-1];
+        ImageBuf[1][i] = ImageBuf[1][i-1];
+        ImageBuf[2][i] = ImageBuf[2][i-1];
+    }
+    ImageBuf[0][0] = 0;
+    ImageBuf[1][0] = 0;
+    ImageBuf[2][0] = 0;
+}
+
+void TestMode(void)
+{
+    unsigned char i = 0;
+    
+    for(i=0;i<16;i++)
+    {
+        ImageBuf[0][i] = 0xffff;
+    }
+    for(i=16;i<16+16;i++)
+    {
+        ImageBuf[1][i] = 0xffff;
+    }
+    for(i=32;i<32+16;i++)
+    {
+        ImageBuf[2][i] = 0xffff;
+    }
+
+    outBordData();
+    
+    while(1)
+    {
+        wait(0.1);
+        bufLeftShift_Loop();
+        outBordData();
+    }
+}
+
+void SetRandamColer(void)
+{
+    unsigned char color = 0;
+    unsigned int i=0,j=0;
+
+    memset(ColerMap,0,sizeof(ColerMap));
+    for(j=0;j<DISPLAY_XSIZE;j++)
     {
-        ImageBuf[0][i+1] = ImageBuf[0][i];
-        ImageBuf[1][i+1] = ImageBuf[1][i];
-        ImageBuf[2][i+1] = ImageBuf[2][i];
+        for(i=0;i<16;i++)
+        {
+            color = rand()%7;
+            if(color == COLOR_G || color == COLOR_C || color == COLOR_Y || color == COLOR_W)
+            {
+                ColerMap[0][j] |= 1<<i;
+            }
+            if(color == COLOR_R || color == COLOR_Y || color == COLOR_M || color == COLOR_W)
+            {
+                ColerMap[1][j] |= 1<<i;
+            }
+            if(color == COLOR_B || color == COLOR_C || color == COLOR_M || color == COLOR_W)
+            {
+                ColerMap[2][j] |= 1<<i;
+            }
+        }
     }
-    ImageBuf[0][0] = ImageBuf[0][96];
-    ImageBuf[1][0] = ImageBuf[1][96];
-    ImageBuf[2][0] = ImageBuf[2][96];
+}
+
+unsigned char ConvHue(unsigned char num)
+{
+    unsigned char Hue[7] = {COLOR_G,COLOR_C,COLOR_B,COLOR_M,COLOR_R,COLOR_Y,COLOR_W};
+    return Hue[num];
+    
 }
 
+void SetRainbowColer(void)
+{
+    unsigned char color = 0;
+    unsigned int j=0;
+
+    memset(ColerMap,0,sizeof(ColerMap));
+    for(j=0;j<DISPLAY_XSIZE;j++)
+    {
+        color = ConvHue((++color)%7);
+        
+        if(color == COLOR_G || color == COLOR_C || color == COLOR_Y || color == COLOR_W)
+        {
+            ColerMap[0][j] |= 0xffff;
+        }
+        if(color == COLOR_R || color == COLOR_Y || color == COLOR_M || color == COLOR_W)
+        {
+            ColerMap[1][j] |= 0xffff;
+        }
+        if(color == COLOR_B || color == COLOR_C || color == COLOR_M || color == COLOR_W)
+        {
+            ColerMap[2][j] |= 0xffff;
+        }
+    }
+}
+
+
+void ApplyColerMap(void)
+{
+    unsigned i = 0;
+    for(i=0;i<DISPLAY_XSIZE;i++)
+    {
+        ImageBuf[0][i] = ImageBuf[0][i]&ColerMap[0][i];
+        ImageBuf[1][i] = ImageBuf[1][i]&ColerMap[1][i];
+        ImageBuf[2][i] = ImageBuf[2][i]&ColerMap[2][i];
+    }
+}
+
+
 int main()
 {
     unsigned char f_mode = 0;
     unsigned int i = 0;
     unsigned char coler = 0;
-//    char strs[100]={0x82,0x6d,0x82,0x73,0x8b,0xe0,0x91,0xf2,0x82,0x51,0x82,0x4f,0x82,0x50,0x82,0x52};
-    char strs[100]={0x82,0xE6,0x82,0xA4,0x82,0xB1,0x82,0xBB,0x82,0x8D,0x82,0x82,0x82,0x85,0x82,0x84,
-                    0x8D,0xD5,0x82,0xE8,0x82,0xD6,0x81,0x49};
-
+    char strs[100]={0x82,0x6C,0x82,0x81,0x82,0x8B,0x82,0x85,0x82,0x65,0x82,0x81,0x82,0x89,0x82,0x92,
+        0x82,0x73,0x82,0x8F,0x82,0x8B,0x82,0x99,0x82,0x8F,0x82,0x51,0x82,0x4F,0x82,0x50,0x82,0x52,0x00};
     spi.format(16,1);
     spi.frequency(1000000);
 
     memset(ImageBuf,0,sizeof(ImageBuf));
-    f_mode = 1;
     i=0;
     wait(3);
+    //TestMode();
+    SetRandamColer();
+    SetRainbowColer();
+    while(1)
+    {
+        memset(ImageBuf,0,sizeof(ImageBuf));
+//        read_font(0x826d);
+//        draw_kanji_15x16(0,0);
 
+        for(i=0;i<DISPLAY_XSIZE+(15*CountChar(strs)-1);i++)
+        {
+            drawStr15x16(strs ,i,COLOR_W);
+            ApplyColerMap();
+            outBordData();
+            wait(0.2);
+        }
+    }
     while(1)
     {
         if(f_mode == 0)
@@ -135,12 +412,7 @@
         if(pc.readable()) {
             f_mode = 0;
         }
-        outBordData(0);
-        outBordData(1);
-        outBordData(2);
-        outBordData(3);
-        outBordData(4);
-        outBordData(5);
+        outBordData();
         wait(0.1);
     }