Dependencies:   TextLCD mbed

Fork of Mbed-Mensch-1 by Projekte_werkstatt

Revision:
7:903a6653a43b
Parent:
6:eda2b36d5be1
--- a/main.cpp	Fri Mar 11 07:52:54 2016 +0000
+++ b/main.cpp	Fri Mar 18 13:18:27 2016 +0000
@@ -1,7 +1,6 @@
 #include "mbed.h"
 #include "WS2812B.h"
 #include "TextLCD.h"
-#include <string>
 
 I2C i2c(P0_5,P0_4);
 Serial port(P0_19,P0_18);
@@ -9,20 +8,85 @@
 AnalogIn ldr(P0_12);
 AnalogIn poti(P0_11);
 //BusOut rgb(P0_9,P1_15,P0_21);
-//BusIn btn(P1_16,P0_23,P0_10,P0_15);
 
 RGBOut rgb(P0_21,P0_9,P1_15);
-TextLCD lcd(p21, p22, p26, p25, p24, p23,TextLCD::LCD20x4);
-
-LedOut Feld (P0_13);
-//LedOut Other(P0_13); //-> Würfel (ersten 3 LED in Binär-4. LED für 7. Zustand), Spieler, Start, Ziel, Referenz(auch RGB am board)
-// übrige 8 LED für zeit-Anzeige des Zuges einer KI
+                    //Old: P0_13
+LedOut Feld (P1_23);//P1_23 ->14
+LedOut Home (P1_20);//P1_20->13
+LedOut Dice (P1_21);//P1_21->12
+//-> Würfel (ersten 3 LED in Binär-4. LED für 7. Zustand), Spieler, Referenz(auch RGB am board)
 
 InterruptIn Button0(P0_1);
 InterruptIn Button1(P1_16);
 InterruptIn Button2(P0_23);
 InterruptIn Button3(P0_10);
 InterruptIn Button4(P0_15);
+BusIn btn(P1_16,P0_23,P0_10,P0_15,P0_1);
+//Dice
+Timer Random;
+Timer RandomDice;
+
+Ticker DiceAnim;
+void DiceRun();
+void setDice(char number);
+void DicePrint();
+void DiceCal(); //umrechning des 7.Zustands in eine Zahl zwischen 1 & 6
+char dicePos = 0;
+char diceEn = 0;
+char diceTick = 0;
+char diceNum = 0;
+int16_t diceDecode [7]= {0x1,0x88,0x89,0xAA,0xAB,0x1BA,0x154};
+//Game
+#define Feldgrose 10
+#define Spieler 4
+#define Steine 4
+
+char PlayPos[Spieler*Steine];   //Zahl zwischen 0 & 47 ((0xC0 -> Spieler)>>2 / 0x03 -> Stein)
+char Start[Spieler*Steine];           //Enthält den Index für PlayPos (nur das 'normale' Spielfend)
+char Ziel[Spieler*Steine];            //Enthält den Index für PlayPos (nur das 'normale' Spielfend)
+char Drann;                    //Spieler, der drann ist
+char count;                    //Für 6er & mehrfaches Würfeln beim Ansetzen
+char last[Spieler];            //zuletzt benutzte Spielstein, der zuletzt benutzt wurde
+char moglich[Steine];
+char selected;                 //der aktuell ausgewählte Zug
+char Field[Spieler*Feldgrose]; //Enthält den Index für PlayPos (nur das 'normale' Spielfend)
+void FieldPrint();             //Ausgabe auf Com-Port
+void FieldGenerate();          //Berechnung von PlayPos auf Start, Ziel, Field
+void FeldPrint();              //Ausgabe von Field auf den Led-Streifen (Feld) der Farben
+void GameCal();
+Ticker MoveAnim;
+void MoveRun();
+char MoveShow;
+void ColorPrint(uint32_t color);
+//Animations
+char AnimField[Spieler*Feldgrose];
+char AnimStart[Spieler*Steine];           
+char AnimZiel[Spieler*Steine];
+char AnimPos[Spieler*Steine];
+char AnimDo; //Feld_Animieren,Start Animieren, Ziel Animierenw
+void AnimCal();
+//Core Interactions
+void reset();
+void MoveDo();
+void moveNext();
+void moveLast();
+void DiceStart();
+void DiceStop();
+//Button entprellung (funktioniert nicht...)
+void btn0();
+void btn1();
+void btn2();
+void btn3();
+void btn4();
+char btnLast;
+//Farben
+uint32_t Farben[Spieler];      //Berechnete Hellichkeiten der Speiler fertig zur Ausgabe...
+uint32_t Background = 0;       //Hintergrund (Leerfeld)
+uint32_t DiseC = 0x00FF00;     //Würfelfarbe
+void ColorPrint(uint32_t color); //vorher den Header senden (manuell...)
+
+
+TextLCD lcd(p21, p22, p26, p25, p24, p23,TextLCD::LCD20x4);
 
 InterruptIn DD_A(P0_22);//20
 InterruptIn DD_B(P0_16);//19
