Lobster controller program

Dependencies:   FatFileSystem HBridge MSCFileSystem WavPlayer mbed

Fork of RSALB_hbridge_helloworld by Giles Barton-Owen

Files at this revision

API Documentation at this revision

Comitter:
p07gbar
Date:
Fri Sep 21 14:24:10 2012 +0000
Parent:
1:939f893149f2
Commit message:
Working

Changed in this revision

ActionCue/Action.h Show annotated file Show diff for this revision Revisions of this file
ActionCue/ActionCue.cpp Show annotated file Show diff for this revision Revisions of this file
ActionCue/ActionCue.h Show annotated file Show diff for this revision Revisions of this file
FatFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
MSCFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
WavPlayer.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 939f893149f2 -r f8199cc69b20 ActionCue/Action.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ActionCue/Action.h	Fri Sep 21 14:24:10 2012 +0000
@@ -0,0 +1,30 @@
+#ifndef ACTION_H
+#define ACTION_H
+
+class Action
+{
+    public:
+        Action()
+        {
+            actionTime = 0;
+            actionType = -1;
+            actionInt = 0;
+            actionFloat = 0;
+        }
+       
+       Action(float time, int type, int aint, float afloat)
+       {
+            actionTime  = time;
+            actionType  = type;
+            actionInt   = aint;
+            actionFloat = afloat;
+       }
+        
+        float   actionTime;
+        int     actionType;
+        int     actionInt;
+        float   actionFloat;
+};
+
+
+#endif
\ No newline at end of file
diff -r 939f893149f2 -r f8199cc69b20 ActionCue/ActionCue.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ActionCue/ActionCue.cpp	Fri Sep 21 14:24:10 2012 +0000
@@ -0,0 +1,94 @@
+#include "ActionCue.h"
+#include "mbed.h"
+
+#define ACTIONCUE_ORDERLIMIT 100
+
+ActionCue::ActionCue()
+{
+    start = 0;
+    finish = 0;
+    numStored = 0;
+    for(int i = 0; i < ACTIONCUE_SIZE; i++)
+    {
+        cue[i] = Action();
+    }
+}
+    
+void ActionCue::addAction(Action action_to_add)
+{
+    cue[finish] = action_to_add;
+    finish++;
+    numStored++;
+    if(finish >= start && numStored > ACTIONCUE_SIZE)
+    {
+        start++;
+    }
+    finish = wrap(finish);
+    start  = wrap(start);
+}
+
+int ActionCue::numActionsStored()
+{
+    return numStored;
+}
+
+Action ActionCue::nextAction()
+{
+    return cue[start];
+}
+
+void ActionCue::usedFirst()
+{
+    start++;
+    numStored--;
+    finish = wrap(finish);
+    start  = wrap(start);
+}
+
+Action ActionCue::actionAt(float time)
+{
+    for(int i = 0; i < ACTIONCUE_SIZE; i++)
+    {
+        if(cue[i].actionTime == time) return cue[i];
+    }
+    return Action();
+}
+
+void ActionCue::orderCue()
+{
+    int swapcount = 1;
+    int runcount = 0;
+    Action temp;
+    while((swapcount > 0) && runcount < ACTIONCUE_ORDERLIMIT)
+    {   
+        swapcount = 0;
+        for(int i = 0; i < (numStored - 1); i++)
+        {
+            if(cue[wrap(i+1+start)].actionTime <= cue[wrap(i+start)].actionTime)
+            {
+                temp = cue[wrap(i+start)];
+                cue[wrap(i+start)] = cue[wrap(i+1+start)];
+                cue[wrap(i+1+start)] = temp;
+                swapcount++;
+            }
+        }
+        runcount++;
+    }
+    //printf("Ran: %i\n\r",runcount);
+}
+
+int ActionCue::wrap(int in)
+{
+    while(in >= ACTIONCUE_SIZE)
+    {
+        in -= ACTIONCUE_SIZE;
+    }
+    return in;
+}
+
+
+
+
+
+
+
diff -r 939f893149f2 -r f8199cc69b20 ActionCue/ActionCue.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ActionCue/ActionCue.h	Fri Sep 21 14:24:10 2012 +0000
@@ -0,0 +1,40 @@
+#ifndef ACTIONCUE_H
+#define ACTIONCUE_H
+
+#define ACTIONCUE_SIZE 100
+#include "Action.h"
+
+class ActionCue
+{
+    public:
+    
+    ActionCue();
+    
+    void addAction(Action action_to_add);
+    
+    int numActionsStored();
+    
+    Action nextAction();
+    
+    void usedFirst();
+    
+    Action actionAt(float time);
+    
+    void orderCue();
+    
+    private:
+    
+    Action cue[ACTIONCUE_SIZE];
+    
+    int start;
+    
+    int finish;
+    
+    int numStored;
+    
+    int wrap(int in);
+     
+    
+};
+
+#endif
\ No newline at end of file
diff -r 939f893149f2 -r f8199cc69b20 FatFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FatFileSystem.lib	Fri Sep 21 14:24:10 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/p07gbar/code/FatFileSystem/#e869ee8f3c3d
diff -r 939f893149f2 -r f8199cc69b20 MSCFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MSCFileSystem.lib	Fri Sep 21 14:24:10 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/chris/code/MSCFileSystem/#dcc326e4d358
diff -r 939f893149f2 -r f8199cc69b20 WavPlayer.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WavPlayer.lib	Fri Sep 21 14:24:10 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/p07gbar/code/WavPlayer/#a7380cfc1987
diff -r 939f893149f2 -r f8199cc69b20 main.cpp
--- a/main.cpp	Thu Sep 20 11:38:48 2012 +0000
+++ b/main.cpp	Fri Sep 21 14:24:10 2012 +0000
@@ -1,14 +1,155 @@
-#include "mbed.h"
-#include "HBridge.h"
-
-HBridge hbridge(p18,p15,p22);
-DigitalIn button(p16);
-AnalogIn pot1(p19);
-
-int main() {
- button.mode(PullDown);
-    while(1) {
-        hbridge.speed((pot1 - 0.5) * 2);
-        hbridge.power(bool(button));
-    }
-}
+#include "mbed.h"
+#include "HBridge.h"
+#include "ActionCue.h"
+#include "WavPlayer.h"
+#include "MSCFileSystem.h"
+
+#define TICK_TIME 0.05
+
+MSCFileSystem msc("msc"); // Mount flash drive under the name "msc"
+LocalFileSystem local("local");
+WavPlayer player;
+
+HBridge hbridge(p18,p15,p22);
+DigitalIn button(p16);
+AnalogIn pot1(p19);
+
+HBridge hbridge2(p28,p27,p21);
+DigitalIn button2(p17);
+AnalogIn pot2(p20);
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+
+ActionCue cue;
+
+Ticker ticks;
+
+float c_time;
+
+int audioNumber = 0;
+float audioTime = 0;
+
+extern "C" void HardFault_Handler()
+{
+    error("Hard Fault!\n");
+}
+
+
+enum actionTypes
+{
+    Null = -1,
+    Debug = 0,
+    c_led1,
+    c_led2,
+    motor1,
+    motor2,
+    play
+};
+
+void onTick()
+{
+    while(cue.numActionsStored() > 0)
+    {
+        Action act = cue.nextAction();
+        if(act.actionTime <= c_time)
+        {
+            cue.usedFirst();
+            //printf("Action. Time: %f Type: %i\n\r",act.actionTime, act.actionType);
+            switch(act.actionType)
+            {
+                case Debug:
+                    printf("Action. Time: %f Type: %i\n\r",act.actionTime, act.actionType);
+                    break;
+                case c_led1:
+                    led1 = bool(act.actionInt);
+                    break;
+                case c_led2:
+                    led2 = bool(act.actionInt);
+                    break;
+                case motor1:
+                    hbridge.speed(act.actionFloat);
+                    hbridge.power(true);
+                    break;
+                case motor2:
+                    hbridge2.speed(act.actionFloat);
+                    hbridge2.power(true);
+                    break;
+                case play:
+                    audioNumber = act.actionInt;
+                    audioTime = act.actionFloat;
+                    break;
+                default:
+                    break;
+            }
+        }
+        else
+        {
+            break;
+        }
+    }
+    c_time += float(TICK_TIME);
+}
+
+actionTypes parseType(char in[4])
+{
+         if(strcmp(in, "NULL") == 0) return Null;
+    else if(strcmp(in, "DEBG") == 0) return Debug;
+    else if(strcmp(in, "LED1") == 0) return c_led1;
+    else if(strcmp(in, "LED2") == 0) return c_led2;
+    else if(strcmp(in, "MTR1") == 0) return motor1;
+    else if(strcmp(in, "MTR2") == 0) return motor2;
+    else if(strcmp(in, "PLAY") == 0) return play;
+    else return Null;
+}
+
+int main() {
+    button.mode(PullDown);
+    ticks.attach(&onTick,TICK_TIME);
+    c_time = 0;
+    int returned = 4;
+    printf("Opening the file \n\r");
+    FILE* sequence = fopen("/local/seq.txt","r");
+    
+    if(sequence != NULL)
+    {
+        printf("File opened\n\r");
+        while(returned >= 2)
+        {
+            float temptime = 0;
+            char code[5];
+            int tempint = 0;
+            float tempfloat = 0;
+            returned = fscanf(sequence, "%f %4s %d %f", &temptime, &code, &tempint, &tempfloat);
+
+            if(returned >= 2)
+            {
+                actionTypes temptype = parseType(code);
+                Action act(temptime, temptype, tempint, tempfloat);
+                cue.addAction(act);
+                printf("Read Action. Time: %f Type: %i\n\r",act.actionTime, act.actionType);
+
+            }
+        }
+    }
+    
+    cue.orderCue();
+    
+    while(1)
+    {
+        if(audioNumber != 0)
+        {
+            char filename[16];
+            sprintf(filename, "/msc/%2.2i.wav",audioNumber);
+            printf("Playin': %s\n\r",filename);
+            FILE* file = fopen(filename, "r");
+            player.open(&file);
+            if(audioTime == 0) player.play();
+            else player.play(audioTime);
+            fclose(file);
+            audioNumber = 0;
+        }
+    }
+    
+
+}