Aliexpressなどで販売されている64x32のフルカラードットマトリクスLED2枚とNucleo F401REを利用して、 E233系の駅停車時、路線名表示ありのLED側面行先表示を再現するプログラムです。 3秒間隔、3段階切替で、路線名、種別、行先、次停車駅を個別に指定することが可能です。

Dependencies:   SDFileSystem mbed

Revision:
33:c934bc793c5a
Parent:
32:3bf210e07de7
Child:
34:9ee64899b004
--- a/main.cpp	Sat Feb 28 05:37:16 2015 +0000
+++ b/main.cpp	Thu Mar 19 10:55:18 2015 +0000
@@ -1,27 +1,18 @@
-/*
-The goal of this program is to show the basic connections and workings of Adafruits 32x16 RGB LED matrix board (http://www.adafruit.com/products/420),
-also sold on other places, for instance http://www.ebay.com/itm/PH6-RGB-Full-Color-LED-16-32-Dot-Matrix-Display-Module-/310303408628?pt=LH_DefaultDomain_0&hash=item483f8641f4 (no
-affiliation with either of them).
-This program is not intended to be highly optimized or a guideline in C programming in any way (more of the opposite actually).
-To have more than 7 colors on this thing, you need to implement software PWM of some sort. I have obviously not done that, but if YOU do, please let me know!
-Adafruit have a wicked demo program for an arduino - www.youtube.com/watch?v=lY-flFEfsHo
-There are probably lots of ways to make this perform better, perhaps by using Neal Hormans port of the Adafruit_GFX library (http://mbed.org/users/nkhorman/code/Adafruit_GFX/).
-No error checking or out-of-bounds checking is done. Use at your own peril.
-For more detailed information on the driver chip, see http://www.bjtopspace.com/ziliao/CYT62726.pdf
-Although the chips on my board says jx15020, I've been informed that they are equvivalent to the CYT62726, and so far it's a match.
-Feel free to use all or parts of this work.
-If you choose to do so, I would appreciate a small mentioning in the scrolling opening credits ;)
+//E233SideLED E233系側面LED再現
+//Ver:0.90
 
-Best regards,
-Hugo Harming
-upgraded@hotmail.com
-*/
+//簡単な説明,また表示データを以下で配布しています。
+//https://chirashitech.wordpress.com/
+
+//このソースはSDカードが存在することが前提です。
+//SDカードがない状態で停車駅スクロールが有効になっている場合エラー文が出力されるため起動までに時間がかかります。
+//SDカードがない場合ScrollEnableを0にしてください。
+
 
 #include "mbed.h"
 #include "SDFileSystem.h"
 
 #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
-
 #define bitSet(value, bit) ((value) |= (1UL << (bit)))
 #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
 #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
@@ -48,7 +39,9 @@
 #define LED_Width 128
 #define LED_Height 16
 
-
+//表示処理Wait
+//ここを変更することで輝度調整可能
+//ただし消費電力も変動します。出力に余裕のない電源を利用している場合変更しないでください
 #define DisplayWait 15
 
 //最大停車駅数
@@ -131,53 +124,58 @@
 
 //表示切替用タイマ
 Ticker ChangeTimer;
-//スクロール用タイマ(現状未使用)
+//スクロール用タイマ
 Ticker ScrollTimer;
 
 //Debug
 bool Debug = 0;
 
 
-//Mode
-
-//3:固定表示 LEDBuffer3固定
-
+//Mode(未使用?)
 //1固定
 int WriteMode = 1;
 
 
 
 //1:3段階 LEDBuffer-LEDBuffer2-LEDBuffer3
-//2:2段階 LEDBuffer-LEDBuffer2
-//3:固定  LEDBuffer2
-//4:2段階 LEDBuffer2-LEDBuffer3
+//2:2段階 LEDBuffer5-LEDBuffer6
+//3:固定  LEDBuffer
+//4:2段階 LEDBuffer2-LEDBuffer3(次駅あり)
+//5:4段階 LEDBuffer-LEDBuffer4-LEDBuffer2-LEDBuffer3
 int DisplayMode = 1;
 
-//スクロール有効/無効(テスト用)
+//スクロール有効/無効
 bool Scroll = 1;
 
 //起動時スクロール読み込み有効/無効(SDがない場合0にする)
 //(1になっている場合SDカードが刺さっていないと起動に数分かかります)
 bool ScrollEnable = 1;
 
+
 int ChangeCount = 0;
+//路線コード
 int LineNumber = 1;
+//種別コード
 int KindNumber = 22;
