123

Dependencies:   WS2812 PixelArray DebounceIn

Files at this revision

API Documentation at this revision

Comitter:
turumputum
Date:
Thu Jul 02 10:33:57 2020 +0000
Parent:
0:ea88f8c194d2
Commit message:
233

Changed in this revision

WS2812.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
diff -r ea88f8c194d2 -r af17c65e86dc WS2812.lib
--- a/WS2812.lib	Wed Jun 24 12:41:45 2020 +0000
+++ b/WS2812.lib	Thu Jul 02 10:33:57 2020 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/bridadan/code/WS2812/#31c18f8b2ddf
+https://os.mbed.com/users/turumputum/code/WS2812/#31c18f8b2ddf
diff -r ea88f8c194d2 -r af17c65e86dc main.cpp
--- a/main.cpp	Wed Jun 24 12:41:45 2020 +0000
+++ b/main.cpp	Thu Jul 02 10:33:57 2020 +0000
@@ -13,10 +13,11 @@
 
 #define verbose 1
 
-#define LED_ON_BRIGHT 200
-#define LedWaitK 6
-#define LedOnK 100
-#define blinkBright 125
+#define LED_ON_BRIGHT 50
+#define LED_BLINK_PERIOD 100
+#define LedWaitK 0
+#define LedOnK 50
+#define blinkBright 50
 
 
 
@@ -30,6 +31,7 @@
 int k;
 bool GameComplete=0;
 float tS,tM,tI;
+int sleepMode;
 
 
 
@@ -64,12 +66,7 @@
                 } else if (tableState[3][__placeInTable]>LedWaitK) {
                     tableState[3][__placeInTable]--;
                 }
-                int Bright = tableState[3][__placeInTable];
-
-                R=(gameRule[gameState][30]/100)*Bright;
-                G=(gameRule[gameState][31]/100)*Bright;
-                B=(gameRule[gameState][32]/100)*Bright;
-                __pa.SetAll(color_set(R,G,B));
+                __pa.SetAll(color_set(0,0,0));
                 __leds.write(__pa.getBuf());
             }
             break;
@@ -77,19 +74,25 @@
         case LedOn:
 
 
