Maxint R&D / Mbed 2 deprecated mBuino_Dice

Dependencies:   mbed

Fork of mBuino_Dice by Valentin Ivanov

Revision:
1:38fcbf88615a
Parent:
0:5d9ccbe9d49d
Child:
2:0d311a6cfb96
--- a/main.cpp	Wed Jul 23 19:02:43 2014 +0000
+++ b/main.cpp	Mon Sep 01 11:33:47 2014 +0000
@@ -1,33 +1,210 @@
+/*
+** mBuino_Dice
+** 
+** This electronic dice application allows the user to throw an electronic dice by the press of a button
+** Setup requirements: connect any type of switch between P0.4 and GND.
+** On mBuino P0.4 is close to GND and the two pitch distance is exactly the same as a mini-switch! 
+**
+** Made for mBuino on mbed.org by maxint on 1-sep-2014
+**
+** Feel free to use this code anyway you want, but please acknowledge my work by mentioning my name.
+**
+*/
+
+
 #include "mbed.h"
-//#include "rtos.h"
+
+DigitalOut LED[] = {(P0_7), (P0_8), (P0_2), (P0_20), (P1_19), (P0_17), (P0_23)};// declare 7 LEDs
+InterruptIn ActionButton = (P0_4);  // declare the input for the button (or for other single switch, such as movement switch). On mBuino P0.4 is close to GND
+AnalogIn RandomIn = (P0_14); // use the random noise on this analog input to seed the random generator
+
+
+bool DicePattern[6][7]=
+{
+    {0, 0, 0, 1, 0, 0, 0 },
+    {0, 1, 0, 0, 0, 1, 0 },
+    {0, 1, 0, 1, 0, 1, 0 },
+    {1, 0, 1, 0, 1, 0, 1 },
+    {1, 0, 1, 1, 1, 0, 1 },
+    {1, 1, 1, 0, 1, 1, 1 }
+};
 
 float delayTime = .05;
 
