perturh room legacy

Dependencies:   SMARTWAV USBDevice mbed stateScript

Fork of stateScript by Mattias Karlsson

Revision:
5:e62cd80aa22f
Parent:
4:34aca2142df9
--- a/main.cpp	Thu Sep 25 23:42:30 2014 +0000
+++ b/main.cpp	Sat May 16 23:41:46 2015 +0000
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <sstream>
 #include "SMARTWAV.h"
+#include "USBSerial.h"
 
 
 uint32_t timeKeeper; //the main clock (updated every ms) 
@@ -14,7 +15,7 @@
 
 //static char buf1[0x2000] __attribute__((section("AHBSRAM0")));
 __attribute((section("AHBSRAM0"),aligned)) outputStream textDisplay(512);
-__attribute((section("AHBSRAM0"),aligned)) char buffer[256];
+__attribute((section("AHBSRAM0"),aligned)) char buffer[128];
 
 
 __attribute((section("AHBSRAM1"),aligned)) event eventBlock[NUMEVENTS];
@@ -56,59 +57,72 @@
 //Define the digial ports
 
 //Pins for clock syncing
-InterruptIn clockResetInt(p5);
-DigitalOut clockOutSync(p6);
-DigitalOut clockOutSignal(p7);
-InterruptIn clockExternalIncrement(p8);
-
-//Camera trigger signal
-//DigitalOut camera30Hz(p27);
+//InterruptIn clockResetInt(p24);
+DigitalOut clockOutSync(p19);
+DigitalOut clockOutSignal(p29);
+InterruptIn clockExternalIncrement(p30);
 
 
 //Pins for digital ports.  Each port has 1 out and 1 in
 //DigitalOut out1(LED1); //route to LED for debugging
-DigitalOut out1(p11);
-DigitalIn in1(p12);
-InterruptIn int1(p12);
+//1A,1B
+DigitalOut out1(p5);
+DigitalIn in1(p6);
+InterruptIn int1(p6);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port1(&out1, &in1);
-
-DigitalOut out2(p13);
-DigitalIn in2(p14);
-InterruptIn int2(p14);
+//1C,1D
+DigitalOut out2(p9);
+DigitalIn in2(p10);
+InterruptIn int2(p10);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port2(&out2, &in2);
-
-
-DigitalOut out3(p15);
-DigitalIn in3(p16);
-InterruptIn int3(p16);
+//2A,2B
+DigitalOut out3(p13);
+DigitalIn in3(p14);
+InterruptIn int3(p14);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port3(&out3, &in3);
-
-DigitalOut out4(p18);
-DigitalIn in4(p17);
-InterruptIn int4(p17);
+//2C,2D
+DigitalOut out4(p17);
+DigitalIn in4(p18);
+InterruptIn int4(p18);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port4(&out4, &in4);
-
+//3A,3B
 DigitalOut out5(p21);
 DigitalIn in5(p22);
 InterruptIn int5(p22);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port5(&out5, &in5);
-
-DigitalOut out6(p23);
-DigitalIn in6(p24);
-InterruptIn int6(p24);
-__attribute((section("AHBSRAM0"),aligned)) digitalPort port6(&out6, &in6);
+////3C,3D
+//DigitalOut out6(p21);
+//DigitalIn in6(p22);
+//InterruptIn int6(p22);
+//__attribute((section("AHBSRAM0"),aligned)) digitalPort port6(&out6, &in6);
+////4A,4B
+////DigitalOut out7(p9);
+////DigitalIn in7(p10);
+////InterruptIn int7(p10);
+////__attribute((section("AHBSRAM0"),aligned)) digitalPort port7(&out7, &in7);
+////5A,5B
+//DigitalOut out7(p13);
+//DigitalIn in7(p14);
+//InterruptIn int7(p14);
+//__attribute((section("AHBSRAM0"),aligned)) digitalPort port7(&out7, &in7);
+////6A,6B
+//DigitalOut out8(p23);
+//DigitalIn in8(p24);
+//InterruptIn int8(p24);
+//__attribute((section("AHBSRAM0"),aligned)) digitalPort port8(&out8, &in8);
+////Pump1
+//DigitalOut out9(p25);
+//__attribute((section("AHBSRAM0"),aligned)) digitalPort port9(&out9);
+////Pump2
+//DigitalOut out10(p26);
+//__attribute((section("AHBSRAM0"),aligned)) digitalPort port10(&out10);
 
-DigitalOut out7(p25);
-DigitalIn in7(p26);
-InterruptIn int7(p26);
-__attribute((section("AHBSRAM0"),aligned)) digitalPort port7(&out7, &in7);
+
 
-DigitalOut out8(p29);
-DigitalIn in8(p30);
-InterruptIn int8(p30);
-__attribute((section("AHBSRAM0"),aligned)) digitalPort port8(&out8, &in8);
 //Serial communication
-Serial pc(USBTX, USBRX); // tx, rx
+//Serial pc(USBTX, USBRX); // tx, rx
+USBSerial pc;
+
 
 //Main event queue
 eventQueue mainQueue(portVector, &timeKeeper);
@@ -117,8 +131,8 @@
 scriptStream parser(&pc, portVector, NUMPORTS, &mainQueue);
 
 //The sound output uses a SmartWav device and their simple serial library
