Platform controller for the Mobile Arcade project

Dependencies:   APA102 Motor

Files at this revision

API Documentation at this revision

Comitter:
abraha2d
Date:
Tue Dec 11 17:29:25 2018 +0000
Parent:
2:774edcf60939
Commit message:
Better game/menu lighting

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Dec 11 14:44:55 2018 +0000
+++ b/main.cpp	Tue Dec 11 17:29:25 2018 +0000
@@ -23,8 +23,10 @@
 #define GAME_START          51
 #define NUM_GAME            19
 
-#define COLOR_GAME_MENU     CRGB(1, 1, 1)
-#define COLOR_GAME_GAME     CRGB(0, 1, 2)
+#define COLOR_GAME_MENU     CRGB(32, 32, 32)
+#define COLOR_GAME_GAME     CRGB(0, 64, 128)
+#define COLOR_GAME_NUM      3 
+#define COLOR_GAME_DEN      3
 
 #define COLOR_HEADLIGHT_DIM CRGB(8, 8, 8)
 #define COLOR_HEADLIGHT_ON  CRGB(32, 32, 32)
@@ -54,9 +56,7 @@
 int numGame = 0;
 
 CRGB gameColor = COLOR_GAME_MENU;
-int gamePwmNum = 1;
-int gamePwmDen = 4;
-int gamePwmMax = 8;
+double gameBrightness = 0;
 
 SPI l_strip(p5, p6, p7);
 SPI r_strip(p11, p12, p13);
@@ -73,6 +73,7 @@
 Thread pcThread;
 Thread btThread;
 Thread blinkerThread;
+Thread gameThread;
 
 void pc_thread() {
     for(;;) {
@@ -81,13 +82,29 @@
             case 's':
                 // Switch to game
                 while(pc.getc() != '\n'); // Flush rest of command
-                while (gamePwmNum != 1) wait(0.01); // Wait for dim before switch
+                while (gameBrightness > 0.1250168966) wait(0.01); // Wait for dim before switch
+                for (int i = 0; i < 100; i++) {
+                    gameColor = CRGB(
+                        (COLOR_GAME_GAME.r - COLOR_GAME_MENU.r) * (i / 100.0) + COLOR_GAME_MENU.r,
+                        (COLOR_GAME_GAME.g - COLOR_GAME_MENU.g) * (i / 100.0) + COLOR_GAME_MENU.g,
+                        (COLOR_GAME_GAME.b - COLOR_GAME_MENU.b) * (i / 100.0) + COLOR_GAME_MENU.b
+                    );
+                    wait(0.01);
+                }
                 gameColor = COLOR_GAME_GAME;
                 break;
             case 'e':
                 // Switch to menu
                 while(pc.getc() != '\n'); // Flush rest of command
-                while (gamePwmNum != 1) wait(0.01); // Wait for dim before switch
+                while (gameBrightness > 0.1250168966) wait(0.01); // Wait for dim before switch
+                for (int i = 0; i < 100; i++) {
+                    gameColor = CRGB(
+                        (COLOR_GAME_MENU.r - COLOR_GAME_GAME.r) * (i / 100.0) + COLOR_GAME_GAME.r,
+                        (COLOR_GAME_MENU.g - COLOR_GAME_GAME.g) * (i / 100.0) + COLOR_GAME_GAME.g,
+                        (COLOR_GAME_MENU.b - COLOR_GAME_GAME.b) * (i / 100.0) + COLOR_GAME_GAME.b
+                    );
+                    wait(0.01);
+                }
                 gameColor = COLOR_GAME_MENU;
                 break;
             case 'b':
@@ -195,6 +212,16 @@
   }
 }
 
+void game_thread() {
+    int bCtr = 0;
+    for(;;) {
+        gameBrightness = (exp(sin(bCtr*0.01570796327))-0.3678794412)*0.3722766811+0.125;
+        bCtr = ++bCtr % 400;
+        pc.printf("%i\r\n", bCtr);
+        wait(0.01);
+    }
+}
+
 // main() runs in its own thread in the OS
 int main() {
     
@@ -212,20 +239,9 @@
     pcThread.start(pc_thread);
     btThread.start(bt_thread);
     blinkerThread.start(blinker_thread);
-    
-    int ctr = 0;
-    bool up = true;
+    gameThread.start(game_thread);
     
     for(;;) {
-        if (ctr % (gamePwmDen * 7) == 0) {
-            gamePwmNum += up ? 1 : -1;
-            if (gamePwmNum == gamePwmMax) {
-                up = false;
-            } else if (gamePwmNum == 1) {
-                up = true;
-                ctr = 0;
-            }
-        }
         
         if (lights) {
         
@@ -260,14 +276,12 @@
             
         }
         
-        double scaleFactor = ceil((double) gamePwmNum / gamePwmDen);
         if (numGame != NUM_GAME) {
             fill_solid(&(l_leds[GAME_START]), NUM_GAME, CRGB::Black);
             fill_solid(&(r_leds[GAME_START]), NUM_GAME, CRGB::Black);
         }
-        fill_solid(&(l_leds[GAME_START]), numGame, (ctr % gamePwmDen) < (gamePwmNum / scaleFactor) ? gameColor * scaleFactor : CRGB::Black);
-        fill_solid(&(r_leds[GAME_START]), numGame, (ctr % gamePwmDen) < (gamePwmNum / scaleFactor) ? gameColor * scaleFactor : CRGB::Black);
-        ctr++;
+        fill_solid(&(l_leds[GAME_START]), numGame, gameColor * gameBrightness);
+        fill_solid(&(r_leds[GAME_START]), numGame, gameColor * gameBrightness);
         
         APA102_write(l_strip, l_leds, NUM_LEDS);
         APA102_write(r_strip, r_leds, NUM_LEDS);