-DigitalOut LED[] = {(P0_7), (P0_8), (P0_2), (P0_20), (P1_19), (P0_17), (P0_23)};// declare 7 LEDs
+
+void SwitchAllLeds(bool fOn)
+{   // switch all leds on or off
+    for(int x = 0; x < 7; x++)
+    {
+        LED[x] = fOn?1:0;   // turn on or off
+    }
+}
+
+void BlinkAllLeds(float flDelay=0.2)
+{   // blink all leds
+    SwitchAllLeds(true);
+    wait(flDelay);
+    SwitchAllLeds(false);
+    wait(flDelay);
+}
+
+void BlinkOneLed(int nLed=0, float flDelayOn=0.2, float flDelayOff=0.2)
+{
+    LED[nLed] = 1; // turn on
+    wait(flDelayOn); // delay
+
+    LED[nLed] = 0; // turn off
+    wait(flDelayOff); // delay
+}
+
+void SweepSingleLed(bool fLeftToRight=true, float flDelay=0.2)
+{   // sweep a single led from left to rigth or vise-versa
+    for(int n=0; n<7; n++)
+        BlinkOneLed(fLeftToRight?n:6-n, flDelay, 0);
+}
+
+void SweepAllLeds(bool fLeftToRight=true, float flDelay=0.1)
+{   // light all leds up from left to rigth or vise-versa and then switch them of from reverse direction
+    // leds on, left to right
+    for(int n=0; n<7; n++)
+    {
+        LED[fLeftToRight?n:6-n] = 1; // turn on
+        wait(flDelay); // delay
+    }
+    // leds off, right to left
+    for(int n=0; n<7; n++)
+    {
+        LED[!fLeftToRight?n:6-n] = 0; // turn off
+        wait(flDelay); // delay
+    }
+}
+
+void SwitchDiceLed(int nNumber, bool fOn=true)
+{   // switch the leds of a particular dice-number on or off
+    if(nNumber<1) nNumber=1;
+    if(nNumber>6) nNumber=6;
+    
+    for(int n=0; n<7; n++)
+        if(DicePattern[nNumber-1][n])
+            LED[n]=fOn;
+}
+
+void BlinkDiceLed(int nNumber, float flDelayOn=0.6, float flDelayOff=0.1)
+{   // blink a particular dice value
+    SwitchDiceLed(nNumber, true);
+    wait(flDelayOn);
+    SwitchDiceLed(nNumber, false);
+    wait(flDelayOff);
+}
+
+int RollDice(int nRolls=10, int nBlinks=2, float flDelayRoll=0.2)
+{   // roll the dice and show the outcome value
+    int nDiceValue;
+    
+    // first roll the dice
+    for(int n=0; n<nRolls; n++)
+    {
+        nDiceValue=rand()%6+1;
+        BlinkDiceLed(nDiceValue, flDelayRoll, 0);
+    }
+    
+    // then show the result
+    nDiceValue=rand()%6+1;
+    for(int n=0; n<nBlinks; n++)
+        BlinkDiceLed(nDiceValue, flDelayRoll, 0);
+    BlinkDiceLed(nDiceValue, true);        
+    
+    return(nDiceValue);
+}
+
+bool fButtonPressed=false;      // if the button is pressed, mBuino will switch from demo mode to play mode. 
+bool fDemoDone=false;
+
+void interruptButtonPressed()
+{
+    fButtonPressed=true;
+}
+
+void setup(void)
+{   // perform initialisations
+    srand(RandomIn.read_u16());     // seed the random generator with the background noise of an analog input
+    
+    ActionButton.fall(interruptButtonPressed);
+}
+
+void loop(void)
+{   // run forever
+    if(!fDemoDone && !fButtonPressed)
+    {   // Run an entertaining demo show until button is pressed
+        // For all parts of the show we check to see if the actionbutton was pressed to stop the show asap
+        if(!fButtonPressed)
+            SweepAllLeds(true);
+        if(!fButtonPressed)
+            SweepAllLeds(false);
+        if(!fButtonPressed)
+            SweepAllLeds(true, 0.05);
+        if(!fButtonPressed)
+            SweepAllLeds(false, 0.05);
+        
+        // show the dice numbers from one to six
+        for(int n=1; n<=6; n++)
+            if(!fButtonPressed)
+                BlinkDiceLed(n);
+
+        if(!fButtonPressed)
+            SweepSingleLed(true, 0.2);
+        if(!fButtonPressed)
+            SweepSingleLed(false, 0.1);
+        
+        // show three random dice throws
+        for(int n=0; n<3; n++)
+        {
+            if(!fButtonPressed)
+            {
+                RollDice();
+                wait(1);
+                SwitchAllLeds(false);
+            }
+        }
+
+        // blink all leds as the same time at increasing and decreasing speeds
+        for(float flDelay=0.3; flDelay>0; flDelay-=0.05)
+            if(!fButtonPressed)
+                BlinkAllLeds(flDelay);
+        for(float flDelay=0.05; flDelay<0.4; flDelay+=0.05)
+            if(!fButtonPressed)
+                BlinkAllLeds(flDelay);
+        
+        if(!fButtonPressed)
+            wait(1);
+    }
+    else
+    {   // demo mode is ended, roll the dice upon each button press
+        fDemoDone=true;
+        fButtonPressed=false;
+        
+        while(!fButtonPressed)
+        {   // flash the LEDS to show we wait for a roll call
+            SweepAllLeds(true, 0.01);
+            SweepAllLeds(false, 0.01);
+        }
+        fButtonPressed=false;
+
+        Timer tWait;
+        
+        int nDiceValue=RollDice();
+        wait(1);
+        tWait.start();
+        fButtonPressed=false;
+        while(!fButtonPressed && tWait.read_ms()<10000)
+            BlinkDiceLed(nDiceValue, 0.2, 0.01);  // hmmmm, something switched off the leds after the wait? Strange, let's just blink them...
+        SwitchAllLeds(false);
+    }
+}
 
 int main()
-{
-    int i = 0;
-    while( i < 10 ) {
-        for(int x = 0; x < 7; x++) {
-            LED[x] = 1; // turn on
-            wait(delayTime); // delay
-        }
-        for(int x = 0; x < 7; x++) {
-            LED[x] = 0; // turn off
-            wait(delayTime); // delay
-        }
-        for(int x = 6; x >= 0; x--) {
-            LED[x] = 1; // turn on
-            wait(delayTime); // delay
-        }
-        for(int x = 6; x >= 0; x--) {
-            LED[x] = 0; // turn off
-            wait(delayTime); // delay
-        }
-
-        i++;
-    }
-
-    //Thread::wait(osWaitForever);
+{   // implement Arduino-style setup() and loop()
+    setup();
+    while(true)
+        loop();
 }