@@ -41,18 +105,26 @@
 void accept();
 void exit();
 void print_menu();
+void print_pos();
+void print_default();
+void posUpdate();
 void Control();
+char ausfuehren();
 
 //string test ("");
 //                                "1234567890123456789","1234567890123456789","1234567890123456789","1234567890123456789",  ,"1234567890123456789","1234567890123456789","1234567890123456789",
 const char * const  text_ln0[] = {"MENU:              ","Spiel-Menu:        ","Anzeige-Menu:      ","Speicher-Menu:     ","","7.Zustand-Menu:    ","Farben-Menu:       ","Spiel-Menu:        ","","Spielregeln-Menu:  ","Hell./Sp.Led-Menu: ","Config-Menu:       ","","Autplay-Menu:      ","Wurfel-Menu:       ","Ofline/Com-Menu:   "};
-const char * const  text_ln1[] = {"Spiel              ","7.Zustand          ","Farben             ","Spiel              ","","Disable            ","                   ","Laden              ","","Smeiszwang <I/0>   ","Wert: <wert>       ","Laden              ","","Zeit               ","Farbverhalten      ","New                "};
-const char * const  text_ln2[] = {"Anzeige            ","Spielregeln        ","Hellichkeit/Sp.Led ","Config.            ","","kleinstmoglich     ","SP: 1   2   3   4  ","Speichern          ","","Go Home first <I/0>","Sp.Led-Verhalten   ","Speichern          ","","Agro/Taktik        ","Farb def           ","Reset              "};
+const char * const  text_ln1[] = {"Spiel              ","7.Zustand          ","Farben             ","Spiel              ","","Disable            ","Hoffentlich        ","Laden              ","","Smeiszwang <I/0>   ","Wert: <wert>       ","Laden              ","","Zeit               ","Farbverhalten      ","New                "};
+const char * const  text_ln2[] = {"Anzeige            ","Spielregeln        ","Hellichkeit/Sp.Led ","Config.            ","","kleinstmoglich     ","   1   2   3   4   ","Speichern          ","","Go Home first <I/0>","Sp.Led-Verhalten   ","Speichern          ","","Agro/Taktik        ","Farb def           ","Reset              "};
 const char * const  text_ln3[] = {"Speicher           ","Autoplay           ","Wurfel             ","Ofline/Com         ","","grostmoglich       ","                   ","Reset              ","","Ausfahrzwang <I/0> ","Andert: <wert>     ","Reset              ","","Enable/Manuell     ","Hellichkeit        ","Com                "};
+char textPos[]                 = {0x0                  ,0x1                  ,0x2                  ,0x3                  ,0x4,0x5                 ,0x6                  ,0x7                  ,0x8,0x9                 ,0xA                  ,0xB                  ,0xC,0xD                 ,0xE                  ,0xF                  ,0x10,0x11};
 
 
-char selected[256];
+char VHS = 0;
+char mSelected[256];
 char depth = 0;
+uint32_t postion = 0;//Dafür brauche ich die Methode
+
 
 int main() 
 {
@@ -73,10 +145,11 @@
     __disable_irq();
     __enable_irq();
     */
-    wait(0.01);
+    
     DD_A.mode(PullDown);
     DD_B.mode(PullDown);
     DD_C.mode(PullDown);
+    wait(0.01);
     DD_A.rise(&dreh);
     DD_A.fall(&dreh);
     DD_B.rise(&dreh);
@@ -88,10 +161,21 @@
     
     //text_ln0[0]="";
     //port.printf("D%c%ctest\n",27,27);
-    print_menu();
+    print_default();
     
     while(1) ;        
 }