+//行先コード
 int ForNumber = 114;
+//次駅コード
 int NextStaNumber = 1;
-
+//運用パターン番号
 int RoutePatternNumber = 1;
+//始発駅コード
 int DeptStaNumber = 3;
+//終着駅コード
 int TermStaNumber = 32;
 
-char SerialBuffer[30];
+
+char SerialBuffer[60];
 //シリアル処理用バッファ
-char ABuffer[30];
+char ABuffer[60];
 bool busyflag = false;
 int count = 0;
 
-
-
+//スクロール用変数
 int ScrollCount = 0;
 int ScrollWriteCount = 80;
 
@@ -185,26 +183,18 @@
 //SDCardFilePath
 char SDFilePath[80]= "/sd/a.txt";
 
-//unsigned char gm[32][6]; // Buffer with 32x6 bytes. Graphics memory if you like.
-unsigned long CT; //        Counter for demo code
-
+unsigned long CT;         
 
 //StopStationCode:停車駅コード(最大100駅 それ以上の場合MaxStopStationを増やしてください)
 //停車駅コード終端は0にする
 //StationNameLength:駅名の文字数(最大8文字)
-//停車駅名データ:/sd/E233/StationName/(停車駅コード).bin
-//128x32
-
-//int StopStationCode[MaxStopStation] = {2,3,7,11,14,20,24,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,0};
-//Keihin-Tohoku Rapid Ofuna
-//int StopStationCode[MaxStopStation] = {202,203,204,205,206,207,208,209,210,211,212,213,214,218,220,222,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,0};
 int StopStationCode[MaxStopStation] = {320,319,318,317,316,315,314,313,312,311,310,309,308,307,306,305,304,303,302,301,351,352,353,354,355,356,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,0};
 int StationNameLength[100] = {0,2,2,4,3,3,3,3,3,4,3,2,3,3,2,3,4,2,3,3,2,3,4,5,3,4,2,2,2,2,3,4,2,3,2,3,3,2,2,2,2,3,3,2,2,3,3,7,4,2,3,1,5,3,3,3,8,3}; 
 
 
 
 
-int8_t LEDBuffer [32][128] = {
+int8_t LEDBuffer [32][LED_Width] = {
 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,7,7,0,7,7,0,0,0,0,0,0,0,0,0,0},
@@ -349,6 +339,17 @@
 
 };
 
+int8_t LEDBuffer5[32][LED_Width] = {
+
+};
+
+int8_t LEDBuffer6[32][LED_Width] = {
+
+};
+int8_t LEDMainBuffer[32][LED_Width] = {
+
+};
+
 int8_t ScrollBuffer[16][2048] = {
 
 };
@@ -360,13 +361,14 @@
 //04 Yellow(R,G)
 //05(G,B)
 //06 purple(R,B)
+//07 White(R,G,B)
 //08 Blue(Keihin-tohoku Line)
 //10 Yellow(Nambu Local)
 //11 Green (Yokohama Line)
-//12 Orange(Rapid Acty,Urbun)
-
+//12 Orange(Rapid Acty,Urban)
 //13 Green(Saikyo Line)
-
+//14
+//15
 //16 Green(Utsunomiya Line)
 //17 Green(Joban Local Local)
 //18 Keikyu Logo(Gray
@@ -421,7 +423,7 @@
 
 }
 