-            if(tableState[3][__placeInTable]!=LedOnK) {
+            if((tableState[3][__placeInTable]!=LedOnK)||(sleepMode==1)) {
                 if(tableState[3][__placeInTable]<LedOnK) {
                     tableState[3][__placeInTable]++;
                 } else if (tableState[3][__placeInTable]>LedOnK) {
                     tableState[3][__placeInTable]--;
                 }
-                float Bright = tableState[3][__placeInTable]/100;
+                int Bright = tableState[3][__placeInTable];
+
+                R=0;
+                G=LED_ON_BRIGHT*Bright/100;
+                B=0;
 
-                R=LED_ON_BRIGHT*Bright;
-                G=LED_ON_BRIGHT*Bright;
-                B=LED_ON_BRIGHT*Bright;
-                __pa.SetAll(color_set(R,G,B));
-                __leds.write(__pa.getBuf());
+                if(sleepMode==0) {
+                    __pa.SetAll(color_set(R,G,B));
+                    __leds.write(__pa.getBuf());
+                } else {
+                    __pa.SetAll(color_set(0,0,0));
+                    __leds.write(__pa.getBuf());
+                }
             }
             break;
 
@@ -99,8 +102,30 @@
             break;
 
         case LedBlink:
+            if((tableState[3][__placeInTable]!=LedOnK)||(sleepMode==1)) {
+                if(tableState[3][__placeInTable]<LedOnK) {
+                    tableState[3][__placeInTable]++;
+                } else if (tableState[3][__placeInTable]>LedOnK) {
+                    tableState[3][__placeInTable]--;
+                }
+                int Bright = tableState[3][__placeInTable];
+
+                R=LED_ON_BRIGHT*Bright/100;
+                G=0;
+                B=0;
+                if(sleepMode==0) {
+                    __pa.SetAll(color_set(R,G,B));
+                    __leds.write(__pa.getBuf());
+                } else {
+                    __pa.SetAll(color_set(0,0,0));
+                    __leds.write(__pa.getBuf());
+                }
+            }
+            break;
+
+        case LedRun:
             if(gameState!=idle) {
-                if(tableState[3][__placeInTable]<100) {
+                if(tableState[3][__placeInTable]<LED_BLINK_PERIOD) {
                     tableState[3][__placeInTable]++;
                 } else {
                     tableState[3][__placeInTable]=0;
@@ -108,37 +133,16 @@
 
                 float blinkVal=0;
 
-                blinkVal = sin((6.28/100)*tableState[3][__placeInTable]);
+                blinkVal =0.5 + sin((6.28/LED_BLINK_PERIOD)*tableState[3][__placeInTable])/2;
                 //pc.printf("blinkVal %f \n", blinkVal);
-
-                __pa.SetAll(color_set(blinkBright*blinkVal,0,0));
-                __leds.write(__pa.getBuf());
-            }
-            break;
-
-        case LedRun:
-
-            //беру значение прогреса из таблицы
-            if(tableState[3][__placeInTable]<frontSize) {
-                tableState[3][__placeInTable]++;
-            } else {
-                tableState[3][__placeInTable]=0;
+                if(sleepMode==0) {
+                    __pa.SetAll(color_set(0,blinkBright*blinkVal,0));
+                    __leds.write(__pa.getBuf());
+                } else {
+                    __pa.SetAll(color_set(0,0,0));
+                    __leds.write(__pa.getBuf());
+                }
             }
-            int startPos = tableState[3][__placeInTable];
-            //pc.printf("progres: %d bV1: %f bV2: %f\n",tableState[3][__placeInTable], bV1, bV2);
-
-            for(int w=0; w<__size; w++) {
-                int pos = (startPos + w)%frontSize;
-                R=(gameRule[gameState][30])*front[pos];
-                G=(gameRule[gameState][31])*front[pos];
-                B=(gameRule[gameState][32])*front[pos];
-                __pa.Set(w,color_set(R,G,B));
-
-                //pc.printf("R %f G %f B %f \n",R,G,B);
-                //pc.printf("pos %d \n",pos);
-            }
-
-            __leds.write(__pa.getBuf());
             break;
     }
 }
@@ -175,17 +179,13 @@
                 } else if (tableState[3][__placeInTable]>LedWaitK) {
                     tableState[3][__placeInTable]-=1;
                 }
-                int Bright = tableState[3][__placeInTable];
 
-                R=(gameRule[gameState][30]/100)*Bright;
-                G=(gameRule[gameState][31]/100)*Bright;
-                B=(gameRule[gameState][32]/100)*Bright;
-                writeLed(__type,R,G,B);
+                writeLed(__type,0,0,0);
             }
             break;
 
         case LedOn://_______________________________________________________________________________________________________________________________
