eLab Team / Mbed 2 deprecated LaLaBox

Dependencies:   mbed CREALIB

Revision:
25:3d3ebfd0a73f
Parent:
24:59caeaa9f82d
Child:
26:bf432a28d0c6
diff -r 59caeaa9f82d -r 3d3ebfd0a73f main.cpp
--- a/main.cpp	Wed Oct 03 11:42:21 2018 +0000
+++ b/main.cpp	Thu Dec 06 18:06:21 2018 +0000
@@ -1,38 +1,43 @@
 /*
- *   LaLaBOX program
+ *   Rotator program
  *
- *  Version 3.0, July 17, 2017
- *  for Nucleo32: F303K8
- *   
+ *  Version 1.0, December 2, 2018
+ *  for Nucleo32: L432
+ *
  */
- 
-#define DEBUG_MODE 1 // 0= no Debug, 1 = BT only, 2 = PC only, 3 = BT + PC 
+
+#define DEBUG_MODE 3 // 0= no Debug, 1 = BT only, 2 = PC only, 3 = BT + PC 
 #include "Crealab.h"
 Serial bt_uart(PA_9, PA_10);
 Serial pc_uart(USBTX, USBRX);
 
 // ---------------- Local global variables --------------
-
-Music song_greensleaves("Greensleaves:d=4,o=5,b=140:g,2a#,c6,d.6,8d#6,d6,2c6,a,f.,8g,a,2a#,g,g.,8f,g,2a,f,2d,g,2a#,c6,d.6,8e6,d6,2c6,a,f.,8g,a,a#.,8a,g,f#.,8e,f#,2g");
-Music song_lightmyfire("LightMyFire:d=4,o=5,b=140:8b,16g,16a,8b,8d6,8c6,8b,8a,8g,8a,16f,16a,8c6,8f6,16d6,16c6,16a#,16g,8g#,8g,8g#,16g,16a,8b,8c#6,16b,16a,16g,16f,8e,8f,1a,a");
-Music song_xfile("Xfiles:d=4,o=5,b=140:e,b,a,b,d6,2b.");
-Music song_christmas("Christmas:d=4,o=5,b=100:f#,g#,2a#,2a#,d#.,8f,f,f,2f#,2d#,2f#.6,f#,#g,8g#,g#,8a#,b,8c#,c#,2c#,8d#,8f.,8f#.,8f.,d#,f,2f#.,a#,8b.,8b.,8b.,d#,f,2c#.,a#,8b.,8b.,8b.,d#,f,f#,p,g#,g#,g#,8g#,8a#,8g#,f#.,g#,a#,p,c,c,c,8c,8c#,8c,a#.,c,c#,a#,b,a#,a,a#,b,c,");
-
+int UICommad=0;     // New Command from the UI
+int32_t CSpeed=0;       // Current Speed
+int32_t CMod=0;         // Current Mode: 0: Stop; 1: Run; -1: Zero
+int BTControl=0;    // Control taken by Bluetooth
+char ExecCommand;   // Command to be execute
 // ---------------- PIN DEFINITIONS ---------------------
 DigitalOut myled(LED1);     // Blinking LED
-InterruptIn  buttonBox(PB_1); 
+//InterruptIn  buttonBox(PB_1);
 
 // --- Define the Four PINs & Time of movement used for Motor drive -----
-Motor motorBox(PA_8, PA_11, PB_5, PB_4, 5000);
-Motor motorDancer(PA_4, PA_13, PA_1, PA_0, 2000);
+Motor motorRotator(PA_8, PA_11, PB_5, PB_4, 1000);
+// --- Define the LEDS
+int NoLED; // No LED
+DigitalOut LedRouge(PB_7);
+DigitalOut LedJaune(PB_0);
+DigitalOut LedVerte(PA_12);
+// --- Define the Speed Potentiometer
+AnalogIn    SpeedPlus(PA_7);
+AnalogIn    SpeedMoins(PA_6);
+// --- Define the three mode switch
+DigitalIn   ControlZero(PA_0, PullUp);
+DigitalIn   ControlStop(PA_1, PullUp);
+DigitalIn   ControlRun(PA_3, PullUp);
 
