Another dice program for the mbuino.

Dependencies:   mbed mBuino_Sleep

You probably want http://mbed.org/users/maxint/code/mBuino_Dice/ rather than this one, that was the original mbuino dice program.

This version is based off the original release of the project above. It was then significantly re-written for a mixture of power consumption, randomness and coding style reasons. Most of the changes and improvements have since been incorporated into later versions of maxint's dice program (together with a few of his later ideas being copied into this version) so there are no meaningful functional differences between the two.

This version is posted mainly to provide an example of a slightly different way to do the same thing.

Revision:
1:05f369319854
Parent:
0:24177fc1e0e3
--- a/dice.cpp	Wed Sep 17 13:48:57 2014 +0000
+++ b/dice.cpp	Tue Sep 23 15:43:29 2014 +0000
@@ -12,21 +12,18 @@
 */
 
 #include "mbed.h"
+#include "mBuinoSleep.h"
 
-BusOut LEDOuts(LED1, LED2, LED3, LED4, LED5, LED6, LED7);// declare 7 LEDs
 InterruptIn ActionButton(P0_4);  // Vibration sensor
 AnalogIn RandomIn(P0_14); // use the random noise on this analog input to seed the random generator
 
 // LED bus value to display
-uint8_t LEDs = 0;
-
-// fix power draw
-DigitalIn progMode(P0_3);
+uint8_t LEDValue = 0;
 
 // sleep can be sleep, deepsleep or power down.
 // clean power down and cleen deep sleep do a clean shutdown of the PLL.
 // However any code called during the wakeup IRQ will then run at a slower speed.
-enum sleepMode_t {powerDown=0, deepSleep, lightSleep, cleanPowerDown, cleanDeepSleep};
+//enum sleepMode_t {powerDown=0, deepSleep, lightSleep, cleanPowerDown, cleanDeepSleep};
 
 
 // idle time to wait before sleeping
@@ -57,7 +54,7 @@
 void on1msTick()
 {
     static uint8_t ledIndex = 0;
-    LEDOuts = LEDs & 0x01<<ledIndex++;
+    LEDs = LEDValue & 0x01<<ledIndex++;
     if (ledIndex == 7)
         ledIndex = 0;
 }
@@ -69,38 +66,38 @@
 void SetLeds(bool on)
 {
     if (on)
-        LEDs=0x7f;
+        LEDValue=0x7f;
     else
-        LEDs=0;
+        LEDValue=0;
 }
 
 void SetLed(uint8_t ledID, bool on)
 {
     if (ledID <= 6) {
         if (on)
-            LEDs = LEDs | (0x01 << ledID);
+            LEDValue = LEDValue | (0x01 << ledID);
         else
-            LEDs = LEDs & ~(0x01 << ledID);
+            LEDValue = LEDValue & ~(0x01 << ledID);
     }
 }
 
 void ToggleLeds()
 {
-    LEDs = ~LEDs;
+    LEDValue = ~LEDValue;
 }
 
 void ToggleLed(uint8_t ledID)
 {
     if (ledID <= 6)
-        LEDs = LEDs ^ (0x01 << ledID);
+        LEDValue = LEDValue ^ (0x01 << ledID);
 }
 
 void BlinkLeds(bool on=true, float delay=0.1)
 {
-    uint8_t state = LEDs;
+    uint8_t state = LEDValue;
     SetLeds(on);
     wait(delay);
-    LEDs = state;
+    LEDValue = state;
     wait(delay);
 }
 
@@ -110,7 +107,7 @@
     if(nNumber<1) nNumber=1;
     if(nNumber>6) nNumber=6;
 
-    LEDs=DicePattern[nNumber - 1];
+    LEDValue=DicePattern[nNumber - 1];
 }
 
 
@@ -125,8 +122,11 @@
 void SweepSingleLed(bool leftToRight = true, float delay = 0.2)
 {
     SetLeds(false);
-    for(int n=0; n<7; n++)
-        BlinkOneLed(leftToRight?n:6-n, delay);
+    for(int n=0; n<7; n++) {
+        SetLed(leftToRight?n:6-n,true);
+        wait(delay);
+        SetLed(leftToRight?n:6-n,false);
+    }
 }
 
 void StackLEDs(float delay = 0.2)
@@ -157,92 +157,28 @@
 }
 
 
