TTDC / Mbed 2 deprecated switches

Dependencies:   C12832 MMA7660 mbed

Files at this revision

API Documentation at this revision

Comitter:
ksaito
Date:
Wed Feb 21 09:13:28 2018 +0000
Parent:
0:c3bc6981ad28
Child:
2:0c52cdb80d9d
Commit message:
?????????

Changed in this revision

MMA7660.lib Show annotated file Show diff for this revision Revisions of this file
accel.cpp Show annotated file Show diff for this revision Revisions of this file
accel.h Show diff for this revision Revisions of this file
color.cpp Show annotated file Show diff for this revision Revisions of this file
color.h Show diff for this revision Revisions of this file
commands.h Show annotated file Show diff for this revision Revisions of this file
led3.cpp Show annotated file Show diff for this revision Revisions of this file
led3.h Show diff for this revision Revisions of this file
liner.cpp Show annotated file Show diff for this revision Revisions of this file
liner.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
sound.cpp Show annotated file Show diff for this revision Revisions of this file
sound.h Show diff for this revision Revisions of this file
--- a/MMA7660.lib	Wed Jan 31 10:58:19 2018 +0000
+++ b/MMA7660.lib	Wed Feb 21 09:13:28 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/Sissors/code/MMA7660/#36a163511e34
+http://mbed.org/users/Sissors/code/MMA7660/#3d6fab6f2345
--- a/accel.cpp	Wed Jan 31 10:58:19 2018 +0000
+++ b/accel.cpp	Wed Feb 21 09:13:28 2018 +0000
@@ -1,21 +1,23 @@
 #include "mbed.h"
 #include "MMA7660.h"
-#include "accel.h"
+#include "commands.h"
 
 DigitalOut Zaxis_p(LED2);
 DigitalOut Zaxis_n(LED3);
 MMA7660 MMA(p28, p27);
 