-// PIN & number of LEDS. Available color ==> BLUE, LIGHTBLUE, RED, GREEN, BLACK, WHITE, PURPLE, PINK, YELLOW
-LED_WS2812 ledBand(PB_3,16);
-
-// --- Define PB_0 as the output of PWM use for Tones -----
-Buzzer buzzer(PB_0);
-
-Note la("A#4",50);  //the sound
+// ------------ Process ----------------------------------
+Ticker  HWUI;
 
 void help() // Display list of Commands
 {
@@ -43,27 +48,96 @@
 /* Stop all processes */
 void stop_all()
 {
-    motorBox.Stop();
-    motorDancer.Stop();
-    ledBand.ResetColor();
-    buzzer.stopPlay();
+    motorRotator.Stop();
+}
+
+int32_t    LireSpeed()
+{
+    double  inp;
+    double  inm;
+    int32_t TargetSpeed;
+    inp=(double) SpeedPlus.read();
+    inm=(double) SpeedMoins.read();
+    TargetSpeed=int((inp-inm)*15.0);
+    if (TargetSpeed > 9) TargetSpeed = 9;
+    if (TargetSpeed < -9) TargetSpeed = -9;
+    return(TargetSpeed);
+}
+
+int32_t    LireControl()
+{
+    if (ControlStop==0) return(0);
+    if (ControlRun==0) return(1);
+    if (ControlZero==0) return(-1);
+    return(0);
+}
+
+void    TestHW()
+{
+    LedRouge=1;
+    wait(0.5);
+    LedJaune=1;
+    wait(0.5);
+    LedVerte=1;
+    wait(0.5);
+    LedRouge=0;
+    wait(0.5);
+    LedJaune=0;
+    wait(0.5);
+    LedVerte=0;
+    wait(0.5);
 }
 
-/* Interrupt routine, switch of end of course */
-void clicked()
+void    HardwareUI()
 {
-    DEBUG("End of Course\n\r");
-    stop_all();
+    // Display Motor status with Green  and Yellow LEDs
+    if (!NoLED) {
+        if (motorRotator.GetState() == Motor_RUN) LedVerte=1;
+        else if (motorRotator.GetState() == Motor_ZERO) LedVerte = !LedVerte;
+        else LedVerte=0;
+        if (motorRotator.direction==CLOCKWISE) LedJaune=1;
+        else LedJaune=0;
+        LedRouge=BTControl;
+    }
 }
 
-/* Launch Light Mega Show ! */
-void illuminati() {
-                        ledBand.StopRotation(); ledBand.ResetColor();
-                     ledBand.InsertColorNtimes(3,YELLOW,5.0);
-                     ledBand.InsertColorNtimes(1,PURPLE,5);
-                     ledBand.InsertColorNtimes(4,GREEN,10.0);
-                     ledBand.StartRotation(0.1);
- 
+char    ComposeCommandFromUI()
+{
+    char NewUICommand='-';   // Null command
+    if (!BTControl) {
+        int32_t TSp;
+        int32_t TMod;
+        int32_t TSpeed;
+        int32_t TDir;
+        TSp=LireSpeed();
+        if (TSp>0) TDir=CLOCKWISE;
+        if (TSp<0) TDir=COUNTERCLOCKWISE;
+        TSpeed=abs(TSp);
+        TMod=LireControl();
+        if (CMod!=TMod) {
+            if (TMod==0) NewUICommand = 'S';
+            if (TMod==1) {
+                if (TDir == CLOCKWISE) NewUICommand='R';
+                else NewUICommand='L';
+            }
+            if (TMod==-1) NewUICommand = 'Z';
+            CMod=TMod;
+        } else {
+            if (TSpeed!=CSpeed) {
+                if (TSpeed==0)NewUICommand = 'S';
+                else NewUICommand = char('0'+TSpeed);
+                CSpeed=TSpeed;
+            }
+        }
+    }
+    return (NewUICommand);
+}
+
+/* Message */
+void message()
+{
+    DEBUG("-------------------------------------------------------\n\r");
+    DEBUG("-------------------------------------------------------\n\r");
 }
 
 /* Main Routine */
@@ -71,49 +145,75 @@
 {
     myled = 1;      // To see something is alive
     bool flaghelp;
-    DEBUG("------------------------------------------\n\r");
-    DEBUG("----- LaLaBox (CreaLab)   version 2.0 ----\n\r");
-    DEBUG("------------------------------------------\n\r");
-    ledBand.SetColor(WHITE);
-    DEBUG("SystemCoreClock = %d Hz =\n\r", SystemCoreClock);
-    DEBUG("Wait 2s\n\r");
-
-    /* Connect EoC button */
-    CATCH_BUTTON(buttonBox,clicked);
-
-    /* Play a tone */
-    PLAY_NOTE(la, buzzer);
-
-    wait(2);        // Some delay
+// -- Uncomment to program the HC-06 Module
+//    DEBUG("AT+NAMECreaRoll_x");
+//    wait(10);
+    if (ControlZero==0) NoLED=1;
+    else {
+        NoLED=0;
+        TestHW();
+    }
+    DEBUG("-------------------------------------------------------\n\r");
+    DEBUG("-----        CreaRoll         version 1.0         -----\n\r");
+    DEBUG("-----     faite par fbd38 le   2 Dec 2018         -----\n\r");
+    DEBUG("-------------------------------------------------------\n\r");
+//    DEBUG("SystemCoreClock = %d Hz =\n\r", SystemCoreClock);
+//    DEBUG("Wait 2s\n\r");
     myled = 0;      // Real stuff starts here
-    ledBand.SetColor(BLACK);
-    DEBUG("Enter a command : \n\r");
+    motorRotator.setSpeed(20.0); /* Default */
+    // Install UI
+    HWUI.attach(&HardwareUI, 0.3);
 
     while(1) {
-        char command;   // Command to execute
-        DEBUG(">> ");
-        command = bt_uart.getc();
-        DEBUG("%c", command);
-        flaghelp = false;
-        switch (command) {
-            case 'h':
-                help();
-                flaghelp=true;
-                CASE('o', "OpenBox", motorBox.RunDegrees(COUNTERCLOCKWISE, (float)140.0);    )
-                CASE('c', "CloseBox", motorBox.RunDegrees(CLOCKWISE, (float)140.0);    )
-                CASE('d', "1 step OpenBox ", motorBox.RunDegrees(COUNTERCLOCKWISE, (float)1.0);       )
-                CASE('e', "1 step CloseBox ", motorBox.RunDegrees(CLOCKWISE, (float)1.0);       )
-                CASE('f', "Dancer Clock Wise ", motorDancer.RunInfinite(CLOCKWISE);    )
-                CASE('j', "Dancer Counter Clock Wise ", motorDancer.RunInfinite(COUNTERCLOCKWISE);    )
-                CASE('s', "STOP ", stop_all();   )
-                CASE('m', "Music 1", PLAY_MUSIC(song_xfile,buzzer);   )
-                CASE('n', "Music 2", PLAY_MUSIC(song_lightmyfire,buzzer);   )
-                CASE('p', "Music 3", PLAY_MUSIC(song_greensleaves,buzzer);   )
-                CASE('q', "Music 4", PLAY_MUSIC(song_christmas,buzzer);   )
-                CASE('l', "Light  ", illuminati(); )                
+        ExecCommand='-';    // Clear The Command List
+        /*      Wait Until some command is in the pipe
+         *          eitheir from BT module or HWUI
+         */
+        if (bt_uart.readable()) {
+            char BTlc;   // BT Character to execute
+            BTlc = bt_uart.getc();
+            if (BTlc=='!') {
+                BTControl = 1;
+            } else {
+                if (BTlc=='.') {
+                    BTControl=0;
+                } else if (BTControl) ExecCommand=BTlc;
+            }
+        }
+        // In cas of not controlled by BT, get command from the HW UI
+        if (!BTControl) {
+            ExecCommand=ComposeCommandFromUI();
+            }
+        // ------ Test according with the receved command
+        if (ExecCommand!='-') {
+            DEBUG("Commande Recue: [%c]", ExecCommand);
+            flaghelp = false;
+            switch (ExecCommand) {
+                case 'h':
+                    help();
+                    flaghelp=true;
+                    CASE('R', "Infinite Clockwise", motorRotator.RunInfinite(CLOCKWISE);    )
+                    CASE('L', "Infinite Counterclockwise", motorRotator.RunInfinite(COUNTERCLOCKWISE);    )
+                    CASE('Z', "Go Back to Zero", motorRotator.SetZero(); )
+                    CASE('z', "Define the Zero", motorRotator.DefineZero(); )
+                    CASE('r', "15deg Clockwise", motorRotator.RunDegrees(CLOCKWISE, (float)15.0);       )
+                    CASE('l', "15deg Counterclockwise", motorRotator.RunDegrees(COUNTERCLOCKWISE, (float)15.0);  )
+                    CASE('1', "Speed 10m/t", motorRotator.setSpeed(600.0);  )
+                    CASE('2', "Speed 2m/t", motorRotator.setSpeed(120.0);  )
+                    CASE('3', "Speed 1m/t", motorRotator.setSpeed(60.0);  )
+                    CASE('4', "Speed 30s/t", motorRotator.setSpeed(30.0);  )
+                    CASE('5', "Speed 20s/t", motorRotator.setSpeed(20.0);  )
+                    CASE('6', "Speed 15s/t", motorRotator.setSpeed(15.0);  )
+                    CASE('7', "Speed 10s/t",motorRotator.setSpeed(10.0);    )
+                    CASE('8', "Speed 7s/t",motorRotator.setSpeed(7.0);    )
+                    CASE('9', "Speed 5s/t",motorRotator.setSpeed(5.0);    )
+                    CASE('0', "STOP ", stop_all();   )
+                    CASE('S', "STOP ", stop_all();   )
+                    CASE('?', "Mistery", message(); )
 
-            default :
-                DEBUG("invalid command; use: 'h' for help()");
+                default :
+                    DEBUG("invalid command; use: 'h' for help()");
+            }
         }
     }
 }
\ No newline at end of file