-            if(tableState[3][__placeInTable]!=LedOnK) {
+            if((tableState[3][__placeInTable]!=LedOnK)||(sleepMode==1)) {
                 if(tableState[3][__placeInTable]<LedOnK) {
                     tableState[3][__placeInTable]+=2;
                 } else if (tableState[3][__placeInTable]>LedOnK) {
@@ -194,10 +194,14 @@
 
                 int Bright = tableState[3][__placeInTable];
 
-                R=(gameRule[gameState][30]/100)*Bright;
-                G=(gameRule[gameState][31]/100)*Bright;
-                B=(gameRule[gameState][32]/100)*Bright;
-                writeLed(__type,R,G,B);
+                R=0;
+                G=LED_ON_BRIGHT*Bright/100;
+                B=0;
+                if(sleepMode==0) {
+                    writeLed(__type,R,G,B);
+                } else {
+                    writeLed(__type,0,0,0);
+                }
             }
             break;
 
@@ -206,36 +210,45 @@
             break;
 
         case LedBlink://_____________________________________________________________________________________________________________
+            if((tableState[3][__placeInTable]!=LedOnK)||(sleepMode==1)) {
+                if(tableState[3][__placeInTable]<LedOnK) {
+                    tableState[3][__placeInTable]+=2;
+                } else if (tableState[3][__placeInTable]>LedOnK) {
+                    tableState[3][__placeInTable]-=2;
+                }
+
+                int Bright = tableState[3][__placeInTable];
+
+                R=LED_ON_BRIGHT*Bright/100;
+                G=0;
+                B=0;
+                if(sleepMode==0) {
+                    writeLed(__type,R,G,B);
+                } else {
+                    writeLed(__type,0,0,0);
+                }
+            }
+            break;
+
+        case LedRun://________________________________________________________________________________________________________________________________________
+
             if(gameState!=idle) {
-                if(tableState[3][__placeInTable]<100) {
+                if(tableState[3][__placeInTable]<LED_BLINK_PERIOD) {
                     tableState[3][__placeInTable]++;
                 } else {
                     tableState[3][__placeInTable]=0;
                 }
 
                 float blinkVal=0;
-                blinkVal = sin((6.28/100)*tableState[3][__placeInTable]);
-                writeLed(__type,blinkBright*blinkVal,0,0);
-                //setStaticValToLed(__type,__sp1, __size1, __sp2, __size2, blinkBright*blinkVal,0,0);
-            }
-            break;
-
-        case LedRun://________________________________________________________________________________________________________________________________________
+                blinkVal = 0.5 + sin((6.28/LED_BLINK_PERIOD)*tableState[3][__placeInTable])/2;
 
-            if(tableState[3][__placeInTable]<LedOnK) {
-                tableState[3][__placeInTable]++;
-            } else if (tableState[3][__placeInTable]>LedOnK) {
-                tableState[3][__placeInTable]--;
-            }
+                if(sleepMode==0) {
+                    writeLed(__type,0,blinkBright*blinkVal,0);
+                } else {
+                    writeLed(__type,0,0,0);
+                }
 
-            if(tableState[3][__placeInTable]!=LedOnK) {
-                int Bright = tableState[3][__placeInTable];
-
-                R=(gameRule[gameState][30]/100)*Bright;
-                G=(gameRule[gameState][31]/100)*Bright;
-                B=(gameRule[gameState][32]/100)*Bright;
-
-                writeLed(__type,R,G,B);
+                //setStaticValToLed(__type,__sp1, __size1, __sp2, __size2, blinkBright*blinkVal,0,0);
             }
             break;
     }
@@ -262,12 +275,12 @@
 
     } else if(type==1) {                                                                         //нормальная стенка
 
-        if(gameState!=bio) {
+        //if(gameState!=bio) {
             for(int t=__sp2; t<(__sp2+__size2); t++) {
                 pa_stenkaLed_2.Set(numOfLed_stenkaLed_2-t-1,color_set(r,g,b));
             }
             //stenkaLed_2.write(pa_stenkaLed_2.getBuf());
-        }
+        //}
 
         for(int t=__sp1; t<(__sp1+__size1); t++) {
             pa_stenkaLed_1.Set(numOfLed_stenkaLed_1-t-1,color_set(r,g,b));
@@ -382,7 +395,7 @@
             }
 
             setLedRule(gameState);
-            buttonLight(1,0,0,1);
+            buttonLight(0,0,1);
         }
         if(Button_2) {
             gameState=bio;
@@ -390,7 +403,7 @@
                 pc.printf("Game mode chenged to: bio\n");
             }
             setLedRule(gameState);
-            buttonLight(1,1,0,0);
+            buttonLight(1,0,0);
         }
         if(Button_3) {
             gameState=bacterium;
@@ -398,35 +411,49 @@
                 pc.printf("Game mode chenged to: bacterium\n");
             }
             setLedRule(gameState);