-void WriteRow(unsigned char Row, int8_t Buffer[32][128], int8_t index2)
+void WriteRow(unsigned char Row, int8_t Buffer[32][LED_Width], int8_t index2)
 {
     ABC = 15-Row; 
     for(int col=0; col<LED_Width; col++) { 
@@ -456,23 +458,7 @@
     LAT = LOW;
 }
 
-//void Pset(unsigned char x,unsigned char y, unsigned char c)
-//{
-    // Set pixel (x,y) to color c
-    // Manipulates graphics memory, so you won't see any change til you Paint() it.
-//    unsigned char ud,l,r0,g0,b0;
-//    ud=(y & 8)>>3; // 0 = upper half, 1 = lower half
-//    l=y & 7; // Extract row in upper/lower half
-//    r0=(c & 4) >>2; // Extract red bit from color
-//    g0=(c & 2) >>1; // Extract green bit from color
-//    b0=(c & 1); //     Extract blue bit from color
-    //            *******Removes current bit ******* *Adds bit**
-//    gm[x][0+3*ud]=(gm[x][0+3*ud] & (255-(1<<(7-l))))+(r0<<(7-l)); // Red byte
-//    gm[x][1+3*ud]=(gm[x][1+3*ud] & (255-(1<<(7-l))))+(g0<<(7-l)); // Green byte
-//    gm[x][2+3*ud]=(gm[x][2+3*ud] & (255-(1<<(7-l))))+(b0<<(7-l)); // Blue byte
-//}
-
-void Paint(int8_t Buffer2[32][128])
+void Paint(int8_t Buffer2[32][LED_Width])
 {
     for(int index = 0; index < 4; index++){
         for(int Row=0; Row<LED_Height; Row++) {
@@ -498,7 +484,7 @@
     }
 }
 
-void CopyBuffer(int8_t ReadBuffer[32][128],int8_t WriteBuffer[32][128]){
+void CopyBuffer(int8_t ReadBuffer[32][LED_Width],int8_t WriteBuffer[32][LED_Width]){
     for(int y = 0; y < LED_Height * 2; y++){
         if(Scroll != 0 && y > 15 ){
             for(int x = 0; x < LED_Width - 80;  x++){
@@ -512,7 +498,18 @@
     }
 }
 
-void CopyScrollBuffer(int8_t ReadBuffer[16][2048],int8_t WriteBuffer[32][128],int Startx){
+//バッファ初期化
+void ClearBuffer(int8_t TargetBuffer[32][LED_Width]){
+    for(int y = 0; y < LED_Height * 2; y++){
+        for(int x = 0; x < LED_Width;  x++){
+            TargetBuffer[y][x] = 0;    
+        }    
+    }      
+    
+}    
+    
+
+void CopyScrollBuffer(int8_t ReadBuffer[16][2048],int8_t WriteBuffer[32][LED_Width],int Startx){
     for(int y = 0; y < 16; y++){
         int xCount = 0;       
         for(int x = 0; x < 80; x++){
@@ -527,7 +524,7 @@
 }
 
 //1文字256byte 
-void CopyScrollBuffer2(int8_t ReadBuffer[16][2048],int8_t WriteBuffer[32][128],int Startx){
+void CopyScrollBuffer2(int8_t ReadBuffer[16][2048],int8_t WriteBuffer[32][LED_Width],int Startx){
     for(int y = 0; y < 16; y++){
         for(int x = 0; x < 80; x++){
             LEDMainBuffer[y + 16][x + 48] = ScrollBuffer[y][x + Startx];   
@@ -561,11 +558,12 @@
     if (DisplayMode == 2){
         
         if(ChangeCount == 0 ){
-            CopyBuffer(LEDBuffer,LEDMainBuffer);
+            //CopyBuffer(LEDBuffer,LEDMainBuffer);
+            CopyBuffer(LEDBuffer5,LEDMainBuffer);
             ChangeCount = ChangeCount + 1;
-            //ChangeCount = 0;
         }else if(ChangeCount == 1){
-            CopyBuffer(LEDBuffer2,LEDMainBuffer);     
+            //CopyBuffer(LEDBuffer2,LEDMainBuffer);
+            CopyBuffer(LEDBuffer6,LEDMainBuffer);     
             ChangeCount = 0;
         }else{
             ChangeCount = 0;
@@ -587,7 +585,27 @@
             ChangeCount = 1;
         }  
     }
-    
+
+    //DisplayMode = 5 5段階表示ならば
+    if(DisplayMode == 5){
+        if (ChangeCount == 0){
+            CopyBuffer(LEDBuffer,LEDMainBuffer);
+            ChangeCount = ChangeCount + 1;
+        }else if(ChangeCount == 1 ){
+            CopyBuffer(LEDBuffer4,LEDMainBuffer);
+            ChangeCount = ChangeCount + 1;
+            //ChangeCount = 0;
+        }else if(ChangeCount == 2){
+            CopyBuffer(LEDBuffer2,LEDMainBuffer);
+            ChangeCount = ChangeCount + 1;
+        }else if(ChangeCount == 3){
+            CopyBuffer(LEDBuffer3,LEDMainBuffer);
+            ChangeCount = 0;
+        }else{
+            ChangeCount = 0;
+        }
+    }
+
     //OE = LOW;
     //クロック確認
     //pc.printf("CPU SystemCoreClock is %.2f MHz\r\n", (float)SystemCoreClock/1000/1000); 
@@ -604,20 +622,20 @@
 
 
 
-//書込み対象バッファ,書込み開始位置x,書込み開始位置y,読み出し幅x,読み出し高さy
-void SDBufferWrite(int8_t TargetBuffer[32][128], int Startx, int Starty, int Readx, int Ready){
+//書込み対象バッファ,書込み開始位置x,書込み開始位置y,読み出し幅x,読み出し高さy,読み出し開始位置(バイト)
+bool SDBufferWrite(int8_t TargetBuffer[32][LED_Width], int Startx, int Starty, int Readx, int Ready, int StartByte ){
     FILE *fp = fopen(SDFilePath, "r");
         if(fp == NULL) {
             pc.printf("SDFileOpen Error %s\r\n",SDFilePath);
             //error("Could not open file for write\r\n");
+            return 1;
         }else{
-            //fprintf(fp, "Hello fun SD Card World!");
             pc.printf("SDFileOpen Success %s\r\n",SDFilePath);
         
             //SDDataReadtest
             int8_t Data;
             //先頭2バイトはヘッダ
-            fseek(fp, 2, SEEK_SET);
+            fseek(fp, 2 + StartByte, SEEK_SET);
             
             for(int y = Starty; y < Starty + Ready; y++){  
                 for(int x = Startx; x < Startx + Readx; x++){
@@ -626,6 +644,7 @@
                 }           
             }           
             fclose(fp); 
+            return 0;
         }
 }
 
@@ -709,13 +728,17 @@
     int DeptStaIndex = 0;
     int TermStaIndex = 0;
     //sprintf(SDFilePath,"/sd/E233/RoutePattern/RoutePattern%d.bin",1);
-    sprintf(SDFilePath,"/sd/E233/RoutePattern/RoutePattern001001.bin");
+    //sprintf(SDFilePath,"/sd/E233/RoutePattern/RoutePattern001001.bin");
+    sprintf(SDFilePath,"/sd/E233/RoutePattern/RoutePattern%03d%03d.bin",LineNumber,RoutePatternNumber);
+    pc.printf("%s\r\n",SDFilePath);
      
     //始発駅インデックス取得
     for(int i = 0; i < MaxStopStation; i++){
         FILE *fp = fopen(SDFilePath, "r");
         if(fp == NULL) {
+            //停車駅データを読み込めないならスクロールを無効にする
             pc.printf("SDFileOpen Error %s\r\n",SDFilePath);
+            Scroll = 0;
             break;   
         }else{
             //停車駅コード取得
@@ -744,7 +767,9 @@
     for(int i = 0; i < MaxStopStation; i++){
         FILE *fp = fopen(SDFilePath, "r");
         if(fp == NULL) {
+            //停車駅データを読み込めないならスクロールを無効にする
             pc.printf("SDFileOpen Error %s\r\n",SDFilePath);
+            Scroll = 0;
             break;   
         }else{
             //停車駅コード取得
@@ -770,11 +795,7 @@
     }
         
     
-    
-    
     if(DeptStaIndex != 0 && TermStaIndex != 0){
-        
-        
         //SDRead StopStationCode
         //始発駅インデックス < 終着駅インデックスなら
         if(DeptStaIndex < TermStaIndex){
@@ -785,7 +806,9 @@
         
                 FILE *fp = fopen(SDFilePath, "r");
                 if(fp == NULL) {
+                    //停車駅データを読み込めないならスクロールを無効にする
                     pc.printf("SDFileOpen Error %s\r\n",SDFilePath);
+                    Scroll = 0;
                     break;    
                 }else{
                     //pc.printf("SDFileOpen Success %s\r\n",SDFilePath);
@@ -815,6 +838,7 @@
     //始発駅インデックス、終着駅インデックスのどちらか、または両方が0の場合運用パターンデータに駅名コードが存在しない
         pc.printf("Error DeptStaIndex or TermStaIndex = 0\r\n");
         pc.printf("Error DeptStaIndex:%d,TermStaIndex:%d",DeptStaIndex,TermStaIndex);
+        Scroll = 0;
     }  
         
     //test end    
@@ -830,8 +854,11 @@
     SDScrollBufferWrite(ScrollBuffer,ScrollWriteCount,0,80,16);       
     ScrollWriteCount = ScrollWriteCount + 80;  
 
+    int StaNameLength = 0;
     for(int i = 0; i < MaxStopStation; i++){        
         if(StopStationCode[i] == 0){
+            //駅名コードが0ならScrollWriteCountから終着駅の文字数 x 16 + 16 を引いてループを抜ける
+            ScrollWriteCount = ScrollWriteCount - (StaNameLength * 16 + 16);  
             break;
         }
         //駅名
@@ -839,7 +866,7 @@
         SDScrollBufferWrite(ScrollBuffer,ScrollWriteCount,0,128,16);
         
         //駅名文字数取得
-        int test = 0;
+        //int test = 0;
         sprintf(SDFilePath,"/sd/E233/StationNameLength.bin");
         FILE *fp = fopen(SDFilePath, "r");
         if(fp == NULL) {
@@ -847,16 +874,19 @@
         }else{
             //pc.printf("SDFileOpen Success %s\r\n",SDFilePath);
             fseek(fp, StopStationCode[i], SEEK_SET);
-            test = getc(fp);
-            //pc.printf("StationNameLength:%d\r\n",test);
+            StaNameLength = getc(fp);
+            //pc.printf("StationNameLength:%d\r\n",StaNameLength);
             fclose(fp); 
         }
                
-        ScrollWriteCount = ScrollWriteCount + (test * 16);  
+        ScrollWriteCount = ScrollWriteCount + (StaNameLength * 16);  
         sprintf(SDFilePath,"/sd/E233/StopStation/0x8132.bin");
         SDScrollBufferWrite(ScrollBuffer,ScrollWriteCount,0,16,16);       
         ScrollWriteCount = ScrollWriteCount + 16;      
     }
+    
+    
+    
     ScrollWriteCount = ScrollWriteCount - 16;
     sprintf(SDFilePath,"/sd/E233/StopStation/StopStation%d.bin",3);
     SDScrollBufferWrite(ScrollBuffer,ScrollWriteCount,0,80,16);       
@@ -869,72 +899,98 @@
 
 
 void SDFileRead(){
+    //バッファ初期化
+    ClearBuffer(LEDBuffer);
+    ClearBuffer(LEDBuffer2);
+    ClearBuffer(LEDBuffer3);
+    ClearBuffer(LEDBuffer4);
+    ClearBuffer(LEDBuffer5);
+    ClearBuffer(LEDBuffer6);
+    
     
     //3段階表示 LEDBuffer  [種別]路線名(・次駅)
     //         LEDBuffer2 [種別]行先・次駅
     //         LEDBuffer3 [種別(英)]行先(英)・次駅(英)
-
-    //SDCard
-    //種別
-    sprintf(SDFilePath,"/sd/E233/Kind/%d.bin",KindNumber);
-    SDBufferWrite(LEDBuffer,0,0,48,32);
-    sprintf(SDFilePath,"/sd/E233/Kind/%d.bin",KindNumber);
-    SDBufferWrite(LEDBuffer2,0,0,48,32);    
-    //種別(英語)
-    sprintf(SDFilePath,"/sd/E233/KindE/%d.bin",KindNumber);
-    SDBufferWrite(LEDBuffer3,0,0,48,32); 
-           
-    //路線名
-    sprintf(SDFilePath,"/sd/E233/Line/%d.bin",LineNumber);
-    SDBufferWrite(LEDBuffer,48,0,80,32);
-    //行先
-    sprintf(SDFilePath,"/sd/E233/For/%d.bin",ForNumber);
-    SDBufferWrite(LEDBuffer2,48,0,80,16);
-    //行先(英語)
-    sprintf(SDFilePath,"/sd/E233/ForE/%d.bin",ForNumber);
-    SDBufferWrite(LEDBuffer3,48,0,80,16);
-    
-    //次停車駅(路線名表示)
-    //路線名表示の次停車駅は路線名表示が上半分に収まるときのみ表示
-    if(BufferBlankCheck() == 0){
-        sprintf(SDFilePath,"/sd/E233/NextStation/%d.bin",NextStaNumber);
-        SDBufferWrite(LEDBuffer,48,16,80,16);
-    }
-    //次停車駅
-    sprintf(SDFilePath,"/sd/E233/NextStation/%d.bin",NextStaNumber);
-    SDBufferWrite(LEDBuffer2,48,16,80,16);    
-    //次停車駅(英語)
-    sprintf(SDFilePath,"/sd/E233/NextStationE/%d.bin",NextStaNumber);
-    SDBufferWrite(LEDBuffer3,48,16,80,16);   
-    
-    
-        
     //路線コードが0なら2段階表示に変更
     if(LineNumber == 0){
         DisplayMode = 4;
     }else{
         DisplayMode = 1;
     }
+
+    //SDCard
+    //種別
+    sprintf(SDFilePath,"/sd/E233/Kind/%d.bin",KindNumber);
+    SDBufferWrite(LEDBuffer,0,0,48,32,0);
+    sprintf(SDFilePath,"/sd/E233/Kind/%d.bin",KindNumber);
+    SDBufferWrite(LEDBuffer2,0,0,48,32,0);    
+    //種別(英語)
+    sprintf(SDFilePath,"/sd/E233/KindE/%d.bin",KindNumber);
+    SDBufferWrite(LEDBuffer3,0,0,48,32,0); 
+           
+    //路線名
+    sprintf(SDFilePath,"/sd/E233/Line/%d.bin",LineNumber);
+    //路線名データが読み込めない場合(返って来た値が0でない場合)2段階表示に変更
+    if(SDBufferWrite(LEDBuffer,48,0,80,32,0) != 0){
+        DisplayMode = 4;
+    }
+    
+    //行先
+    sprintf(SDFilePath,"/sd/E233/For/%d.bin",ForNumber);
+    SDBufferWrite(LEDBuffer2,48,0,80,16,0);
+    //行先(英語)
+    //sprintf(SDFilePath,"/sd/E233/ForE/%d.bin",ForNumber);
+    //SDBufferWrite(LEDBuffer3,48,0,80,16,0);
+    sprintf(SDFilePath,"/sd/E233/For/%d.bin",ForNumber);
+    SDBufferWrite(LEDBuffer3,48,0,80,16,1280);    
+    
+    //次停車駅(路線名表示)
+    //路線名表示の次停車駅は路線名表示が上半分に収まるときのみ表示
+    if(BufferBlankCheck() == 0){
+        sprintf(SDFilePath,"/sd/E233/NextStation/%d.bin",NextStaNumber);
+        SDBufferWrite(LEDBuffer,48,16,80,16,0);
+    }else if(Scroll == 1){
+        //路線名が下半分も使用している&スクロール有効の場合4段階表示に切り替え
+        DisplayMode = 5;
+        sprintf(SDFilePath,"/sd/E233/Kind/%d.bin",KindNumber);
+        SDBufferWrite(LEDBuffer4,0,0,48,32,0);       
+        sprintf(SDFilePath,"/sd/E233/Line/%d.bin",LineNumber);
+        SDBufferWrite(LEDBuffer4,48,0,80,16,1280);        
+    }
+    //次停車駅
+    sprintf(SDFilePath,"/sd/E233/NextStation/%d.bin",NextStaNumber);
+    SDBufferWrite(LEDBuffer2,48,16,80,16,0);    
+    //次停車駅(英語)
+    //sprintf(SDFilePath,"/sd/E233/NextStationE/%d.bin",NextStaNumber);
+    //SDBufferWrite(LEDBuffer3,48,16,80,16,0);   
+    sprintf(SDFilePath,"/sd/E233/NextStation/%d.bin",NextStaNumber);
+    SDBufferWrite(LEDBuffer3,48,16,80,16,1280);     
+    
+        
+
     
     //2段階(次駅なし)
     //次駅コードが0なら次駅なしの2段階表示に変更
-    // 2段階表示 LEDBuffer  [種別]路線名
-    //                       LEDBuffer2 [種別]行先(次駅表示なし) 
+    // 2段階表示 LEDBuffer5 [種別]行先(次駅表示なし) 
+    //          LEDBuffer6 [種別]路線名
     //路線名がない場合(E233-0など)は固定表示
 
     if(NextStaNumber == 0){
         //種別
         sprintf(SDFilePath,"/sd/E233/Kind/%d.bin",KindNumber);
-        SDBufferWrite(LEDBuffer,0,0,48,32);
-        sprintf(SDFilePath,"/sd/E233/Kind/%d.bin",KindNumber);
-        SDBufferWrite(LEDBuffer2,0,0,48,32);        
+        //SDBufferWrite(LEDBuffer,0,0,48,32,0);
+        //SDBufferWrite(LEDBuffer2,0,0,48,32,0);
+        SDBufferWrite(LEDBuffer5,0,0,48,32,0);
+        SDBufferWrite(LEDBuffer6,0,0,48,32,0);        
     
        //路線名
         sprintf(SDFilePath,"/sd/E233/Line2/%d.bin",LineNumber);
-        SDBufferWrite(LEDBuffer,48,0,80,32);
+        //SDBufferWrite(LEDBuffer2,48,0,80,32,0);
+        SDBufferWrite(LEDBuffer6,48,0,80,32,0);
         //行先
         sprintf(SDFilePath,"/sd/E233/For2/%d.bin",ForNumber);
-        SDBufferWrite(LEDBuffer2,48,0,80,32);    
+        //SDBufferWrite(LEDBuffer,48,0,80,32,0);
+        SDBufferWrite(LEDBuffer5,48,0,80,32,0);       
         
         //路線コードが0なら行先で固定表示       
         if(LineNumber == 0){
@@ -951,26 +1007,24 @@
         
         //種別
         sprintf(SDFilePath,"/sd/E233/Kind/%d.bin",KindNumber);
-        SDBufferWrite(LEDBuffer2,0,0,48,32);
+        SDBufferWrite(LEDBuffer,0,0,48,32,0);
         sprintf(SDFilePath,"/sd/E233/Kind2/%d.bin",KindNumber);
-        SDBufferWrite(LEDBuffer2,0,0,128,32);        
-  
+        SDBufferWrite(LEDBuffer,0,0,128,32,0);        
         //固定表示に変更       
         DisplayMode = 3;
     }
     
-    
-    
-    //WriteMode = 3 固定表示 LEDBuffer2 行先(次駅表示なし 32x128)
-    //else if(WriteMode == 3){
-    if(WriteMode == 3){
+    //全面行先固定表示
+    //DisplayMode:3 固定表示 LEDBuffer 行先(次駅表示なし 32x128)
+    //路線名、種別、次駅がすべて0なら行先を全面に固定表示
+    if(LineNumber == 0 && KindNumber == 0 && NextStaNumber == 0){
         //データ作ってないからとりあえず80x32の行先データを表示
+        //前面行先データフォルダ:For3
         
-        sprintf(SDFilePath,"/sd/E233/For2/%d.bin",ForNumber);
-        SDBufferWrite(LEDBuffer2,48,0,80,32);          
-        
+        sprintf(SDFilePath,"/sd/E233/For3/%d.bin",ForNumber);
+        SDBufferWrite(LEDBuffer,0,0,128,32,0);          
+        //固定表示に変更 
         DisplayMode = 3;
-        
     }
     
         //Debug
@@ -1078,8 +1132,14 @@
                 pc.printf("NextStation:%d\r\n",NextStaNumber);
             }
             
-            //スクロール有効/無効 S
-            if(ABuffer[0] == 'S'){
+
+            
+            //起動 Set
+            if(ABuffer[0] == 'S' && ABuffer[1] == 'e' && ABuffer[2] == 't'){
+                pc.printf("Set\r\n"); 
+                SDFileRead();
+            }else if(ABuffer[0] == 'S'){
+                //スクロール有効/無効 S
                 unsigned char Sertemp1 = ABuffer[1];
                 int n1 = 0 ;
                 int n = 0;
@@ -1096,12 +1156,6 @@
                 pc.printf("Scroll:%d\r\n",n);     
             }
             
-            //起動 Set
-            if(ABuffer[0] == 'S' && ABuffer[1] == 'e' && ABuffer[2] == 't'){
-                pc.printf("Set\r\n"); 
-                SDFileRead();
-            }
-            
             //データ一括設定
             //先頭文字:'A'
             if(ABuffer[0] == 'A'){
@@ -1135,6 +1189,13 @@
             
             //データ一括設定2
             //先頭文字:'B'
+            //BLxxxNxxxPxxxyyyzzzKxxxFxxxSx$
+            //Lxxx:路線コード
+            //Nxxx:次駅コード
+            //Pxxxyyyzzz:停車駅パターン(xxx:運用パターン番号 yyy:始発駅コード zzz:終着駅コード)
+            //Kxxx:種別コード
+            //Fxxx:行先コード
+            //Sx:スクロール(1:有効 0:無効)
             if(ABuffer[0] == 'B'){
                 //路線名 L
                 //ABuffer[1]は'L'
@@ -1160,22 +1221,49 @@
                 TermStaNumber = CharToInt(ABuffer[16],ABuffer[17],ABuffer[18]);
                 pc.printf("TermStaNumber:%d\r\n",TermStaNumber); 
                 
+                //種別 K
+                //ABuffer[19]は'K'                  
+                KindNumber = CharToInt(ABuffer[20],ABuffer[21],ABuffer[22]);
+                pc.printf("Kind:%d\r\n",KindNumber); 
+                
+                //行先 F
+                //ABuffer[23]は'F'                  
+                ForNumber = CharToInt(ABuffer[24],ABuffer[25],ABuffer[26]);
+                pc.printf("For:%d\r\n",ForNumber);      
+                
+                //スクロール S
+                if(ABuffer[27] == 'S'){
+                //スクロール有効/無効 S
+                    unsigned char Sertemp1 = ABuffer[28];
+                    int n1 = 0 ;
+                    int n = 0;
+                
+                    if ( Sertemp1 < '0' || Sertemp1 > '9' ) {
+                        // error
+                    } else {
+                        n1 = (int)(Sertemp1 - '0') ;
+                        //pc.printf("%d,",n1); 
+                    } 
+                     
+                        n = n1;
+                    Scroll = n;
+                    pc.printf("Scroll:%d\r\n",n);     
+                }                           
+                       
                 
                 //一括設定時は続けてSDからの読み込み
                 pc.printf("Set\r\n"); 
                 SDFileRead(); 
                 ScrollRead2();
-                Scroll = 1;
+                //Scroll = 1;
                 //即時反映
                 TimerTick();                                
-            }
-            
-        busyflag = false;
-    }else{
-      count++;
+            }  
+            busyflag = false;
+        }else{
+            count++;
+        }
     }
-    }
-    
     OE = LOW;
 }
 
@@ -1273,53 +1361,12 @@
 
 
 
-
-
 int main(){
     Init(); // Set things up
     //Serial
-    
     pc.printf("Power ON\r\n");
     
     
-    //SumSW
-    //int testData[16] = {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0};
-    //SI = LOW;
-    //for(int a = 0; a < 16; a++){  
-    //    if(testData[a] == 1){
-    //        SI = HIGH;
-    //        pc.printf("1,"); 
-    //    }else{
-    //        SI = LOW;
-    //        pc.printf("0,"); 
-    //    }
-    //    SCK = HIGH;
-    //    wait_us(15);  
-    //    SCK = LOW; 
-    //    wait_us(15);
-    //}
-    
-    //RCK = HIGH;
-    //wait_us(15);  
-    //RCK = LOW;
-    //pc.printf("\r\n"); 
-    
-    //int SumSWNum = 0;
-    //if(SumSW1 == 1 ){
-    //    SumSWNum = SumSWNum + 1;
-    //}
-    //if(SumSW2 == 1){
-    //    SumSWNum = SumSWNum + 2;
-    //}
-    //if(SumSW4 == 1){
-    //    SumSWNum = SumSWNum + 4;
-    //}
-    //if(SumSW8 == 1){
-    //    SumSWNum = SumSWNum + 8;
-    //}    
-    //pc.printf("SumSW:%d\r\n",SumSWNum);
-    
-
     //SDカードから表示データを読み込み
     SDFileRead();
     
@@ -1328,12 +1375,14 @@
         ScrollRead2();
     }
     
+    //2段階表示に変更
+    //DisplayMode = 2;
     
     //Debug
     if(Debug == 1){
         //DataSerialOut
         for(int y = 0; y < 32; y++){
-            for(int x = 0; x <128; x++){
+            for(int x = 0; x <LED_Width; x++){
                 if(LEDBuffer[y][x]== 0){
                     //pc.printf("0,"); 
                     pc.printf("  "); 
@@ -1350,39 +1399,23 @@
     //sprintf(SDFilePath,"/sd/E233/whitetest.bin");
     //SDBufferWrite(LEDBuffer,0,0,128,32);   
     
-    //2段階表示に変更
-    DisplayMode = 2;
-    
-    //Serial     
+
+    //Serial割り込み有効     
     pc.attach(pc_rx, Serial::RxIrq);
     
-    //DisplayTimer
+    //表示切替タイマ有効
     ChangeTimer.attach(&TimerTick,3);
     
-    //スクロールが有効なら
-    //if(Scroll == 1){
-        ////スクロール用タイマ
-    //    ScrollTimer.attach(ScrollTimerTick,0.02);
-    //}
-    
     
     while(1) { 
         //CT++;
-//        if (ChangeCount == 0){
-//            Paint(LEDBuffer);
-//        }else if(ChangeCount == 1){
-//            Paint(LEDBuffer2);
-//        }else if(ChangeCount == 2){
-//            Paint(LEDBuffer3);
-//        }
         //表示切替は切り替え時にメインバッファに書き込む
         Paint(LEDMainBuffer);
         
-        //スクロール(現状不動作)
+        //スクロール
         //スクロールが有効なら
         if(Scroll == 1){
             //if(CT>0) {
-                //MkPattern(); // Restore original priceless artwork
                 //CT=0; // Start all over.
                 OE = HIGH;
                 ScrollTimerTick();
@@ -1393,11 +1426,6 @@
             //    CT=0;
             //}
         }
-        
-        
-        
         //PaintOFF();
-        //wait_us(10);
     }
-    
 }