-SMARTWAV sWav(p9,p10,p19);    //(TX,RX,Reset);
-
+SMARTWAV sWav(p28,p27,p26);    //(TX,RX,Reset);
+//Serial device(p9,p10);
 //Erases the input buffer for serial input
 void eraseBuffer(char* buffer,int numToErase) {
     for (int i = 0; i < numToErase; i++) {
@@ -200,7 +214,6 @@
             currentBroadcastBit = 0;
         }
     
-                
         //Every second, we broadcast out the current time 
         if ((timeKeeper % 1000) == 0) {
             currentBroadcastTime = timeKeeper;
@@ -264,10 +277,13 @@
 void callback_port5_fall(void) { int_callback(5, 0); }
 void callback_port6_rise(void) { int_callback(6, 1); }
 void callback_port6_fall(void) { int_callback(6, 0); }
-void callback_port7_rise(void) { int_callback(7, 1); }
-void callback_port7_fall(void) { int_callback(7, 0); }
-void callback_port8_rise(void) { int_callback(8, 1); }
-void callback_port8_fall(void) { int_callback(8, 0); }
+//void callback_port7_rise(void) { int_callback(7, 1); }
+//void callback_port7_fall(void) { int_callback(7, 0); }
+//void callback_port8_rise(void) { int_callback(8, 1); }
+//void callback_port8_fall(void) { int_callback(8, 0); }
+//void callback_port9_rise(void) { int_callback(9, 1); }
+//void callback_port9_fall(void) { int_callback(9, 0); }
+
 
 //This function is attached to an interrupt pin for external clock reset
 void callback_clockReset(void) {
@@ -281,11 +297,12 @@
 
 int main() {
     timeKeeper = 0; //set main clock to 0;
-    sWav.reset(); 
-    pc.baud(115200);
+//    sWav.reset(); 
+    //pc.baud(115200);
+    //device.baud(4800);
     //pc.baud(9600);
    
-    for (int i = 0; i < 9; i++) {
+    for (int i = 0; i <NUMPORTS+1; i++) {
         portVector[i] = NULL;
     }
     //We keep portVector 1-based to eliminate confusion
@@ -294,9 +311,12 @@
     portVector[3] = &port3;
     portVector[4] = &port4;
     portVector[5] = &port5;
-    portVector[6] = &port6;
-    portVector[7] = &port7;
-    portVector[8] = &port8;
+//    portVector[6] = &port6;
+//    portVector[7] = &port7;
+//    portVector[8] = &port8;
+//    portVector[9] = &port9;
+//    portVector[10] = &port10;
+    //portVector[11] = &port11;  
    
     //Callback to update the main clock 
     //timeTick1.attach_us(&incrementTime, 100);
@@ -315,15 +335,17 @@
     int4.fall(&callback_port4_fall);
     int5.rise(&callback_port5_rise);
     int5.fall(&callback_port5_fall);
-    int6.rise(&callback_port6_rise);
-    int6.fall(&callback_port6_fall);
-    int7.rise(&callback_port7_rise);
-    int7.fall(&callback_port7_fall);
-    int8.rise(&callback_port8_rise);
-    int8.fall(&callback_port8_fall);
+//    int6.rise(&callback_port6_rise);
+//    int6.fall(&callback_port6_fall);
+//    int7.rise(&callback_port7_rise);
+//    int7.fall(&callback_port7_fall);
+//    int8.rise(&callback_port8_rise);
+//    int8.fall(&callback_port8_fall);
+//   int9.rise(&callback_port9_rise);
+//   int9.fall(&callback_port9_fall);
     
-    clockResetInt.rise(&callback_clockReset);
-    clockResetInt.mode(PullDown);
+    //clockResetInt.rise(&callback_clockReset);
+    //clockResetInt.mode(PullDown);
     
     clockExternalIncrement.mode(PullDown);
       
@@ -333,21 +355,23 @@
     in3.mode(PullDown);
     in4.mode(PullDown);
     in5.mode(PullDown);
-    in6.mode(PullDown);
-    in7.mode(PullDown);
-    in8.mode(PullDown);
+//    in6.mode(PullDown);
+//    in7.mode(PullDown);
+//    in8.mode(PullDown);
+//    in9.mode(PullDown);
      
     //Set up input buffer for the serial port
     //char buffer[128];
     int bufferPos = 0;
-    eraseBuffer(buffer,256);
+    eraseBuffer(buffer,128);
     
     ostringstream timeConvert;   // stream used for the conversion
     ostringstream stateConvert; 
     char junkChar;
     int tmpChar;
     
-    while (pc.readable()) {
+    //while (pc.readable()) {
+    while (pc.available()) {
         junkChar = pc.getc();
     }   
          
@@ -363,14 +387,14 @@
             if ((tmpChar == 13) || (tmpChar == 10)) { //carrriage return
                 parser.addLineToCurrentBlock(buffer);                          
                 bufferPos = 0;
-                eraseBuffer(buffer,256);             
+                eraseBuffer(buffer,128);             
             }            
             //pc.putc(tmpChar);
         } while (tmpChar != EOF);
         
         buffer[bufferPos] = 59;
         parser.addLineToCurrentBlock(buffer);
-        eraseBuffer(buffer,256);  
+        eraseBuffer(buffer,128);  
         fclose(fp);
     } else {
         pc.printf("No startup script found.\r\n"); 
@@ -381,8 +405,13 @@
        //check the main event queue to see if anything needs to be done
        mainQueue.check();
        
+       //https://developer.mbed.org/handbook/Serial
+        //if(device.readable()) {
+//            pc.putc(device.getc());
+//        }
        //check if anything has been written to the serial input
-       if (pc.readable()) {
+       //if (pc.readable()) {
+       if (pc.available()) {
                 
             buffer[bufferPos] = pc.getc();
             bufferPos++;