-void AccelInitialize(void) {
+void _Initialize(void) {
     Zaxis_p = 0;
     Zaxis_n = 0;
 }
 
-void AccelProcess(void) {
+void _Process(void) {
     Zaxis_p = MMA.z();
     Zaxis_n = -MMA.z();
 }
 
-void AccelFinalize(void) {
-    AccelInitialize();
+void _Finalize(void) {
+    _Initialize();
 }
+
+COMMAND_DEFINE Command_accel = {"accel", 0x02, _Initialize, _Finalize, _lProcess};
--- a/accel.h	Wed Jan 31 10:58:19 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-extern void AccelInitialize(void);
-extern void AccelProcess(void);
-extern void AccelFinalize(void);
\ No newline at end of file
--- a/color.cpp	Wed Jan 31 10:58:19 2018 +0000
+++ b/color.cpp	Wed Feb 21 09:13:28 2018 +0000
@@ -1,16 +1,22 @@
 #include "mbed.h"
-#include "color.h"
+#include "commands.h"
 
 PwmOut r (p23);
 PwmOut g (p24);
 PwmOut b (p25);
 
-void ColorInitialize(void) {
-    r.period(0.001);
-    ColorFinalize();
+static void _Finalize(void) {
+    r = 1.0;
+    g = 1.0;
+    b = 1.0;    
 }
 
-void ColorProcess(void) {
+static void _Initialize(void) {
+    r.period(0.001);
+    _Finalize();
+}
+
+static void _Process(void) {
     for (float i = 0.0; i < 1.0 ; i += 0.001) {
         float p = 3 * i;
         r = 1.0 - ((p < 1.0) ? 1.0 - p : (p > 2.0) ? p - 2.0 : 0.0);
@@ -20,8 +26,4 @@
     }
 }
 
-void ColorFinalize(void) {
-    r = 1.0;
-    g = 1.0;
-    b = 1.0;    
-}
+COMMAND_DEFINE Command_color = {"color", 0x04, _Initialize, _Finalize, _Process};
--- a/color.h	Wed Jan 31 10:58:19 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-extern void ColorInitialize(void);
-extern void ColorProcess(void);
-extern void ColorFinalize(void);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/commands.h	Wed Feb 21 09:13:28 2018 +0000
@@ -0,0 +1,7 @@
+typedef struct command{
+    char *name;
+    int trigger;
+    void (*initialize)(void);
+    void (*finalize)(void);
+    void (*process)(void);
+} COMMAND_DEFINE;
--- a/led3.cpp	Wed Jan 31 10:58:19 2018 +0000
+++ b/led3.cpp	Wed Feb 21 09:13:28 2018 +0000
@@ -1,16 +1,19 @@
 #include "mbed.h"
+#include "commands.h"
 
 DigitalOut myled(LED3);
 
-void Led3Initialize(void) {
+void _Initialize(void) {
     myled = 0;
 }
 
-void Led3Process(void) {
+void _Process(void) {
     myled = (myled == 0) ? 1 : 0;
     wait(0.3);
 }
 
-void Led3Finalize(void) {
-    Led3Initialize();
-}
\ No newline at end of file
+void _Finalize(void) {
+    _Initialize();
+}
+
+COMMAND_DEFINE Command_led3 = {"led3", 0xff, _Initialize, _Finalize, _Process};
--- a/led3.h	Wed Jan 31 10:58:19 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-extern void Led3Initialize(void);
-extern void Led3Process(void);
-extern void Led3Finalize(void);
\ No newline at end of file
--- a/liner.cpp	Wed Jan 31 10:58:19 2018 +0000
+++ b/liner.cpp	Wed Feb 21 09:13:28 2018 +0000
@@ -1,19 +1,19 @@
 #include "mbed.h"
-#include "liner.h"
+#include "commands.h"
 
 DigitalOut liner1(LED1);
 DigitalOut liner2(LED2);
 DigitalOut liner3(LED3);
 DigitalOut liner4(LED4);
 
-void LinerInitialize(void) {
+void _Initialize(void) {
     liner1 = 0;
     liner2 = 0;
     liner3 = 0;
     liner4 = 0;
 }
 
-void LinerProcess(void) {
+void _Process(void) {
     liner1 = 1;
     wait(0.1);
     liner1 = 0;
@@ -31,6 +31,8 @@
     liner4 = 0; 
 }
 
-void LinerFinalize(void) {
-    LinerInitialize();
+void _Finalize(void) {
+    _Initialize();
 }
+
+COMMAND_DEFINE Command_liner = {"liner", 0x08, _Initialize, _Finalize, _Process};
--- a/liner.h	Wed Jan 31 10:58:19 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-extern void LinerInitialize(void);
-extern void LinerProcess(void);
-extern void LinerFinalize(void);
\ No newline at end of file
--- a/main.cpp	Wed Jan 31 10:58:19 2018 +0000
+++ b/main.cpp	Wed Feb 21 09:13:28 2018 +0000
@@ -1,16 +1,9 @@
 #include "mbed.h"
 #include "C12832.h"
 
-#include "color.h"
-#include "led3.h"
-#include "sound.h"
-#include "accel.h"
-#include "liner.h"
+#include "commands.h"
 
-#define JOY_UP      (0x01)
-#define JOY_DOWN    (0x02)
-#define JOY_LEFT    (0x04)
-#define JOY_RIGHT   (0x08)
+extern COMMAND_DEFINE *commandList[];
 
 BusIn joy(p15,p12,p13,p16);
 DigitalIn fire(p14);
@@ -22,53 +15,33 @@
     lcd.locate(0,3);
     lcd.printf("mbed Switches application");
     
-    ColorInitialize();
-    Led3Initialize();
-    SoundInitialize();
-    AccelInitialize();
-    LinerInitialize();
+    for(int index = 0; commandList[index] != NULL; index++) {
+        commandList[index]->initialize();
+    }
 
     while(true) {
-        char*   message = NULL;
-    
-        if (fire) {
-            message = "Fire!";
-            Led3Process();
-        } else {
-            switch (joy) {
-                case JOY_UP: {
-                    message = "Up!";
-                    SoundProcess();
-                    SoundFinalize();
-                    break;
-                }
-                case JOY_DOWN: {
-                    message = "Down!";
-                    AccelProcess();
-                    break;
-                }
-                case JOY_LEFT: {
-                    message = "Left!";
-                    ColorProcess();
-                    ColorFinalize();
-                    break;
-                }
-                case JOY_RIGHT: {
-                    message = "Right!";
-                    LinerProcess();
-                    break;
-                }
-                default: {
-                    message = NULL;
-                    break;
-                }
+        COMMAND_DEFINE *command = NULL;
+        int trigger = fire ? 0xff : joy;
+/*
+          switch (joy) {
+            case JOY_UP:    {message = "Up!";       SoundProcess(); SoundFinalize();    break;}
+            case JOY_DOWN:  {message = "Down!";     AccelProcess();                     break;}
+            case JOY_LEFT:  {message = "Left!";     ColorProcess(); ColorFinalize();    break;}
+            case JOY_RIGHT: {message = "Right!";    LinerProcess();                     break;}
+            default:        {message = NULL;                                            break;}
+        }
+*/
+        for(int index = 0; commandList[index] != NULL; index++) {
+            if (trigger == commandList[index]->trigger) {
+                command = commandList[index];
             }
         }
-        if (message != NULL) {
+        if (command != NULL) {
             lcd.locate(0,15);
             lcd.printf("        ");
             lcd.locate(0,15);
-            lcd.printf(message);
+            lcd.printf(command->name);
+            command->process();
         }
     }
 }
\ No newline at end of file
--- a/sound.cpp	Wed Jan 31 10:58:19 2018 +0000
+++ b/sound.cpp	Wed Feb 21 09:13:28 2018 +0000
@@ -1,12 +1,13 @@
 #include "mbed.h"
+#include "commands.h"
 
 PwmOut spkr(p26);
 
-void SoundInitialize(void) {
+void _Initialize(void) {
     spkr=0.0;
 }
 
-void SoundProcess(void) {
+void _Process(void) {
     for (float i = 2000.0; i < 10000.0; i += 100) {
         spkr.period(1.0 / i);
         spkr = 0.5;
@@ -14,6 +15,8 @@
     }
 }
 
-void SoundFinalize(void) {
-    SoundInitialize();
-}
\ No newline at end of file
+void _Finalize(void) {
+    _Initialize();
+}
+
+COMMAND_DEFINE Command_sound = {"sound", 0x01, _Initialize, _Finalize, _Process};
\ No newline at end of file
--- a/sound.h	Wed Jan 31 10:58:19 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-extern void SoundInitialize(void);
-extern void SoundProcess(void);
-extern void SoundFinalize(void);
\ No newline at end of file