+void print_default()
+{
+    lcd.cls();
+    lcd.printf(" Spiel lauft...");
+    lcd.locate(0,3);
+    lcd.printf("12345678901234567890");
+    port.printf("D%c%c Spiel lauft...     \n",27,27);
+    port.printf("D%c%c                    \n",27,28);
+    port.printf("D%c%c                    \n",27,29);
+    port.printf("D%c%c12345678901234567890\n",27,30);
+}
 void Control()
 {
     while(port.readable ())
@@ -100,7 +184,7 @@
         switch(message)
         {
             case 'D':
-                port.printf("Ldepth:%d,selected[depth-1]:%d\n",depth,selected[depth-1]);
+                port.printf("Ldepth:%d,mSelected[depth-1]:%d\n",depth,mSelected[depth-1]);
             break;
             case 'E':
                 exit();
@@ -116,7 +200,7 @@
             break;
             case 'I':
                 depth=0;
-                selected[0]=0;
+                mSelected[0]=0;
                 print_menu();
             break;
         }
@@ -142,7 +226,18 @@
         DD_last = temp;
     }
 }
-
+char ausfuehren()
+{
+    char out = 1;
+    switch (postion)
+    {
+    case 0x5:
+        break;
+    default:
+        break;
+    }
+    return out;
+}
 void druck()
 {
     if(DD_D==DD_d)
@@ -151,121 +246,170 @@
         accept();
     DD_d=DD_D;
 }
