fork of StateScript

Dependencies:   mbed SOMO_II

Fork of stateScript_v2 by Mattias Karlsson

Revision:
4:abee20c0bf2a
Parent:
3:d7b0a0890d96
Child:
7:5fe7329751d4
--- a/mbedInterface/mbedInterface.cpp	Sat Oct 10 22:37:17 2015 +0000
+++ b/mbedInterface/mbedInterface.cpp	Fri Jan 15 22:13:23 2016 +0000
@@ -18,7 +18,7 @@
 extern bool changeToStandAlone;
 extern outputStream textDisplay;
 
-int externalIncrementMod = 30;
+int externalIncrementMod = 1;
 int externalIncrementCounter = 0;
 
 
@@ -118,11 +118,13 @@
 
     //The external clock reset signal pulse has come back down.  If the pulse was long
     //enough, then we condsider it a valid pulse (the pulses should be 1 ms long)
+    /*
+    textDisplay << uSec_SinceLastReset;
     if ((clockSlave)&&(uSec_SinceLastReset >= 700)) {
         uSec_SinceLastReset = 0;
         timeKeeper = 1; //It has been 1ms since the reset pulse went up
         textDisplay << timeKeeper << " Clock reset\r\n";
-    }
+    }*/
 }
 
 //------------------------------------------------------------------------
@@ -132,10 +134,9 @@
 //---------------------------------------------------------------------
 
 //translate pin numbers to hardware pins
-//PinName outPins[NUMPORTS] = {p11,p13,p15,p18,p21,p23,p25,p29,p20};
-PinName outPins[NUMPORTS] = {p18,p15,p13,p11,p29,p25,p23,p21,p20};
-PinName inPins[NUMPORTS] = {p12,p14,p16,p17,p22,p24,p26,p30,p7};
-
+PinName outPins[NUMOUTPORTS] = {p11,p13,p15,p18,p21,p23,p25,p29,p20,p6}; //Old board output pins
+//PinName outPins[NUMPORTS] = {p18,p15,p13,p11,p29,p25,p23,p21,p20}; //New board output pins
+PinName inPins[NUMINPORTS] = {p12,p14,p16,p17,p22,p24,p26,p30,p7};
 
 
 
@@ -145,11 +146,19 @@
 //This is the callback for the MBED timer
 extern "C" void TIMER0_IRQHandler (void) {
 
-    if (clockSlave) {
+    /*if (clockSlave) {
+
         //The function is called every 100 us
-        uSec_SinceLastClockInc = uSec_SinceLastClockInc+100;
-        uSec_SinceLastReset = uSec_SinceLastReset+100;
-    } else {
+        if((LPC_TIM0->IR & 0x01) == 0x01) {  // if MR0 interrupt, proceed
+
+            LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
+            uSec_SinceLastClockInc = uSec_SinceLastClockInc+100;
+            uSec_SinceLastReset = uSec_SinceLastReset+100;
+
+        }
+
+
+    } else {*/
         //The function is called every 1 ms
         if((LPC_TIM0->IR & 0x01) == 0x01) {  // if MR0 interrupt, proceed
 
@@ -161,7 +170,7 @@
                 resetTimer = false;
             }
         }
-    }
+    //}
 
 }
 //-----------------------------------------------------------------------
@@ -193,7 +202,6 @@
     }
 
 
-
     sWav.reset();
 
 }
@@ -207,8 +215,11 @@
     //LPC_SC->PCLKSEL0 &= (3 << 3); //mask
     //LPC_SC->PCLKSEL0 |= (1 << 3); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual)
     LPC_SC->PCONP |=1<1;            //timer0 power on   
+
+
     LPC_TIM0->MR0 = 23980;        //1 msec
 
+
     //LPC_TIM0->PR  = (SystemCoreClock / 1000000); //microsecond steps
     //LPC_TIM0->MR0 = 1000;        //100 msec
     //LPC_TIM0->MR0  = (SystemCoreClock / 1000000); //microsecond steps
@@ -279,12 +290,13 @@
     uSec_SinceLastReset = 0;
 
 
-    /*
+
     if (clockSlave) {
         LPC_TIM0->TCR = 0x02;    // reset timer
         externalIncrementCounter = 0;
+
         immediateClockReset();
-    }*/
+    }
 
 }
 
@@ -363,37 +375,55 @@
 
 //-----------------------------------------------------
 MBEDDigitalOut::MBEDDigitalOut() {
-
+    pinExists = false;
 }
 
 void MBEDDigitalOut::init(int pin) {
-    outpin = new DigitalOut(outPins[pin]);
+    if (pin < NUMOUTPORTS) {
+        outpin = new DigitalOut(outPins[pin]);
+        pinExists = true;
+    }
 }
 
 int MBEDDigitalOut::read() {
-    return outpin->read();
+    if (pinExists) {
+        return outpin->read();
+    } else {
+        return 0;
+    }
 }
 
 void MBEDDigitalOut::write(int value) {
-    outpin->write(value);
+    if (pinExists) {
+
+        outpin->write(value);
+    }
 }
 //--------------------------------------------------------
 
 MBEDDigitalIn::MBEDDigitalIn() {
-
+    pinExists = false;
 }
 
 void MBEDDigitalIn::init(int pin) {
-    inpin = new DigitalIn(inPins[pin]);
-    inpin_interrupt = new InterruptIn(inPins[pin]);
-    inpin->mode(PullDown);
-    //Set up callbacks for the port interrupts
-    inpin_interrupt->rise(this, &MBEDDigitalIn::interrupt_up_callback);
-    inpin_interrupt->fall(this, &MBEDDigitalIn::interrupt_down_callback);
+
+    if (pin < NUMINPORTS) {
+        inpin = new DigitalIn(inPins[pin]);
+        inpin_interrupt = new InterruptIn(inPins[pin]);
+        inpin->mode(PullDown);
+        //Set up callbacks for the port interrupts
+        inpin_interrupt->rise(this, &MBEDDigitalIn::interrupt_up_callback);
+        inpin_interrupt->fall(this, &MBEDDigitalIn::interrupt_down_callback);
+        pinExists = true;
+    }
 }
 
 int MBEDDigitalIn::read() {
-    return inpin->read();
+    if (pinExists) {
+        return inpin->read();
+    } else {
+        return 0;
+    }
 }
 
 void MBEDDigitalIn::interrupt_up_callback() {