-void mySleep() // ONLY CALL FROM MAIN LOOP
-{
-
-    LPC_PMU->PCON = 0x0;
-    SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
-    __WFI();
-}
-
-// deepSleep is higher power but faster wakeup
-// clean PLL shutdown is technically needed but seems to work without it.
-// Note - If using clean PLL shutdown the IRQ that wakes us runs on the IRC
-// not the system clock because the clock isn't set up until after the IRQ is complete.
-void myPowerDown(bool cleanPLLShutdown = false, bool deepSleep = false)
+void enterSleep(enum sleepMode_t mode)
 {
 
-    if (deepSleep)
-        LPC_PMU->PCON = 0x1;
-    else
-        LPC_PMU->PCON = 0x2;
-
-
-    LPC_SYSCON->PDSLEEPCFG |= 0x7f;  // shut off everything we can.
-    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
-
-    bool IRCPowered = (LPC_SYSCON->PDRUNCFG & 0x01); // only used for cleen shutdown but defined here for scope reasons.
-
-    if (cleanPLLShutdown) {
-        if (!IRCPowered)
-            LPC_SYSCON->PDRUNCFG &= 0xfffffffe; // power up the IRC
-
-        LPC_SYSCON->MAINCLKSEL    = 0x00; // switch to IRC to avoid glitches
-        LPC_SYSCON->MAINCLKUEN    = 0x01;               /* Update MCLK Clock Source */
-        LPC_SYSCON->MAINCLKUEN    = 0x00;               /* Toggle Update Register   */
-        LPC_SYSCON->MAINCLKUEN    = 0x01;
-        while (!(LPC_SYSCON->MAINCLKUEN & 0x01));       /* Wait Until Updated       */
-    }
+    SweepSingleLed(true, 0.1);
+    SweepSingleLed(false, 0.1);
 
-    LPC_SYSCON->PDAWAKECFG = LPC_SYSCON->PDRUNCFG; // switch on everything that is currently on when we wake up.
-
-    __WFI();
-
-    if (cleanPLLShutdown) {
-        LPC_SYSCON->MAINCLKSEL    = 0x03; // switch to PLL output
-        LPC_SYSCON->MAINCLKUEN    = 0x01;               /* Update MCLK Clock Source */
-        LPC_SYSCON->MAINCLKUEN    = 0x00;               /* Toggle Update Register   */
-        LPC_SYSCON->MAINCLKUEN    = 0x01;
-        while (!(LPC_SYSCON->MAINCLKUEN & 0x01));       /* Wait Until Updated       */
-
-        if (!IRCPowered)
-            LPC_SYSCON->PDRUNCFG |= 0x01; // power down the IRC
-    }
-}
-
-
-void enterSleep(enum sleepMode_t mode = powerDown)
-{
-
-    SweepSingleLed(0.1);
-
-    // all LEDs off.
-    SetLeds(false);
     // stop timers.
     timeTracker.stop();
     ledCycle.detach();
 
-    switch (mode) {
-        case powerDown:
-        case cleanPowerDown:
-        default:
-            myPowerDown(mode == cleanPowerDown);
-            break;
-        case lightSleep:
-            mySleep();
-            break;
-        case deepSleep:
-        case cleanDeepSleep:
-            myPowerDown(mode == cleanDeepSleep, true);
-            break;
-    }
+    // enter sleep
+    mBuinoSleep(mode);
+    
+    // awake again amd running at full speed at this point.
+    sleepNow = false;
 
-    // awake again amd running at full speed at this point.
+    // restart timers
     timeTracker.start();
     ledCycle.attach_us(on1msTick,1000);
-    sleepNow = false;
-    StackLEDs(0.1);
-    //  SweepAllLeds();
+
+    // show startup animation.
+    StackLEDs(0.05);
 }
 
 
@@ -259,14 +195,10 @@
 }
 
 
-
 void setup(void)
 {
     // perform initialisations
 
-    // ensure no pullup on the progMode pin
-    progMode.mode(PullNone);
-
     // create a 32 bit number out of 32 LSBs from the ADC
     uint32_t seedValue = 0;
     uint16_t value;
@@ -291,6 +223,7 @@
     StackLEDs();
 
     // vibration sensor/button
+    ActionButton.mode(PullUp);
     ActionButton.fall(buttonPressedIRQ);
 
     // Sleep timeout.
@@ -303,7 +236,7 @@
     while(true) {
 
         while(!fButtonPressed) {
-            if (sleepNow) enterSleep(cleanPowerDown);
+            if (sleepNow) enterSleep(PowerDown);
         }
 
         uint8_t cycles = (timeTracker.read_us() % 20) + 30;