-
+void posUpdate()
+{
+    postion = 0;
+    for(char i = 0;i<depth-1;i++)
+        postion = postion|(mSelected[i]<<(i*2));
+}
 void up()
 {
     if(depth>0)
     {
-        if(selected[depth-1]<1)
-            selected[depth-1]=3;
+        if(mSelected[depth-1]<1)
+        {
+            if(VHS==0)
+                mSelected[depth-1]=3;
+            else if(VHS==1)
+                mSelected[depth-1]=4;
+            else
+                mSelected[depth-1]=VHS;
+        }
         else
-            selected[depth-1]--;
-        print_menu();
+            mSelected[depth-1]--;
+        print_pos();
     }
 }
 void down()
 {
     if(depth>0)
     {
-        if(selected[depth-1]>2)
-            selected[depth-1]=0;
+        char temp=3;
+        if(VHS==0)
+            temp=3;
+        else if(VHS==1)
+            temp=4;
         else
-            selected[depth-1]++;
-        print_menu();
+            temp=VHS;
+        if(mSelected[depth-1]>(temp-1))
+            mSelected[depth-1]=0;
+        else
+            mSelected[depth-1]++;
+        print_pos();
     }
 }
 void accept()
 {
     if(depth>0)
     {
-        if(depth<=3)
+        postion = 0;
+        for(char i = 0;i<depth;i++)
+            postion = postion|(mSelected[i]<<(i*2));
+        char temp = ausfuhren();
+        if(temp>0)
         {
-            if(selected[depth-1]==0)
-                exit();
+            if(mSelected[depth-1]==0)
+            {
+                depth--;//Exit()
+                mSelected[depth]=0;
+            }
             else
             {
-                selected[depth]=1;
+                mSelected[depth]=1;
                 depth++;
             }
         }
     }
     else
         depth=1;
-    print_menu();
+    posUpdate();
+    if(postion==0x6)
+        VHS=1;
+    else
+        VHS=0;
+    if(depth>0)
+    {
+        print_menu();
+        print_pos();
+    }
+    else
+        print_default();
 }
 void exit()
 {
     if(depth>0)
     {
         depth--;
-        if(depth<=3)
-            selected[depth]=0;
+        mSelected[depth]=0;
     }
-    print_menu();
+    posUpdate();
+    if(postion==0x6)
+        VHS=1;
+    else
+        VHS=0;
+    if(depth>0)
+    {
+        print_menu();
+        print_pos();
+    }
+    else
+        print_default();
 }
 void print_menu()
 {
-    if(depth>0)
+    port.printf("DR\n");
+    lcd.cls();
+    lcd.locate(1,0);
+    lcd.printf(text_ln0[textPos[postion]]);
+    port.printf("D%c%c%s\n",28,27,text_ln0[textPos[postion]]);
+    lcd.locate(1,1);
+    lcd.printf(text_ln1[textPos[postion]]);
+    port.printf("D%c%c%s\n",28,28,text_ln1[textPos[postion]]);
+    lcd.locate(1,2);
+    lcd.printf(text_ln2[textPos[postion]]);
+    port.printf("D%c%c%s\n",28,29,text_ln2[textPos[postion]]);
+    lcd.locate(1,3);
+    lcd.printf(text_ln3[textPos[postion]]);
+    port.printf("D%c%c%s\n",28,30,text_ln3[textPos[postion]]);
+}
+void print_pos()
+{
+    if (VHS==1)
     {
-        if(depth>3)//        "                    "
+        lcd.locate(0,3);
+        //         "--------------------"
+        lcd.printf("                    ");
+        port.printf("D%c%c                    \n",27,27+3);
+
+        lcd.locate(0,0);
+        if(mSelected[depth-1]==0)
         {
-            port.printf("D%c%c Custom              \n",27,27);
-            lcd.locate(0,0);
-            lcd.printf(" Custom              ");
+            port.printf("D%c%c<\n",27,27);
+            lcd.putc('<');
         }
         else
         {
-            uint32_t _selected = 0;
-            for(char i = 0;i<depth-1;i++)
-                _selected = _selected|(selected[i]<<(i*2));
-            //port.printf("L_selected:%d\n",_selected);
-            lcd.locate(1,0);
-            lcd.printf(text_ln0[_selected]);
-            port.printf("D%c%c%s\n",28,27,text_ln0[_selected]);
-            lcd.locate(1,1);
-            lcd.printf(text_ln1[_selected]);
-            port.printf("D%c%c%s\n",28,28,text_ln1[_selected]);
-            lcd.locate(1,2);
-            lcd.printf(text_ln2[_selected]);
-            port.printf("D%c%c%s\n",28,29,text_ln2[_selected]);
-            lcd.locate(1,3);
-            lcd.printf(text_ln3[_selected]);
-            port.printf("D%c%c%s\n",28,30,text_ln3[_selected]);
-            
-            for(char i=0;i<4;i++)
-            {
-                lcd.locate(0,i);
-                if((selected[depth-1]+0)==i)
-                {
-                    if(i==0)
-                    {
-                        port.printf("D%c%c<\n",27,i+27);
-                        lcd.putc('<');
-                    }
-                    else
-                    {
-                        port.printf("D%c%c>\n",27,i+27);
-                        lcd.putc('>');
-                    }
-                }
-                else
-                {
-                    port.printf("D%c%c \n",27,i+27);
-                    lcd.putc(' ');
-                }
-            }
+            port.printf("D%c%c \n",27,27);
+            lcd.putc(' ');
+
+            lcd.locate(4*mSelected[depth-1],3);
+            lcd.putc('^');
+            port.printf("D%c%c^\n",27+4*mSelected[depth-1],27+3);
         }
     }
     else
     {
-        lcd.cls();
-        lcd.printf(" Spiel lauft...");
-        lcd.locate(0,3);
-        lcd.printf("12345678901234567890");
-        port.printf("D%c%c Spiel lauft...     \n",27,27);
-        port.printf("D%c%c                    \n",27,28);
-        port.printf("D%c%c                    \n",27,29);
-        port.printf("D%c%c12345678901234567890\n",27,30);
+        for(char i=0;i<4;i++)
+        {
+            lcd.locate(0,i);
+            if((mSelected[depth-1]+0)==i)
+            {
+                if(i==0)
+                {
+                    port.printf("D%c%c<\n",27,i+27);
+                    lcd.putc('<');
+                }
+                else
+                {
+                    port.printf("D%c%c>\n",27,i+27);
+                    lcd.putc('>');
+                }
+            }
+            else
+            {
+                port.printf("D%c%c \n",27,i+27);
+                lcd.putc(' ');
+            }
+        }
     }
 }
-