-            buttonLight(1,0,1,0);
+            buttonLight(0,1,0);
         }
 
-    } else if ((gameState==rasta)||(gameState==bio)||(gameState==bacterium)) {
-        if((Button_1)||(gameDog>dogIdleCount)) {
+    } else if (((gameState==rasta)||(gameState==bio)||(gameState==bacterium))&&(plasedSum==0)) {
+        if((Button_1)) {
             gameState=idle;
             if(verbose) {
                 pc.printf("Game mode chenged to: idle  gameDog: %d \n", gameDog);
             }
             setLedRule(gameState);
-            buttonLight(0,1,1,1);
+            buttonLight(1,1,1);
             //stateReport();
             gameDog=0;
         }
     }
+
+    if(gameDog>dogIdleCount) {
+        sleepMode=1;
+    } else {
+        if(sleepMode==1) {
+            sleepMode=0;
+            setLedRule(gameState);
+        }
+    }
+
+    if(plasedSum==0) {
+        int R=(gameRule[idle][30] );
+        int G=(gameRule[idle][31]);
+        int B=(gameRule[idle][32]);
+        pa_ButtonLed_1.SetAll(color_set(R,G,B));
+        ButtonLed_1.write(pa_ButtonLed_1.getBuf());
+    } else {
+        pa_ButtonLed_1.SetAll(color_set(0,0,0));
+        ButtonLed_1.write(pa_ButtonLed_1.getBuf());
+    }
 }
 
-void buttonLight(int b1, int b2, int b3, int b4)
+void buttonLight(int b2, int b3, int b4)
 {
 
-    int R=(gameRule[idle][30] )*b1;
-    int G=(gameRule[idle][31])*b1;
-    int B=(gameRule[idle][32])*b1;
-    pa_ButtonLed_1.SetAll(color_set(R,G,B));
-    ButtonLed_1.write(pa_ButtonLed_1.getBuf());
-
-    R=(gameRule[bio][30])*b2;
-    G=(gameRule[bio][31])*b2;
-    B=(gameRule[bio][32])*b2;
+    int R=(gameRule[bio][30])*b2;
+    int G=(gameRule[bio][31])*b2;
+    int B=(gameRule[bio][32])*b2;
     pa_ButtonLed_2.SetAll(color_set(R,G,B));
     ButtonLed_2.write(pa_ButtonLed_2.getBuf());
 
@@ -477,11 +504,11 @@
             tableState[2][r]=LedWait;
             //plasedSum--;
         } else {
-            tableState[2][r]=LedWait; 
+            tableState[2][r]=LedWait;
             if(GameComplete) {            //элемент нет на месте и не должен там быть, и конец игры
                 tableState[2][r]=LedOff;             //выкл
                 tableState[3][r]=0;
-            } 
+            }
         }
     }
 
@@ -492,9 +519,6 @@
         }
     }
 
-    if(plasedSum<0) {
-        plasedSum=0;
-    }
 
     if(gameRule[gameState][33]==plasedSum) {  //проверяем конец игры
         GameComplete=1;             // перводим все диоды в бегущий режим
@@ -531,7 +555,7 @@
     //startUp_Led();
 
     gameState=idle;
-    buttonLight(0,1,1,1);
+    buttonLight(1,1,1);
     setLedRule(gameState);
 
 
diff -r ea88f8c194d2 -r af17c65e86dc main.h
--- a/main.h	Wed Jun 24 12:41:45 2020 +0000
+++ b/main.h	Thu Jul 02 10:33:57 2020 +0000
@@ -69,7 +69,7 @@
 void lightLeds();
 void setLedRule(int mode);
 void setStaticValToLed(int type,int __sp1, int __size1, int __sp2, int __size2, int r, int g, int b);
-void buttonLight(int b1, int b2, int b3, int b4);
+void buttonLight(int b2, int b3, int b4);
 void writeStenksLeds();